我有一个简单的RandomForest回归模型,该模型进行训练和测试,然后打印预测和模型准确性。我想编写一个Python自动脚本来安排此代码,该脚本每月进行一次培训,每周自动进行一次测试。
型号代码:
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','education','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)
return self.result
if __name__ == '__main__':
model_instance = Model()
model_instance.split(0.2)
model_instance.fit()
model_instance.predict()
print(model_instance.result)
print("Accuracy: ", model_instance.model.score(model_instance.X_test, model_instance.y_test))
这是我想每月安排一次的培训部分
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)
return self.result
如何编写在所描述的时间范围内安排培训和测试的代码?