![](/img/trans.png)
[英]Interpretation of Training, Testing (Dev) and Validation Score in Machine Learning
[英]Schedule training and testing machine learning
我在 Model 類中編寫了這個簡單的隨機森林回歸的小型機器學習代碼。 創建這個類的對象后,我打印了預測和准確度分數,並編寫了一個代碼來安排每 30 天的訓練和每 7 天的測試。 但我正面臨一個錯誤
代碼:
import schedule
import time
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from main import data as df
class Model():
def __init__(self):
self.df = df
self.linear_reg = LinearRegression()
self.random_forest = RandomForestRegressor()
def split(self, test_size):
X = np.array(self.df[['age','experience','certificates']])
y = np.array(self.df['salary'])
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size = test_size, random_state = 42)
def fit(self):
self.model = self.random_forest.fit(self.X_train, self.y_train)
def predict(self):
self.result = self.random_forest.predict(self.X_test)
print(self.result)
print("Accuracy: ", self.model.score(self.X_test, self.y_test))
if __name__ == '__main__':
model_instance = Model()
model_instance.split(0.2)
schedule.every(30).days.at("05:00").do(model_instance.fit())
schedule.every(7).days.at("05:00").do(model_instance.predict())
while 1:
schedule.run_pending()
time.sleep(1)
在這條線上schedule.every(30).days.at("05:00").do(model_instance.fit())
我收到以下錯誤: the first argument must be callable
我不熟悉 schedule 包,但我想do
的參數必須是可調用的。 這意味着您實際上不應該調用該函數。 嘗試這個:
schedule.every(30).days.at("05:00").do(model_instance.fit)
schedule.every(7).days.at("05:00").do(model_instance.predict)
注意我在fit
和predict
之后刪除了括號。
我想到了。 為訓練和測試創建單獨的模塊,然后導入模型類,然后創建一個將執行調度的函數。
訓練功能:
import schedule
import time
def job():
model_instance.split(0.2)
model_instance.fit()
print("Training Completed")
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
測試功能:
import schedule
import time
def job():
model_instance.predict()
print(model_instance.result)
print("Accuracy: ", model_instance.model.score(model_instance.X_test, model_instance.y_test))
print("Testing Completed")
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.