私の問題:
大きな JSON ファイルであるデータセットがあります。それを読み取って変数に保存しますtrainList
。
次に、作業できるように前処理を行います。
それが終わったら分類を始めます。
kfold
平均精度を取得し、分類器をトレーニングするために、クロス検証法を使用します。- 予測を行い、そのフォールドの精度と混同行列を取得します。
True Positive(TP)
この後、、、の値True Negative(TN)
を取得したいと思います。これらのパラメータを使用して、False Positive(FP)
False Negative(FN)
感度そして特異性。
最後に、これを 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
ベストアンサー1
マルチクラスの場合、必要なものはすべて混同行列から見つけることができます。たとえば、混同行列が次のようになっているとします。
クラスごとに探しているものは次のように見つかります。
pandas/numpy を使用すると、次のようにすべてのクラスに対してこれを一度に実行できます。
FP = confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)
FN = confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)
TP = np.diag(confusion_matrix)
TN = confusion_matrix.values.sum() - (FP + FN + TP)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# False discovery rate
FDR = FP/(TP+FP)
# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)