ML Pipeline OneHotEncoder未拟合

我是数据科学的新手,我能够构建一个模型并使用OneHotEncoder设置一个管道。然而,当我调用我构建的函数时,它会报错。请查看下面的内容并提供建议。提前感谢!

clf = Pipeline(steps=[('ohe', OneHotEncoder()),                  ('rfc', RandomForestClassifier(n_estimators=1000,criterion="entropy",max_features=None))])  pickle.dump(clf,open('model.pkl','wb'))# load modelmodel = pickle.load(open('model.pkl','rb'))def predict(A,B,C,D,E,F,G):    result = model.predict(x)    # send back to browser    output = {'results': int(result[0])}    # return data    return jsonify(results=output)

调用函数:

predict('A','B','C','D','E','F','G')

错误:

NotFittedError: This OneHotEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

回答:

在将数据输入RandomForestClassifier之前,使用这个来转换你的数据:

def trainPipeline(pipeline, X, y):    X_transformed = X    for name, step in pipeline.steps[:-1]:        X_transformed = step.fit_transform(X_transformed, y)    pipeline.steps[-1][1].fit(X_transformed, y)

注意:这仅在你的管道有两个步骤且第一步是OneHotEncoder()时有效。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注