python - 经过训练的 ANN 模型无法正确预测测试数据
问题描述
我有一个训练和测试数据集。我使用 keras 和 tensorflow 训练了 ANN 模型。当测试数据用于预测时,模型将所有输出作为一个类别。代码和混淆指标结果与数据集一起在这里。为什么模型不能以正确的方式工作。
from keras.models import Sequential, model_from_json
from keras.layers import Dense
from keras.utils import np_utils
import numpy
import pandas as pd
# Import training dataset
from sklearn.metrics import confusion_matrix, accuracy_score
training_dataset = pd.read_csv('training_dataset.csv', header=0)
train_x = training_dataset.iloc[:, 0:21].values
train_y = training_dataset.iloc[:, 21].values
# Import testing dataset
test_dataset = pd.read_csv('test_dataset.csv', header=0)
test_x = test_dataset.iloc[:, 0:21].values
test_y = test_dataset.iloc[:, 21].values
# Encoding training dataset
encoding_train_y = np_utils.to_categorical(train_y)
# Encoding testing dataset
encoding_test_y = np_utils.to_categorical(test_y)
# Creating a model
model = Sequential()
model.add(Dense(30, input_dim=21, activation='sigmoid'))
model.add(Dense(30, activation='relu'))
model.add(Dense(30, activation='relu'))
model.add(Dense(6, activation='relu'))
# Compiling model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=
['accuracy'])
# Training a model
model.fit(train_x, encoding_train_y, epochs=1000, batch_size=20)
# Evaluate the model
scores = model.evaluate(test_x, encoding_test_y)
print("Accuracy of the Model : %.2f%%" % (scores[1]*100))
#testing_data = pd.read_csv('testing_dataset.csv', header=0)
#testing_x = testing_data.iloc[:, 0:4].values
#training_y = testing_data.iloc[:, 4].values
predictions = model.predict_classes(test_x)
print(confusion_matrix(test_y,predictions))
print(accuracy_score(test_y, predictions)*100)
print("Predictions : ",predictions)
#rounded = [round(x[0]) for x in predictions]
#print("Predictions : ",rounded)
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# later...
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
我得到的混淆矩阵
> [[ 0 0 0 49 0 0]
[ 0 0 0 50 0 0]
[ 0 0 0 50 0 0]
[ 0 0 0 50 0 0]
[ 0 0 0 50 0 0]
[ 0 0 0 50 0 0]]
16.722408026755854
Predictions : [3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3]
解决方案
推荐阅读
- dask - 为什么对从 Parquet 文件构建的 Dask DataFrame 进行分类会大大增加其大小?
- python - 从数据框中获取一列以将其写入 csv 文件
- java - 如何在另一个类中引用常量的枚举方法
- r - POSIXct 日期对象被 apply() 转换
- javascript - JS类中的方法没有一个函数
- android - 无法在 Mac Visual Studio 中为 Xamarin Forms Android 创建发布存档
- flutter - 颤振 - 使用 image_picker 处理方法调用和 already_active 异常失败
- python-sphinx - Sphinx Autodoc 输出顺序
- intellij-idea - 如何在 IntelliJ IDEA 中隐藏此栏
- python - 使用循环减少代码并使用 tkinter 删除图像