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()
相关文章推荐
- 机器学习:逻辑回归与Python代码实现
- Logistic Regression 逻辑回归算法例子,python代码实现
- 逻辑回归-梯度下降法 python实现
- 【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
- 机器学习:逻辑回归python实现
- 逻辑回归(logistic regression)及其Python实现
- 逻辑回归python实现实例
- 21-城里人套路深之用python实现逻辑回归算法
- 分类算法系列2----逻辑回归Logistic原理和Python实现
- Logistic Regression 逻辑回归算法例子,python代码实现
- [深度学习]Python/Theano实现逻辑回归网络的代码分析
- 逻辑回归及其python实现
- 逻辑回归-线性判定边界Python代码实现
- 【Python学习系列二十四】scikit-learn库逻辑回归实现唯品会用户购买行为预测
- python实现逻辑回归识别猫的图片
- [机器学习]逻辑回归公式推导及其梯度下降法的Python实现
- 逻辑回归详解及Python实现
- 逻辑回归原理(python代码实现)
- 二,机器学习算法之逻辑回归(python实现)
- python实现带l1正则化的逻辑回归,采用sgd。