如果标题令人困惑,请原谅我,让我来解释一下。
所以,我编写了一个程序,使用nltk和sklearn的工具按主题对电子邮件进行分类。
这是那段代码:
#Extract Emailstech = extract_message("C:\\Users\\Cody\\Documents\\Emails\\tech.html")gary = extract_message("C:\\Users\\Cody\\Documents\\Emails\\gary.html")gary2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\gary2.html")jesus = extract_message("C:\\Users\\Cody\\Documents\\Emails\\Jesus.html")jesus2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\jesus2.html")hockey = extract_message("C:\\Users\\Cody\\Documents\\Emails\\hockey.html")hockey2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\hockey2.html")shop = extract_message("C:\\Users\\Cody\\Documents\\Emails\\shop.html")#Build dictionary of featurescount_vect = CountVectorizer()x_train_counts = count_vect.fit_transform(news.data)#Downscalingtfidf_transformer = TfidfTransformer()x_train_tfidf = tfidf_transformer.fit_transform(x_train_counts)tf_transformer = TfidfTransformer(use_idf=False).fit(x_train_counts)x_train_tf = tf_transformer.transform(x_train_counts)#Train classifierclf = MultinomialNB().fit(x_train_tfidf, news.target)#List of the extracted emailsdocs_new = [gary, gary2, jesus, jesus2, shop, tech, hockey, hockey2]#Extract feautures from emailsx_new_counts = count_vect.transform(docs_new)x_new_tfidf = tfidf_transformer.transform(x_new_counts)#Predict the categories for each emailpredicted = clf.predict(x_new_tfidf)
现在我想根据预测的标签将每个变量存储在相应的列表中。我想我可以这样做:
#Store Files in a categoryhockey_emails = []computer_emails = []politics_emails = []tech_emails = []religion_emails = []forsale_emails = []#Print out results and store each email in the appropritate category listfor doc, category in zip(docs_new, predicted): print('%r ---> %s' % (doc, news.target_names[category])) if(news.target_names[category] == 'comp.sys.ibm.pc.hardware'): computer_emails.append(doc) if(news.target_names[category] == 'rec.sport.hockey'): hockey_emails.append(doc) if(news.target_names[category] == 'talk.politics.misc'): politics_emails.append(doc) if(news.target_names[category] == 'soc.religion.christian'): religion_emails.append(doc) if(news.target_names[category] == 'misc.forsale'): forsale_emails.append(doc) if(news.target_names[category] == 'comp.sys.ibm.pc.hardware'): computer_emails.append(doc)
如果我打印出这些列表中的一个,比如说hockey,我的输出显示的是变量中存储的内容,而不是变量本身。
我想要的是这样:
print(hockey_emails)output: ['hockey', 'hockey2']
但我得到的是这样:
output: ['View View online click here Hi Thanks for signing up as a EA SPORTS NHL insider You ll now receive all of the latest and greatest news and info at this e mail address as you ve requested EA com If you need technical assistance please contact EA Help Privacy Policy Our Certified Online Privacy Policy gives you confidence whenever you play EA games To view our complete Privacy and Cookie Policy go to privacy ea com or write to Privacy Policy Administrator Electronic Arts Inc Redwood Shores Parkway Redwood City CA Electronic Arts Inc All Rights Reserved Privacy Policy User Agreement Legal ActionsMark as UnreadMark as ReadMark as SpamStarClear StarArchive Previous Next ', 'View News From The Hockey Writers The Editor s Choice stories from The Hockey Writers View this email in your browser edition Recap Stars Steamroll Predators By Matt Pryor on Dec am As the old Mary Chapin Carpenter song goes Sometimes you re the windshield Sometimes you re the bug It hasn t happened very often this season but the Dallas Stars had a windshield Continue Reading A Review of Years in Blue and White Damien Cox One on One By Anthony Fusco on Dec pm The Toronto Maple Leafs are one of the most storied and iconic franchises in the entire National Hockey League They have a century of history that spans all the way back to the early s When you have an Continue Reading Bruins Will Not Miss Beleskey By Kyle Benson on Dec am On Monday it was announced that Matt Beleskey will miss the next six weeks due to a knee injury he sustained over the weekend in a game against the Buffalo Sabres Six weeks is a long stint to be without a potential top Continue Reading Recent Articles Galchenyuk Injury Costly for CanadiensFacing Off Picking Team Canada for World JuniorsAre Johnson s Nomadic Days Over Share Tweet Forward Latest News Prospects Anaheim Ducks Arizona Coyotes Boston Bruins Buffalo Sabres Calgary Flames Carolina Hurricanes Chicago Blackhawks Colorado Avalanche Columbus Blue Jackets Dallas Stars Detroit Red Wings Edmonton Oilers Florida Panthers Los Angeles Kings Minnesota Wild Montreal Canadiens Nashville Predators New Jersey Devils New York Islanders New York Rangers Philadelphia Flyers Pittsburgh Penguins Ottawa Senators San Jose Sharks St Louis Blues Tampa Bay Lightning Toronto Maple Leafs Vancouver Canucks Washington Capitals Winnipeg Jets Copyright The Hockey Writers All rights reserved You are receiving this email because you opted in at The Hockey Writers or one of our Network Sites Our mailing address is The Hockey Writers Victoria Ave St Lambert QC J R R CanadaAdd us to your address book unsubscribe from this list update subscription preferences ActionsMark as UnreadMark as ReadMark as SpamStarClear StarArchive Previous Next ']
我以为这会很简单,但我现在正坐在这里挠头。这是否可能呢?我应该使用除列表之外的其他东西吗?这可能很简单,只是我想不出来。
回答:
你需要自己跟踪这些名称,Python不会为你做这件事。
names = 'gary gary2 Jesus jesus2 shop tech hockey hockey2'.split()docs_new = [extract_message("C:\\Users\\Cody\\Documents\\Emails\\%s.html" % name) for name in names]for name, category in zip(names, predicted): print('%r ---> %s' % (name, news.target_names[category])) if (news.target_names[category] == 'comp.sys.ibm.pc.hardware'): computer_emails.append(name)