機器學習——樸素貝葉斯

來源:互聯網
上載者:User

標籤:form   intel   機器   樸素貝葉斯   vector   rap   ace   article   大量   

 在考慮一個結果的機率時候,要考慮眾多的屬性,貝葉斯演算法利用所有可能的資料來進行修正預測,如果大量的特徵產生的影響較小,放在一起,組合的影響較大,適合於樸素貝葉斯分類

匯入類庫
1 from sklearn.datasets import fetch_20newsgroups2 from sklearn.feature_extraction.text import TfidfVectorizer3 from sklearn.naive_bayes import MultinomialNB
代碼
 1 def article_category(): 2     categories = [‘alt.atheism‘, ‘soc.religion.christian‘, ‘comp.graphics‘, ‘sci.med‘] 3     twenty_train = fetch_20newsgroups(subset=‘train‘, categories=categories) 4     # print(twenty_train) 5     print(twenty_train.data) 6     print(twenty_train.target) 7     # 將x訓練集詞頻向量化 8     tfidf_transformer = TfidfVectorizer() 9     X_train_tfidf = tfidf_transformer.fit_transform(twenty_train.data)10 11     # 貝葉斯訓練12     clf = MultinomialNB(alpha=1.0).fit(X_train_tfidf, twenty_train.target)13     docs_new = [‘Chemical reaction‘, ‘Intel CPU is good‘]14 15     # 將要預測的資料詞頻向量化16     X_new_tfidf = tfidf_transformer.transform(docs_new)17     # 預測18     predicted = clf.predict(X_new_tfidf)19     print(predicted)20     for doc, category in zip(docs_new, predicted):21         print(‘%r => %s‘ % (doc, twenty_train.target_names[category]))

 

機器學習——樸素貝葉斯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.