首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复: ValueError: labels=21638的数量与samples=13的数量不匹配

如何修复: ValueError: labels=21638的数量与samples=13的数量不匹配
EN

Stack Overflow用户
提问于 2018-05-21 19:52:04
回答 1查看 6.1K关注 0票数 2

我对python 2.7非常陌生,我试图在我的数据集上运行决策树分类器,但是在一个教程之后,我首先将我的特性列向量化并保存到一个数组中,然后使用标签编码器将目标列保存在数组中。请你解释一下我该如何纠正这个错误。

数据:

代码:

代码语言:javascript
复制
import pandas as pd

dataset = "C:/Users/ashik swaroop/Desktop/anaconda/Gene Dataset/Final.csv"
datacan = pd.read_csv(dataset)
datacan = datacan.fillna('')
features = datacan[[ 
"Tumour_Types_Somatic","Tumour_Types_Germline",
"Cancer_Syndrome","Tissue_Type", 
"Role_in_Cancer","Mutation_Types","Translocation_Partner",
"Other_Syndrome","Tier","Somatic","Germline",
"Molecular_Genetics","Other_Germline_Mut"]]

 from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import LabelEncoder

X_dict = features.to_dict().values()
vect = DictVectorizer(sparse=False)
X_vector = vect.fit_transform(X_dict)

le = LabelEncoder()
y_train = le.fit_transform(datacan['Gene_Symbol'][:-1])


X_Train = X_vector[:-1]

X_Test = X_vector[-1:] 

from sklearn import tree

clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(X_Train,y_train)    `

我得到了这个错误:

代码语言:javascript
复制
from sklearn import tree
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(X_Train,y_train)
Traceback (most recent call last):

File "<ipython-input-49-fef4fc045a54>", line 4, in <module>
clf = clf.fit(X_Train,y_train)

File "C:\Users\ashik swaroop\Anaconda2\lib\site- 
packages\sklearn\tree\tree.py", line 739, in fit
X_idx_sorted=X_idx_sorted)

File "C:\Users\ashik swaroop\Anaconda2\lib\site- 
packages\sklearn\tree\tree.py", line 240, in fit
"number of samples=%d" % (len(y), n_samples))

ValueError: Number of labels=21638 does not match number of samples=12

Traceback (most recent call last):

File "<ipython-input-49-fef4fc045a54>", line 4, in <module>
clf = clf.fit(X_Train,y_train)

File "C:\Users\ashik swaroop\Anaconda2\lib\site- 
packages\sklearn\tree\tree.py", line 739, in fit
X_idx_sorted=X_idx_sorted)

File "C:\Users\ashik swaroop\Anaconda2\lib\site- 
packages\sklearn\tree\tree.py", line 240, in fit
"number of samples=%d" % (len(y), n_samples))

ValueError: Number of labels=21638 does not match number of samples=12
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-22 00:34:44

首先,要理解错误:似乎你的训练样本数(即。np.shape(X_train)[0])不匹配标签的数量(即。np.shape(y_train)[0])。

在查看您的代码时,我注意到了一些不一致的地方。请参阅下面的内联评论。

代码语言:javascript
复制
import pandas as pd
from apyori import apriori
dataset = "C:/Users/ashik swaroop/Desktop/anaconda/Gene Dataset/Final.csv"
datacan = pd.read_csv(dataset)
datacan = datacan.fillna('')
features = datacan[[ 
"Tumour_Types_Somatic","Tumour_Types_Germline",
"Cancer_Syndrome","Tissue_Type", 
"Role_in_Cancer","Mutation_Types","Translocation_Partner",
"Other_Syndrome","Tier","Somatic","Germline",
"Molecular_Genetics","Other_Germline_Mut"]]
# EDIT replace by features = [ 
#"Tumour_Types_Somatic","Tumour_Types_Germline",
#"Cancer_Syndrome","Tissue_Type", 
#"Role_in_Cancer","Mutation_Types","Translocation_Partner",
#"Other_Syndrome","Tier","Somatic","Germline",
#"Molecular_Genetics","Other_Germline_Mut"]

orders = datacan[features].to_dict( orient = 'records' ) # this variable is not used

from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import LabelEncoder

X_dict = features.to_dict().values() # try replacing this line with X_dict = orders
vect = DictVectorizer(sparse=False)
X_vector = vect.fit_transform(X_dict)

le = LabelEncoder()
y_train = le.fit_transform(datacan['Gene_Symbol'][:-1])


X_Train = X_vector[:-1]

X_Test = X_vector[-1:] 

from sklearn import tree

clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(X_Train,y_train)  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50455722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档