【问题标题】:Scikit-learn: How to extract features from the text?Scikit-learn:如何从文本中提取特征?
【发布时间】:2016-09-11 05:53:25
【问题描述】:

假设我有一个字符串数组:

['Laptop Apple Macbook Air A1465, Core i7, 8Gb, 256Gb SSD, 15"Retina, MacOS' ... 'another device description']

我想从这个描述中提取如下特征:

item=Laptop
brand=Apple
model=Macbook Air A1465
cpu=Core i7
...

我应该先准备好预定义的已知特征吗?喜欢

brands = ['apple', 'dell', 'hp', 'asus', 'acer', 'lenovo']
cpu = ['core i3', 'core i5', 'core i7', 'intel pdc', 'core m', 'intel pentium', 'intel core duo']

我不确定我是否需要在这里使用CountVectorizerTfidfVectorizer,使用DictVictorizer 更合适,但是如何使用从整个字符串中提取值的键来制作字典?

scikit-learn 的特征提取有可能吗?还是我应该创建自己的 .fit().transform() 方法?

更新: @sergzach,如果我理解正确,请查看:

data = ['Laptop Apple Macbook..', 'Laptop Dell Latitude...'...]

for d in data:
    for brand in brands:
       if brand in d:
          # ok brand is found
for model in models:
       if model in d:
          # ok model is found

那么为每个特征创建 N 循环?这可能有效,但不确定它是否正确和灵活。

【问题讨论】:

  • 您可以手动列出所有品牌,然后从文本中提取它们(可能使用 str.lower() 并删除不必要的字符),然后检查它们是否大部分被识别。然后查看未被识别的特征并决定如何处理它们。然后使用 DV.fit_transform 将它们转换为数字特征,缩放它们并将它们用作数字。
  • @sergzach 谢谢,我已经更新了我的问题,你能复习一下吗?
  • 我认为你可以使用这里提到的 sklearn 的CountVectorizer()scikit-learn.org/stable/tutorial/text_analytics/…。但无论如何你应该为fit_transform()准备数据。

标签: machine-learning scikit-learn text-classification


【解决方案1】:

Scikit Learn 的矢量化器会将字符串数组转换为倒排索引矩阵(二维数组,每个找到的术语/单词都有一列)。原始数组中的每一行(第一维)映射到输出矩阵中的一行。每个单元格将包含一个计数或权重,具体取决于您使用的矢量化器类型及其参数。

根据您的代码,我不确定这是您需要的。你能告诉你打算在哪里使用你正在寻找的这个功能吗?你打算训练分类器吗?目的是什么?

【讨论】:

    【解决方案2】:

    是的,类似下一个。

    对不起,也许你应该更正下面的代码。

    import re
    
    data = ['Laptop Apple Macbook..', 'Laptop Dell Latitude...'...]
    
    features = {
        'brand': [r'apple', r'dell', r'hp', r'asus', r'acer', r'lenovo'],
        'cpu': [r'core\s+i3', r'core\s+i5', r'core\s+i7', r'intel\s+pdc', r'core\s+m', r'intel\s+pentium', r'intel\s+core\s+duo']
        # and other features
    }
    
    cat_data = [] # your categories which you should convert into numbers
    
    not_found_columns = []
    
    for line in data:
        line_cats = {}
    
        for col, features in features.iteritems():
            for i, feature in enumerate(features):
                found = False
    
                if re.findall(feature, line.lower(), flags=re.UNICODE) != []:
                    line_cats[col] = i + 1 # found numeric category in column. For ex., for dell it's 2, for acer it's 5.               
                    found = True
                    break # current category is determined by a first occurence
    
            # cycle has been end but feature had not been found. Make column value as default not existing feature
            if not found:       
                line_cats[col] = 0
                not_found_columns.append((col, line))
    
            cat_data.append(line_cats)
    
    # now we have cat_data where each column is corresponding to a categorial (index+1) if a feature had been determined otherwise 0.
    

    现在您的列名带有未找到的行 (not_found_columns)。查看它们,可能您忘记了一些功能。

    我们也可以将字符串(而不是数字)写成类别,然后使用DV。结果这些方法是等效的。

    【讨论】:

    • 谢谢。我想,在 sciki-learn 中已经有内置的方法来解决这样的问题。但知道它更有意义。再次感谢。 :)
    • @Novitoll 可能是。但是许多这样的方法在任何情况下都需要准备好的数据。你可以等待另一个答案,人们的想法对我来说也很有趣。
    猜你喜欢
    • 2013-11-22
    • 2018-05-13
    • 2012-09-25
    • 2016-09-02
    • 2019-12-11
    • 2016-06-23
    • 2016-02-25
    • 2018-02-24
    • 2016-03-18
    相关资源
    最近更新 更多