은 다음과 데모를 고려
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의 도움을 받아 타겟에 기여할 수 있습니다. 물론 일반적인 주파수 분포, 상관 관계 등을 수행 할 것입니다. –