在使用fit transform方法编译我的代码时,它显示有关数组形状的错误 ” ValueError:检查输入时出错:预期density_1_input的形状为(6,),但数组的形状为(11,)“
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Churn_Modelling.csv')
x = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_x_1 = LabelEncoder()
x[:, 1] = labelencoder_x_1.fit_transform(x[:, 1])
labelencoder_x_2 = LabelEncoder()
x[:, 2] = labelencoder_x_2.fit_transform(x[:, 2])
onehotencoder = OneHotEncoder(categorical_features =[1])
x = onehotencoder.fit_transform(x).toarray()
x =x[:, 1:]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size =0.2, random_state =0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
import keras
from keras.models import Sequential
from keras.layers import Dense
classifier = Sequential()
classifier.add(Dense(output_dim =6, init = 'uniform', activation= 'relu', input_dim= 6))
classifier.add(Dense(output_dim =6, init = 'uniform', activation= 'relu' ))
classifier.add(Dense(output_dim =1, init = 'uniform', activation = 'sigmoid' ))
classifier.compile(optimizer ='adam', loss = 'binary_crossentropy', metrics =['accuracy'])
classifier.fit(x_train, y_train, batch_size = 10, nb_epoch = 100)
y_pred = classifier.predict(x_test)
y_pred = (y_pred > 0.5)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
答案 0 :(得分:0)
差异在x_train
中,可能在x_test
中。如果您查看print(x_train.shape)
,可能会得到类似(N, 11)
的信息,其中N是每个样本包含11个特征的数量。但是,等等,您的模型已定义为具有6个input_dim
功能。因此您可以:
input_dim=11