Chapter 7 Naive Bayesian Model
1. Simple code demonstration of Naive Bayesian model
from sklearn.naive_bayes import GaussianNB
X = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
y = [0, 0, 0, 1, 1]
model = GaussianNB()
model.fit(X, y)
print(model.predict([[5, 5]]))
[0]
2. Case study - tumor prediction model
2.1 Read data
import pandas as pd
df = pd.read_excel('肿瘤数据.xlsx')
df.head()
Maximum circumference | maximum sag | average sag | maximum area | maximum radius | average gray value | tumor properties | |
---|---|---|---|---|---|---|---|
0 | 184.60 | 0.2654 | 0.14710 | 2019.0 | 25.38 | 17.33 | 0 |
1 | 158.80 | 0.1860 | 0.07017 | 1956.0 | 24.99 | 23.41 | 0 |
2 | 152.50 | 0.2430 | 0.12790 | 1709.0 | 23.57 | 25.53 | 1 |
3 | 98.87 | 0.2575 | 0.10520 | 567.7 | 14.91 | 26.50 | 0 |
4 | 152.20 | 0.1625 | 0.10430 | 1575.0 | 22.54 | 16.67 | 0 |
2.2 Divide feature variables and target variables
X = df.drop(columns='肿瘤性质')
y = df['肿瘤性质']
2.3 Model building
2.3.1 Divide training set and test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
2.3.2 Naive Bayesian model
from sklearn.naive_bayes import GaussianNB
nb_clf = GaussianNB() # 高斯朴素贝叶斯模型
nb_clf.fit(X_train,y_train)
GaussianNB(priors=None, var_smoothing=1e-09)