【发布时间】:2022-01-10 01:11:20
【问题描述】:
我有以下数据集(我将只上传4行的样本,真实的有15,000行):
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
import nltk
from nltk.corpus import stopwords
from sklearn.feature_selection import chi2
quotes=["Sip N Shop Come thru right now Marjais PopularNobodies MMR Marjais SipNShop",
"I do not know about you but My family and I will not take the Covid19 vaccine anytime soon",
"MSignorile Immunizations should be mandatory Period In Oklahoma they will not let kids go to school without them It is dangerous otherwise",
"President Obama spoke in favor of vaccination for children Fox will start telling its viewers to choose against vaccination in 321"]
labels=[0,1,2,0]
dummy = pd.DataFrame({"quote": quotes, "label":labels})
我想应用著名的卡方检验来消除每个类别 (0,1,2) 中不相关单词的数量。其中 0:中性,1:正,2:负。
以下是我的方法(类似于实现的方法here)
简而言之,我创建了一个等于语料库长度的 0 的空列表。 0 代表 y = 0 的第一个标签。对于第二个标签(1=正),我将创建一个空列表 1。第三个标签也是如此(2=否定)。
应用此 3 次后(对于每个目标标签),我将拥有三个 3 列表,每个标签具有最相关的单词。这个最终列表将是我的 TF-IDF 矢量化器的新词汇。
def tweeter_tokenizer(tweet):
return tweet.split(' ')
vectorizer = TfidfVectorizer(tokenizer=tweeter_tokenizer, ngram_range=(1,2), stop_words=english_stopwords)
vectorizer.fit(dummy["quote"])
X_train = vectorizer.transform(dummy["quote"])
y_train = dummy["label"]
feature_names = vectorizer.get_feature_names_out()
y_neutral = np.array([0]*X_train.shape[0])
pValue = 0.90
chi_neutral, p_neutral = chi2(X_train, y_neutral)
chi_neutral
chi_neutral 对象是:
数组([南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南,南,南,南,南,南,南,南,南,南,南,南, 南,南])
最后,我想创建一个数据框,该数据框等于每个标签的唯一标记 (feature_names) 的长度。我将只保留 score > pValue 的单词。数据框将向我显示语料库的总令牌中有多少依赖于 0 类(中性)。其余标签将遵循相同的方法(1:正面,2:负面)。
y_df = np.array([0]*X_train.shape[1])
tokens_neutral_dependent = pd.DataFrame({
"tweet_token": feature_names,
"chi2_score" : 1-p_neutral,
"neutral_label": y_df #length = length of feature_names()
})
tokens_neutral_dependent = tokens_neutral_dependent.sort_values(["neutral_label","chi2_score"], ascending=[True,False])
tokens_neutral_dependent = tokens_neutral_dependent[tokens_neutral_dependent["chi2_score"]>pValue]
tokens_neutral_dependent.shape
【问题讨论】:
标签: python pandas scikit-learn chi-squared