我有2599个文件,从1到5的文件五个标记文本分类任务是如何使用scikit-learn和matplotlib绘制不平衡数据集的SVC分类?
label | texts
----------
5 |1190
4 |839
3 |239
1 |204
2 |127
所有准备将此划文本具有非常低的性能数据,并同时获得约不明确的指标警告:
Accuracy: 0.461057692308
score: 0.461057692308
precision: 0.212574195636
recall: 0.461057692308
'precision', 'predicted', average, warn_for)
confussion matrix:
[[ 0 0 0 0 153]
'precision', 'predicted', average, warn_for)
[ 0 0 0 0 94]
[ 0 0 0 0 194]
[ 0 0 0 0 680]
[ 0 0 0 0 959]]
clasification report:
precision recall f1-score support
1 0.00 0.00 0.00 153
2 0.00 0.00 0.00 94
3 0.00 0.00 0.00 194
4 0.00 0.00 0.00 680
5 0.46 1.00 0.63 959
avg/total 0.21 0.46 0.29 2080
显然,这是事实,我有一个不平衡的数据集发生的事情,所以我发现这个paper这里作者提出了几个aproaches来解决这个问题:
The problem is that with imbalanced datasets, the learned boundary is too close to the positive instances. We need to bias SVM in a way that will push the boundary away from the positive instances. Veropoulos et al [14] suggest using different error costs for the positive (C +) and negative (C -) classes
我知道,这可能是非常复杂的,但SVC提供了几个超参数,所以我的问题是:有没有什么办法偏见SVC在与超参数,提供SVC分类推边界远离独到之处实例的方式?。我知道这可能是一个困难的问题,但任何帮助是值得欢迎的,这要归功于前辈们。
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
tfidf_vect= TfidfVectorizer(use_idf=True, smooth_idf=True, sublinear_tf=False, ngram_range=(2,2))
from sklearn.cross_validation import train_test_split, cross_val_score
import pandas as pd
df = pd.read_csv('/path/of/the/file.csv',
header=0, sep=',', names=['id', 'text', 'label'])
reduced_data = tfidf_vect.fit_transform(df['text'].values)
y = df['label'].values
from sklearn.decomposition.truncated_svd import TruncatedSVD
svd = TruncatedSVD(n_components=5)
reduced_data = svd.fit_transform(reduced_data)
from sklearn import cross_validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(reduced_data,
y, test_size=0.33)
#with no weights:
from sklearn.svm import SVC
clf = SVC(kernel='linear', class_weight={1: 10})
clf.fit(reduced_data, y)
prediction = clf.predict(X_test)
w = clf.coef_[0]
a = -w[0]/w[1]
xx = np.linspace(-5, 5)
yy = a * xx - clf.intercept_[0]/w[1]
# get the separating hyperplane using weighted classes
wclf = SVC(kernel='linear', class_weight={1: 10})
wclf.fit(reduced_data, y)
ww = wclf.coef_[0]
wa = -ww[0]/ww[1]
wyy = wa * xx - wclf.intercept_[0]/ww[1]
# plot separating hyperplanes and samples
import matplotlib.pyplot as plt
h0 = plt.plot(xx, yy, 'k-', label='no weights')
h1 = plt.plot(xx, wyy, 'k--', label='with weights')
plt.scatter(reduced_data[:, 0], reduced_data[:, 1], c=y, cmap=plt.cm.Paired)
plt.legend()
plt.axis('tight')
plt.show()
但我什么也没有,我不能明白发生了什么,这是剧情:
则:
#Let's show some metrics[unweighted]:
from sklearn.metrics.metrics import precision_score, \
recall_score, confusion_matrix, classification_report, accuracy_score
print '\nAccuracy:', accuracy_score(y_test, prediction)
print '\nscore:', clf.score(X_train, y_train)
print '\nrecall:', recall_score(y_test, prediction)
print '\nprecision:', 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)
#Let's show some metrics[weighted]:
print 'weighted:\n'
from sklearn.metrics.metrics import precision_score, \
recall_score, confusion_matrix, classification_report, accuracy_score
print '\nAccuracy:', accuracy_score(y_test, prediction)
print '\nscore:', wclf.score(X_train, y_train)
print '\nrecall:', recall_score(y_test, prediction)
print '\nprecision:', 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)
这是即时通讯使用的data。我该如何解决这个问题,并以正确的方式策划这个问题?在此先感谢家伙!
从这个问题的答案我删除此行:
#
# from sklearn.decomposition.truncated_svd import TruncatedSVD
# svd = TruncatedSVD(n_components=5)
# reduced_data = svd.fit_transform(reduced_data)
#
# w = clf.coef_[0]
# a = -w[0]/w[1]
# xx = np.linspace(-10, 10)
# yy = a * xx - clf.intercept_[0]/w[1]
# ww = wclf.coef_[0]
# wa = -ww[0]/ww[1]
# wyy = wa * xx - wclf.intercept_[0]/ww[1]
#
# # plot separating hyperplanes and samples
# import matplotlib.pyplot as plt
# h0 = plt.plot(xx, yy, 'k-', label='no weights')
# h1 = plt.plot(xx, wyy, 'k--', label='with weights')
# plt.scatter(reduced_data[:, 0], reduced_data[:, 1], c=y, cmap=plt.cm.Paired)
# plt.legend()
#
# plt.axis('tight')
# plt.show()
This where the results:
Accuracy: 0.787878787879
score: 0.779437105112
recall: 0.787878787879
precision: 0.827705441238
这个指标的改善。 如何绘制这个结果以获得像文档一样的好例子。我想看看两架超飞机的行为?。多谢你们!
'显然这是由于我有一个不平衡的数据集的事实发生的 - 根据你所说的,我根本没有发现这一点。你能告诉我们你的代码甚至数据吗? – IVlad 2015-02-12 11:05:14
没有SVD和没有触及class_weight参数,你会得到什么?尝试首先关注性能,然后再进行绘图。 – IVlad 2015-02-15 09:17:10
@Ivlad不使用从不平衡数据集这个文档的例子是我得到的性能:'精度:0.461057692308 评分:0.461057692308 精度:0.212574195636 召回:0.461057692308'这是我可以用网格搜索做到最好。 – tumbleweed 2015-02-15 18:10:20