Scikit-learn: 如何从文本中提取特征?

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

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

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

item=Laptopbrand=Applemodel=Macbook Air A1465cpu=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() 方法?

更新:@人名,请审阅我是否正确理解了你的意思:

data = ['Laptop Apple Macbook..', 'Laptop Dell Latitude...'...]for d in data:    for brand in brands:       if brand in d:          # ok brand is foundfor model in models:       if model in d:          # ok model is found

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


回答:

是的,类似这样。

抱歉,可能你需要修正下面的代码。

import redata = ['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']    # 和其他特征}cat_data = [] # 你需要转换成数字的类别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 # 找到列中的数字类别。例如,dell 是 2,acer 是 5。                               found = True                break # 当前类别由第一次出现决定        # 循环结束但未找到特征。将列值设为默认不存在的特征        if not found:                   line_cats[col] = 0            not_found_columns.append((col, line))        cat_data.append(line_cats)# 现在我们有了 cat_data,其中每一列对应于一个类别(索引+1),如果找到了特征,否则为 0。

现在你有列名和未找到的行(not_found_columns)。查看它们,可能你忘记了一些特征。

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注