2016-05-12 3 views
0

自分のデータセットでランダムフォレスト分類モデルを実行しています。 spark1.6によって提供されたoneHotEncodeメソッドを使用して、それらを扱ういくつかのカテゴリ変数があります。最後に、私は多くのスパースベクトルspark-1.6のカテゴリ変数を扱う方法は?

私のコードを得た:

 def oneHotEncode(a: String,b:String,c:String,selectedData:DataFrame) : 

     DataFrame = { 
     val indexer = new StringIndexer().setInputCol(a).setOutputCol(b).fit(selectedData) 
     val indexed = indexer.transform(selectedData) 
     val encoder = new OneHotEncoder().setInputCol(b).setOutputCol(c) 
     val encoded = encoder.transform(indexed) 
     return encoded 
} 
var data1 = oneHotEncode("ispromoteroom","ispromoteroomIndex","ispromoteroomVec",selectedData) 

質問は、私がLabeledPointデータ型に設定されたデータでそれらのスパースベクトルおよび他のオリジナル連続変数を変換することができる方法ですか?

答えて

0

私はこれに従いましたtutorial、それは非常に有用です。

def create_labeled_point(line_split): 
    # leave_out = [41] 
    clean_line_split = line_split[0:41] 

    # convert protocol to numeric categorical variable 
    try: 
     clean_line_split[1] = protocols.index(clean_line_split[1]) 
    except: 
     clean_line_split[1] = len(protocols) 

    # convert service to numeric categorical variable 
    try: 
     clean_line_split[2] = services.index(clean_line_split[2]) 
    except: 
     clean_line_split[2] = len(services) 

    # convert flag to numeric categorical variable 
    try: 
     clean_line_split[3] = flags.index(clean_line_split[3]) 
    except: 
     clean_line_split[3] = len(flags) 

    # convert label to binary label 
    attack = 1.0 
    if line_split[41]=='normal.': 
     attack = 0.0 

    return LabeledPoint(attack, array([float(x) for x in clean_line_split])) 

training_data = csv_data.map(create_labeled_point) 
test_data = test_csv_data.map(create_labeled_point) 
関連する問題