我正在研究二元分类模型,分类器是天真的贝叶斯.我有一个几乎平衡的数据集,但是当我预测时,我收到以下错误消息:
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
Run Code Online (Sandbox Code Playgroud)
我正在使用带有CV k-fold 10的gridsearch.测试集和预测包含两个类,所以我不理解该消息.我正在为其他6个模型研究相同的数据集,训练/测试分裂,cv和随机种子,这些工作非常完美.数据被外部摄取到数据帧中,随机化并且种子是固定的.然后,朴素贝叶斯分类模型将该文件放在此代码片段之前的开头.
X_train, X_test, y_train, y_test, len_train, len_test = \
train_test_split(data['X'], data['y'], data['len'], test_size=0.4)
pipeline = Pipeline([
('classifier', MultinomialNB())
])
cv=StratifiedKFold(len_train, n_folds=10)
len_train = len_train.reshape(-1,1)
len_test = len_test.reshape(-1,1)
params = [
{'classifier__alpha': [0, 0.0001, 0.001, 0.01]}
]
grid = GridSearchCV(
pipeline,
param_grid=params,
refit=True,
n_jobs=-1,
scoring='accuracy',
cv=cv,
)
nb_fit = grid.fit(len_train, y_train)
preds = nb_fit.predict(len_test)
print(confusion_matrix(y_test, preds, labels=['1','0'])) …Run Code Online (Sandbox Code Playgroud) 我试图使用SciKit的Logistic回归来预测一组标签.我的数据实际上是不平衡的(有更多'0'而不是'1'标签)所以我必须在交叉验证步骤中使用F1得分指标来"平衡"结果.
[Input]
X_training, y_training, X_test, y_test = generate_datasets(df_X, df_y, 0.6)
logistic = LogisticRegressionCV(
Cs=50,
cv=4,
penalty='l2',
fit_intercept=True,
scoring='f1'
)
logistic.fit(X_training, y_training)
print('Predicted: %s' % str(logistic.predict(X_test)))
print('F1-score: %f'% f1_score(y_test, logistic.predict(X_test)))
print('Accuracy score: %f'% logistic.score(X_test, y_test))
[Output]
>> Predicted: [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]
>> Actual: [0 0 0 1 0 0 0 0 0 1 1 0 …Run Code Online (Sandbox Code Playgroud) python machine-learning scikit-learn cross-validation logistic-regression
我该如何解决这个错误?
警告:精度和 F 分数定义不明确,在没有预测样本的标签中设置为 0.0。使用
zero_division参数来控制此行为。_warn_prf(平均值,修饰符,msg_start,len(结果))
当我添加 Adam 的调整参数时出现错误。
# Tuning parameter from keras.optimizers import Adam
optimize = Adam(learning_rate=0.00001, beta_1=0.9, beta_2=0.99)
model.compile(optimizer=optimize, loss='categorical_crossentropy', metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
这段代码的错误是什么?
from sklearn.metrics import confusion_matrix, classification_report
prediksi = model.predict(test_data_generator)
y_pred = np.argmax(prediksi, axis=1)
print(confusion_matrix(test_data_generator.classes, y_pred))
print(classification_report(test_data_generator.classes, y_pred))
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用labels=np.unique(y_pred),但结果并没有显示 的准确性值。
我想计算我的模型的 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 …Run Code Online (Sandbox Code Playgroud) 我在sklearn中使用F1_score指标。对于某些训练数据集,y = 1(罕见情况)的总数为零,F1_score为零,这是正常的。但是sklearn发出以下警告:
“ UndefinedMetricWarning:F得分定义不正确,由于没有预测样本,因此将其设置为0.0”。
有人知道如何使此警告静音吗?总的来说,我们可以让sklearn中的所有警告静音吗?