我正在做LDA模型。
from helper import *
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import gensim
from gensim.models import ldamodel
import gensim.corpora
#sp = split.astype('str')
text_list = [i.split() for i in text]
#text_list = split[0]
train_headlines = [i.split() for i in text_list[0]];
num_topics = 10
id2word = gensim.corpora.Dictionary(train_headlines)
corpus = [id2word.doc2bow(text) for text in train_headlines]
lda = ldamodel.LdaModel(corpus=corpus, id2word=id2word, num_topics=num_topics)
def get_lda_topics(model, num_topics):
word_dict = {}
for i in range(num_topics):
words = model.show_topic(i, topn = 50);
word_dict['Topic # ' + '{:02d}'.format(i+1)] = [i[0] for i in words]
#print(word_dict)
return pd.DataFrame(word_dict)
topics_lda = get_lda_topics(lda, num_topics)
结果是数据帧 #1 包含单词,如下所示:
Topic # 01 Topic # 02 Topic # 03 Topic # 04 Topic # 05 Topic # 06 Topic # 07 Topic # 08
0 mendapat Kompascom ini resmi dalam jaringan baru KOMPAScom
1 dunia secara fakta IFCN lain Network selain Di
2 selain selain Lembaga secara Kompascom secara secara penguji
3 ada Network Kompascom Kompascom ini 49 Kompascom dunia
我想检查数据帧 # 1 的内容是否也在列表 # 2 中。 以下是列表 #2:
['dalam', 'database', 'dilihat', 'sini', 'atau', 'bisa', 'hoaks', 'fakta', 'di', 'KOMPAScom']
['liputan6com', 'mafindo', 'itu', 'tirtoid', 'tempoco', 'lima', 'turnbackhoaxid', 'adalah', 'lembaga', 'dan']
['lembaga', 'checking', 'fact', '49', 'jaringan', 'ada', 'international', 'tersertifikasi', 'network', 'penguji']
['sumber', 'dijadikan', 'beritanya', 'pendanaan', 'partisan', 'non', 'sikap', 'dasar', 'transparan', 'mengutip']
因此,输出将是:
'mendapat':0, 'Kompascom':0, 'ini': 0, 'resmi':0, 'dalam':1, 'jaringan':1, 'baru':0, 'KOMPAScom':1,.....
如果文本位于数据框和列表中,则为 1;如果文本仅位于一个数据帧或列表中,则为 0。 任何人都可以提供帮助吗?谢谢。任何帮助,不胜感激。
用isin
试试这段代码:
l = ['dalam', 'database', 'dilihat', 'sini', 'atau', 'bisa', 'hoaks', 'fakta', 'di', 'KOMPAScom', 'liputan6com', 'mafindo', 'itu', 'tirtoid', 'tempoco', 'lima', 'turnbackhoaxid', 'adalah', 'lembaga', 'dan', 'lembaga', 'checking', 'fact', '49', 'jaringan', 'ada', 'international', 'tersertifikasi', 'network', 'penguji', 'sumber', 'dijadikan', 'beritanya', 'pendanaan', 'partisan', 'non', 'sikap', 'dasar', 'transparan', 'mengutip']
print(dict(zip(df.values.flatten().tolist(), df.isin(l).astype(int).values.flatten().tolist())))
输出:
{'mendapat': 0, 'Kompascom': 0, 'ini': 0, 'resmi': 0, 'dalam': 1, 'jaringan': 1, 'baru': 0, 'KOMPAScom': 1, 'dunia': 0, 'secara': 0, 'fakta': 1, 'IFCN': 0, 'lain': 0, 'Network': 0, 'selain': 0, 'Di': 0, 'Lembaga': 0, 'penguji': 1, 'ada': 1, '49': 1}
将数据帧中的所有值收集到一个集中,并与一组比较项进行比较。
import pandas as pd
df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
print(df)
# use set comprehension to "flatten" the nested list that comes from df.values
df_values = {item for sublist in df.values for item in sublist}
print(df_values)
compares = {1,4,9}
# find the items that are in both
print(compares & df_values)
# find the items that are only in the compares set
print(compares - df_values)
生产:
a b
0 1 4
1 2 5
2 3 6
{1, 2, 3, 4, 5, 6}
{1, 4}
{9}