ValueError:发现输入变量的样本数量不一致吗?

时间:2018-09-30 10:59:18

标签: python machine-learning

尝试执行我的代码时,出现以下错误

ValueError: Found input variables with inconsistent numbers of samples: [92001, 1]

我了解这是某种格式错误,但我不知道如何解决。

我还搜索了其他所有问题,并且每个问题的代码都与我的不同,因此如果有人帮助我进行纠正,那就太好了。

dataset = pd.read_csv('data.csv')
print(dataset.head())
x = dataset.iloc[:1,:-1]
y = dataset.iloc[:1,-1]

print(y[0:5])
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y.values)
y = y.T
print(x.shape)
x_train,x_test,y_train,y_test = train_test_split(x.values,y)
print(y.shape)
print(np.unique(y))

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load dataset

# encode class values as integers

# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(y)
# define baseline model
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(8, input_dim=4, activation='relu'))
    model.add(Dense(3, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
Traceback:

Traceback (most recent call last):

  File "<ipython-input-12-318474aa38f5>", line 1, in <module>
    runfile('/Users/vivanksharma/Downloads/temp.py', wdir='/Users/vivanksharma/Downloads')

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",
     

第705行,在运行文件中           execfile(文件名,命名空间)

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",
     

第102行,在execfile中           exec(compile(f.read(),文件名,'exec'),命名空间)

  File "/Users/vivanksharma/Downloads/temp.py", line 57, in <module>
    results = cross_val_score(estimator, X, dummy_y, cv=kfold)

  File "/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py",
     

第342行,在cross_val_score中           pre_dispatch = pre_dispatch)

  File "/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py",
     

第192行,在cross_validate中           X,y,组=可索引的(X,y,组)

  File "/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py",
     

第229行,可索引           check_consistent_length(* result)

  File "/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py",
     

第204行,格式为check_consistent_length           “样本:%r”%[长度为l的int(l)])

ValueError: Found input variables with inconsistent numbers of samples: [92001, 1]

0 个答案:

没有答案