python - 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]
解决方案
推荐阅读
- r - 在 quasiquotation 上下文之外取消引用 quosure
- javascript - 当一个内部部分折叠时,折叠内部部分保持未折叠
- cloud-storage - Backblaze B2 原生 URL 与友好 URL - 交易上限?
- ios - UILabel 的幻灯片动画
- java - 使用 newOutputStream 方法定义文件位置
- node.js - node.js 文件是否可以具有“.js”以外的扩展名来区分文本编辑器中的客户端和服务器代码?
- javascript - 在 R/Shiny 中使用 Simple Webaudiorecorder.js 并将录音发布到服务器
- powershell - 如何使用批处理文件执行 powershell 脚本?
- javascript - 为什么我的 NodeJS 服务器上的 ZIP 文件不可读?
- mysql - 选择日期并分配给变量后将日期插入回数据库