简体   繁体   English

安排训练和测试机器学习

[英]Schedule training and testing machine learning

I have written this small machine learning code of a simple random forest regression in the class Model.我在 Model 类中编写了这个简单的随机森林回归的小型机器学习代码。 After creating an object of this class I have printed the predictions and the accuracy score along with that I have written a code to schedule training every 30 days and testing every 7 days.创建这个类的对象后,我打印了预测和准确度分数,并编写了一个代码来安排每 30 天的训练和每 7 天的测试。 But I'm facing an error但我正面临一个错误

Code:代码:

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)

On this line schedule.every(30).days.at("05:00").do(model_instance.fit()) I'm getting the following error: the first argument must be callable在这条线上schedule.every(30).days.at("05:00").do(model_instance.fit())我收到以下错误: the first argument must be callable

I'm not familiar with the schedule package, but I guess the argument to do must be a callable.我不熟悉 schedule 包,但我想do的参数必须是可调用的。 Which means you shouldn't actually call that function.这意味着您实际上不应该调用该函数。 Try this:尝试这个:

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

Note I removed the parentheses after fit and predict .注意我在fitpredict之后删除了括号。

I figured it out.我想到了。 Created separate modules for training and testing and then imported the Model class and then created a function which will perform the scheduling.为训练和测试创建单独的模块,然后导入模型类,然后创建一个将执行调度的函数。

Function for Training:训练功能:

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)

Function for testing:测试功能:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM