训练有素的ANN模型无法正确预测测试数据

时间:2018-06-19 07:50:07

标签: python tensorflow keras

我有一个培训和测试数据集。我使用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")

Training and testing data

我得到了混淆矩阵

> [[ 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]

0 个答案:

没有答案