我仿照sk-learn 中 GaussionNB 的结构, 重写了该算法的轮子,命名为 MyGaussionNB, 如下:

# !/usr/bin/python
# -*- coding:utf-8 -*- """
Reimplement Gaussion naive Bayes algorithm as a practice
""" # Author: 相忠良(Zhong-Liang Xiang) <ugoood@163.com>
# Finished at June 3, 2017 import numpy as np
from sklearn import datasets, cross_validation
import math
import matplotlib.pyplot as plt
from sklearn import naive_bayes def load_data():
iris = datasets.load_iris()
return cross_validation.train_test_split(iris.data, iris.target, test_size=0.50, random_state=0) class MyGaussianNB:
"""
注意: 使用该分类器前, 必须把标签处理成 0,1,2,3.... 这样的形式
"""
class_prior_dic = {}
class_prior_arr = [] class_count_dic = {}
class_count_arr = [] theta_ = []
sigma_ = [] predict_label = [] # 最终预测值 def __init__(self):
pass def fit(self, X, y):
"""
Fit Gaussian naive Bayes according to X, y Parameters
----------
X : array-like, shape(n-samples, m-features)
X part of training data
y : array-like, shape(n-samples,)
labels of training data Returns:
--------
self : object
Return self.
""" # calculate class_prior and class_count-------------
dic = {} for item in y:
if item in dic.keys():
dic[item] += 1
else:
dic[item] = 1 dic_temp = dic.copy()
self.class_count_dic = dic_temp
self.class_count_arr = dic_temp.values() for item in dic:
dic[item] = float(dic[item]) / y.shape[0] self.class_prior_dic = dic
self.class_prior_arr = dic.values()
# --------------------------------------------------
# 调用本类 私有方法
self.__cal_theta_sigma_arr(X_train, y_train) def predict(self, X):
"""
Predict class labels of X Parameters
----------
X : array-like, shape(n-samples, m-features)
X a set of test data Returns:
--------
a list of class labels of X
""" post_arr_matrix = [] for c in self.class_prior_dic.keys():
post_per_sample = []
for sample in X:
i = 0
temp = 0.0
for element in sample:
# 注意: 使用该分类器前, 必须把标签处理成 0,1,2,3.... 这样的形式
# 原因在 下面的 theta_[c][] 处
# 重要: 用 np.log(x)相加的形式, 因为有的概率值特别特别小, 导致后验概率为0
# log sum 越大, post 概率 越大!
# 注意: 我们并未采用 -np.log(x)的形式
temp = temp + np.log(self.__Gaussion_function(element, self.theta_[c][i], self.sigma_[c][i])) i += 1
# print '在某类, 一个样例结束'
temp = temp + np.log(self.class_prior_dic[c]) # temp - log(p(c))
post_per_sample.append(temp) # 某类下, X 中所有 sample 的 post 概率, shape(n-samples,)
# print '某类, 所有样本概率', post_per_sample
post_arr_matrix.append(post_per_sample) # 各类下, X 中所有 sample 的 post 概率, shape(n-classes, n-samples)
self.predict_label = np.argmax(post_arr_matrix, 0) # 返回 matrix 每列最大值索引. 这里, 索引值恰好是每个 sample 的预测 label. return self.predict_label def score(self, X, y):
# 返回正确率
temp_1 = list(X)
temp = list(temp_1 == y) return 1.0 * temp.count(True) / temp.__len__() # 私有方法: 计算 每类 各列的 均值theta 和 标准差sigma
def __cal_theta_sigma_arr(self, X, y):
theta_arr = []
sigma_arr = [] xxx = [] # including (X,y)
for item in X:
xxx.append(list(item)) ii = 0
for item in xxx:
item.append(y[ii])
ii += 1 # 担心改了原数据
sss = np.array(xxx).copy()
ssss = np.array(xxx).copy() for k in self.class_count_dic.keys():
row_mask = np.array(sss[:, -1] == k, dtype = bool) # 行网子
temp = sss[row_mask, :] # 用 行 网 子 !
theta_arr.append(np.mean(temp, axis = 0)) # axis=0 表示列 row_mask_1 = np.array(ssss[:, -1] == k, dtype = bool) # 行网子
temp_1 = ssss[row_mask_1, :] # 用 行 网 子 !
sigma_arr.append(np.std(temp_1, axis = 0)) self.theta_ = theta_arr
self.sigma_ = sigma_arr
return theta_arr, sigma_arr # Gaussian function
def __Gaussion_function(self, x, theta, sigma): # private method
return np.exp(-(x - theta) ** 2 / (2 * sigma ** 2)) / (np.sqrt(2 * np.pi) * sigma) X_train, X_test, y_train, y_test = load_data()
MGN = MyGaussianNB()
MGN.fit(X_train, y_train) a = MGN.predict(X_test)
b = np.array(a)
# print b
# print X_test print '预测值: ', a
print '实际值: ', y_test
print a == y_test
print 'MyGaussionNB 预测正确率: ', MGN.score(MGN.predict_label, y_test) # sk-learn 中的 GaussionNB 的性能, 且和我的实现 比较一下, 验证我的 implementation 的正确性.
cls = naive_bayes.GaussianNB()
cls.fit(X_train, y_train)
result = cls.predict(X_test)
print 'sklearn 的 GaussionNB 预测正确率: ', MGN.score(result, y_test)
# 结果几乎完全一致, 但在 test_size=0.95 及 训练集更小时, 我的程序会出现问题 ! '''
下面是编程过程中留下的经验
''' # 重要1: 判断column value真假,用mask,取想要rows的方法
# row_mask = np.array(a[:, -1] == 0, dtype=bool)
# print a[row_mask, :]
# print np.mean(a[mask, :], axis=0) # 重要2: 提取字典的keys集合和values集合
# print MGN.class_count_dic.keys()
# print MGN.class_count_dic.values() # 重要3: 用 np.log(x)相加的形式, 因为有的概率值特别特别小, 导致后验概率为0
# log sum 越大, post 概率 越大!
# 注意: 我们并未采用 -np.log(x)的形式 # 重要4: Numpy中找出array中最大值所对应的行和列
# a = np.array([[.5, 2, 0],
# [5, 3, 6],
# [.5, 1, 0]])
#
# re = np.where(a == np.max(a[:,1])) a中第一列最大元素 在a中的坐标
# print re # 重要5: 找出 列 or 行 的最大值索引 np.argmax(a,0), a 是矩阵, 0:列, 1:行 # 重要6: 必须要有用于测试的小数据, 来探测每一个func的正确性.
# 下面是我用来测试的小矩阵. 不仅仅测试自己编写的函数,
# 还得对numpy, python 中的函数探测其功能和使用方法.
# a = np.array([[.5, 2, 0],
# [.25, 3, 6],
# [.51, 1, 0]])
#
# b = np.array([11, 22, 33])
# aa = np.array(zip(a, b))
#
# cc = [True, False, True, False, True, True]
# print cc.__len__()
# print cc.count(True) ################################################
# 以下内容是我编程过程中用于测试和探查的各种乱七八糟的代码
#
# 我抛弃了这种做法----->: Gaussion这种东西, 算出的值 极有可能非常小, 得用 -log 相加 处理.
# log sum 越小, post 概率 越大!
# 取而代之的是------->: 直接 log后 相加, 取和的最大值的 为 那个样例 应得的标签. # def Gaussion_function(x, u, sig):
# return np.exp(-(x - u) ** 2 / (2 * sig ** 2)) / (math.sqrt(2 * math.pi) * sig) # x1 = Gaussion_function(6., 5.006, 0.34894699)
# x2 = Gaussion_function(2.2, 3.418, 0.37719491)
# x3 = Gaussion_function(4., 1.464, 0.17176728)
# x4 = Gaussion_function(1., 0.244, 0.10613199) # x1 = Gaussion_function(5., 4.99574468, 0.35247299)
# x2 = Gaussion_function(2.2, 3.418, 0.37719491)
# x3 = Gaussion_function(4., 1.464, 0.17176728)
# x4 = Gaussion_function(1., 0.244, 0.10613199) # print 'x1 ', x1
# print 'x2 ', x2
# print 'x3 ', x3
# print 'x4 ', x4
#
# x1 = -np.log(x1)
# x2 = -np.log(x2)
# x3 = -np.log(x3)
# x4 = -np.log(x4)
#
# pc = -np.log(0.33098591549295775) # print 'x1 ', x1
# print 'x2 ', x2
# print 'x3 ', x3
# print 'x4 ', x4
# print 'pc ', pc
# print "x1-x4 log sum:", x1 + x2 + x3 + x4 + pc
#
# print MGN.class_prior_dic
# print MGN.theta_
# [array([ 5.006, 3.418, 1.464, 0.244, 0. ]),
# array([ 5.93469388, 2.78163265, 4.26530612, 1.33265306, 1. ]),
# array([ 6.60408163, 2.97755102, 5.56122449, 2.01836735, 2. ])]
# # print MGN.sigma_
# [array([ 0.34894699, 0.37719491, 0.17176728, 0.10613199, 0. ]),
# array([ 0.51608851, 0.30282579, 0.4684107 , 0.19207541, 0. ]),
# array([ 0.62562921, 0.32151764, 0.54802664, 0.26929497, 0. ])] # print 'x=0, 均值为0, 方差为1', Gaussion_function(0, 0, 1) # x1 = Gaussion_function(5.8, 5.006, 0.34894699)
# x2 = Gaussion_function(2.8, 3.418, 0.37719491)
# x3 = Gaussion_function(5.1, 1.464, 0.17176728)
# x4 = Gaussion_function(2.4, 0.244, 0.10613199)
# print 'x1 ', x1
# print 'x2 ', x2
# print 'x3 ', x3
# print 'x4 ', x4
# print "x1-x4乘积:", x1 * x2 * x3 * x4 * 1.0 # x1-x4乘积: 2.53455055621e-188 # print X_test
# print MGN.class_prior_dic.keys() # print "标准差", MGN.sigma_
# print "均值", MGN.theta_ # print MGN.class_count_dic
# print MGN.class_prior_dic
# print 'theta: ', MGN.theta_[0][0]
# print 'sigma: ', MGN.sigma_
# print a.__len__()
# print len(X_test)*4
# print MGN.theta_
# print X_train
# print np.mean(X_train, axis=0) # print MGN.class_count_dic.keys() # print 'x_train: ', X_train
# print 'y_train: ', y_train
#
# print 'MGN.class_prior_arr: ', MGN.class_prior_arr
# print 'MGN.class_prior_dic: ', MGN.class_prior_dic
# print 'MGN.class_count_arr: ', MGN.class_count_arr
# print 'MGN.class_count_dic: ', MGN.class_count_dic # print np.argmax(a, 0)
#
# re = np.where(a == np.max(a[:, 0]))
# print re
# print int(re[0]) # print a[[True,False,True],:]

重写轮子之 GaussionNB的更多相关文章

  1. 重写轮子之 ID3

    这是半成品, 已完成了 fit() 部分, 形成了包含一棵完整树的 node 对象. 后续工作是需解析该 node对象, 完成 predict() 工作. # !/usr/bin/python # - ...

  2. 重写轮子之 kNN

    # !/usr/bin/python # -*- coding:utf-8 -*- """ Re-implement kNN algorithm as a practic ...

  3. 【转】C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1)

    C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1) #region 截获消息        /// 截获消息  处理XP不能关机问题        protected ...

  4. Asp.net Mvc 请求是如何到达 MvcHandler的——UrlRoutingModule、MvcRouteHandler分析,并造个轮子

    这个是转载自:http://www.cnblogs.com/keyindex/archive/2012/08/11/2634005.html(那个比较容易忘记,希望博主不要生气的) 前言 本文假定读者 ...

  5. 拆解轮子之XRecyclerView

    简介 这个轮子是对RecyclerView的封装,主要完成了下拉刷新.上拉加载更多.RecyclerView头部.在我的Material Design学习项目中使用到了项目地址,感觉还不错.趁着毕业答 ...

  6. 跨平台技术实践案例: 用 reactxp 重写墨刀的移动端

    Authors:  Gao Cong, Perry Poon Illustrators:  Shena Bian April 20, 2019 重新编写,又一次,我们又一次重新编写了移动端应用和移动端 ...

  7. 星级评分原理 N次重写的分析

    使用的是雪碧图,用的软件是CSS Sprite Tools 第一次实现与分析: <!DOCTYPE html> <html> <head> <meta cha ...

  8. [18/11/29] 继承(extends)和方法的重写(override,不是重载)

    一.何为继承?(对原有类的扩充) 继承让我们更加容易实现类的扩展. 比如,我们定义了人类,再定义Boy类就只需要扩展人类即可.实现了代码的重用,不用再重新发明轮子(don’t  reinvent  w ...

  9. C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值

    接收拦截+发送消息 对于处理所有消息.net 提供了wndproc进行重写 WndProc(ref Message m)protected override void WndProc(ref Mess ...

随机推荐

  1. 配置ssh无密钥登陆

    ssh 无密码登录要使用公钥与私钥. linux下可以用用ssh-keygen生成公钥/私钥对,下面以CentOS为例. 有机器LxfN1(192.168.136.128),LxfN2(192.168 ...

  2. 详解Ajax请求(一)前言——同步请求的原理

    我们知道,ajax是一种异步请求的方式,想要了解异步请求,就必须要先从同步请求说起.常见的同步请求的方式是form表单的提交,我们先从一种同步请求的示例说起. 我们希望输入姓名可以从后台得到身份证号. ...

  3. 找出一个文件夹下后缀名为.jpg的文件

    import os list1=os.lisdir('E//') #方法一列表推导式 list2=[i for i in list1 if i.endswith('.jpg')] #方法二for循环 ...

  4. 【转载】Ubuntu 12.04 LTS 中文输入法的安装

    原文地址 :  http://www.cnblogs.com/zhj5chengfeng/archive/2013/06/23/3150620.html 我装的是英文版的 Ubuntu12.04,如果 ...

  5. Spring-cloud (一):Eureka注册中心搭建

    前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...

  6. 使用Git简单笔记

    这里只是作为简单的笔记整理,第一次使用的推荐先看一下廖大的教程,内容很多很细,可以边看边练.看不懂的地方先记着.争取七七八八看下来. ================================= ...

  7. [LeetCode] Minimum Time Difference 最短时间差

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  8. webstorm git团队开发技巧总结(一)

    ---恢复内容开始--- 1.git查看和修改用户名,邮箱 用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变.每次commit都会用用户名和邮箱记录. (1)查看用户名和地址 git ...

  9. 原生nodejs在线聊天系统

    前端自动化由来已久,最近为了编写自己的自动化工具,本人开始详细学习node,为了检验学习成果,决定编写一个类似于webqq的聊天系统.以下是该系统具有的模块. 登录模块(自动登录) 聊天模块(私聊,群 ...

  10. Spring Cloud Eureka 自我保护机制

    Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果 ...