NameError:名称“ fit_classifier”未定义

时间:2018-08-20 06:26:47

标签: python python-3.x scikit-learn classification text-classification

我正在尝试做一个文本分类器

import pandas as pd
import pandas
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsOneClassifier
from sklearn.svm import SVC
from sklearn import cross_validation
from sklearn.metrics import confusion_matrix

dataset = pd.read_csv('data.csv', encoding = 'utf-8')
data = dataset['text']
labels = dataset['label']

X_train, X_test, y_train, y_test = train_test_split (data, labels, test_size = 0.2, random_state = 0)

count_vector = CountVectorizer()
tfidf = TfidfTransformer()

classifier = OneVsOneClassifier(SVC(kernel = 'linear', random_state = 84))

train_counts = count_vector.fit_transform(X_train)
train_tfidf = tfidf.fit_transform(train_counts)
classifier.fit(train_tfidf, y_train)

test_counts = count_vector.transform(X_test)
test_tfidf = tfidf.transform(test_counts)
classifier.predict(test_tfidf)

fit_classifier(X_train, y_train)
predicted = predict(X_test)

print("confusion matrix")
print(confusion_matrix(X_test, predicted, labels = labels))

print("cross validation")
test_counts = count_vector.fit_transform(data)
test_tfidf = tfidf.fit_transform(test_counts)

scores = cross_validation.cross_val_score(classifier, test_tfidf, labels, cv = 10)
print(scores)
print("Accuracy: {} +/- {}".format(scores.mean(), scores.std() * 2))

但是我有以下错误,我无法理解。

  

回溯(最近通话最近一次):

     

文件“ classificacao.py”,第37行,在       fit_classifier(X_train,y_train)

     

NameError:名称'fit_classifier'未定义

但是 fit 并不是默认情况下总是定义的吗?

1 个答案:

答案 0 :(得分:4)

您正在调用一个不存在的函数:

  

fit_classifier(X_train,y_train)

要适合您的分类器,您将使用

  

classifier.fit(X_train,y_train)

相反。 尝试预测测试数据时,您会得到相同的错误。 您需要更改

  

预测=预测(X_test)

  

predicted = classifier.predict(X_test)

您的Confusionmatrix应该得到您的标签,而不是您的测试数据:

  

print(confusion_matrix( y_test ,预测,标签=标签)