在度量的准确性计算中会出现错误。我导入了该库以计算准确性指标,但仍然出现错误,未定义指标名称
from sklearn.feature_extraction.text import TfidfVectorizer
tf_idf_vect = TfidfVectorizer()
reviews_tfidf = tf_idf_vect.fit_transform(x_train['question_text'].values)
reviews_tfidf1 = tf_idf_vect.transform(x_cv['question_text'].values)
reviews_tfidf2 = tf_idf_vect.transform(test['question_text'].values)
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import BernoulliNB
import sklearn.metrics import accuracy_score
b = BernoulliNB()
param_grid = {'alpha':[1000,100,10,1,0.1,0.01,0.001]} #params we need to try on classifier
gsv = GridSearchCV(nb,param_grid,cv=2,verbose=1,n_jobs=-1,scoring='f1')
gsv.fit(reviews_tfidf,y_train)
nb = BernoulliNB(alpha=0.1)
nb.fit(reviews_tfidf,y_train)
train_pred = nb.predict(reviews_tfidf)
cv_pred = nb.predict(reviews_tfidf1)
test_pred = nb.predict(reviews_tfidf2)
print("Train Set Accuracy: {}".format(metrics.accuracy_score(train_pred, y_train)))
print("Train Set ROC: {}".format(metrics.roc_auc_score(train_pred, y_train)))
print("Train Set F1 Score: {}\n".format(metrics.f1_score(train_pred, y_train)))
print("Validation Set Accuracy: {}".format(metrics.accuracy_score(cv_pred, y_cv)))
print("Validation Set ROC: {}".format(metrics.roc_auc_score(cv_pred, y_cv)))
print("Validation Set F1 Score: {}\n".format(metrics.f1_score(cv_pred, y_cv)))
print("Confusion Matrix of test set:\n [ [TN FP]\n [FN TP] ]\n")
df_cm = pd.DataFrame(confusion_matrix(y_test, y_pred), range(2),range(2))
sns.set(font_scale=1.4)#for label size
sns.heatmap(df_cm, annot=True,annot_kws={"size": 16}, fmt='g')
NameErrorTraceback (most recent call last)
<ipython-input-53-061462cd9519> in <module>()
16 cv_pred = nb.predict(reviews_tfidf1)
17 test_pred = nb.predict(reviews_tfidf2)
---> 18 print("Train Set Accuracy: {}".format(metrics.accuracy_score(train_pred, y_train)))
19 print("Train Set ROC: {}".format(metrics.roc_auc_score(train_pred, y_train)))
20 print("Train Set F1 Score: {}\n".format(metrics.f1_score(train_pred, y_train)))
NameError: name 'metrics' is not defined
答案 0 :(得分:1)
尝试更改
import sklearn.metrics import accuracy_score
至:
import sklearn.metrics as metrics.
请查看以下内容,以更好地了解导入的工作方式:https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1