【问题标题】:Python Scheduled Script to a docker containerPython 预定脚本到 docker 容器
【发布时间】:2020-03-27 20:37:46
【问题描述】:

我已将 RandomForest 回归模型构建为 Python 脚本。它接受两个 csv 文件进行训练和测试,执行训练和测试。然后打印预测和准确性,最后将预测保存为 csv 文件。 我已将代码文件保存为 RandomForest.py。之后,我为 RandomForest.py 创建了一个批处理执行 (.bat) 文件。之后,我使用 Windows 任务计划程序来安排我的 Python 脚本(RandomForest.py)每周运行一次。之后,我将计划任务导出为“.xml”文件。 我的问题:我想把这个 .xml 文件放在一个 docker 容器中,以便它每周运行一次。

RandomForest.py 代码:

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 文件:

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

pause

【问题讨论】:

  • 如果我没记错 .bat 文件仅用于在 Windows 上运行,如果您的 docker 容器是 linux ... 例如 Ubuntu,那么您需要创建一个 bash 脚本。我建议看一下 Apache 气流(用 python 实现),它非常适合调度作业。 airflow.apache.org/docs/stable/start.html
  • 我想把它放在一个容器里,这样以后我可以把它移到云端。
  • 我明白了,你能指定你打算在 docker 容器上使用哪个操作系统吗? linux、windows?您可以轻松地在容器内运行气流。我将尝试写一个快速答案,展示如何在气流中执行计划任务。看看:airflow.apache.org/docs/stable/scheduler.html
  • Windows 操作系统。
  • 很遗憾您没有得到任何有价值的答案,因为这是个好问题。

标签: python docker machine-learning scikit-learn containers


【解决方案1】:

您可以运行cron job in the docker container

这个 cron 字符串将每周运行一次 0 0 * * 0。从here得到这个。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-04-17
  • 2022-10-21
  • 2022-01-13
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 2020-12-31
  • 2015-03-30
相关资源
最近更新 更多