Python代码中的F1评分
我有一些关于Python中指标的问题。我遇到了一个错误:
"ValueError: Can't handle mix of multiclass and continuous"。
我的代码大概是这样的(还有一些关于参数的详细信息):
X_train,X_test,y_train,y_test = cross_validation.train_test_split(data, target, test_size=0.3, random_state=42)
clf = RFC()
clf = clf.fit(X_train,y_train)
y_predict = clf.predict_proba(X_test)[:,1]
print f1_score(y_test,y_predict)
>>>X_train.shape
(7000, 576)
>>>X_test.shape
(3000, 576)
>>>y_train.shape
(7000,)
>>>y_test.shape
(3000,)
>>>X_train.dtype
dtype('float64')
>>>X_test.dtype
dtype('float64')
>>>y_train.dtype
dtype('float64')
>>>y_test.dtype
dtype('float64')
>>>y_predict.shape
(3000,)
>>>y_predict.dtype
dtype('float64')
我觉得可能有某个参数设置错了,但乍一看一切都没问题……我真的找不到问题出在哪里……
1 个回答
4
这是问题所在:
y_predict = clf.predict_proba(X_test)[:,1]
print f1_score(y_test,y_predict)
F1是基于标签来定义的,而不是基于概率分布的,所以要使用predict
,而不是predict_proba
。