2017-11-28 6 views
0

私はsklearnモデルに供給することを計画したデータを持っています。 (例えば1列が{genres: [comedy, horror]}あるので、その動画データ)列の数は、カテゴリのリストです。何が行は、次のようないくつかのデータを持っている隣接行列は、あるモデルにFRBのようにカテゴリーのリストであるデータを隣接関係マトリックスにフォーマットする最良の方法は何ですか?

私は、これらの列を処理するために何ができますか?

{comedy: 1, action: 0, horror: 1, documentary: 0} 

答えて

1

あなたが探しているプリプロセッサはLabelBinarizer

import pandas as pd 
import numpy as np 
from sklearn.preprocessing import LabelBinarizer 

data = [{'genres': ['comedy', 'horror']}, {'genres': ['action', 'documentary']}] 
df = pd.DataFrame(data) 

# explode the list to separate rows 
X = pd.concat([ 
     pd.DataFrame(v, index=np.repeat(k,len(v)), columns=['genre']) 
      for k,v in df.genres.to_dict().items()]) 

lb = LabelBinarizer() 
# make the binary fields 
dd = pd.DataFrame(lb.fit_transform(X), index=df_exploded.index, columns=lb.classes_) 
dd.groupby(dd.index).max() 

action comedy documentary horror 
0  0  1   0  1 
1  1  0   1  0 
+0

ありがとう、パンダとの中間のステップは、私が行方不明になったものです与えます。それは束を助ける、私はそれを実装するとすぐにあなたの答えを受け入れるよ。 – DrewDiezel

関連する問題