ValueError: 检查输入时出错:期望 dense_1_input 的形状为 (3,),但得到的数组形状为 (2,)

我是机器学习的新手。我正在尝试训练一个ANN,fit方法产生了一个关于数组形状的错误 “ValueError: Error when checking input: expected dense_1_input to have shape (3,) but got array with shape (2,)”。我知道我有3个输入(年龄,性别,标签)。数据集包含3356行。数据前5行的图片在这里

如下是代码展示:

import pandas as pdfrom keras.models import Sequentialfrom keras.layers import Densefrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder# 导入数据集dataset = pd.read_csv('Train_data1.csv')X = dataset.iloc[:, 1:3].values # 年龄,性别y = dataset.iloc[:, 3].values #标签# 编码分类数据# 编码独立变量labelencoder_X = LabelEncoder()X[:, 1] = labelencoder_X.fit_transform(X[:, 1])labelencoder_X_1 = LabelEncoder()# 编码依赖变量labelencoder_y = LabelEncoder()y = labelencoder_y.fit_transform(y)print(X.shape) #运行后: (3356, 2)print(y.shape) #运行后: (3356,)# 将数据集拆分为训练集和测试集X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)# 特征缩放sc = StandardScaler()X_train = sc.fit_transform(X_train)X_test = sc.transform(X_test)print(X_train.shape)print(y_train.shape)# 初始化ANNmodel = Sequential()model.add(Dense(10, input_dim = 3, activation = 'relu'))model.add(Dense(10, activation = 'relu'))model.add(Dense(2, activation = 'softmax'))# 编译ANNmodel.compile(loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = ['accuracy'] )# 将ANN拟合到训练集model.fit(X_train, y_train, batch_size = 10, epochs = 100)

运行后的错误:

---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-11-fc00dae1105e> in <module>      1 # 将ANN拟合到训练集----> 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         # 准备验证数据。    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,  # 不要强制执行批量大小。--> 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,)

回答:

Label 是 Y。X 的维度是 2。所以将 input_dim = 3 更改为 input_dim = 2y_train = to_categorical(y_train)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注