我在做什么?使用scikit-learn处理分类特征
我正在解决使用随机森林的分类问题。我有一组固定长度的字符串(10个字符长),代表DNA序列。 DNA字母由4个字母组成,即A
,C
,G
,T
。
这是我的原始数据的样本:
ATGCTACTGA
ACGTACTGAT
AGCTATTGTA
CGTGACTAGT
TGACTATGAT
每个DNA序列附带的实验数据描述一个真正的生物反应;该分子被认为引起生物反应(1),或不(0)。
问题:
训练集由两者的,分类(名义)和数值的特征。它是以下结构:
training_set = [
{'p1':'A', 'p2':'T', 'p3':'G', 'p4':'C', 'p5':'T',
'p6':'A', 'p7':'C', 'p8':'T', 'p9':'G', 'p10':'A',
'mass':370.2, 'temp':70.0},
{'p1':'A', 'p2':'C', 'p3':'G', 'p4':'T', 'p5':'A',
'p6':'C', 'p7':'T', 'p8':'G', 'p9':'A', 'p10':'T',
'mass':400.3, 'temp':67.2},
]
target = [1, 0]
我成功地使用DictVectorizer类编码标称功能创建的分类,但我有在执行我的测试数据的预测问题。
下面是我的代码的简化版本,至今完成:
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction import DictVectorizer
training_set = [
{'p1':'A', 'p2':'T', 'p3':'G', 'p4':'C', 'p5':'T',
'p6':'A', 'p7':'C', 'p8':'T', 'p9':'G', 'p10':'A',
'mass':370.2, 'temp':70.0},
{'p1':'A', 'p2':'C', 'p3':'G', 'p4':'T', 'p5':'A',
'p6':'C', 'p7':'T', 'p8':'G', 'p9':'A', 'p10':'T',
'mass':400.3, 'temp':67.2},
]
target = [1, 0]
vec = DictVectorizer()
train = vec.fit_transform(training_set).toarray()
clf = RandomForestClassifier(n_estimators=1000)
clf = clf.fit(train, target)
# The following part fails.
test_set = {
'p1':'A', 'p2':'T', 'p3':'G', 'p4':'C', 'p5':'T',
'p6':'A', 'p7':'C', 'p8':'T', 'p9':'G', 'p10':'A',
'mass':370.2, 'temp':70.0}
vec = DictVectorizer()
test = vec.fit_transform(test_set).toarray()
print clf.predict_proba(test)
其结果是,我得到了一个错误:
ValueError: Number of features of the model must match the input.
Model n_features is 20 and input n_features is 12
可能的复制te [如何强制scikit-learn DictVectorizer不放弃功能?](http://stackoverflow.com/questions/19770147/how-to-force-scikit-learn-dictvectorizer-not-to-discard-features) –