温馨提示×

Pandas中怎么调整分类模型的阈值

小亿
85
2024-05-13 10:56:54
栏目: 编程语言

Pandas是一个用于数据分析和处理的Python库,不直接提供调整分类模型阈值的功能。调整分类模型的阈值通常是在使用机器学习库(如scikit-learn)训练模型后进行的。

在scikit-learn中,可以使用predict_proba()方法来获取模型预测的概率值,然后根据需要调整阈值。以下是一个示例代码:

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# 假设已经训练好了一个逻辑回归模型
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)

# 获取测试集的预测概率值
probs = model.predict_proba(X_test)[:,1]

# 自定义阈值
threshold = 0.6
y_pred = [1 if prob >= threshold else 0 for prob in probs]

# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy with threshold {}: {}".format(threshold, accuracy))

在上面的示例中,我们首先使用predict_proba()方法获取模型预测的概率值,然后根据自定义的阈值(这里设为0.6)将概率值转换为二元分类结果,最后计算准确率。根据实际情况可调整阈值来获得最佳模型性能。

0