0

数字の機能とテキスト機能の両方に基づいて、benignまたはmaliciousクラスに属するURLの確率を予測するための教師付き機械学習モデルを作成しようとしています。機械学習のトレーニングセットでテキストと数値の機能を組み合わせる方法は?

数値Featursから

  • 長プライマリドメインのURLの

    • はIPなどが含まれてドット数

    テキスト機能 -

    • 登録
    • レジストラ名の名前国籍
    • URL内の単語の一覧など

    私は必要な機能を備えたデータフレームを持っているが、私は何をすべきか分かりませんテキストデータ。誰かがこれで私を導くことができますか?

    以下は、私が事前に

    url_length length_domain is_ip registrar registrants tokens_in_url 
    0  50    18   0   a1   z1  [abc, def, ghi, jkl] 
    1  98    23   0   a2   z2  [mno, pqr, stu] 
    2  146    8   0   a3   z3  [vwx, yz] 
    

    感謝をhave-そのサンプルデータフレームです。

    出典DF:

  • +2

    カテゴリーデータであるテキスト機能のためにOneHotEncoder使用してください。参考までに[OneHotEncoder](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html#sklearn-preprocessing-onehotencoder)@NileshShaikh –

    +0

    データの特徴をどのように分析するかhttp://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.htmlの助けを借りて、ターゲットに寄与しているかもしれません。もちろん、典型的な頻度分布、相関などを行っています。 –

    答えて

    1

    は、次のデモを検討

    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 
    
    +0

    この1人の仲間。これは実際に機械学習のための最良の方法で数値とテキストの入力を組み合わせているのですか? – roganjosh

    +0

    @roganjosh、おそらくそうではありません。これは、異なる列に異なる値が多すぎる場合に役立ちます... – MaxU

    +0

    私はこれが少し広いと思います。カントリーはカテゴリーとして使うことができますが、これはデータの準備と同じくらいモデルの選択に沸きます。 – roganjosh

    関連する問題