sklearn Naive Bayes

One. Fundamental

Bayesian formula

 two. In the case of text classification

 sklearn achieve

. 1  from sklearn.datasets Import fetch_20newsgroups
 2  from sklearn.model_selection Import train_test_split
 . 3  from sklearn.feature_extraction.text Import TfidfVectorizer
 . 4  from sklearn.naive_bayes Import MultinomialNB
 . 5  
. 6  DEF news_classification ():
 . 7      "" " 
. 8      Naive Bayes classification of news
 . 9      : return:
 10      "" " 
. 11      # 1. acquires data 
12 is      News fetch_20newsgroups = ( " C: / new new ", Subset = " All " )
 13 is      # Print (News) 
14      # 2. Data Partitioning 
15      x_train, x_test, y_train, android.permission.FACTOR. = train_test_split (news.data, news.target)
 16      #   Print (x_train) 
. 17      # 3. Characteristics Engineering: extract features -tfidf 
18 is      Transfer = TfidfVectorizer ()
 . 19      x_train = transfer.fit_transform (x_train)
 20 is      x_test = transfer.transform (x_test)
 21 is      # 4. predictor naive Bayes' 
22 is      Estimator = MultinomialNB ()
 23 is      estimator.fit (x_train, y_train)
 24     # The model evaluation 
25      y_predict = estimator.predict (x_test)
 26 is      Print (y_predict)
 27      Print ( " : direct comparison of the predicted value and the true value \ n- " , y_predict == android.permission.FACTOR.)
 28      # 2: Direct calculation accuracy 
29      Score = estimator.score (x_test, android.permission.FACTOR.)
 30      Print ( " accuracy rate: " , Score)
 31 is  IF  the __name__ == " __main__ " :
 32      news_classification ()

 

 

 

Guess you like

Origin www.cnblogs.com/sclu/p/11763666.html