我是python 2.7的新手我试图在我的数据集上运行决策树分类器,但是按照教程我遇到这个问题我首先将我的功能列向量化并将其保存到一个数组中,然后保存在一个数组中的目标列使用labelencoder。请问您能解释我如何修复此错误?
数据:
代码:
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
答案 0 :(得分:0)
首先,要了解错误:
您的训练样本数量(即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)