指标 F1 警告零除

new*_*ie1 6 python metrics classification machine-learning scikit-learn

我想计算我的模型的 F1 分数。但我收到警告并得到 0.0 F1 分数,但我不知道该怎么办。

这是源代码:

def model_evaluation(dict):

    for key,value in dict.items():

        classifier = Pipeline([('tfidf', TfidfVectorizer()),
                         ('clf', value),
    ])
        classifier.fit(X_train, y_train)
        predictions = classifier.predict(X_test)
        print("Accuracy Score of" , key ,  ": ", metrics.accuracy_score(y_test,predictions))
        print(metrics.classification_report(y_test,predictions))
        print(metrics.f1_score(y_test, predictions, average="weighted", labels=np.unique(predictions), zero_division=0))
        print("---------------","\n")


dlist =  { "KNeighborsClassifier": KNeighborsClassifier(3),"LinearSVC":
    LinearSVC(), "MultinomialNB": MultinomialNB(), "RandomForest": RandomForestClassifier(max_depth=5, n_estimators=100)}

model_evaluation(dlist)
Run Code Online (Sandbox Code Playgroud)

这是结果:

Accuracy Score of KNeighborsClassifier :  0.75
              precision    recall  f1-score   support

not positive       0.71      0.77      0.74        13
    positive       0.79      0.73      0.76        15

    accuracy                           0.75        28
   macro avg       0.75      0.75      0.75        28
weighted avg       0.75      0.75      0.75        28

0.7503192848020434
--------------- 

Accuracy Score of LinearSVC :  0.8928571428571429
              precision    recall  f1-score   support

not positive       1.00      0.77      0.87        13
    positive       0.83      1.00      0.91        15

    accuracy                           0.89        28
   macro avg       0.92      0.88      0.89        28
weighted avg       0.91      0.89      0.89        28

0.8907396950875212
--------------- 

Accuracy Score of MultinomialNB :  0.5357142857142857
              precision    recall  f1-score   support

not positive       0.00      0.00      0.00        13
    positive       0.54      1.00      0.70        15

    accuracy                           0.54        28
   macro avg       0.27      0.50      0.35        28
weighted avg       0.29      0.54      0.37        28

0.6976744186046512
--------------- 

C:\Users\Cey\anaconda3\lib\site-packages\sklearn\metrics\_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
Accuracy Score of RandomForest :  0.5714285714285714
              precision    recall  f1-score   support

not positive       1.00      0.08      0.14        13
    positive       0.56      1.00      0.71        15

    accuracy                           0.57        28
   macro avg       0.78      0.54      0.43        28
weighted avg       0.76      0.57      0.45        28

0.44897959183673475
--------------- 
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我该怎么做吗?我仅在使用“MultinomialNB()”分类器时收到此消息


第二:

当使用高斯分类器 (GaussianNB()) 扩展字典时,我收到以下错误消息:

TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
Run Code Online (Sandbox Code Playgroud)

我应该在这里做什么?

yat*_*atu 1

有人可以告诉我该怎么做吗?我仅在使用“MultinomialNB()”分类器时收到此消息

第一个错误似乎表明使用 时未预测特定标签MultinomialNB,这会导致未定义f-score定义不明确,因为缺失值设置为0这里解释一下

当使用高斯分类器 (GaussianNB()) 扩展字典时,我收到此错误消息: TypeError: 传递了稀疏矩阵,但需要密集数据。使用 X.toarray() 转换为密集 numpy 数组。

根据这个问题,错误非常明确,问题是TfidfVectorizer返回一个sparse矩阵,该矩阵不能用作GaussianNB. 因此,在我看来,您要么避免使用GaussianNB,要么添加一个中间变换器将稀疏数组转换为密集数组,我不建议将其作为矢量化的结果tf-idf