您的位置:首页 > 编程语言 > Python开发

Python实现逻辑回归

2017-12-11 22:45 393 查看
import numpy as np
import math
import matplotlib.pyplot as plt

def sigmoid(z):
h=1.0/(1+np.exp(-z))
return h

def costfunction(X,beta,y,b):
m, n = np.shape(X)
h=sigmoid(np.dot(X,beta.T)+b)
J=(np.dot(y.T,np.log(h))+np.dot((1-y).T,np.log(1-h)))/m
return -J

def grad(X,beta,y,b,alpha):
m, n = np.shape(X)
h = sigmoid(np.dot(X,beta.T) + b)
dz=h-y
dbeta=1/m*np.dot(dz.T,X)
db=1/m*np.sum(dz)
beta=beta-alpha*dbeta
b=b-alpha*db
return beta,b

def plotpoint(X):
for i in range(X.shape[0]):
if y[i]==0:
plt.plot(X[i,0],X[i,1],'ro')
else:
plt.plot(X[i,0],X[i,1],'bo')
plt.show()

def train(X,beta,y,b,alpha,number):
for i in range(0, number):
beta, b = grad(X, beta, y, b, alpha)
J = costfunction(X, beta, y, b)
if(i%100==0):
print(i)
#plt.plot(i, J, 'ro')
#plt.show()
return beta,b

mat=np.loadtxt('gua.txt')
X=mat[:,1:3]
y=mat[:,3]
n,m=np.shape(X)
y=y.reshape(n,1)
beta=np.ones([1,2])
b=1
alpha=0.1
Jaim=0
number=20000
beta,b=train(X,beta,y,b,alpha,number)
plotpoint(X)
x = np.linspace(0,0.9,1000)
yy=-beta[0,0]*x/beta[0,1]-b/beta[0,1]
plt.plot(x,yy)
plt.show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: