NameError:名称“fit_classifier”未定义
Posted
技术标签:
【中文标题】NameError:名称“fit_classifier”未定义【英文标题】:NameError: name 'fit_classifier' is not defined 【发布时间】:2019-01-26 06:02:15 【问题描述】:我正在尝试制作一个文本分类器
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))
但我有以下错误,我无法理解。
Traceback(最近一次调用最后一次):
文件“classificacao.py”,第 37 行,在 fit_classifier(X_train, y_train)
但是 fit 并不总是默认定义的?
【问题讨论】:
【参考方案1】:你正在调用一个不存在的函数:
fit_classifier(X_train, y_train)
为了适合你的分类器,你会使用
classifier.fit(X_train, y_train)
相反。 尝试预测测试数据时,您会遇到同样的错误。 你需要改变
预测 = 预测(X_test)
到
predicted = classifier.predict(X_test)
你的 Confusionmatrix 应该得到你的标签,而不是你的测试数据:
打印(confusion_matrix(y_test,预测,标签=标签))
【讨论】:
以上是关于NameError:名称“fit_classifier”未定义的主要内容,如果未能解决你的问题,请参考以下文章
如何修复“NameError:名称方法名称未定义”? [复制]