Python 神经网络分类

Python 神经网络分类,python,keras,neural-network,Python,Keras,Neural Network,我正在尝试训练一个多层前馈神经网络,用于- 这是一个多类分类任务。目标属性为“Class” 我的代码如下- # 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.re

我正在尝试训练一个多层前馈神经网络,用于-

这是一个多类分类任务。目标属性为“Class”

我的代码如下-

# 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
_________________________________________________________________
'''

这给了我一个错误-

ValueError:检查目标时出错:预期密集_3具有形状(7),但获得具有形状(1)的数组

根据“Class”属性(这是我们的目标),似乎总共有7个类(尽管存在严重的类不平衡)。那我为什么会犯这个错误呢?有什么线索吗

谢谢

错误跟踪-

---------------------------------------------------------------------------ValueError回溯(最近的调用 最后)在 ---->1历史=model.fit(X_序列,y_序列,历代=200,批量=50,验证数据=(X_测试,y_测试),详细=1,随机=False)

~/.local/lib/python3.6/site-packages/keras/engine/training.py in 拟合(自我、x、y、批量大小、年代、详细、回调、, 验证分割、验证数据、洗牌、等级权重、, 样本重量、初始历元、每历元步骤、验证步骤、, **kwargs) 950样品重量=样品重量, 951类重量=类重量, -->952批次大小=批次大小) 953#准备验证数据。 954 do_验证=错误

~/.local/lib/python3.6/site-packages/keras/engine/training.py in _标准化用户数据(自身、x、y、样本重量、类别重量、检查数组长度、批量大小) 787馈送输出形状, 788检查批处理轴=False,#不强制执行批处理大小。 -->789例外(前缀为“目标”) 790 791#根据
样本权重生成样本权重值

~/.local/lib/python3.6/site-packages/keras/engine/training\u utils.py in 标准化输入数据(数据、名称、形状、检查批处理轴、, 例外情况(前缀) 136':预期“+名称[i]+”具有形状”+ 137 str(shape)+'但得到了具有shape的数组'+ -->138街(数据_形)) 139返回数据 140

ValueError:检查目标时出错:预期密集_3具有形状 (7,)但得到了形状为(1,)的数组


您需要将您的y_序列/y_测试转换为分类的热向量。在列车/测试拆分后添加此代码

y_test = to_categorical(y_test)
y_train = to_categorical(y_train)

模型摘要中没有
density_3
层(也没有任何具有预期输入的层
(7,)
);请重新运行脚本并使用新的
model.summary()
和错误的结果编辑文章。还包括完整的错误跟踪…@desertnaut编辑了“model.summary()”以显示“dense_3”输出层
y_test = to_categorical(y_test)
y_train = to_categorical(y_train)