#bayes 算法 python代码实现(数据来源:https://github.com/fuqiuai/lihang_algorithms/blob/master/naive_bayes/naive_bayes.py)
import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score
import csv
SEED = 222
random_state = SEED
#数据处理,图像像素的二值化处理
data = pd.read_csv("bayes.train.csv")
data = data.values
label = data[:,0]
feature = data[:,1:]
feature = (((feature[:,:] > 50) == 1)&((feature[:,:] <= 50) == 0))
#将True和False转换成0,1值
feature = (feature + 0)
#概率计算:包括先验概率和条件概率计算
def probability(train_feature,train_label):
elem_spe_train_label = np.unique(train_label)
#先验概率
prio_proba = np.zeros((len(elem_spe_train_label)))
condi = np.zeros((len(elem_spe_train_label),784,2))
for i in range(len(train_label)):
bayes 算法 python代码实现
最新推荐文章于 2023-01-05 17:09:40 发布
本文详细介绍了如何使用Python编程实现贝叶斯定理,通过实例代码展示了贝叶斯分类器的工作原理,帮助读者理解并掌握贝叶斯算法在实际问题中的应用。
摘要由CSDN通过智能技术生成