我的问题:
我有一个数据集,它是一个很大的 JSON 文件。我读取它并将其存储在trainList
多变的。
接下来,我对其进行预处理 - 以便能够使用它。
完成后,我开始分类:
- 我用
kfold
交叉验证方法以获得平均值
准确性并训练分类器。
- 我做出预测并获得该折叠的准确性和混淆矩阵。
- 在此之后,我想获得
True Positive(TP)
, True Negative(TN)
, False Positive(FP)
and False Negative(FN)
价值观。我将使用这些参数来获取灵敏度 and 特异性.
最后,我将使用它放入 HTML 中,以便显示包含每个标签的 TP 的图表。
Code:
我目前拥有的变量:
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
对于多类情况,您需要的一切都可以从混淆矩阵中找到。例如,如果您的混淆矩阵如下所示:
然后,您可以按类找到您要查找的内容,如下所示:
使用 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)