Pythonでワンホットエンコードするにはどうすればいいですか? 質問する

Pythonでワンホットエンコードするにはどうすればいいですか? 質問する

80% のカテゴリ変数を含む機械学習分類問題があります。分類に何らかの分類器を使用する場合、ワンホット エンコーディングを使用する必要がありますか? エンコーディングなしでデータを分類器に渡すことはできますか?

機能選択のために次のことを実行しようとしています。

  1. 私は電車のファイルを読みました:

    num_rows_to_read = 10000
    train_small = pd.read_csv("../../dataset/train.csv",   nrows=num_rows_to_read)
    
  2. カテゴリ特徴のタイプを「カテゴリ」に変更します。

    non_categorial_features = ['orig_destination_distance',
                              'srch_adults_cnt',
                              'srch_children_cnt',
                              'srch_rm_cnt',
                              'cnt']
    
    for categorical_feature in list(train_small.columns):
        if categorical_feature not in non_categorial_features:
            train_small[categorical_feature] = train_small[categorical_feature].astype('category')
    
  3. 私はワンホットエンコーディングを使用します:

    train_small_with_dummies = pd.get_dummies(train_small, sparse=True)
    

問題は、強力なマシンを使用しているにもかかわらず、3 番目の部分が頻繁に詰まってしまうことです。

したがって、ワンホットエンコーディングがなければ、特徴の重要性を判断するための特徴選択を行うことはできません。

おすすめは何ですか?

ベストアンサー1

アプローチ 1: pandas の を使用できますpd.get_dummies

例1:

import pandas as pd
s = pd.Series(list('abca'))
pd.get_dummies(s)
Out[]: 
     a    b    c
0  1.0  0.0  0.0
1  0.0  1.0  0.0
2  0.0  0.0  1.0
3  1.0  0.0  0.0

例2:

以下は、指定された列を 1 つのホット ダミーに変換します。複数のダミーを使用するには、プレフィックスを使用します。

import pandas as pd
        
df = pd.DataFrame({
          'A':['a','b','a'],
          'B':['b','a','c']
        })
df
Out[]: 
   A  B
0  a  b
1  b  a
2  a  c

# Get one hot encoding of columns B
one_hot = pd.get_dummies(df['B'])
# Drop column B as it is now encoded
df = df.drop('B',axis = 1)
# Join the encoded df
df = df.join(one_hot)
df  
Out[]: 
       A  a  b  c
    0  a  0  1  0
    1  b  1  0  0
    2  a  0  0  1

アプローチ2: Scikit-learnを使用する

を使用すると、同じインスタンスを使用して、一部のトレーニング データに対して を実行し、次に他の一部のデータに対してOneHotEncoderを実行できるという利点があります。また、エンコーダーが目に見えないデータに対して何を実行するかをさらに制御する必要があります。fittransformhandle_unknown

3 つの特徴と 4 つのサンプルを含むデータセットが与えられた場合、エンコーダーは特徴ごとに最大値を見つけ、データをバイナリ ワンホット エンコーディングに変換します。

>>> from sklearn.preprocessing import OneHotEncoder
>>> enc = OneHotEncoder()
>>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])   
OneHotEncoder(categorical_features='all', dtype=<class 'numpy.float64'>,
   handle_unknown='error', n_values='auto', sparse=True)
>>> enc.n_values_
array([2, 3, 4])
>>> enc.feature_indices_
array([0, 2, 5, 9], dtype=int32)
>>> enc.transform([[0, 1, 1]]).toarray()
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.]])

この例のリンクは次のとおりです: http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

おすすめ記事