标签:read 实现 += 自动 app bsp print imp highlight
1、
简述分类与聚类的联系与区别。
简述什么是监督学习与无监督学习。
分类与聚类:分类是一种有监督的算法,是在已经有目标分类的情况下对数据进行类别判断(朴素贝叶斯算法)。而聚类是一种无监督算法,是在建立模型之前还没有目标分类,将特征相似的数据自动聚为一类的算法(KMeans聚类算法)。
有监督学习和无监督学习:有监督学习是在建立模型之前已经给出训练数据集,机器根据训练数据集训练出模型并对新数据进行预测。无监督学习是对未进行人工标注的数据进行分析,机器根据数据间的相似性自行分类。相似度高的数据会被聚为一类。
2、
朴素贝叶斯分类算法 实例
利用关于心脏情患者的临床数据集,建立朴素贝叶斯分类模型。
有六个分类变量(分类因子):性别,年龄、KILLP评分、饮酒、吸烟、住院天数
目标分类变量疾病:–心梗–不稳定性心绞痛
新的实例:–(性别=‘男’,年龄<70, KILLP=‘I‘,饮酒=‘是’,吸烟≈‘是”,住院天数<7)
最可能是哪个疾病?
上传演算过程。
假设患有心梗为事件A,患有心绞痛为事件B。性别为男为x1,年龄<70为x2,KILLP=1为x3,饮酒为x4,吸烟为x5,住院<7为x6.
p(x1|A)=7/16 P(X2|A)=4/16 P(X3|A)=9/16
P(X4|A)=3/16 P(X5|A)=7/16 P(X6|A)=4/16
P(X1,X2,X3,X4,X5,X6|A)=7/16*4/16*9/16*3/16*7/16*4/16=0.001262
p(X1|B)=1/4 P(X2|B)=1/4 P(X3|B)=1/4
P(X4|B)=1/4 P(X5|B)=2/4 P(X6|B)=2/4
P(X1,X2,X3,X4,X5,X6|B)=1/4*1/4*1/4*1/4*2/4*2/4=0.000977
p(A|x)=p(x|A)*P(A)/P(X)=0.001009/P(X)
P(B|X)=P(x|B)*p(B)/P(X)=0.000195/P(X)
因为分母是相同的,只要比较分子的数就行。
所以该患者的症状是患心梗的可能更大
3、编程实现朴素贝叶斯分类算法
import pandas as pd import numpy as np dataDF = pd.read_excel(r‘data/心脏病患者临床数据.xlsx‘) # 数据处理,对男女(男1女0),年龄(<70 -1,70-80 0,>80 1), # 住院天数(<7 -1,7-14 0,>14 1)三个列进行处理 sex = [] for s in dataDF[‘性别‘]: if s == ‘男‘: sex.append(1) else: sex.append(0) age = [] for a in dataDF[‘年龄‘]: if a == ‘<70‘: age.append(-1) elif a == ‘70-80‘: age.append(0) else: age.append(1) days = [] for d in dataDF[‘住院天数‘]: if d == ‘<7‘: days.append(-1) elif d == ‘7-14‘: days.append(0) else: days.append(1) # 另外生成一份处理后的DF dataDF2 = dataDF dataDF2[‘性别‘] = sex dataDF2[‘年龄‘] = age dataDF2[‘住院天数‘] = days # 转为数组用于计算 dataarr = np.array(dataDF) dataarr # 用贝叶斯模型判断病人属于哪种病:性别=‘男’,年龄<70, KILLP=1,饮酒=‘是’,吸烟=‘是”,住院天数<7 def beiyesi(sex, age, KILLP, drink, smoke, days): # 初始化变量 x1_y1,x2_y1,x3_y1,x4_y1,x5_y1,x6_y1 = 0,0,0,0,0,0 x1_y2,x2_y2,x3_y2,x4_y2,x5_y2,x6_y2 = 0,0,0,0,0,0 y1 = 0 y2 = 0 for line in dataarr: if line[6] == ‘心梗‘:# 计算在心梗条件下出现各症状的次数 y1 += 1 if line[0] == sex: x1_y1 += 1 if line[1] == age: x2_y1 += 1 if line[2] == KILLP: x3_y1 += 1 if line[3] == drink: x4_y1 += 1 if line[4] == smoke: x5_y1 += 1 if line[5] == days: x6_y1 += 1 else: # 计算不稳定性心绞痛条件下出现各症状的次数 y2 += 1 if line[0] == sex: x1_y2 += 1 if line[1] == age: x2_y2 += 1 if line[2] == KILLP: x3_y2 += 1 if line[3] == drink: x4_y2 += 1 if line[4] == smoke: x5_y2 += 1 if line[5] == days: x6_y2 += 1 # print(‘y1:‘,y1,‘ y2:‘,y2) # 计算,转为x|y1, x|y2 # print(‘x1_y1:‘,x1_y1, ‘ x2_y1:‘,x2_y1, ‘ x3_y1:‘,x3_y1, ‘ x4_y1:‘,x4_y1, ‘ x5_y1:‘,x5_y1, ‘ x6_y1:‘,x6_y1) # print(‘x1_y2:‘,x1_y2, ‘ x2_y2:‘,x2_y2, ‘ x3_y2:‘,x3_y2, ‘ x4_y2:‘,x4_y2, ‘ x5_y2:‘,x5_y2, ‘ x6_y2:‘,x6_y2) x1_y1, x2_y1, x3_y1, x4_y1, x5_y1, x6_y1 = x1_y1/y1, x2_y1/y1, x3_y1/y1, x4_y1/y1, x5_y1/y1, x6_y1/y1 x1_y2, x2_y2, x3_y2, x4_y2, x5_y2, x6_y2 = x1_y2/y2, x2_y2/y2, x3_y2/y2, x4_y2/y2, x5_y2/y2, x6_y2/y2 x_y1 = x1_y1 * x2_y1 * x3_y1 * x4_y1 * x5_y1 * x6_y1 x_y2 = x1_y2 * x2_y2 * x3_y2 * x4_y2 * x5_y2 * x6_y2 # 计算各症状出现的概率 x1,x2,x3,x4,x5,x6 = 0,0,0,0,0,0 for line in dataarr: if line[0] == sex: x1 += 1 if line[1] == age: x2 += 1 if line[2] == KILLP: x3 += 1 if line[3] == drink: x4 += 1 if line[4] == smoke: x5 += 1 if line[5] == days: x6 += 1 # print(‘x1:‘,x1, ‘ x2:‘,x2, ‘ x3:‘,x3, ‘ x4:‘,x4, ‘ x5:‘,x5, ‘ x6:‘,x6) # 计算 length = len(dataarr) x = x1/length * x2/length * x3/length * x4/length * x5/length * x6/length # print(‘x:‘,x) # 分别计算 给定症状下心梗 和 不稳定性心绞痛 的概率 y1_x = (x_y1)*(y1/length)/x # print(y1_x) y2_x = (x_y2)*(y2/length)/x # 判断是哪中疾病的可能性大 if y1_x > y2_x: print(‘该病人患心梗的可能性较大,可能性为:‘,y1_x) else: print(‘该病人患不稳定性心绞痛的可能性较大,可能性为:‘,y2_x) # 判断:性别=‘男’,年龄<70, KILLP=1,饮酒=‘是’,吸烟=‘是”,住院天数<7 beiyesi(1,-1,1,‘是‘,‘是‘,-1)
标签:read 实现 += 自动 app bsp print imp highlight
原文地址:https://www.cnblogs.com/1158Z/p/10000267.html