python 召回率_python – 如何计算精度,召回率,准确性和f1分数的多类案例与scikit学习?...

我在一个情绪分析问题工作,数据看起来像这样:

label instances

5 1190

4 838

3 239

1 204

2 127

所以我的数据是不平衡的,因为1190实例标记为5.对于分类Im使用scikit的SVC.问题是我不知道如何平衡我的数据在正确的方式,以准确计算精度,召回,准确性和f1 – 多类别情况的分数。所以我尝试了以下方法:

第一:

wclf = SVC(kernel='linear', C= 1, class_weight={1: 10})

wclf.fit(X, y)

weighted_prediction = wclf.predict(X_test)

print 'Accuracy:', accuracy_score(y_test, weighted_prediction)

print 'F1 score:', f1_score(y_test, weighted_prediction,average='weighted')

print 'Recall:', recall_score(y_test, weighted_prediction,

average='weighted')

print 'Precision:', precision_score(y_test, weighted_prediction,

average='weighted')

print '\n clasification report:\n', classification_report(y_test, weighted_prediction)

print '\n confussion matrix:\n',confusion_matrix(y_test, weighted_prediction)

第二:

auto_wclf = SVC(kernel='linear', C= 1, class_weight='auto')

auto_wclf.fit(X, y)

auto_weighted_prediction = auto_wclf.predict(X_test)

print 'Accuracy:', accuracy_score(y_test, auto_weighted_prediction)

print 'F1 score:', f1_score(y_test, auto_weighted_prediction,

average='weighted')

print 'Recall:', recall_score(y_test, auto_weighted_prediction,

average='weighted')

print 'Precision:', precision_score(y_test, auto_weighted_prediction,

average='weighted')

print '\n clasification report:\n', classification_report(y_test,auto_weighted_prediction)

print '\n confussion matrix:\n',confusion_matrix(y_test, auto_weighted_prediction)

第三:

clf = SVC(kernel='linear', C= 1)

clf.fit(X, y)

prediction = clf.predict(X_test)

from sklearn.metrics import precision_score, \

recall_score, confusion_matrix, classification_report, \

accuracy_score, f1_score

print 'Accuracy:', accuracy_score(y_test, prediction)

print 'F1 score:', f1_score(y_test, prediction)

print 'Recall:', recall_score(y_test, prediction)

print 'Precision:', precision_score(y_test, prediction)

print '\n clasification report:\n', classification_report(y_test,prediction)

print '\n confussion matrix:\n',confusion_matrix(y_test, prediction)

F1 score:/usr/local/lib/python2.7/site-packages/sklearn/metrics/classification.py:676: DeprecationWarning: The default `weighted` averaging is deprecated, and from version 0.18, use of precision, recall or F-score with multiclass or multilabel data or pos_label=None will result in an exception. Please set an explicit value for `average`, one of (None, 'micro', 'macro', 'weighted', 'samples'). In cross validation use, for instance, scoring="f1_weighted" instead of scoring="f1".

sample_weight=sample_weight)

/usr/local/lib/python2.7/site-packages/sklearn/metrics/classification.py:1172: DeprecationWarning: The default `weighted` averaging is deprecated, and from version 0.18, use of precision, recall or F-score with multiclass or multilabel data or pos_label=None will result in an exception. Please set an explicit value for `average`, one of (None, 'micro', 'macro', 'weighted', 'samples'). In cross validation use, for instance, scoring="f1_weighted" instead of scoring="f1".

sample_weight=sample_weight)

/usr/local/lib/python2.7/site-packages/sklearn/metrics/classification.py:1082: DeprecationWarning: The default `weighted` averaging is deprecated, and from version 0.18, use of precision, recall or F-score with multiclass or multilabel data or pos_label=None will result in an exception. Please set an explicit value for `average`, one of (None, 'micro', 'macro', 'weighted', 'samples'). In cross validation use, for instance, scoring="f1_weighted" instead of scoring="f1".

sample_weight=sample_weight)

0.930416613529

但是,我收到类似这样的警告:/usr/local/lib/python2.7/site-packages/sklearn/metrics/classification.py:1172:DeprecationWarning:defaultweightedaveraging不推荐使用,从版本0.18,使用精度,回忆或F分数与多类或多标签数据或pos_label = None将导致异常。请为平均值设置显式值(无,’micro’,’macro’,’weighted’,’samples’)。在交叉验证中,例如,scoring =“f1_weighted”,而不是scoring =“f1”。如何正确处理我的不平衡数据,以便以正确的方式计算分类器的度量?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值