-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBGA.py
127 lines (114 loc) · 3.97 KB
/
BGA.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
122
123
124
125
126
127
#!/usr/bin/python
#-*- coding: utf-8 -*-
from numpy import *
import matplotlib.pyplot as plt
import matplotlib.animation as ai
import numpy as np
import time
def loadData(): #文件读取函数
data=open('ex2data1.txt').readlines();
l=len(data) #mat为l*3的矩阵,元素都为0
mat=zeros((l,3))
index=0
xdata = ones((l,3)) #xdata为l*3的矩阵,元素都为1
for line in data:
line = line.strip().split(',') #去除多余字符
mat[index:,] = line[0:3] #得到一行数据
index +=1
xdata[:,1] = mat[:,0] #得到第一列数据
xdata[:,2] = mat[:,1] #得到第二列数据
return xdata,mat[:,2]
def sigmoid(z):
return 1.0/(1+exp(-z));
def cost(weight,x,y):
weight = np.matrix(weight)
x = np.matrix(x)
y = np.matrix(y)
first = np.multiply(-y,np.log(sigmoid(x*weight)))
second = np.multiply((1-y),np.log(1-sigmoid(x*weight)))
return np.sum((first-second)/len(x))
def prdict(weight,x,y):
l =len(x)
c = 0
for i in range(l):
u = x[i][0]*weight[0][0]+x[i][1]*weight[1][0]+x[i][2]*weight[2][0]
u = float(sigmoid(u))
if (y[i]==1 and u >0.5 ) or (y[i]==0 and u <0.5 ):
c+=1
i +=1
return float(c)/float(l)
def gradAscent(xArr,yArr):
xArr = mat(xArr)
yArr = mat(yArr).transpose()
alpha = 0.004
maxCycles = 600000
m,n = shape(xArr)
weights = zeros((n,1))
weights_history1 = []
weights_history2 = []
weights_history3 = []
cost_h = []
iters = []
print "初始代价为:"
print cost(weights,xArr,yArr)
for k in range(maxCycles):
h = sigmoid(xArr*weights)
error = (yArr - h)
cost_h.append(float(cost(weights,xArr,yArr)))
weights_history1.append(float(weights[0][0]))
weights_history2.append(float(weights[1][0]))
weights_history3.append(float(weights[2][0]))
weights +=alpha * xArr.transpose()*error/100
iters.append(k)
k+=1
print "最终代价为: "
print cost(weights,xArr,yArr)
return weights,weights_history1,weights_history2,weights_history3,iters,cost_h
def show(xArr,yArr,weights):
n = len(yArr)
dataArr = array(xArr)
xcord1 = [];ycord1 = []
xcord2 = [];ycord2 = []
x = arange(25,100,1)
y = (-weights[0]-weights[1]*x)/weights[2]
for i in range(n):
if int(yArr[i])==1:
xcord1.append(dataArr[i,1]);ycord1.append(dataArr[i,2])
else:
xcord2.append(dataArr[i,1]);ycord2.append(dataArr[i,2])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord1,ycord1,s=30,c='r',marker='o',label='Admitted')
ax.scatter(xcord2,ycord2,s=30,c='g',marker='x',label='Not Admitted')
ax.plot(x,y)
plt.xlabel('Score1',size=25) # 横坐标
plt.ylabel('Score2',size=25) # 纵坐标
plt.title('Logistic',size=30) # 标题
plt.show()
def show1(weight1,weight2,weight3,iters,cost_h):
plt.figure(1)
plt.subplot(221)
plt.xlabel('isers',size=10)
plt.ylabel('weight1',size=10)
plt.plot(iters,weight1,'bo')
plt.subplot(222)
plt.xlabel('isers',size=10)
plt.ylabel('weight2',size=10)
plt.plot(iters,weight2,'bo')
plt.subplot(223)
plt.xlabel('isers',size=10)
plt.ylabel('weight3',size=10)
plt.plot(iters,weight3,'go')
plt.subplot(224)
plt.xlabel('isers',size=10)
plt.ylabel('cost',size=10)
plt.plot(iters,cost_h,'go')
plt.show()
xArr,yArr= loadData()
weight,weight1,weight2,weight3,iters,cost_h= gradAscent(xArr,yArr)
print "求得的参数为:"
print weight
print "某考生两次的考试成绩分别为45,85时,录取的概率为:"
print prdict(weight,xArr,yArr)
show(xArr,yArr,weight)
show1(weight1,weight2,weight3,iters,cost_h)