查找每班 TF-IDF 分数最高的前 n 个术语



假设我有一个数据帧,pandas中有两列,类似于下面的一列:

    text                                label
0   This restaurant was amazing         Positive
1   The food was served cold            Negative
2   The waiter was a bit rude           Negative
3   I love the view from its balcony    Positive

然后我在这个数据集上使用来自sklearn TfidfVectorizer

找到每节课 TF-IDF 分数词汇的前 n 名的最有效方法是什么?

显然,我的实际数据帧包含的数据行比上面的 4 行多得多。

我帖子的重点是找到适用于任何类似于上述数据帧的代码; 4行数据帧或1M行数据帧。

我认为我的帖子与以下帖子有很大关系:

  • Scikit Learn TfidfVectorizer : 如何获得具有最高tf-idf分数的前n个术语
  • 如何在scikit-learn中查看tfidf之后术语文档矩阵的前n个条目

下面的代码将完成这项工作(感谢Mariia Havrylovych(。

假设我们有一个与您的结构对齐的输入数据帧 df

from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
# override scikit's tfidf-vectorizer in order to return dataframe with feature names as columns
class DenseTfIdf(TfidfVectorizer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for k, v in kwargs.items():
            setattr(self, k, v)
    def transform(self, x, y=None) -> pd.DataFrame:
        res = super().transform(x)
        df = pd.DataFrame(res.toarray(), columns=self.get_feature_names())
        return df
    def fit_transform(self, x, y=None) -> pd.DataFrame:
        # run sklearn's fit_transform
        res = super().fit_transform(x, y=y)
        # convert the returned sparse documents-terms matrix into a dataframe to further manipulations
        df = pd.DataFrame(res.toarray(), columns=self.get_feature_names(), index=x.index)
        return df

用法:

# assume texts are stored in column 'text' within a dataframe
texts = df['text']
df_docs_terms_corpus = DenseTfIdf(sublinear_tf=True,
                 max_df=0.5,
                 min_df=2,
                 encoding='ascii',
                 ngram_range=(1, 2),
                 lowercase=True,
                 max_features=1000,
                 stop_words='english'
                ).fit_transform(texts)

# Need to keep alignment of indexes between the original dataframe and the resulted documents-terms dataframe
df_class = df[df["label"] == "Class XX"]
df_docs_terms_class = df_docs_terms_corpus.iloc[df_class.index]
# sum by columns and get the top n keywords
df_docs_terms_class.sum(axis=0).nlargest(n=50)

在下文中,你可以找到我三年多前为类似目的写的一段代码。我不确定这是否是做你将要做的事情的最有效方式,但据我所知,它对我有用。

# X: data points
# y: targets (data points` label)
# vectorizer: TFIDF vectorizer created by sklearn
# n: number of features that we want to list for each class
# target_list: the list of all unique labels (for example, in my case I have two labels: 1 and -1 and target_list = [1, -1])
# --------------------------------------------
# splitting X vectors based on target classes
for label in target_list:
    # listing the most important words in each class
    indices = []
    current_dict = {}
    # finding indices the of rows (data points) for the current class
    for i in range(0, len(X.toarray())):
        if y[i] == label:
            indices.append(i)
    # get rows of the current class from tf-idf vectors matrix and calculating the mean of features values
    vectors = np.mean(X[indices, :], axis=0)
    # creating a dictionary of features with their corresponding values
    for i in range(0, X.shape[1]):
        current_dict[X.indices[i]] = vectors.item((0, i))
    # sorting the dictionary based on values
    sorted_dict = sorted(current_dict.items(), key=operator.itemgetter(1), reverse=True)
    # printing the features textual and numeric values
    index = 1
    for element in sorted_dict:
        for key_, value_ in vectorizer.vocabulary_.items():
            if element[0] == value_:
                print(str(index) + "t" + str(key_) + "t" + str(element[1]))
                index += 1
                if index == n:
                    break
        else:
            continue
        break
top_terms = pd.DataFrame(columns = range(1,6))
for i in term_doc_mat.index:
    top_terms.loc[len(top_terms)] = term_doc_mat.loc[i].sort_values(ascending = False)[0:5].index

这将为您提供每个文档的前 5 个术语。根据需要进行调整。

最新更新