from sklearn.datasets import fetch_20newsgroupscategory_map = {'misc.forsale': 'Sales', 'rec.motorcycles': 'Motorcycles','rec.sport.baseball': 'Baseball', 'sci.crypt': 'Cryptography','sci.space': 'Space'}training_data = fetch_20newsgroups(subset='train',categories=category_map.keys(), shuffle=True, random_state=7)# Feature extractionfrom sklearn.feature_extraction.text import CountVectorizervectorizer = CountVectorizer()X_train_termcounts = vectorizer.fit_transform(training_data.data)print "\nDimensions of training data:", X_train_termcounts.shape# Training a classifierfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.feature_extraction.text import TfidfTransformerinput_data = ["The curveballs of right handed pitchers tend to curve to the left","Caesar cipher is an ancient form of encryption","This two-wheeler is really good on slippery roads"]# tf-idf transformertfidf_transformer = TfidfTransformer()X_train_tfidf = tfidf_transformer.fit_transform(X_train_termcounts)# Multinomial Naive Bayes classifierclassifier = MultinomialNB().fit(X_train_tfidf, training_data.target)X_input_termcounts = vectorizer.transform(input_data)X_input_tfidf = tfidf_transformer.transform(X_input_termcounts)# Predict the output categoriespredicted_categories = classifier.predict(X_input_tfidf)# Print the outputsfor sentence, category in zip(input_data, predicted_categories):print '\nInput:', sentence, '\nPredicted category:', \category_map[training_data.target_names[category]]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。