简体   繁体   English

如何查看混淆矩阵中标记为假阳性和假阴性的行

[英]How to view the rows marked as False Positive and False Negative from confusion matrix

I have created a very simple Artificial Neural Network in Python.我用 Python 创建了一个非常简单的人工神经网络。 In the example below I get an accuracy based off of values in a confusion matrix.在下面的示例中,我根据混淆矩阵中的值获得了准确度。 These are results of the confusion matrix:这些是混淆矩阵的结果:

array([[3990,    2],
       [  56,  172]])

I am interested in finding the rows where it was marked as false positive(2) and false negative(56).我有兴趣找到被标记为假阳性(2)和假阴性(56)的行。

The following is my code:以下是我的代码:

#Import the dataset
X = DBF2.iloc[:, 1:2].values
y = DBF2.iloc[:, 2].values

#Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 0] = labelencoder_X_1.fit_transform(X[:, 0])

#Create dummy variables
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
#Remove 2 variables to avoid falling into the dummy variable trap
X = np.delete(X, [0], axis=1)

#Splitting the dataset
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
random_state = 0)

#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

#Make the ANN
import keras
from keras.models import Sequential
from keras.layers import Dense

#Initialising the ANN
classifier = Sequential()

#Adding the input layer and the first hidden layer
classifier.add(Dense(units=200, kernel_initializer='uniform', activation='relu', input_dim=400))

#Adding a second hidden layer
classifier.add(Dense(units=200, kernel_initializer='uniform', 
activation='relu'))

#Adding the output layer
classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

#Compiling the ANN
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

#Fitting the ANN to the training set
classifier.fit(X_train, y_train, batch_size=10, epochs=20)                                

#Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

#Making the confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

#Find accuracy of test set
TruePos = cm[0,0]
FalsePos = cm[0,1]
TrueNeg = cm[1,1]
FalseNeg = cm[1,0]

accuracy = float(TruePos + TrueNeg) / float(TruePos + FalsePos + TrueNeg + FalseNeg)
accuracy = accuracy*100
print "Test Accuracy: " ,accuracy

In order to do that, you can use a mask on ypred and ytest:为此,您可以在 ypred 和 ytest 上使用掩码:

X_test[(y_test == 1) & (y_pred[:,0].T == 0)]
X_test[(y_test == 0) & (y_pred[:,0].T == 1)]

Or if you don't care about separating FN from FP:或者,如果您不关心将 FN 与 FP 分开:

X_test[(y_test != y_pred[:,0].T).T]

A longer but smoother option would be to feed the actual and predicted values into 2 separate lists and generate miss-classifications.更长但更流畅的选择是将实际值和预测值输入 2 个单独的列表并生成错误分类。 Index the miss-classifications and then use the "loc" function to extract that certain row!索引未命中分类,然后使用“loc”函数提取该行!

Mark all row:标记所有行:

def mark_ravel_data(y_true:np.array, predicted:np.array)-> np.array:
    #np.hstack(y_true, predicted)
    
    @numba.jit
    def mark_ravel(y_true, predicted):
        #print (rg, '  val:', y_true, '>>> pred:', predicted,)
        return  y_true | predicted << 1
    
    tp = mark_ravel(True , True   )
    tn = mark_ravel(False , True  )
    fp = mark_ravel(True  , False )
    fn = mark_ravel(False , False )
    print (f'tp:{tp} tn:{tn} fp:{fp} fn:{fn}')

    v_mark_ravel = np.vectorize(mark_ravel, otypes=[int])
    
    
    return v_mark_ravel(y_true=y_true, predicted=predicted)


print ('tp, tn, fp, fn, tp,')
mark_ravel_data(np.array([1,0,1,0, 1]), np.array([1,1,0,0,1]))

output:输出:

tp, tn, fp, fn, tp,
tp:3 tn:2 fp:1 fn:0 
array([3, 2, 1, 0, 3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多类分类的混淆矩阵中提取假阳性,假阴性 - How to extract False Positive, False Negative from a confusion matrix of multiclass classification 从混淆矩阵中获取假阴性、假阳性、真阳性和真阴性的相关数据集 - Getting relevant datasets of false negatives, false positives, true positive and true negative from confusion matrix 有没有办法用已知的真阳性、真阴性、假阳性和假阴性来绘制混淆矩阵? - Is there a way to draw confusion matrix with known True Positive, True Negative, False Positive and False Negative? 混淆矩阵中的误报率 - False Positive Rate in Confusion Matrix 如何计算误报率 (FPR) 和误报率百分比? - How to compute false positive rate (FPR) and False negative rate percantage? Scikit-learn:如何获取True Positive、True Negative、False Positive和False Negative - Scikit-learn: How to obtain True Positive, True Negative, False Positive and False Negative True positive, False positive, False Negative 计算数据帧 python - True positive, False positive, False Negative calculation data frame python 如何找到神经网络的假阳性率和假阴性率? - How do I find the false positive and false negative rates for a neural network? filecmp.cmp()什么时候返回假阳性或假阴性? - When will filecmp.cmp() return a false positive or false negative? 如何从scikit-learn中的混淆矩阵返回假阳性数组? - How to return an array of false positives from a confusion matrix in scikit-learn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM