简体   繁体   中英

Python Scheduled Script to a docker container

I have built a RandomForest Regression model as a python script. It accepts two csv files train and test, performs training and testing. It then prints the predictions and accuracy and finally saved the predictions as csv file. I have saved the code file as RandomForest.py. After which I have created a batch execution (.bat) file for RandomForest.py. After that I have used Windows Task Scheduler to schedule my Python Script(RandomForest.py) to run once a week. After that I have exported the scheduled task as a '.xml' file. My Question: I want to put this .xml file inside a docker container so that it runs itself once a week.

RandomForest.py code:

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


class Modelrf():

    def __init__(self, train = "train.csv", test = "test.csv"):
        self.X_train = pd.read_csv(train)
        self.X_test = pd.read_csv(test)
        self.linear_reg = LinearRegression()
        self.random_forest = RandomForestRegressor()
    def split(self):
        self.X_train.dropna(axis=0, subset=['final_hourly_fee'], inplace=True)
        self.X_test.dropna(axis=0, subset=['final_hourly_fee'], inplace=True)
        self.y_train = self.X_train.final_hourly_fee
        self.y_test = self.X_test.final_hourly_fee

    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



model_instance = Modelrf()
model_instance.split()
model_instance.fit()
model_instance.predict()
print(model_instance.result)
print("Accuracy: ", model_instance.model.score(model_instance.X_test, model_instance.y_test))

output = pd.DataFrame({'Id': model_instance.X_test.index,'Y Original': model_instance.y_test, 'Y predicted':model_instance.result})
output.to_csv('outputTest.csv', index=False)

.bat file:

python C:\Python\Headstrt\gitlab_pricing\myproject.git\RandomForest.py

pause

You could run a cron job in the docker container .

This cron string will run once a week 0 0 * * 0 . Got this from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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