python实现logistic函数实现_Logistic回归 Python实现

Logistic回归 Logistic函数 f(x)=11+e−x 其函数图像为: 绘图方法 >>> import numpy as np>>> x = np.arange(-10,10,0.1)>>> y = 1/(1+np.exp(-x))>>> import matplotlib.pyplot as plt>>> plt.plot(x,y)[]>>> plt.show() 其在近0点陡峭的上升特点决定了它可以将一个回归问题转换为一个分类问题。 详情请见我的另外一篇博客 http://blog.csdn.net/taiji1985/article/details/50969697 在ml/dt.py中的数据生成 def createD3():

np.random.seed(101)

ndim = 2

n2

=10

a = 3+5*np.random.randn(n2,ndim)

b = 18+4*np.random.randn(n2,ndim)

X = np.concatenate((a,b))

ay = np.zeros(n2)

by = np.ones(n2)

ylabel = np.concatenate((ay,by))

return {'X':X,'ylabel':ylabel} ml/fig.py中的绘图 # -*- coding: UTF-8 -*-''' Created on 2016-4-24 @author: Administrator '''import numpy as npimport operatorimport matplotlib.pyplot as plt#绘图def plotData(ds,type='o'):

X= ds['X']

y=ds['ylabel']

n = X.shape[0]

cn = len(np.unique(y))

cs = ['r','g']

dd

= np.arange(n)

for i in range(2):

index= y == i

xx=X[dd[index]]

plt.plot(xx[:,0],xx[:,1],type,markerfacecolor=cs[i],markersize=14)

xmax = np.max(X[:,0])

xmin = np.min(X[:,0])

ymax = np.max(X[:,1])

ymin = np.min(X[:,1])

print xmin,xmax,ymin,ymax

dx = xmax - xmin

dy = ymax - ymin

plt.axis([xmin-dx*0.1, xmax + dx*0.1, ymin-dy*0.1, ymax +dy*0.1]) 分类器代码 # -*- coding: UTF-8 -*-''' Logistic 回归 Created on 2016-4-26 @author: taiji1985 '''import numpy as npimport operatorimport matplotlib.pyplot as pltdef sigmoid(X):

return 1.0/(1+np.exp(-X))

pass#生成该矩阵的增广矩阵,添加最后一列(全部为1)def augment(X):

n,ndim = X.shape

a = np.mat(np.ones((n,1)))

return np.concatenate((X,a),axis=1)def classify(X,w):

X = np.mat(X)

w = np.mat(w)

if w.shape[0] < w.shape[1]:

w = w.T

#增广

X= augment(X)

d = X*w

r = np.zeros((X.shape[0],1))

r[d>0] = 1

return r

pass#梯度下降法学习#alpha 学习因子def learn(dataset,alpha=0.001):

X= dataset['X']

ylabel = dataset['ylabel']

n,ndim = X.shape

cls_label = np.unique(ylabel)

cn=len(cls_label)

X = np.mat(X)

ylabel = np.mat(ylabel).T

#生成增广矩阵

X = augment(X)

ndim += 1

max_c = 500

w = np.ones((ndim,1))

i = 0

ep= 0.0001

cha = 1

while cha > ep:

#计算y = wx + b

ypred =X*w

#计算 logistic

ypred = sigmoid(ypred)

#计算误差

error = ylabel - ypred

cha = alpha*X.T*error

w = w + cha

cha = np.abs( np.sum(cha))

print i,w.T

i=i+1

return w 测试 ''' Created on 2016-4-26 @author: Administrator '''from ml import lsdfrom ml import dtfrom ml import figimport numpy as npimport matplotlib.pyplot as pltds = dt.createD3()w = lsd.learn(ds,0.01)#draw linelx = [0, -w[2]/w[0]]ly = [-w[2]/w[1],0]plt.figure()fig.plotData(ds, 'o')plt.plot(lx,ly)plt.show()ypred = lsd.classify(ds['X'],w)ylabel = np.mat(ds['ylabel']).Tprint ypredprint ylabeldist = np.sum(ypred != ylabel)print dist,len(ylabel), 1.0*dist/len(ylabel)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值