我用Python创建了一个非常简单的人工神经网络。在下面的示例中,我基于混淆矩阵中的值获得了准确性。这些是混淆矩阵的结果:
array([[3990, 2],
[ 56, 172]])
我有兴趣查找标记为误报(2)和误报(56)的行。
以下是我的代码:
#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
答案 0 :(得分:2)
为此,您可以在ypred和ytest上使用蒙版:
X_test[(y_test == 1) & (y_pred[:,0].T == 0)]
X_test[(y_test == 0) & (y_pred[:,0].T == 1)]
或者如果您不在乎将FN与FP分开:
X_test[(y_test != y_pred[:,0].T).T]
答案 1 :(得分:0)
更长但更平滑的选择是将实际值和预测值输入2个单独的列表中并生成未分类。 为未分类分类建立索引,然后使用“ loc”功能提取特定行!
答案 2 :(得分:0)
标记所有行:
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]))
输出:
tp, tn, fp, fn, tp,
tp:3 tn:2 fp:1 fn:0
array([3, 2, 1, 0, 3])