如何解决method ='predict_proba'for cross_val_predict返回“索引1超出尺寸1的轴1的范围”
这是数据集的一部分:
a b c result
0 1 1 positive
0 0 1 negative
0 1 1 negative
0 0 0 positive
result = [1 if v=='positive' else 0 for v in data['result'].tolist()]
Output = result
X = data["a","b","c"]
y = np.reshape(Output,(X.shape[0],1))
我正在尝试使用sklearn中的交叉验证方法来预测X数据的类别:这部分代码有效:
logreg = LogisticRegression('l2')
y_pred_class = cross_val_predict(logreg,X,y,cv=10,method= 'predict' )
但是当我想使用此代码来计算类的概率时:
y_pred_prob = cross_val_predict(logreg,method='predict_proba')
出现此错误:
index 1 is out of bounds for axis 1 with size 1
你知道是什么问题吗?
解决方法
致电method="predict"
时会收到警告:
DataConversionWarning:当期望一个1d数组时,传递了列向量y。请将y的形状更改为(n_samples,),例如使用ravel()。 返回f(** kwargs) /usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py:72:DataConversionWarning:当期望使用1d数组时,传递了列向量y。请将y的形状更改为(n_samples,),例如使用ravel()。 返回f(** kwargs)
如果您只是注意该警告,它将解决method="predict_proba"
中的错误。而您要做的就是更改此行
y = np.reshape(Output,(X.shape[0],1))
到
y = np.reshape(Output,))
甚至
y = np.array(result)
或者甚至不用理会列表理解并留在大熊猫中:
y = data["result"].replace({"positive": 1,"negative": 0})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。