我有一个数据集df_train,具有6个要素和一个多类目标要素,该要素由3个类组成。
以下链接包含火车数据和测试数据 https://drive.google.com/drive/folders/1S4y15BOKmKBE2i2U6bl2NMcTySrtFVkt?usp=sharing
但是使用多类分类,我只能得到1类输出。有人可以帮我指出错误吗?
代码:
#Building the model
from keras.models import Sequential
from keras.layers import Dense
model=Sequential()
# Add 3 dense layers of 128, 64 and 32 neurons each
model.add(Dense(128, input_shape=(6,), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
# Add a dense layer with as many neurons as target variables
model.add(Dense(3, activation='softmax'))
# Compile your model using categorical_crossentropy loss
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#Preparing the dataset
df_train.category = pd.Categorical(df_train.category)
df_train.category = df_train.category.cat.codes
# Print the label encoded target variables
print('Label encoded target variables: \n',df_train.category.head())
# Import to_categorical from keras utils module
from keras.utils import to_categorical
data = df_train.drop(['category'], axis=1)
# Use to_categorical on your labels
category = to_categorical(df_train.category)
# Now print the one-hot encoded labels
# print('One-hot encoded category: \n',category)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, category, test_size=0.33, random_state=42)
# Fit your model to the training data for 200 epochs
model.fit(X_train,y_train,epochs=100)
# Evaluate your model accuracy on the test data
accuracy = model.evaluate(X_test, y_test)[1]
# Print accuracy
print('Accuracy:', accuracy)
# Predict on test data
preds = model.predict(df_test)
preds
# Extract the position of highest probability from each pred vector
preds_chosen = [np.argmax(pred) for pred in preds]
preds_chosen