pos機發(fā)朋友圈的好句子,「火爐煉AI」機器學(xué)習(xí)041

 新聞資訊2  |   2023-06-14 09:23  |  投稿人:pos機之家

網(wǎng)上有很多關(guān)于pos機發(fā)朋友圈的好句子,「火爐煉AI」機器學(xué)習(xí)041的知識,也有很多人為大家解答關(guān)于pos機發(fā)朋友圈的好句子的問題,今天pos機之家(m.dsth100338.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、pos機發(fā)朋友圈的好句子

pos機發(fā)朋友圈的好句子

【火爐煉AI】機器學(xué)習(xí)041-NLP句子情感分析

【火爐煉AI】機器學(xué)習(xí)041-NLP句子情感分析

(本文所使用的Python庫和版本號: Python 3.6, Numpy 1.14, scikit-learn 0.19, matplotlib 2.2 )

在NLP中有一個非常實用的應(yīng)用領(lǐng)域--情感分析,情感分析是用NLP技術(shù)分析一段給定文本的情感類型,是積極的還是消極的,是樂觀的還是悲觀的等。比如在股市中,我們知道,往往大眾最悲觀的時候往往是股市的大底,而最樂觀的時候卻是股市的頂部,所以,如果我們能夠掌握大眾的心里情感狀況,那么也就能大概知道股市的底和頂,換言之,也就能夠在股市上掙得大把大把的銀子了。

1. 準備數(shù)據(jù)集

本項目所使用的數(shù)據(jù)集也是由nltk內(nèi)部提供,其中的corpus模塊中有movies_reviews,可以給我們提供“積極”和“消極”的語句文本。

# 1, 準備數(shù)據(jù)集from nltk.corpus import movie_reviewspos_fileIds=movie_reviews.fileids(\'pos\') # 加載積極文本文件neg_fileIds=movie_reviews.fileids(\'neg\') # 消極文本文件print(len(pos_fileIds)) # 1000print(len(neg_fileIds)) # 1000 print(pos_fileIds[:5])print(neg_fileIds[:5])# 由此可看出,movie_reviews.fileids是加載各種類別文本的文件,# 并返回該文件名組成的list# 如果想要查看某個文本文件的內(nèi)容,可以使用print(movie_reviews.words(fileids=[\'pos/cv000_29590.txt\']))復(fù)制代碼

----------------輸---------出-----------------

1000 1000 [\'pos/cv000_29590.txt\', \'pos/cv001_18431.txt\', \'pos/cv002_15918.txt\', \'pos/cv003_11664.txt\', \'pos/cv004_11636.txt\'] [\'neg/cv000_29416.txt\', \'neg/cv001_19502.txt\', \'neg/cv002_17424.txt\', \'neg/cv003_12683.txt\', \'neg/cv004_12641.txt\'] [\'films\', \'adapted\', \'from\', \'comic\', \'books\', \'have\', ...]

-----------------------完---------------------

雖然上面把文本文件的名稱提取出來,但是我們還需要從這些txt文件中提取出所需要的特征,使用這些特征才能進行后續(xù)的分類器建模。

# 2, 處理數(shù)據(jù)集def extract_features(word_list): \'\'\'專門一個函數(shù)來提取特征\'\'\' return dict([(word,True) for word in word_list]) # 此處加True的作用是構(gòu)成dict,實質(zhì)意義不大pos_features=[(extract_features(movie_reviews.words(fileids=[f])),\'Pos\') for f in pos_fileIds]neg_features=[(extract_features(movie_reviews.words(fileids=[f])),\'Neg\') for f in neg_fileIds]print(pos_features[:3]) # 打印下看看內(nèi)容是否正確dataset=pos_features+neg_features # 將兩部分結(jié)合起來作為一個dataset復(fù)制代碼

打印出來的結(jié)果很長,可以參考我的github里面的代碼。

2. 建立模型,訓(xùn)練特征

# 構(gòu)建模型,訓(xùn)練模型from nltk import NaiveBayesClassifierfrom nltk.classify import Accuracy as nltk_accuracynp.random.shuffle(dataset)rows=int(len(dataset)*0.8) # 80%為train settrain_set,test_set=dataset[:rows],dataset[rows:]print(\'Num of train_set: \',len(train_set), \'/nNum of test_set: \',len(test_set))clf=NaiveBayesClassifier.train(train_set)# 查看該模型在test set上的表現(xiàn)acc=nltk_accuracy(clf,test_set)print(\'Accuracy: {:.2f}%\'.format(acc*100))復(fù)制代碼

-------------------輸---------出-----------------------

Num of train_set: 1600

Num of test_set: 400

Accuracy: 70.75%

-------------------------完---------------------------

由此可以看出該模型在測試集上的表現(xiàn)為:準確率70.75%

# 查看模型內(nèi)部信息# 該分類器是分析某段文本中哪些單詞與“積極”的關(guān)聯(lián)最大,# 哪些與“消極”的關(guān)聯(lián)最大,進而分析這些關(guān)鍵詞的出現(xiàn)來判斷某句話是積極或消極# 打印這些關(guān)鍵詞for key_word in clf.most_informative_features()[:10]: print(key_word[0])復(fù)制代碼

----------------輸---------出-----------------------

outstanding

insulting

ludicrous

affecting

magnificent

breathtaking

avoids

strongest

fascination

slip

------------------------完-------------------------

可以看出,這些關(guān)鍵詞對于區(qū)分一個句子是積極還是消極有著至關(guān)重要的作用,或者說,如果某個句子中有了這些關(guān)鍵詞,就可以區(qū)分這個句子的情感是積極還是消極,這些單詞越多,模型預(yù)測的準確率就越高。

3. 用成熟模型預(yù)測新樣本

# 用該模型來預(yù)測新樣本,查看新句子的情感是積極還是消極new_Samples = [ "It is an amazing movie", "This is a dull movie. I would never recommend it to anyone.", "The cinematography is pretty great in this movie", "The direction was terrible and the story was all over the place" ]for sample in new_samples: predict_P=clf.prob_classify(extract_features(sample.split())) pred_sentiment=predict_P.max() print(\'Sample: {}, Type: {}, Probability: {:.2f}%\'.format( sample,pred_sentiment,predict_P.prob(pred_sentiment)*100))復(fù)制代碼

----------------輸---------出-----------------------

Sample: It is an amazing movie, Type: Pos, Probability: 61.45%

Sample: This is a dull movie. I would never recommend it to anyone., Type: Neg, Probability: 80.12%

Sample: The cinematography is pretty great in this movie, Type: Pos, Probability: 63.63%

Sample: The direction was terrible and the story was all over the place, Type: Neg, Probability: 63.89%

------------------------完----------------------------

########################小**********結(jié)###############################

1,NLTK中所使用的分類器需要用dict類型的數(shù)據(jù)作為features來數(shù)據(jù),故而我們需要自定義一個extract_features函數(shù),來將單詞轉(zhuǎn)變?yōu)閐ict類型。

2,NLTK中已經(jīng)集成了很多分類器,比如NaiveBayesClassifier,這些分類器已經(jīng)集成了字符串處理方面的各種細節(jié),使用起來很方便。

#################################################################

注:本部分代碼已經(jīng)全部上傳到(我的github)上,歡迎下載。

參考資料:

1, Python機器學(xué)習(xí)經(jīng)典實例,Prateek Joshi著,陶俊杰,陳小莉譯參考文獻:K碼農(nóng)-http://kmanong.top/kmn/qxw/form/home?top_cate=28

以上就是關(guān)于pos機發(fā)朋友圈的好句子,「火爐煉AI」機器學(xué)習(xí)041的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機發(fā)朋友圈的好句子的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://m.dsth100338.com/newsone/67727.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報,一經(jīng)查實,本站將立刻刪除。