机器学习(周志华) 个人练习答案7.6
来源:互联网 发布:淘宝加入购物车代码 编辑:程序博客网 时间:2024/05/08 05:50
7.6 试编程实现AODE分类器,并以西瓜数据集3.0为训练集,对p151的‘测1’样本进行判别。
通过下列代码实现,结果为5.144433573365839和0.5121125686430099,显然应该划分为“好瓜”。
# -*- coding: utf-8 -*-# exercise 7.6: AODE method to estimate likelihood based on first 6 DISCRETE attributes with Laplacian correction# it avoids the last 2 attributes in continuous range,that would be more compleximport numpy as npD = np.array([ [1, 1, 1, 1, 1, 1, 1], [2, 1, 2, 1, 1, 1, 1], [2, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 1, 1, 1], [3, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 2, 2, 1], [2, 2, 1, 2, 2, 2, 1], [2, 2, 1, 1, 2, 1, 1], [2, 2, 2, 2, 2, 1, 0], [1, 3, 3, 1, 3, 2, 0], [3, 3, 3, 3, 3, 1, 0], [3, 1, 1, 3, 3, 2, 0], [1, 2, 1, 2, 1, 1, 0], [3, 2, 2, 2, 1, 1, 0], [2, 2, 1, 1, 2, 2, 0], [3, 1, 1, 3, 3, 1, 0], [1, 1, 2, 2, 2, 1, 0]])test = [1,1,1,1,1,1] # the predict samplem, n = D.shape[0], D.shape[1]-1 # number of instances,attributesN_i = [len(np.unique(D[:,i])) for i in range(len(D[0,:-1]))]D_cx = [{},{}] # preserve p(c|xi)for j in range(len(N_i)): D_cx[0][j], D_cx[1][j] = [0]*N_i[j], [0]*N_i[j] for i in range(m): if D[i,-1] == 1: if D[i,j] == 1: D_cx[0][j][0] += 1 elif D[i,j] == 2: D_cx[0][j][1] += 1 else: D_cx[0][j][2] += 1 else: if D[i,j] == 1: D_cx[1][j][0] += 1 elif D[i,j] == 2: D_cx[1][j][1] += 1 else: D_cx[1][j][2] += 1p0, p1 = 0, 0 # likelihood of class positive and negativefor i in range(n): # set every attribute i to be a Super-Parent for key in range(N_i[i]): # loop for every value in attribute i pcx0, pcx1 = 0, 0 # initiate p(c,xi) for class positive and negative pcxx0, pcxx1 = 1, 1 # initiate p(xj|c,xi) for class positive and negative pcx0 += (D_cx[0][i][key] + 1) / (17 + N_i[i]) # each value's p(c,xi) in attribute i of class positive pcx1 += (D_cx[1][i][key] + 1) / (17 + N_i[i]) # each value's p(c,xi) in attribute i of class negative count0,count1 = 0, 0 # initiate D(xj,c,xi) for class positive and negative for j in range(n): # multi-product for p(xj|c,xi) # #### # we need to ensure j to differ from i because i is their 'parent' # #### if j != i: for k in range(8): # D(xj,c,xi) for positive class if D[k,i] == key+1 and test[j]==D[k,j]: count0 += 1 for k in range(8,17): # D(xj,c,xi) for negative class if D[k,i] == key+1 and test[j]==D[k,j]: count1 += 1 # actually multi-product here pcxx0 *= (count0 + 1)/(D_cx[0][i][key] + N_i[j]) pcxx1 *= (count1 + 1) / (D_cx[1][i][key] + N_i[j]) p0 += pcx0 * pcxx0 p1 += pcx1 * pcxx1print(p0, p1)
0 0
- 机器学习(周志华) 个人练习答案7.6
- 机器学习-周志华-个人练习4.3
- 机器学习-周志华-个人练习4.4
- 机器学习-周志华-个人练习9.4
- 机器学习-周志华-个人练习9.6
- 机器学习-周志华-个人练习10.1
- 机器学习-周志华-个人练习10.6
- 机器学习-周志华-个人练习11.1
- 机器学习-周志华-个人练习11.3
- 机器学习-周志华-个人练习12.4
- 机器学习-周志华-个人练习13.1
- 机器学习-周志华-个人练习13.2
- 机器学习-周志华-个人练习13.3
- 机器学习-周志华-个人练习13.4
- 机器学习-周志华-个人练习13.10
- 机器学习-周志华-个人练习8.3和8.5
- 机器学习基石_课后练习答案链接
- 《机器学习(周志华)》 习题4.3答案
- Android studio gradle配置
- CodeForces 789C Functions again
- POJ 2513-Colored Sticks(连接木棍-trie树+并查集+欧拉通路)
- BZOJ 4805 欧拉函数求和 数论
- 字串统计
- 机器学习(周志华) 个人练习答案7.6
- Python 存储数据
- python形式参数
- CSDN-markdown编辑器使用说明
- 链表问题的做题总结
- [最短路]最短路径问题
- 报纸问题
- 蓝桥杯 表格计算
- VS2013+MFC+OPENCV人脸识别考勤软件