Numpy实现NaiveBayes(朴素贝叶斯)

(1)Python所有方向的学习路线(新版)

这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

最近我才对这些路线做了一下新的更新,知识体系更全面了。

在这里插入图片描述

(2)Python学习视频

包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

在这里插入图片描述

(3)100多个练手项目

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

在这里插入图片描述

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

“”“The Gaussian Naive Bayes classifier. “””

def fit(self, X, y):

self.X, self.y = X, y

self.classes = np.unique(y)

self.parameters = []

Calculate the mean and variance of each feature for each class

for i, c in enumerate(self.classes):

Only select the rows where the label equals the given class

X_where_c = X[np.where(y == c)]

self.parameters.append([])

Add the mean and variance for each feature (column)

for col in X_where_c.T:

parameters = {“mean”: col.mean(), “var”: col.var()}

self.parameters[i].append(parameters)

def _calculate_likelihood(self, mean, var, x):

“”" Gaussian likelihood of the data x given mean and var “”"

eps = 1e-4 # Added in denominator to prevent division by zero

coeff = 1.0 / math.sqrt(2.0 * math.pi * var + eps)

exponent = math.exp(-(math.pow(x - mean, 2) / (2 * var + eps)))

return coeff * exponent

def _calculate_prior(self, c):

“”" Calculate the prior of class c

(samples where class == c / total number of samples)“”"

frequency = np.mean(self.y == c)

return frequency

def _classify(self, sample):

“”" Classification using Bayes Rule P(Y|X) = P(X|Y)*P(Y)/P(X),

or Posterior = Likelihood * Prior / Scaling Factor

P(Y|X) - The posterior is the probability that sample x is of class y given the

feature values of x being distributed according to distribution of y and the prior.

P(X|Y) - Likelihood of data X given class distribution Y.

Gaussian distribution (given by _calculate_likelihood)

P(Y) - Prior (given by _calculate_prior)

P(X) - Scales the posterior to make it a proper probability distribution.

This term is ignored in this implementation since it doesn’t affect

which class distribution the sample is most likely to belong to.

Classifies the sample as the class that results in the largest P(Y|X) (posterior)

“”"

posteriors = []

Go through list of classes

for i, c in enumerate(self.classes):

Initialize posterior as prior

posterior = self._calculate_prior©

Naive assumption (independence):

最后

不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~

给大家准备的学习资料包括但不限于:

Python 环境、pycharm编辑器/永久激活/翻译插件

python 零基础视频教程

Python 界面开发实战教程

Python 爬虫实战教程

Python 数据分析实战教程

python 游戏开发实战教程

Python 电子书100本

Python 学习路线规划

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值