实验目的
- 掌握决策树算法原理;
2.编程实现朴素贝叶斯算法算法,并实现分类任务
实验内容
1.使用sklearn的朴素贝叶斯算法对鸢尾花数据集进行分类,要求:
(1)划分训练集和测试集(测试集占20%)
(2)对测试集的预测类别标签和真实标签进行对比
(3)输出分类的准确率
2.动手编写朴素贝叶斯分类算法对手写字体数据集进行分类,要求:
(1)划分训练集和测试集(测试集占20%)
(2)对手写字体进行二值化处理
(3)输出分类的准确率
实验代码
1.
#coding=UTF-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
data = load_iris()
X = data.data
y = data.target
# 划分训练集和测试集
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2, random_state=42)
# 使用朴素贝叶斯算法进行分类
model = GaussianNB()
model.fit(train_X, train_y)
# 对测试集进行预测
pred_y = model.predict(test_X)
# 计算准确率
accuracy = accuracy_score(test_y, pred_y)
print(f"分类的准确率为: {accuracy}")
# 对比预测结果和真实标签
comparison_df = pd.DataFrame({'真实标签': test_y, '预测标签': pred_y})
print(comparison_df)
2.
import gzip
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
def load_data(filename):
with gzip.open(filename, 'rb') as f:
data = pickle.load(f, encoding='latin1')
return data
def binarize_data(data, threshold=127):
return (data > threshold).astype(int)
class NaiveBayes:
def __init__(self):
self.class_prior = None
self.conditional_prob = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.classes = np.unique(y)
n_classes = len(self.classes)
self.class_prior = np.zeros(n_classes)
self.conditional_prob = np.zeros((n_classes, n_features, 2))
for i, c in enumerate(self.classes):
X_c = X[y == c]
self.class_prior[i] = X_c.shape[0] / n_samples
self.conditional_prob[i,:,0] = (np.sum(X_c == 0, axis=0) + 1) / (X_c.shape[0] + 2) # Laplace smoothing
self.conditional_prob[i,:,1] = (np.sum(X_c == 1, axis=0) + 1) / (X_c.shape[0] + 2) # Laplace smoothing
def predict(self, X):
preds = []
for x in X:
prob = []
for i, c in enumerate(self.classes):
likelihood = 1.0
for j in range(len(x)):
if x[j] == 1:
likelihood *= self.conditional_prob[i, j, 1]
else:
likelihood *= self.conditional_prob[i, j, 0]
prob.append(likelihood * self.class_prior[i])
preds.append(np.argmax(prob))
return np.array(preds)
# 加载数据
data = load_data('F:/File/大三下/机器学习/朴素贝叶斯分类实验/实验/mnist.pkl.gz')
X = data[0][0]
y = data[0][1]
# 划分训练集和测试集(测试集占20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 对手写字体进行二值化处理
X_train_binary = binarize_data(X_train)
X_test_binary = binarize_data(X_test)
# 创建朴素贝叶斯分类器
clf = NaiveBayes()
# 训练模型
clf.fit(X_train_binary, y_train)
# 对测试集进行预测
y_pred = clf.predict(X_test_binary)
# 输出分类的准确率
accuracy = accuracy_score(y_test, y_pred)
print("分类准确率:", accuracy)
标签:河南大学,self,贝叶斯,train,test,import,data,prob,朴素
From: https://blog.csdn.net/m0_68231248/article/details/137395889