[英]Neural Network classification
我正在嘗試為(Statlog)Shuttle數據集訓練多層前饋神經網絡 -
這是一個多類分類任務。 目標屬性是“類”。
我的代碼如下 -
# Column names to be used for training and testing sets-
col_names = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'Class']
# Read in training and testing datasets-
training_data = pd.read_csv("shuttle_training.csv", delimiter = ' ', names = col_names)
testing_data = pd.read_csv("shuttle_test.csv", delimiter = ' ', names = col_names)
print("\nTraining data dimension = {0} and testing data dimension = {1}\n".format(training_data.shape, testing_data.shape))
# Training data dimension = (43500, 10) and testing data dimension = (14500, 10)
# Data Preprocessing-
# Check for missing value(s) in training data-
training_data.isnull().values.any()
# False
# Get target attribute class distribution-
training_data["Class"].value_counts()
'''
1 34108
4 6748
5 2458
3 132
2 37
7 11
6 6
Name: Class, dtype: int64
'''
# NOTE: Majority of instances belong to class 1
# Visualizing the distribution of each attribute in dataset using boxplots-
fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')
sns.boxplot(data = training_data)
plt.xticks(rotation = 20)
plt.show()
# # To divide the data into attributes and labels, execute the following code:
# 'X' contains attributes
X = training_data.drop('Class', axis = 1)
# Convert 'X' to float-
X = X.values.astype("float")
# 'y' contains labels
y = training_data['Class']
# Normalize features (X)-
rb_scaler = RobustScaler()
X_std = rb_scaler.fit_transform(X)
# Divide attributes & labels into training & testing sets-
X_train, X_test, y_train, y_test = train_test_split(X_std, y, test_size = 0.30, stratify = y)
print("\nDimensions of training and testing sets are:")
print("X_train = {0}, y_train = {1}, X_test = {2} and y_test = {3}\n\n".format(X_train.shape, y_train.shape, X_test.shape, y_test.shape))
# Dimensions of training and testing sets are:
# X_train = (30450, 9), y_train = (30450,), X_test = (13050, 9) and y_test = (13050,)
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import StratifiedKFold
from sklearn.pipeline import Pipeline
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
# Create Neural Network model-
model = Sequential()
# Input layer-
model.add(Dense(9, input_dim = 9, kernel_initializer = 'normal', activation = 'relu'))
# Hidden layer(s)-
model.add(Dense(9, kernel_initializer = 'normal', activation='relu'))
# Output layer-
model.add(Dense(7, activation = 'softmax')) # 7 output neurons for 7 classes in target attribute
# Compile NN model-
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
'''
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 9) 90
_________________________________________________________________
dense_2 (Dense) (None, 9) 90
_________________________________________________________________
dense_3 (Dense) (None, 7) 70
=================================================================
Total params: 250
Trainable params: 250
Non-trainable params: 0
_________________________________________________________________
“””
# Train model on training data-
history = model.fit(X_train, y_train, epochs = 200, batch_size = 50, validation_data = (X_test, y_test), verbose = 1, shuffle = False)
它給了我錯誤 -
ValueError:檢查目標時出錯:期望dense_3有形狀(7,)但是有形狀的數組(1,)
那么,根據'Class'屬性(這是我們的目標),似乎總共有7個類(盡管有嚴重的類不平衡)。 那我為什么會收到這個錯誤呢? 有線索嗎?
謝謝!
錯誤追蹤 -
-------------------------------------------------- ------------------------- ValueError Traceback(最近一次調用last)在----> 1 history = model.fit(X_train,y_train, epochs = 200,batch_size = 50,validation_data =(X_test,y_test),verbose = 1,shuffle = False)
適合的〜/ .local / lib / python3.6 / site-packages / keras / engine / training.py(self,x,y,batch_size,epochs,verbose,callbacks,validation_split,validation_data,shuffle,class_weight,sample_weight,initial_epoch ,steps_per_epoch,validation_steps,** kwargs)950 sample_weight = sample_weight,951 class_weight = class_weight, - > 952 batch_size = batch_size)953#准備驗證數據。 954 do_validation = False
在_standardize_user_data中的〜/ .local / lib / python3.6 / site-packages / keras / engine / training.py(self,x,y,sample_weight,class_weight,check_array_lengths,batch_size)787 feed_output_shapes,788 check_batch_axis = False,#Don' t強制批量大小。 - > 789 exception_prefix ='target')790 791#在給定
sample_weight
和sample_weight
生成樣本權重值standardize_input_data中的〜/ .local / lib / python3.6 / site-packages / keras / engine / training_utils.py(數據,名稱,形狀,check_batch_axis,exception_prefix)136':預期'+ names [i] +'具有形狀'+ 137 str(shape)+'但是得到了帶有'+ - > 138 str(data_shape)的數組'139返回數據140
ValueError:檢查目標時出錯:期望dense_3有形狀(7,)但是有形狀的數組(1,)
您需要將y_train / y_test轉換為分類的單熱矢量。 在列車/測試拆分之后添加此代碼。
y_test = to_categorical(y_test)
y_train = to_categorical(y_train)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.