【问题标题】:How to Combine text and numerical features in training sets for machine learning?如何在机器学习的训练集中结合文本和数字特征?
【发布时间】:2018-05-10 17:13:45
【问题描述】:

我正在尝试创建一个有监督的机器学习模型,以基于数字特征和文本特征来预测属于 benignmalicious 类的给定 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]

提前致谢。

【问题讨论】:

标签: python machine-learning artificial-intelligence feature-extraction


【解决方案1】:

要结合文本特征和数字特征,请遵循以下步骤:

  • 对于文本特征,使用 BoW、TFIDF、AvgW2V、TFIDFW2V 任何这些文本特征向量化技术对其进行向量化。
  • 对于数值特征,使用归一化或列标准化来缩放数值数据。
  • 如果您还想使用分类特征,请使用 OneHotEncoding、LabelEncoding、ResponseCoding 等,对分类特征进行矢量化处理。
  • 使用 hstack 将所有功能放在一个数据帧中。 例如。 X_tr=hstack((vectorised_text_features....,standardised_numerical_features...,standardised_categorical_features...)) 而且,您的火车数据已准备就绪。现在,可以进行建模了。

【讨论】:

    【解决方案2】:

    考虑以下演示:

    来源 DF:

    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)
    

    更新:

    是否可以在给定的情况下使用 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
    

    【讨论】:

    • 这一位队友有点丢了。这真的是以机器学习的最佳方式结合数字和文本输入吗?
    • @roganjosh,很可能不是。如果不同列中有太多不同的值,这可能会有所帮助...
    • 我觉得这个有点宽泛。国家可以用作一个类别,但我认为这可以归结为模型选择和数据准备。
    • @MaxU,是否可以在给定答案的情况下使用 TF-IDF(URL 中的单词列表)?
    • @NileshShaikh,你能提供一个小的可重复数据集吗?
    猜你喜欢
    • 2016-07-31
    • 2014-12-27
    • 2018-10-10
    • 2017-03-19
    • 2017-06-25
    • 2017-04-06
    • 2015-03-22
    • 2014-06-01
    • 2021-08-23
    相关资源
    最近更新 更多