난 무엇을하고 있지?
Random Forests를 사용하여 분류 문제를 해결하고 있습니다. DNA 서열을 나타내는 고정 길이 (10 자 길이)의 문자열 세트가 있습니다. 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
DictVectorizer
에 기차 데이터 세트를 생성 한 동일한 객체를 사용해야 transform
합니다 test_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)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다