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

Giz*_*lly 4 python machine-learning scikit-learn

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

首先,我收到一条错误消息:““AttributeError: predict_proba is not available whenprobability=False”“所以我用它来纠正:

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

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

array([[   0,    0],
       [5359,   65]])
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题。

我需要帮助,我是新手。谢谢

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()
Run Code Online (Sandbox Code Playgroud)

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

Giz*_*lly 9

这段代码产生了预期的输出,除了在前面的代码中我错误地使用了混淆矩阵的事实之外,我还应该使用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]])
Run Code Online (Sandbox Code Playgroud)

  • 希望您稍微更改了参数,以便此代码不包含 Coursera 课程中作业之一的确切答案:) (2认同)