机器学习day8

自定义数据集 ,使用朴素贝叶斯对其进行分类

代码

python 复制代码
import numpy as np
import matplotlib.pyplot as plt

class1_points = np.array([[2.1, 2.2], [2.4, 2.5], [2.2, 2.0], [2.0, 2.1], [2.3, 2.3], [2.6, 2.4], [2.5, 2.1]])
class2_points = np.array([[4.0, 3.5], [4.2, 3.9], [4.1, 3.8], [3.7, 3.4], [4.4, 3.6], [4.5, 3.7], [4.3, 3.9]])

X = np.concatenate((class1_points, class2_points), axis=0)
Y = np.concatenate((np.zeros(len(class1_points)), np.ones(len(class2_points))), axis=0)
print(Y)

prior_prob = [np.sum(Y == 0) / len(Y), np.sum(Y == 1) / len(Y)]

class_μ = [np.mean(X[Y == 0], axis=0), np.mean(X[Y == 1], axis=0)]
class_cov = [np.cov(X[Y == 0], rowvar=False), np.cov(X[Y == 1], rowvar=False)]

def pdf(x, mean, cov):
    n = len(mean)
    coff = 1 / (2 * np.pi) ** (n / 2) * np.sqrt(np.linalg.det(cov))
    exponent = np.exp(-(1 / 2) * np.dot(np.dot((x - mean).T, np.linalg.inv(cov)), (x - mean)))
    return coff * exponent

xx, yy = np.meshgrid(np.arange(0, 5, 0.05), np.arange(0, 5, 0.05))
grid_points = np.c_[xx.ravel(), yy.ravel()]

grid_label = []
for point in grid_points:
    poster_prob = []
    for i in range(2):
        likelihood = pdf(point, class_μ[i], class_cov[i])
        poster_prob.append(prior_prob[i] * likelihood)
    pre_class = np.argmax(poster_prob)
    grid_label.append(pre_class)

plt.scatter(class1_points[:, 0], class1_points[:, 1], c="blue", label="class 1")
plt.scatter(class2_points[:, 0], class2_points[:, 1], c="red", label="class 2")
plt.legend()

grid_label = np.array(grid_label)
pre_grid_label = grid_label.reshape(xx.shape)
contour = plt.contour(xx, yy, pre_grid_label, level=0.5, color='green')

plt.show()

效果

相关推荐
卷无止境1 分钟前
给AI装上一双眼睛:快速构建代码验证器的思路与落地方案
后端·python
维克兜率天20 分钟前
【维克】指数调整:哪些股票会被“强制买入“?
人工智能·python·算法
come1123430 分钟前
Codex 下不同场景 模式选择
python
罗西的思考1 小时前
【轻量化复刻大模型】线性注意力交互式演示
人工智能·算法·机器学习
神也畏惧菠萝1 小时前
Day08 Python流程控制从入门到上瘾:if/match/while/for四件套,从B站登录一路打到猜数字游戏
python·算法·数据存储·for·while·if·ifelse
言乐61 小时前
通过 3D 动画直观演示球形瓶内可滑动半圆机构在倾倒时防粘盖、回正时刮膏体的运动学过程
python·3d·django·virtualenv·pygame
quantdash_cc1 小时前
ETF 行情数据清洗实战:字段语义拆解、标的池整理与五个校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash
小熊恋旧2 小时前
Python 面向对象编程:方法、属性、继承与多态详解
开发语言·python
威哥爱编程2 小时前
Python 智能体开发实战 05:一问教材内容它就编?先把答案从你自己的资料里检索出来
python·agent