2017-01-12 8 views
2

出力が+50000か-50000かを予測しようとしている機械学習アルゴリズムを作成しようとしています。そうすることで、ランダムなフォレストクラシファイアを使用して11の文字列機能を使用しています。しかし、ランダムフォレストクラシファイアはフロート/数字の形式で入力が必要なので、DictVectorizerを使用して文字列のフィーチャを浮動小数点数に変換しています。しかし、データの異なる行については、DictVectorizerは異なる数のフィーチャ(240〜260)を作成します。これは、モデルからの出力を予測する際にエラーを引き起こしています。 1個のサンプルの入力行は次のとおりです。DictVectorizerの問題:さまざまな入力に異なる数のフィーチャを作成する

{'detailed household summary in household': ' Spouse of householder', 
'tax filer stat': ' Joint both under 65', 
'weeks worked in year': ' 52', 
'age': '32', 
'sex': ' Female', 
'marital status': ' Married-civilian spouse present', 
'full or part time employment stat': ' Full-time schedules', 
'detailed household and family stat': ' Spouse of householder', 
'education': ' Bachelors degree(BA AB BS)', 
'num persons worked for employer': ' 3', 
'major occupation code': ' Adm support including clerical'} 

は、私は出力を予測するために、ランダムフォレスト分類器を使用できるように、私は入力を変換することができ、いくつかの方法があります。

編集:私はこれを行うに使用しています コードは次のとおりです。

X,Y=[],[] 
    features=[0,4,7,9,12,15,19,22,23,30,39] 
    with open("census_income_learn.csv","r") as fl: 
     reader=csv.reader(fl) 
     for row in reader: 
      data={} 
      for i in features: 
       data[columnNames[i]]=str(row[i]) 
      X.append(data) 
      Y.append(str(row[41])) 

    X_train, X_validate, Y_train, Y_validateActual = train_test_split(X, Y, test_size=0.2, random_state=32) 

    vec = DictVectorizer() 
    X_train=vec.fit_transform(X_train).toarray() 
    X_validate=vec.fit_transform(X_validate).toarray() 
    print("data ready") 

    forest = RandomForestClassifier(n_estimators = 100) 
    forest = forest.fit(X_train, Y_train) 
    print("model created") 

    Y_predicted=forest.predict(X_validate) 
    print(Y_predicted) 

だからここに私はトレーニングセットと検証セットの最初の要素を印刷しようとした場合、私はX_trainで252の機能を取得[0 ]、X_validate [0]には249個の機能があります。

+0

構造のどのようなあなたがDictVectorizerに渡すのですか?それは辞書の__list__を期待しています... – MaxU

+0

@MaxU辞書のリストを渡しています。私はちょうど辞書の1つのサンプルを追加しました。すべての辞書は同じ形式です(リスト内の各辞書にはすべてのキーが存在することを意味します) – sohil

+0

これらの列の列名は '[0,4,7,9,12,15,19,22、 23,30,39] '? – MaxU

答えて

2

はこれを試してみてください:

import pandas as pd 
from sklearn.preprocessing import LabelEncoder 
from sklearn.feature_extraction import DictVectorizer 
from sklearn.ensemble import RandomForestClassifier 
from sklearn.model_selection import train_test_split 

cols = [0,4,7,9,12,15,19,22,23,30,39, 41] 
names = [ 
'detailed household summary in household', 
'sex', 
'full or part time employment stat', 
'age', 
'detailed household and family stat', 
'weeks worked in year', 
'num persons worked for employer', 
'major occupation code', 
'tax filer stat', 
'education', 
'marital status', 
'TARGET' 
] 

fn = r'D:\temp\.data\census_income_learn.csv' 
data = pd.read_csv(fn, header=None, usecols=cols, names=names) 

# http://stackoverflow.com/questions/24458645/label-encoding-across-multiple-columns-in-scikit-learn  
df = data.apply(LabelEncoder().fit_transform) 

X, Y = np.split(df, [11], axis=1) 
X_train, X_validate, Y_train, Y_validateActual = train_test_split(X, Y, test_size=0.2, random_state=32) 

forest = RandomForestClassifier(n_estimators = 100) 
forest = forest.fit(X_train, Y_train) 

Y_predicted=forest.predict(X_validate) 
+0

これは私のために働いた。私は警告を取得しています** DataConversionWarning:1d配列が予想されるとき、列ベクトルyが渡されました。 yの形状を(n_samples、)に変更してください。たとえば、ravel()を使用してください。 forest = forest.fit(X_train、Y_train)**。ありがとうございます。 – sohil

関連する問題