簡體   English   中英

如何搭建一個neural.networks model對python中的數據進行分類

[英]How to build a neural networks model to classify data in python

我正在嘗試構建一個 model 來對一些數據(4 個類)進行分類。
這是我嘗試過的:

from keras.models import Sequential
from keras.layers import Dense


# dividing X, y into train and test data
X_train, X_test, y_train, y_test = train_test_split(X_data, y_target, random_state=0)

# define the keras model
model = Sequential()
model.add(Dense(64, input_dim=9, activation='relu'))
model.add(Dense(4, activation='softmax')) 
# compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the model on the dataset
train_history = model.fit(X_train, y_train, epochs=100, batch_size=20, verbose=0, validation_data=(X_test, y_test))
# evaluate the keras model
_, accuracy = model.evaluate(X_data, y_target, verbose=0)
print('Accuracy: %.3f' % (accuracy*100))

我收到此錯誤:

Received a label value of 4 which is outside the valid range of [0, 4).

有人可以幫我了解我的 model 有什么問題嗎?

感謝@furas,我通過使用 pandas 將標簽從[1 2 3 4]更改為[0 1 2 3]解決了我的問題: df["label"] = df["label"] - 1

實際上,如果您使用train_test_split從同一幀拆分數據,那么這個錯誤不應該出現。 請再次檢查數據。 還要確保在train_test_split stratify

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM