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

Poe*_*dit 5 python python-3.x scikit-learn tfidfvectorizer

假设我有一个包含两列的数据框,其中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
Run Code Online (Sandbox Code Playgroud)

然后我在这个数据集上使用TfidfVectorizerfrom sklearn

找到每类 TF-IDF 得分词汇量前 n 名的最有效方法是什么?

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

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

我认为我的帖子与以下帖子有很多相关性:

Gil*_*kan 7

以下代码将完成这项工作(感谢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
Run Code Online (Sandbox Code Playgroud)

用法:

# 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)
Run Code Online (Sandbox Code Playgroud)


Ped*_*ram 2

在下面,您可以找到我三年多前出于类似目的编写的一段代码。我不确定这是否是做你要做的事情的最有效的方法,但据我记得,它对我有用。

# 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
Run Code Online (Sandbox Code Playgroud)