ValueError:找到样本数量不一致的输入变量:[2839,14195]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError:找到样本数量不一致的输入变量:[2839,14195]相关的知识,希望对你有一定的参考价值。
在此数据集之前的所有数据集都可以正常工作之前,现在有了这个新数据集,它引起了以下错误,我试图重塑X_train,但它不是X_trian的属性,任何人都可以提供帮助。谢谢
feature_df = pd.read_excel('thedatasets/boutput.xlsx')
Y = pd.get_dummies(feature_df['class'],drop_first=True)
X = feature_df.drop(['class'],axis=1)
下面的代码在其他py文件中
for i in range(np.shape(generation)[0]):
individual = generation[i, :]
# Subset the columns based on this individual
X_individual = self.dataset[[self.dataset.columns[j] for j in range(len(individual)) if individual[j] == 1]]
# Split into train-test datasets
X_train, X_test, y_train, y_test = train_test_split(X_individual, self.response, test_size=self.test_size)
# Fit the classifier
self.algorithm.fit(X_train, y_train.values.ravel())
grid.fit(X_train,y_train.values.ravel())
错误:
Traceback (most recent call last):
File "/Users/cinci/Desktop/SPLITPROJ/GeneticAlgorithmExample.py", line 57, in <module>
main()
File "/Users/cinci/Desktop/SPLITPROJ/GeneticAlgorithmExample.py", line 17, in main
GA.fit()
File "/Users/cinci/Desktop/SPLITPROJ/GeneticAlgorithm.py", line 167, in fit
old_fitness_array = self.fitness(old_generation)
File "/Users/cinci/Desktop/SPLITPROJ/GeneticAlgorithm.py", line 77, in fitness
self.algorithm.fit(X_train, y_train.values.ravel())
File "/Users/cinci/venv/lib/python3.7/site-packages/sklearn/svm/_base.py", line 148, in fit
accept_large_sparse=True)
File "/Users/cinci/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 765, in check_X_y
check_consistent_length(X, y)
File "/Users/cinci/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 212, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [2839, 14195]
答案
鉴于14916/5是2839,我假设问题的原因是您有多个输出标签(5个标签)并且您使用的是.ravel()
这将使数据变平,并且模型将认为您正在尝试传递#个标签* number_of_training_examples个示例作为您的训练示例。
要解决此问题,而不是使用.ravel(),您应该调整最终的y_train的形状,使其具有number_of_training_examples x number_of_labels个形状
以上是关于ValueError:找到样本数量不一致的输入变量:[2839,14195]的主要内容,如果未能解决你的问题,请参考以下文章
ValueError:找到样本数量不一致的输入变量:[2,921]
ValueError:找到样本数量不一致的输入变量:[2839,14195]
当我尝试为 scikit-learn 模型拟合另外 1 个功能时,出现此错误“ValueError:找到样本数量不一致的输入变量”
ValueError:发现样本数量不一致的输入变量:[143, 426]