Python 中的二元预测绘图不起作用
Plotting of binary prediction in Python it's not working
我正在尝试使用 Python 为二元模型绘制一些数据,但图表没有显示任何数据,我不明白为什么,我没有错误,代码是运行 非常快,二进制模式的结果是正确的,它向我显示了正确的数据,但没有绘制图表,我不明白为什么...这是我的 python 代码,我收到 ['acc']:
的关键错误
#Building and Training the Neural Network
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
# convert into binary classification problem - heart disease or no heart disease
Y_train_binary = y_train.copy()
Y_test_binary = y_test.copy()
Y_train_binary[Y_train_binary > 0] = 1
Y_test_binary[Y_test_binary > 0] = 1
print(Y_train_binary[:20])
def create_binary_model():
# create model
model = Sequential()
model.add(Dense(16, input_dim=13, kernel_initializer='normal', activation='relu'))
model.add(Dense(8, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
adam = Adam(lr=0.001)
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
return model
binary_model = create_binary_model()
print(binary_model.summary())
# fit the binary model on the training data
history=binary_model.fit(X_train, Y_train_binary, validation_data=(X_test, Y_test_binary), epochs=200, batch_size=10, verbose = 10)
import matplotlib.pyplot as plt
# Model accuracy, here the graph it's not plotted
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()
# Model Losss, here the graph it's not plotted
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()
# generate classification report using predictions for categorical model
from sklearn.metrics import classification_report, accuracy_score
# generate classification report using predictions for binary model
binary_pred = np.round(binary_model.predict(X_test)).astype(int)
print('Results for Binary Model')
print(accuracy_score(Y_test_binary, binary_pred))
print(classification_report(Y_test_binary, binary_pred))
这是图表现在的样子,它没有绘制我的数据:
它应该是这样的...:[=13=]
我其实不知道为什么,但我以前遇到过这个错误:有时模型历史记录中的准确性保持为 acc
,有时保持为 accuracy
。可能与编译模型时的metrics
有关。在您的代码中它是 accuracy
,因此您可以尝试使用:history.history['accuracy']
而不是 acc
。
我正在尝试使用 Python 为二元模型绘制一些数据,但图表没有显示任何数据,我不明白为什么,我没有错误,代码是运行 非常快,二进制模式的结果是正确的,它向我显示了正确的数据,但没有绘制图表,我不明白为什么...这是我的 python 代码,我收到 ['acc']:
的关键错误 #Building and Training the Neural Network
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
# convert into binary classification problem - heart disease or no heart disease
Y_train_binary = y_train.copy()
Y_test_binary = y_test.copy()
Y_train_binary[Y_train_binary > 0] = 1
Y_test_binary[Y_test_binary > 0] = 1
print(Y_train_binary[:20])
def create_binary_model():
# create model
model = Sequential()
model.add(Dense(16, input_dim=13, kernel_initializer='normal', activation='relu'))
model.add(Dense(8, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
adam = Adam(lr=0.001)
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
return model
binary_model = create_binary_model()
print(binary_model.summary())
# fit the binary model on the training data
history=binary_model.fit(X_train, Y_train_binary, validation_data=(X_test, Y_test_binary), epochs=200, batch_size=10, verbose = 10)
import matplotlib.pyplot as plt
# Model accuracy, here the graph it's not plotted
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()
# Model Losss, here the graph it's not plotted
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()
# generate classification report using predictions for categorical model
from sklearn.metrics import classification_report, accuracy_score
# generate classification report using predictions for binary model
binary_pred = np.round(binary_model.predict(X_test)).astype(int)
print('Results for Binary Model')
print(accuracy_score(Y_test_binary, binary_pred))
print(classification_report(Y_test_binary, binary_pred))
这是图表现在的样子,它没有绘制我的数据:
它应该是这样的...:[=13=]
我其实不知道为什么,但我以前遇到过这个错误:有时模型历史记录中的准确性保持为 acc
,有时保持为 accuracy
。可能与编译模型时的metrics
有关。在您的代码中它是 accuracy
,因此您可以尝试使用:history.history['accuracy']
而不是 acc
。