我是keras的新手,
我尝试使用我的数据集遵循多层感知器 (MLP( 的 Keras 教程进行多类 softmax 分类。 我的数据有 3 个类,只有一个特征,但我不明白为什么结果总是只显示 0,3 的准确度,并且模型将所有训练数据预测为第一类。 那么混淆矩阵是这样的。
混淆矩阵
这里的编码:
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import pandas as pd
import numpy as np
# Importing the dataset
dataset = pd.read_csv('StatusAll.csv')
X = dataset.iloc[:, 1:].values
y = dataset.iloc[:, 0:1].values
# Splitting the dataset into the Training set and Test set
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)
from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='tanh', input_dim=1))
model.add(Dropout(0.5))
model.add(Dense(64, activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
history = model.fit(x_train, y_train,
epochs=100,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
print('Test score:', score[0])
print('Test accuracy:', score[1])
from sklearn import metrics
prediction = model.predict(x_test)
prediction = np.around(prediction)
y_test_non_category = [ np.argmax(t) for t in y_test ]
y_predict_non_category = [ np.argmax(t) for t in prediction ]
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test_non_category, y_predict_non_category)
print (conf_mat)
我希望我能得到一些建议,谢谢。
x_train示例 x_train
转换为分类之前的y_train
在此处输入图像描述
您的最终密集层有 4 个输出,似乎您正在分类 4 而不是 3。
model.add(Dense(3, activation='softmax')) # Number of classes 3
查看来自x_train和y_train的样本数据以确保预处理正确会很有帮助。因为您只有 1 个功能,所以 MLP 可能矫枉过正。决策树会更简单,除非您想试验 MLP。