安排培训和测试机器学习

时间:2020-03-20 06:39:25

标签: python class oop machine-learning scikit-learn

我已经在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

2 个答案:

答案 0 :(得分:2)

我对schedule包不熟悉,但是我猜到do的参数必须是可调用的。这意味着您实际上不应调用该函数。试试这个:

schedule.every(30).days.at("05:00").do(model_instance.fit)
schedule.every(7).days.at("05:00").do(model_instance.predict)

请注意,我删除了fitpredict之后的括号。

答案 1 :(得分:0)

我知道了。创建了用于培训和测试的单独模块,然后导入Model类,然后创建了将执行计划的功能。

培训功能:

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)