当前位置:网站首页>9. naive Bayes
9. naive Bayes
2022-06-30 05:17:00 【CGOMG】
Introduction to naive Bayes ( Probability Classification )

Probability basis



joint probability 、 Conditional probability and mutual independence

Bayes' formula
Introduce




Case study


API

Emotional analysis of commodity reviews

Import dependence
import pandas as pd
import numpy as py
import jieba
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
get data
data = pd.read_csv("evaluation.csv",encoding="gbk")
data

Basic data processing
# Take out the content column , For later analysis
content = data[" Content "]
content

# Convert the positive and negative comments in the evaluation into figures
data.loc[data.loc[:," evaluation "] == " Praise "," Comment number "] =1
data.loc[data.loc[:," evaluation "] == " Bad review "," Comment number "] =0
data

# Choose a stop word
stopwords = []
with open("stopwords.txt","r",encoding="utf-8") as f:
lines = f.readlines()
for tmp in lines:
line = tmp.strip()
stopwords.append(line)
stopwords = list(set(stopwords))
print(" Stop words :\n",stopwords)

# Convert content to standard format
comment_list = []
for tmp in content:
# Cut words into words
seg_list = jieba.cut(tmp,cut_all=False)
seg_str = ",".join(seg_list)
comment_list.append(seg_str)
comment_list

# Count the number of words
con = CountVectorizer(stop_words=stopwords)
X = con.fit_transform(comment_list)
X.toarray()

## Prepare training and test sets
x_train = X.toarray()[:10,:]
y_train = data[" evaluation "][:10]
print(" Training set :\n",x_train)
print(" Training set :\n",y_train)

x_test = X.toarray()[10:,:]
y_test = data[" evaluation "][10:]
print(" Test set :\n",x_test)
print(" Test set :\n",y_test)

model training
mb = MultinomialNB(alpha=1)
mb.fit(x_train,y_train)
y_pre = mb.predict(x_test)
Model to evaluate
print(" Predictive value ",y_pre)
print(" True value ",y_test)

mb.score(x_test,y_test)

Advantages and disadvantages of naive Bayes

Naive Bayesian content summary
NB Principle

Naive Bayes where is simplicity

Why introduce the conditional independence assumption

In estimating conditional probability P(X|Y) The probability of occurrence is 0 How to deal with the situation of

Why is the assumption of attribute independence difficult to hold in practice , But naive Bayes can still achieve better results

Naive Bayes and LR( Logical regression ) The difference between


边栏推荐
- GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>
- Chapter 7 vertex processing and drawing commands of OpenGL super classic (7th Edition)
- Nestjs configures static resources, template engine, and post examples
- Unity3d packaging and publishing APK process
- Win10 vs2015 compiling curaengine
- 产生 BUG 测试人员需要自己去分析原因吗?
- Summary of common loss functions in pytorch
- C # uses monopinvokecallback to directly call back C # function
- Bessel curve with n control points
- Unit asynchronous jump progress
猜你喜欢

LxC and LXD container summary

Installation and getting started with pytoch

RedisTemplate 常用方法汇总
![[note] usage model tree of the unity resource tree structure virtualizingtreeview](/img/3e/fe5610c797a14554ad735172c3ab54.jpg)
[note] usage model tree of the unity resource tree structure virtualizingtreeview

MinGW-w64下载文件失败the file has been downloaded incorrectly!

3D rotation album

Unity + hololens2 performance test

遥感图像/UDA:Curriculum-Style Local-to-Global Adaptation for Cross-Domain Remote Sensing Image Segmentat

QT connecting external libraries

The minecraft server address cannot be refreshed.
随机推荐
Unity shortcut key
Chapter 8 primitive processing of OpenGL super classic (version 7)
Unity- the camera follows the player
Unit asynchronous jump progress
Unity shader flat shadow
遥感图像/UDA:Curriculum-Style Local-to-Global Adaptation for Cross-Domain Remote Sensing Image Segmentat
[recruitment] UE4 Development Engineer
Pyinstaller flash back
Exploration of unity webgl
Some problems encountered in unity steamvr
Database base (Study & review for self use)
3D rotation album
Unity call Exe program
Golan no tests were run: fmt Printf() < BUG>
【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example
Writing unityshader with sublimetext
E: Topic focus
Revit二次開發---未打開項目使用面板功能
Unity animator does not clip animation to play animation in segments
Records of some problems encountered during unity development (continuously updated)