【问题标题】:Saving the isolaiton forest model in python and applying it to the new data在 python 中保存隔离林模型并将其应用于新数据
【发布时间】:2022-01-23 17:10:06
【问题描述】:

我有一个异常检测程序,我用 python 中的隔离森林方法制作。我想为我拥有的数据保存这个无监督学习模型,当新数据进来时,我想直接对这个模型的结果进行异常估计。我该怎么做?

def fit_model(model, data, column='value'):
    df = data.copy()
    data_to_predict = data[column].to_numpy().reshape(-1, 1)
    predictions = model.fit_predict(data_to_predict)
    df['Predictions'] = predictions

    return df

【问题讨论】:

  • type(model_fit) 返回什么?

标签: python unsupervised-learning


【解决方案1】:

我假设您的模型来自 sklearn 包。 在这种情况下,保存模型的一种方法是使用 joblib 包(您也可以使用 onnx)。

在您的情况下,您可以保存模型

import joblib 

def fit_model(model, data, column='value'):
    df = data.copy()
    data_to_predict = data[column].to_numpy().reshape(-1, 1)
    predictions = model.fit_predict(data_to_predict)
    joblib.dump(model, 'my_model_name.joblib')
    df['Predictions'] = predictions

    return df

然后,您可以将其加载回来:

model = joblib.load('my_model_name.joblib')

您可以在此处阅读更多相关信息: https://scikit-learn.org/stable/modules/model_persistence.html 和这里:https://joblib.readthedocs.io/en/latest/persistence.html (joblib 本身就是一个非常有用的库,不仅用于保存 sklearn 模型)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2020-05-24
    • 2021-03-20
    • 2019-07-20
    • 2014-01-06
    相关资源
    最近更新 更多