【发布时间】:2019-09-11 23:24:10
【问题描述】:
我是 python 新手,正在研究文本分类问题。我对通过线性 SVM 分类器模型对每个类的最重要特征进行可视化感兴趣。我想通过分类模型确定哪些特征有助于分类决策为 Class-1 或 Class-2。这是我的代码。
df = pd.read_csv('projectdatacor.csv')
df = df[pd.notnull(df['types'])]
my_types = ['Requirement','Non-Requirement']
#converting to lower case
df['description'] = df.description.map(lambda x: x.lower())
#Removing the punctuation
df['description'] = df.description.str.replace('[^\w\s]', '')
#splitting the word into tokens
df['description'] = df['description'].apply(nltk.tokenize.word_tokenize)
## This converts the list of words into space-separated strings
df['description'] = df['description'].apply(lambda x: ' '.join(x))
count_vect = CountVectorizer()
counts = count_vect.fit_transform(df['description'])
#tf-idf
transformer = TfidfTransformer().fit(counts)
counts = transformer.transform(counts)
#splitting the data and training the model
#naives-bayes
X_train, X_test, y_train, y_test = train_test_split(counts, df['types'], test_size=0.3, random_state=39)
#svc classification
from sklearn import svm
svclassifier = svm.SVC(gamma=0.001, C=100., kernel = 'linear')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
#evalutaing the model
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
print('accuracy %s' % accuracy_score(y_pred, y_test))
print(classification_report(y_test, y_pred,target_names=my_types))
我已阅读此平台上所有可用的相关问题,但我发现以下有用的代码已添加到我的代码中。
import numpy as np
def show_most_informative_features(vectorizer, clf, n=20):
feature_names = vectorizer.get_feature_names()
coefs_with_fns = sorted(zip(clf.coef_[0], feature_names))
top = zip(coefs_with_fns[:n], coefs_with_fns[:-(n + 1):-1])
for (coef_1, fn_1), (coef_2, fn_2) in top:
print ("\t%.4f\t%-15s\t\t%.4f\t%-15s") % (coef_1, fn_1, coef_2, fn_2)
show_most_informative_features(count_vect, svclassifier, 20)
此代码适用于朴素贝叶斯和逻辑回归,它提供了最重要的功能,但对于 SVM,它给了我错误。
我收到此错误。
File "C:\Users\fhassan\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\fhassan\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "U:/FAHAD UL HASSAN/Python Code/happycsv.py", line 209, in <module>
show_most_informative_features(count_vect, svclassifier, 20)
File "U:/FAHAD UL HASSAN/Python Code/happycsv.py", line 208, in show_most_informative_features
print ("\t%.4f\t%-15s\t\t%.4f\t%-15s" % (coef_1, fn_1, coef_2, fn_2))
TypeError: must be real number, not csr_matrix
任何帮助将不胜感激。
【问题讨论】:
标签: python machine-learning scikit-learn svm text-classification