简体   繁体   English

如何转换Scikit Learn OneVsRestClassifier预测方法输出到谷云ML的密集阵列?

[英]How to convert Scikit Learn OneVsRestClassifier predict method output to dense array for google cloud ML?

I have a model that I've trained using a sklearn Pipeline and the OneVsRestClassifier that I'm trying to deploy to Cloud ML Engine, however when I use the command: 我有一个模型,我使用sklearn Pipeline和我试图部署到Cloud ML Engine的OneVsRestClassifier进行训练,但是当我使用命令时:

gcloud ml-engine predict --model $MODEL_NAME --version $VERSION_NAME --json-instances $INPUT_FILE

I receive the error: 我收到错误:

{ "error": "Prediction failed: Bad output type returned.The predict function should return either a numpy ndarray or a list." }

This leads me to believe it is the fact that the OneVsRestClassifier's predict method output is a sparse matrix, when it should be a numpy array. 这让我相信OneVsRestClassifier的预测方法输出是一个稀疏矩阵,而它应该是一个numpy数组。 How can I convert it's output to a dense array in my Pipeline? 如何将其输出转换为我的管道中的密集数组?

The pipeline's architecture looks like this: 管道的架构如下所示:

Pipeline([('tfidf', tfidf), ('clf', OneVsRestClassifier(XGBClassifier())])

Thanks! 谢谢!

I've tried using the methods here ( Google Cloud ML-engine scikit-learn prediction probability 'predict_proba()' ) to overwrite the OneVsRestClassifier's predict method with it's predict_proba method, however this results in the following error when I try and pickle the new pipeline: 我尝试使用这里的方法( Google Cloud ML-engine scikit-learn预测概率'predict_proba()' )用它的predict_proba方法覆盖OneVsRestClassifier的预测方法,但是当我尝试挑选新的方法时会导致以下错误管道:

PicklingError: Can't pickle <function OneVsRestClassifier.predict_proba at 0x10a8f9d08>: it's not the same object as sklearn.multiclass.OneVsRestClassifier.predict_proba

AI Platform (formerly known as Cloud Machine Learning Engine) serves your model and expects the input and the output to be json-serializable. AI平台(以前称为Cloud Machine Learning Engine)为您的模型提供服务,并期望输入和输出为json可序列化。 If your model returns a sparse matrix, then you need to convert it to a dense matrix (see this for more information). 如果模型返回稀疏矩阵,则需要将其转换为密集矩阵(有关详细信息,请参阅内容)。

If you choose to overwrite predict_proba , then you are deploying your model with some custom code (your code that overwrites the function). 如果您选择覆盖predict_proba ,那么您将使用一些自定义代码(您的代码覆盖该函数)部署您的模型。 You will then need to package up your custom code and pass it alongside your model when you deploy your model. 然后,在部署模型时,您需要打包自定义代码并将其与模型一起传递。 For more information on how to deploy models with custom code, please visit Custom prediction routines on AI Platform. 有关如何使用自定义代码部署模型的更多信息,请访问AI Platform上的自定义预测例程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Cloud ml引擎上调用本地预测或创建模型版本时,Scikit Learn模型导致错误 - Scikit Learn model results in error when calling local predict or creating model version on Google Cloud ml engine Google Cloud ML引擎scikit学习预测概率&#39;predict_proba()&#39; - Google Cloud ML-engine scikit-learn prediction probability 'predict_proba()' 如何将 scikit 的 Predict proba 输出转换为 sigmoid - How to Convert Predict proba output from scikit learn to sigmoid Cloud ML Engine和Scikit-Learn:&#39;LatentDirichletAllocation&#39;对象没有属性&#39;predict&#39; - Cloud ML Engine and Scikit-Learn: 'LatentDirichletAllocation' object has no attribute 'predict' 如何在Scikit学习中处理响应OneVsRestClassifier用于多标签 - How to treat response OneVsRestClassifier in Scikit-learn for multilabel 预测方法如何在 scikit-learn 上工作? - How does the predict method work on scikit-learn? 如何并行化 scikit-learn SVM (SVC) 分类器的 .predict() 方法? - How to parallelise .predict() method of a scikit-learn SVM (SVC) Classifier? scikit学习ML算法的输出 - Output from scikit learn ML algorithms scikit-learn中的predict_proba输出 - Output of predict_proba in scikit-learn 如何将numpy数组转换为适用于Google Cloud ML的JSON? - How convert numpy array to JSON for Google Cloud ML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM