簡體   English   中英

在python中復制預測模型

[英]Copy prediction model in python

在我的預測性python代碼中

我使用sklearn預測模型(KNN,RandomForest,LinearRegression等)

在循環中嘗試尋找最佳的超參數優化。

找到最佳參數后,我想復制訓練好的模型

我怎樣才能做到這一點?

for k in range(1,10):
    knn = KNeighborsClassifier(n_neighbors=k)
    ...
    #some code to fit and train the model here and find the accuracy
    ...
    if accuracy > top_accuracy:
         top_accuracy = accuracy
         top_knn = knn <==== ?


# code to fit top_knn with a new test dataset

這會復制受過訓練的模型與受過訓練的模型嗎,有什么方法可以確保我的模型具有受過訓練的數據

scikit-learn包含用於超參數調整的組件(請參閱https://scikit-learn.org/stable/modules/grid_search.html )。 示例代碼在這里 可以通過clf.best_estimator_訪問在搜索過程中找到的最佳模型。

您在使用sklearn嗎? 每次訓練時,KNN對象都會保留最新的擬合功能。 如果要記錄新擬合是否比上一個擬合更好,我只需創建一個新類並為其分配模型對象即可:

class foo:
    __init__(self,model,accuracy):
        self.current = model
        self.accuracy = accuracy

if accuracy > foo.accuracy:
    foo.current = model

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM