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

代码:
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) `我得到了这个错误:
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发布于 2018-05-22 00:34:44
首先,要理解错误:似乎你的训练样本数(即。np.shape(X_train)[0])不匹配标签的数量(即。np.shape(y_train)[0])。
在查看您的代码时,我注意到了一些不一致的地方。请参阅下面的内联评论。
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) https://stackoverflow.com/questions/50455722
复制相似问题