我在做什麼?使用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) –