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

标签 python machine-learning scikit-learn

我需要将值设置为特定阈值并生成混淆矩阵。数据位于 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/

相关文章:

python-3.x - 有没有一种方法可以使用 scikit learn 的无监督方法将某些列表分类为不同的组?

python - 从数据列表中创建矩阵列表

python - 如何在 Tensorflow 运行时查看或保存一个张量?

python - sklearn MLPRegressor 的 Tensorflow 副本产生其他结果

python - cv_result中的 'mean_test_score'是什么意思?

matlab - 梯度下降法和正规方程法求解线性回归给出不同的解

python - python中的 "test case failed"是什么?

python - 尽可能高效地在 Python 中叠加混合模式(Numpy、OpenCV)

Python:如何找到最频繁的元素组合?

python-2.7 - 如何在 python scikit-learn 中优化精确召回曲线而不是 AUC-ROC 曲线?