は、次のデモを検討
In [113]: df
Out[113]:
registrar registrant country
0 registrar1 registrant1 country1
1 registrar8 registrant2 country2
2 registrar1 registrant3 country1
3 registrar5 registrant4 country3
エンコード:
In [114]: from sklearn.preprocessing import LabelEncoder
In [115]: str_cols = df.columns[df.dtypes.eq('object')]
In [116]: clfs = {c:LabelEncoder() for c in str_cols}
In [117]: for col, clf in clfs.items():
...: df[col] = clfs[col].fit_transform(df[col])
...:
In [118]: df
Out[118]:
registrar registrant country
0 0 0 0
1 2 1 1
2 0 2 0
3 1 3 2
逆変換:
In [119]: clfs['country'].inverse_transform(df['country'])
Out[119]: array(['country1', 'country2', 'country1', 'country3'], dtype=object)
UPDATE:
は、それはあなたの特定の 答えをTF-IDF(URL内の単語のリスト)を使用することは可能ですか?
In [86]: from sklearn.feature_extraction.text import TfidfVectorizer
In [87]: vect = TfidfVectorizer(sublinear_tf=True, max_df=0.5, analyzer='word', stop_words='english')
In [88]: X = vect.fit_transform(df['tokens_in_url'].str.join(' '))
In [89]: X
Out[89]:
<3x9 sparse matrix of type '<class 'numpy.float64'>'
with 9 stored elements in Compressed Sparse Row format>
In [90]: X.A
Out[90]:
array([[ 0.5 , 0.5 , 0.5 , 0.5 , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.57735027, 0.57735027, 0.57735027, 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.70710678, 0.70710678]])
In [91]: vect.get_feature_names()
Out[91]: ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']
In [92]: tok = pd.SparseDataFrame(X, columns=vect.get_feature_names(), index=df.index, default_fill_value=0)
In [93]: tok
Out[93]:
abc def ghi jkl mno pqr stu vwx yz
0 0.5 0.5 0.5 0.5 0.00000 0.00000 0.00000 0.000000 0.000000
1 0.0 0.0 0.0 0.0 0.57735 0.57735 0.57735 0.000000 0.000000
2 0.0 0.0 0.0 0.0 0.00000 0.00000 0.00000 0.707107 0.707107
カテゴリーデータであるテキスト機能のためにOneHotEncoder使用してください。参考までに[OneHotEncoder](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html#sklearn-preprocessing-onehotencoder)@NileshShaikh –
データの特徴をどのように分析するかhttp://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.htmlの助けを借りて、ターゲットに寄与しているかもしれません。もちろん、典型的な頻度分布、相関などを行っています。 –