-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisher.py
121 lines (104 loc) · 3.53 KB
/
fisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from readdata import *
from pylab import *
from Bayes import *
from sklearn.model_selection import LeavePOut
def main():
path_boy = "F:\\study in school\\machine learning\\forstudent\\实验数据\\boynew.txt"
path_girl = "F:\\study in school\\machine learning\\forstudent\\实验数据\\girlnew.txt"
# height = []
# weight = []
# feetsize = []
x_boy=[]
x_girl=[]
label_boy = [] # 1表示男,0表示女
label_girl = []
readdata1(path_boy, x_boy, label_boy, 1)
readdata1(path_girl, x_girl, label_girl, 0)
x_boy=np.mat(x_boy)
x_girl=np.mat(x_girl)
m1=x_boy.mean(0)
m0=x_girl.mean(0)
S1=(x_boy-m1[0]).T*(x_boy-m1[0])
S0=(x_girl-m0[0]).T*(x_girl-m0[0])
Sw=S1+S0
S_inverse=Sw.I
W = S_inverse *(m1-m0).T
M1=float(W.T*m1.T)
M0=float(W.T*m0.T)
w_decision0 =(M0+M1)/2
path_boy_test = "F:\\study in school\\machine learning\\forstudent\\实验数据\\boy.txt"
path_girl_test ="F:\\study in school\\machine learning\\forstudent\\实验数据\\girl.txt"
x = []
label =[]
readdata1(path_boy_test, x, label, 1)
readdata1(path_girl_test, x, label, 0)
label_test = []
y = x*W
errorcount = 0
for i in range(len(label)):
if float(y[i]>w_decision0):
label_test.append(1)
if label[i]!=1:
errorcount=errorcount+1
else:
label_test.append(0)
if label[i]!=0:
errorcount=errorcount+1
e_percentage=errorcount/len(label_test)
print('fisher测试集的错误率为%f'% e_percentage)
#留一法
loo = LeavePOut(p=1)
error = 0
for train, test in loo.split(x, label):
x_boy = []
x_girl = []
label_boy = [] # 1表示男,0表示女
label_girl = []
for i in train:
if label[i] == 1:
x_boy.append(x[i])
label_boy.append(1)
else:
x_girl.append(x[i])
label_girl.append(0)
x_boy = np.mat(x_boy)
x_girl = np.mat(x_girl)
m1 = x_boy.mean(0)
m0 = x_girl.mean(0)
S1 = (x_boy - m1[0]).T * (x_boy - m1[0])
S0 = (x_girl - m0[0]).T * (x_girl - m0[0])
Sw = S1 + S0
S_inverse = Sw.I
W = S_inverse * (m1 - m0).T
M1 = float(W.T * m1.T)
M0 = float(W.T * m0.T)
w_decision0 = (M0 + M1) / 2
for j in test:
if float(x[j] * W > w_decision0):
if label[j] != 1:
error = error + 1
else:
label_test.append(0)
if label[j] != 0:
error = error + 1
print('fisher留一法的错误率为%f'%(error / len(label)))
figure(3)
FPR, TPR = get_roc_fisher(W, w_decision0, x, label)
plot(FPR, TPR, label='fisher')
figure(5)
x1=np.arange(130,190,0.01)
y1=(w_decision0-W[0]*x1)/W[1]
plot(x1,array(y1)[0])
plot(x1,x1*float(W[1])/float(W[0]))
for i in range(len(label)):
if label[i]==1:
plot(float(x[i][0]),float(x[i][1]),'o',color='r')
else:
plot(float(x[i][0]), float(x[i][1]), 'o', color='g')
a=(float(x[i][1])+float(x[i][0])*float(W[0])/float(W[1]))/\
(float(W[1])/float(W[0])+float(W[0])/float(W[1]))
b=a*float(W[1])/float(W[0])
plot([float(x[i][0]),a],[float(x[i][1]),b],'--',color='0.75')
axis([140,190,35,85])
Bayes()
main()