gpt4 book ai didi

python-3.x - 名称错误 : name 'metrics' is not defined

转载 作者:行者123 更新时间:2023-12-05 08:31:30 25 4
gpt4 key购买 nike

它会在计算指标的准确性时产生错误。我导入了库来计算准确度指标,但它仍然给我错误,即指标名称未定义

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

最佳答案

努力改变

import sklearn.metrics import accuracy_score 

到:

import sklearn.metrics as metrics

Take a look at this for a better understanding on how imports work: https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1

关于python-3.x - 名称错误 : name 'metrics' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57671229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com