2014-01-26 61 views
2

我在做什么?使用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 
+0

可能的复制te [如何强制scikit-learn DictVectorizer不放弃功能?](http://stackoverflow.com/questions/19770147/how-to-force-scikit-learn-dictvectorizer-not-to-discard-features) –

回答

3

您需要使用创建的列车一样DictVectorizer对象数据集到transformtest_set

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} 

test = vec.transform(test_set).toarray() 
print clf.predict_proba(test) 
+0

谢谢,它工作得很好。但是,我注意到,处理大量的字符串,使得矩阵非常宽,并使我的记忆过载。我想知道,如果你能建议我用其他方法来创建分类器。在Scikit学习文档中,我阅读了[功能散列](http://scikit-learn.org/stable/modules/feature_extraction.html),但我找不到在我的数据上使用它的方法。 – sherlock85

+0

@s_sherly要使'FeatureHasher'工作,您需要自己用虚拟变量替换分类特征:'“p1 = A”:1'等。但是,使用特征选择和/或降维可能更好在矢量化器出来的稀疏矩阵上的TruncatedSVD。 –