我是机器学习的新手。我正在尝试训练ANN,fit方法会生成有关数组形状的错误“ ValueError:检查输入时出错:预期density_1_input具有形状(3,)但形状为(2,)的数组”。 知道我有3个输入(年龄,性别,标签)。 数据集包含3356行。数据的前5行的图像为here
如下面的代码所示:
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
# Importing the dataset
dataset = pd.read_csv('Train_data1.csv')
X = dataset.iloc[:, 1:3].values # age, sex
y = dataset.iloc[:, 3].values #label
# Encoding categorical data
# Encoding the Independent Variable
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1])
labelencoder_X_1 = LabelEncoder()
# Encoding the Dependent Variable
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
print(X.shape) #after run : (3356, 2)
print(y.shape) #after run : (3356,)
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Feature Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train.shape)
print(y_train.shape)
# Initialising the ANN
model = Sequential()
model.add(Dense(10, input_dim = 3, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(2, activation = 'softmax'))
# Compiling the ANN
model.compile(loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = ['accuracy'] )
# Fitting the ANN to the Training set
model.fit(X_train, y_train, batch_size = 10, epochs = 100)
运行后的错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-fc00dae1105e> in <module>
1 # Fitting the ANN to the Training set
----> 2 model.fit(X_train, y_train, batch_size = 10, epochs = 100)
~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(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 # Prepare validation data.
954 do_validation = False
~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140
ValueError: Error when checking input: expected dense_1_input to have shape (3,) but got array with shape (2,)
答案 0 :(得分:0)
标签为Y。X的尺寸为2。因此将input_dim = 3
更改为input_dim = 2
y_train = to_categorical(y_train)
答案 1 :(得分:0)
执行input_dim = X.shape [1],即可解决该问题。