首页 > 解决方案 > ValueError:找到样本数量不一致的输入变量?

问题描述

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

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(filename, namespace)

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

第 102 行,在 execfile 中 exec(compile(f.read(), filename, 'exec'), namespace)

  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, groups = indexable(X, y, groups)

  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”% [int(l) for l in lengths])

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

标签: pythonmachine-learning

解决方案


推荐阅读