Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:分类指标可以';无法处理未知目标和二进制目标的混合?_Python_Pandas_Machine Learning_Scikit Learn - Fatal编程技术网

Python ValueError:分类指标可以';无法处理未知目标和二进制目标的混合?

Python ValueError:分类指标可以';无法处理未知目标和二进制目标的混合?,python,pandas,machine-learning,scikit-learn,Python,Pandas,Machine Learning,Scikit Learn,我很确定我的随机森林模型是有效的。当我看到预测和测试集中的实际类时,它们非常匹配。第一部分是对分类数据进行编码: Y_train[Y_train == 'Blue'] = 0.0 Y_train[Y_train == 'Green'] = 1.0 Y_test[Y_test == 'Blue'] = 0.0 Y_test[Y_test == 'Green'] = 1.0 rf = RandomForestRegressor(n_estimators=50) rf.fit(X_train, Y_

我很确定我的随机森林模型是有效的。当我看到预测和测试集中的实际类时,它们非常匹配。第一部分是对分类数据进行编码:

Y_train[Y_train == 'Blue'] = 0.0
Y_train[Y_train == 'Green'] = 1.0
Y_test[Y_test == 'Blue'] = 0.0
Y_test[Y_test == 'Green'] = 1.0

rf = RandomForestRegressor(n_estimators=50)
rf.fit(X_train, Y_train)
predictions = rf.predict(X_test)

for i in range(len(predictions)):
    predictions[i] = predictions[i].round()

print(predictions)
print(Y_test)

print(confusion_matrix(Y_test, predictions))
当我运行此代码时,我成功地打印了
预测
Y_测试

[1. 1. 1. 0. 1. 0. 0. 1. 1. 1. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0. 1. 0. 1.
 1. 0. 1. 1. 1. 0. 1. 0. 1. 1. 0. 0. 0. 0. 1. 1. 0. 1. 0. 1. 1. 0. 1. 0.
 0. 0. 0. 0. 1. 1. 0. 1. 1. 1. 1. 1. 1. 0. 0. 1. 0. 0. 1. 0. 1. 1. 1. 0.
 0. 1. 0. 1. 1. 1. 1. 0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 1. 1. 0. 1.
 0. 0. 0. 0.]
615    1
821    1
874    1
403    0
956    1
      ..
932    1
449    0
339    0
191    0
361    0
Name: Colour, Length: 100, dtype: object
如您所见,它们完全匹配,因此模型工作正常。我遇到的问题是最后一部分,当我尝试使用scikit learn中的
混淆矩阵()
函数时,我遇到以下错误:

    Traceback (most recent call last):
  File "G:\Work\Colours.py", line 101, in <module>
    Main()
  File "G:\Work\Colours.py", line 34, in Main
    RandForest(X_train, Y_train, X_test, Y_test)
  File "G:\Work\Colours.py", line 97, in RandForest
    print(confusion_matrix(Y_test, predictions))
  File "C:\Users\Me\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\metrics\classification.py", line 253, in confusion_matrix
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\metrics\classification.py", line 81, in _check_targets
    "and {1} targets".format(type_true, type_pred))
ValueError: Classification metrics can't handle a mix of unknown and binary targets
回溯(最近一次呼叫最后一次):
文件“G:\Work\colors.py”,第101行,在
Main()
文件“G:\Work\colors.py”,第34行,主目录
RandForest(X_训练,Y_训练,X_测试,Y_测试)
文件“G:\Work\colors.py”,第97行,在RandForest中
打印(混淆矩阵(Y检验,预测))
文件“C:\Users\Me\AppData\Local\Programs\Python37\lib\site packages\sklearn\metrics\classification.py”,第253行,在混乱矩阵中
y_type,y_true,y_pred=_check_targets(y_true,y_pred)
文件“C:\Users\Me\AppData\Local\Programs\Python37\lib\site packages\sklearn\metrics\classification.py”,第81行,在检查目标中
“和{1}目标”。格式(type_true,type_pred))
ValueError:分类指标无法处理未知目标和二进制目标的混合
我可以对这两个数据集中的任何一个做些什么,以使
composition\u matrix()
函数不会抛出任何类型错误


编辑-
预测
Y_测试
都是相同的形状,
(100,)
您必须比较具有相同维度的矩阵,因此如果预测包含一列850行的矩阵(例如),Y_测试必须是一列850行的矩阵


打印(混淆矩阵(Y_测试[1],预测))

您必须比较具有相同维度的矩阵,因此如果预测包含一列850行的矩阵(例如),Y_测试必须是一列850行的矩阵


打印(混淆矩阵(Y_测试[1],预测))

通过对分类数据进行如下编码来修复:

for i in range(len(Y_train)):
    if Y_train.iloc[i] == 'Blue':
        Y_train.iloc[i] = 0.0
    else:
        Y_train.iloc[i] = 1.0

for i in range(len(Y_test)):
    if Y_test.iloc[i] == 'Blue':
        Y_test.iloc[i] = 0.0
    else:
        Y_test.iloc[i] = 1.0
如果有人能告诉我为什么这会解决这个问题,这将是有益的


编辑-我找到了问题的真正原因。我用回归模型代替分类模型。愚蠢的错误。这一切都可以通过使用
RandomForestClassifier()
而不是
RandomForestRegressor()

来避免,而是通过如下方式编码分类数据:

for i in range(len(Y_train)):
    if Y_train.iloc[i] == 'Blue':
        Y_train.iloc[i] = 0.0
    else:
        Y_train.iloc[i] = 1.0

for i in range(len(Y_test)):
    if Y_test.iloc[i] == 'Blue':
        Y_test.iloc[i] = 0.0
    else:
        Y_test.iloc[i] = 1.0
如果有人能告诉我为什么这会解决这个问题,这将是有益的


编辑-我找到了问题的真正原因。我用回归模型代替分类模型。愚蠢的错误。使用
RandomForestClassifier()
而不是
randomForestRegregator()

可以避免这一切。不要使用
0.0
1.0
。例如:
Y\u-train[Y\u-train='Blue']=0
或只是
Y\u-train=Y\u-train='Green'
@quanghaang没有解决这个问题,不过还是要感谢:/don不要使用
0.0
1.0
。例如,do:
Y\u train[Y\u train='Blue']=0
或只是
Y\u train=Y\u train='Green'
@QuangHoang没有解决这个问题,不过谢谢:/I当我这样做的时候,我得到了一个
keyrerror:1
Y\u测试和
预测的形状都是
(100,)
当我这样做时,我得到一个
键错误:1
Y\u测试和
预测的形状都是
(100,)