88

私の問題:

大きな JSON ファイルであるデータセットがあります。それを読んで変数に格納しtrainListます。

次に、それを操作できるようにするために、前処理を行います。

それが完了したら、分類を開始します。

  1. kfold平均精度を取得し、分類器をトレーニングするために、交差検証法を使用します。
  2. 私は予測を行い、そのフォールドの精度と混同行列を取得します。
  3. True Positive(TP)この後、 、True Negative(TN)False Positive(FP)およびFalse Negative(FN)値を取得したいと思います。これらのパラメータを使用してSensitivitySpecificityを取得します。

最後に、これを使用して HTML を挿入し、各ラベルの TP を含むグラフを表示します。

コード:

私が今のところ持っている変数:

trainList #It is a list with all the data of my dataset in JSON form
labelList #It is a list with all the labels of my data 

メソッドの大部分:

#I transform the data from JSON form to a numerical one
X=vec.fit_transform(trainList)

#I scale the matrix (don't know why but without it, it makes an error)
X=preprocessing.scale(X.toarray())

#I generate a KFold in order to make cross validation
kf = KFold(len(X), n_folds=10, indices=True, shuffle=True, random_state=1)

#I start the cross validation
for train_indices, test_indices in kf:
    X_train=[X[ii] for ii in train_indices]
    X_test=[X[ii] for ii in test_indices]
    y_train=[listaLabels[ii] for ii in train_indices]
    y_test=[listaLabels[ii] for ii in test_indices]

    #I train the classifier
    trained=qda.fit(X_train,y_train)

    #I make the predictions
    predicted=qda.predict(X_test)

    #I obtain the accuracy of this fold
    ac=accuracy_score(predicted,y_test)

    #I obtain the confusion matrix
    cm=confusion_matrix(y_test, predicted)

    #I should calculate the TP,TN, FP and FN 
    #I don't know how to continue
4

19 に答える 19

58

予測値と実際の値を持つ 2 つのリストがある場合。あなたがしているように、次のようなもので TP、FP、TN、FN を計算する関数にそれらを渡すことができます。

def perf_measure(y_actual, y_hat):
    TP = 0
    FP = 0
    TN = 0
    FN = 0

    for i in range(len(y_hat)): 
        if y_actual[i]==y_hat[i]==1:
           TP += 1
        if y_hat[i]==1 and y_actual[i]!=y_hat[i]:
           FP += 1
        if y_actual[i]==y_hat[i]==0:
           TN += 1
        if y_hat[i]==0 and y_actual[i]!=y_hat[i]:
           FN += 1

    return(TP, FP, TN, FN)

ここから、金利や、特異度や感度などの他のパフォーマンス指標を計算できると思います。

于 2015-07-10T22:14:26.473 に答える
50

scikit-learn のドキュメントによると、

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html#sklearn.metrics.confusion_matrix

定義により、混同行列 C は、 group 内にあることがわかっているが、 group 内にあると予測さC[i, j]れる観測値の数に等しいようなものです。ij

したがって、バイナリ分類では、真陰性の数はC[0,0]、偽陰性はC[1,0]、真陽性はC[1,1]、偽陽性はC[0,1]です。

CM = confusion_matrix(y_true, y_pred)

TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
于 2016-10-29T22:09:28.930 に答える
2

分類子に複数のクラスがある場合は、その部分で pandas-ml を使用することをお勧めします。pandas-ml の混同行列は、より詳細な情報を提供します。それを確認します

結果

于 2017-02-16T19:06:31.970 に答える
1
#False positive cases
train = pd.merge(X_train, y_train,left_index=True, right_index=True)
y_train_pred = pd.DataFrame(y_train_pred)
y_train_pred.rename(columns={0 :'Predicted'}, inplace=True )
train = train.reset_index(drop=True).merge(y_train_pred.reset_index(drop=True),
left_index=True,right_index=True)
train['FP'] = np.where((train['Banknote']=="Forged") & (train['Predicted']=="Genuine"),1,0)
train[train.FP != 0]
于 2020-10-20T04:31:20.803 に答える