41. Learning for Python, Getting Started

Source: Internet
Author: User

1. Bayesian classification algorithm (constructs the word vector from the text)

the build process of the vector is as followsdefLoaddataset (): Postinglist= [['my','Dog',' has','Flea',                    'problems',' Help',' please'],                    ['maybe',' not',' Take','him',                     ' to','Dog','Park','Stupid'],                     ['my','dalmation',' is',' So','Cute',                     'I',' Love','him'],                     ['Stop','Posting','Stupid','Worthless','Garbage'],                     ['Mr','Licks','ate','my','Steak',' How',                     ' to','Stop','him'],                     ['quit','Buying','Worthless','Dog',' Food','Stupid']] Classvec= [0,1,0,1,0,1]#1 for insulting words, 0 for normal speech    returnPostinglist,classvecdefcreatevocablist (dataSet): Vocabset=set ([]) forDocumentinchDataset:vocabset= Vocabset |set (document)returnlist (Vocabset)defSetofwords2vec (vocablist,inputset): Returnvec= [0]*Len (vocablist) forWordinchInputset:ifWordinchVocablist:returnvec[vocablist.index (word)]= 1Else:Print("The Word:%s is isn't in my vocabulary") %WordreturnReturnvec
The python command that is called in CMD's doc command line is as follows Import  == bayes.createvocablist (listoposts) myvocablist
The results of the program run are as follows

Check the glossary above and you will find that there are no duplicate words. Currently the glossary is not sorted, and if needed, it can be sorted later.

Here's a look at how the function Setofwords2vec () works

The function uses a glossary or all the words that you want to check as input, and then constructs a feature for each of these words.

Once a document is given (a message on the Spotted Dog website), the document is converted to a word vector. Next, check the validity of the function. In Myvocablist

What is the word for an element indexed to 2? , it should be the word Help. The word appears in the first document, and now check to see if it appears in the fourth document.

41. Learning for Python, Getting Started

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.