我正在使用 NLP 测试不同的分类模型,并注意到我的混淆矩阵指标没有相加。有没有人见过这个;我错过了什么吗?
X = df[['text']]
y = df[['class']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .10, random_state = 0, stratify = y)
y_test.value_counts()
class
0 20
1 14
dtype: int64
def model_classifiers (model, ModelName):
classifier = model
classifier.fit(X_train, y_train.values.ravel())
y_pred = classifier.predict(X_test)
#y_proba = pd.DataFrame(classifier.predict_proba(X_test))
print(ModelName)
print(accuracy_score(y_train, classifier.predict(X_train)))
print(accuracy_score(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
model_classifiers(LinearSVC(), 'LINEAR SVC')
LINEAR SVC
1.0
0.6176470588235294
[[16 4]
[ 9 5]]
precision recall f1-score support
0 0.64 0.80 0.71 20
1 0.56 0.36 0.43 14
accuracy 0.62 34
macro avg 0.60 0.58 0.57 34
weighted avg 0.61 0.62 0.60 34
model_classifiers(LogisticRegression(penalty = 'l2'), 'LOGISTIC REGRESSION')
LOGISTIC REGRESSION
0.9833333333333333
0.5882352941176471
[[16 4]
[10 4]]
precision recall f1-score support
0 0.62 0.80 0.70 20
1 0.50 0.29 0.36 14
accuracy 0.59 34
macro avg 0.56 0.54 0.53 34
weighted avg 0.57 0.59 0.56 34
线性 SVC --> 组 1:16+9 = 25,组 2:4+5 = 9 逻辑回归 --> group1 = 16+10 = 26, group2: 4+4=8