gpt4 book ai didi

python - 如何在SVC模型中设置特定阈值并生成混淆矩阵?

转载 作者:行者123 更新时间:2023-11-30 09:30:53 25 4
gpt4 key购买 nike

我需要将值设置为特定阈值并生成混淆矩阵。数据位于 csv 文件(11.1 MB)中,下载链接为:https://drive.google.com/file/d/1cQFp7HteaaL37CefsbMNuHqPzkINCVzs/view?usp=sharing

首先,我收到一条错误消息:“AttributeError:当probability=False时predict_proba不可用”所以我用它来修正:

svc = SVC(C=1e9,gamma= 1e-07)
scv_calibrated = CalibratedClassifierCV(svc)
svc_model = scv_calibrated.fit(X_train, y_train)

我在互联网上看到了很多,但不太明白特定的阈值是如何个性化的。听起来很难。现在,我看到错误的输出:

array([[   0,    0],
[5359, 65]])

我不知道出了什么问题。

我需要帮助,而且我是这方面的新手。谢谢

from sklearn.model_selection import train_test_split

df = pd.read_csv('fraud_data.csv')

X = df.iloc[:,:-1]
y = df.iloc[:,-1]

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)



def answer_four():
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn.model_selection import train_test_split


svc = SVC(C=1e9,gamma= 1e-07)
scv_calibrated = CalibratedClassifierCV(svc)
svc_model = scv_calibrated.fit(X_train, y_train)

# set threshold as -220
y_pred = (svc_model.predict_proba(X_test)[:,1] >= -220)

conf_matrix = confusion_matrix(y_pred, svc_model.predict(X_test))

return conf_matrix
answer_four()

这个函数应该返回一个混淆矩阵,一个包含 4 个整数的 2x2 numpy 数组。

最佳答案

这段代码产生了预期的输出,除了在之前的代码中我错误地使用了混淆矩阵这一事实之外,我还应该使用 Decision_function 并获得过滤 220 阈值的输出。

def answer_four():
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn.model_selection import train_test_split

#SVC without mencions of kernel, the default is rbf
svc = SVC(C=1e9, gamma=1e-07).fit(X_train, y_train)

#decision_function scores: Predict confidence scores for samples
y_score = svc.decision_function(X_test)

#Set a threshold -220
y_score = np.where(y_score > -220, 1, 0)
conf_matrix = confusion_matrix(y_test, y_score)

####threshold###
#input threshold in the model after trained this model
#threshold is a limiar of separation of class

return conf_matrix

answer_four()
#output:
array([[5320, 24],
[ 14, 66]])

关于python - 如何在SVC模型中设置特定阈值并生成混淆矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58545711/

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