Pandas Python Sklearn“;ValueError:分类指标可以';t处理多类多输出和二进制目标的混合”;错误
当我使用test\u train\u split函数来使用相同的数据集进行测试和训练时,我没有遇到这个错误。 但是当我尝试使用不同的csv文件进行测试和培训时,我会遇到这个错误。 有人能解释一下我为什么会犯这个错误吗Pandas Python Sklearn“;ValueError:分类指标可以';t处理多类多输出和二进制目标的混合”;错误,pandas,machine-learning,scikit-learn,prediction,sklearn-pandas,Pandas,Machine Learning,Scikit Learn,Prediction,Sklearn Pandas,当我使用test\u train\u split函数来使用相同的数据集进行测试和训练时,我没有遇到这个错误。 但是当我尝试使用不同的csv文件进行测试和培训时,我会遇到这个错误。 有人能解释一下我为什么会犯这个错误吗 from sklearn.linear_model import LogisticRegression logreg=LogisticRegression() logreg.fit(df,survived_df) predictions=logreg.predict(test)
from sklearn.linear_model import LogisticRegression
logreg=LogisticRegression()
logreg.fit(df,survived_df)
predictions=logreg.predict(test)
from sklearn.metrics import accuracy_score
accuracy=accuracy_score(test_survived,predictions) #error here Value Error ""ValueError: Classification metrics can't handle a mix of multiclass-multioutput and binary targets""
print(accuracy)
完全错误
ValueError Traceback (most recent call last)
<ipython-input-243-89c8ae1a928d> in <module>
----> 1 logreg.score(test,test_survived)
2
~/mldl/kaggle_practice/titanic_pilot/venv/lib64/python3.8/site-packages/sklearn/base.py in score(self, X, y, sample_weight)
497 """
498 from .metrics import accuracy_score
--> 499 return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
500
501 def _more_tags(self):
~/mldl/kaggle_practice/titanic_pilot/venv/lib64/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
70 FutureWarning)
71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72 return f(**kwargs)
73 return inner_f
74
~/mldl/kaggle_practice/titanic_pilot/venv/lib64/python3.8/site-packages/sklearn/metrics/_classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
185
186 # Compute accuracy for each possible representation
--> 187 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
188 check_consistent_length(y_true, y_pred, sample_weight)
189 if y_type.startswith('multilabel'):
~/mldl/kaggle_practice/titanic_pilot/venv/lib64/python3.8/site-packages/sklearn/metrics/_classification.py in _check_targets(y_true, y_pred)
88
89 if len(y_type) > 1:
---> 90 raise ValueError("Classification metrics can't handle a mix of {0} "
91 "and {1} targets".format(type_true, type_pred))
92
ValueError: Classification metrics can't handle a mix of multiclass-multioutput and binary targets
您可能希望获得
预测
的准确性以及测试_surved
数据帧的surved
列:
from sklearn.metrics import accuracy_score
accuracy=accuracy_score(test_survived['Survived'],predictions)
print(accuracy)
发生错误,因为精度_score()只接受两个一维数组,一个作为基本真理标签,另一个作为预测标签。但是您提供了一个二维“数组”(数据帧)和一维预测,因此它假设您的第一个输入是一个多类输出
政府在这方面也很有办法
from sklearn.metrics import accuracy_score
accuracy=accuracy_score(test_survived['Survived'],predictions)
print(accuracy)