将特征名称更新到 scikit TFIdfVectorizer [英] Updating the feature names into scikit TFIdfVectorizer

查看:30
本文介绍了将特征名称更新到 scikit TFIdfVectorizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个代码

from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np

train_data = ["football is the sport","gravity is the movie", "education is imporatant"]
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
                                                 stop_words='english')

print "Applying first train data"
X_train = vectorizer.fit_transform(train_data)
print vectorizer.get_feature_names()

print "

Applying second train data"
train_data = ["cricket", "Transformers is a film","AIMS is a college"]
X_train = vectorizer.transform(train_data)
print vectorizer.get_feature_names()

print "

Applying fit transform onto second train data"
X_train = vectorizer.fit_transform(train_data)
print vectorizer.get_feature_names()

这个输出是

Applying first train data
[u'education', u'football', u'gravity', u'imporatant', u'movie', u'sport']


Applying second train data
[u'education', u'football', u'gravity', u'imporatant', u'movie', u'sport']


 Applying fit transform onto second train data
[u'aims', u'college', u'cricket', u'film', u'transformers']

我使用 fit_transform 将第一组数据提供给矢量化器,因此它为我提供了特征名称,例如 [u'education', u'football', u'gravity', u'important', u'movie',u'sport'] 之后,我将另一个训练集应用到同一个矢量化器,但它给了我相同的特征名称,因为我没有使用 fit 或 fit_transform.但我想知道如何在不覆盖以前的 oncs 的情况下更新矢量化器的功能.如果我再次使用 fit_transform,以前的功能将被覆盖.所以我想更新vectorizer的功能列表.所以我想要类似 [u'education', u'football', u'gravity', u'important', u'movie', u'sport',u'aims', u'college', u'cricket', u'film', u'transformers'] 我怎么能得到那个.

I gave the first set of data using fit_transform to vectorizer so it gave me feature names like [u'education', u'football', u'gravity', u'imporatant', u'movie', u'sport'] after that i applied another train set to the same vectorizer but it gave me the same feature names as I didnt use fit or fit_transform. But I want to know how to update the features of a vectorizer without overwriting the previous oncs. If I use fit_transform again the previous features will get overwritten. So I want to update the feature list of the vectorizer. So i want something like [u'education', u'football', u'gravity', u'imporatant', u'movie', u'sport',u'aims', u'college', u'cricket', u'film', u'transformers'] How can I get that.

推荐答案

在 sklearn 术语中,这称为部分拟合,您无法使用 TfidfVectorizer 来实现.有两种方法可以解决这个问题:

In sklearn terminology, this is called a partial fit and you can't do it with a TfidfVectorizer. There are two ways around this:

  • 连接两个训练集并重新矢量化
  • 使用支持部分拟合的 HashingVectorizer.但是,由于是散列功能,因此没有 get_feature_names 方法,因此不会保留原始文件.另一个优点是它的内存效率更高.
  • Concatenate the two training sets and re-vectorize
  • use a HashingVectorizer, which support partial fitting. However, that does not have a get_feature_names method due to the fact that is hashes features, so the original isn't kept. Another advantage is that this is much more memory efficient.

第一种方法的例子:

from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np

train_data1 = ["football is the sport", "gravity is the movie", "education is important"]
vectorizer = TfidfVectorizer(stop_words='english')

print("Applying first train data")
X_train = vectorizer.fit_transform(train_data1)
print(vectorizer.get_feature_names())

print("

Applying second train data")
train_data2 = ["cricket", "Transformers is a film", "AIMS is a college"]
X_train = vectorizer.transform(train_data2)
print(vectorizer.get_feature_names())

print("

Applying fit transform onto second train data")
X_train = vectorizer.fit_transform(train_data1 + train_data2)
print(vectorizer.get_feature_names())

输出:

Applying first train data
['education', 'football', 'gravity', 'important', 'movie', 'sport']

Applying second train data
['education', 'football', 'gravity', 'important', 'movie', 'sport']

Applying fit transform onto second train data
['aims', 'college', 'cricket', 'education', 'film', 'football', 'gravity', 'important', 'movie', 'sport', 'transformers']

这篇关于将特征名称更新到 scikit TFIdfVectorizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆