机器学习:逻辑回归与Python代码实现
来源:互联网 发布:中世纪2 优化九 波兰 编辑:程序博客网 时间:2024/05/22 01:49
前言:本篇博文主要介绍逻辑回归(logistic regression),首先介绍相关的基础概念和原理,然后通过Python代码实现逻辑回归的二分类问题。特别强调,其中大多理论知识来源于《统计学习方法_李航》和斯坦福课程翻译笔记以及Coursera机器学习课程。
本篇博文的理论知识都来自于吴大大的Coursera机器学习课程,人家讲的深入浅出,我就不一一赘述,只是简单概括一下以及记一下自己的见解。
1.逻辑回归假设函数
逻辑回归一般用于分类问题较多,但是叫做“regression”,而线性回归一般不建议用于分类,因为输出的y的值可能超出0/1范围。这也就是为什么逻辑回归假设函数里面有sigmoid函数的原因了。
2.成本函数
逻辑回归问题不在采用“最小均方”误差,因为里面含有非线性的sigmiod函数,使得成本函数J不再是一个平滑的“碗”,容易导致“局部最优”,所以采用如下cost function:
3.参数学习(梯度下降)
你会发现,这个跟线性回归模型的梯度下降表达上一模一样,但是,你要知道,其中的h(x)是不一样的!
4.Python代码实现
先看一下数据集,这是一种花的数据集,只取X中的两个特征(x1,x2),对于y中的两个类别(0,1)
from sklearn.datasets import load_irisimport matplotlib.pyplot as pltimport numpy as npiris = load_iris()data = iris.datatarget = iris.target#print data[:10]#print target[10:]X = data[0:100,[0,2]]y = target[0:100]print X[:5]print y[-5:]label = np.array(y)index_0 = np.where(label==0)plt.scatter(X[index_0,0],X[index_0,1],marker='x',color = 'b',label = '0',s = 15)index_1 =np.where(label==1)plt.scatter(X[index_1,0],X[index_1,1],marker='o',color = 'r',label = '1',s = 15)plt.xlabel('X1')plt.ylabel('X2')plt.legend(loc = 'upper left')plt.show()
编写逻辑回归模型的类
import numpy as npclass logistic(object): def __init__(self): self.W = None def train(self,X,y,learn_rate = 0.01,num_iters = 5000): num_train,num_feature = X.shape #init the weight self.W = 0.001*np.random.randn(num_feature,1).reshape((-1,1)) loss = [] for i in range(num_iters): error,dW = self.compute_loss(X,y) self.W += -learn_rate*dW loss.append(error) if i%200==0: print 'i=%d,error=%f' %(i,error) return loss def compute_loss(self,X,y): num_train = X.shape[0] h = self.output(X) loss = -np.sum((y*np.log(h) + (1-y)*np.log((1-h)))) loss = loss / num_train dW = X.T.dot((h-y)) / num_train return loss,dW def output(self,X): g = np.dot(X,self.W) return self.sigmod(g) def sigmod(self,X): return 1/(1+np.exp(-X)) def predict(self,X_test): h = self.output(X_test) y_pred = np.where(h>=0.5,1,0) return y_pred训练测试一下,并且可视化跟踪的损失loss
import matplotlib.pyplot as plty = y.reshape((-1,1))#add the x0=1one = np.ones((X.shape[0],1))X_train = np.hstack((one,X))classify = logistic()loss = classify.train(X_train,y)print classify.Wplt.plot(loss)plt.xlabel('Iteration number')plt.ylabel('Loss value')plt.show()
最后可视化“决策边界”
label = np.array(y)index_0 = np.where(label==0)plt.scatter(X[index_0,0],X[index_0,1],marker='x',color = 'b',label = '0',s = 15)index_1 =np.where(label==1)plt.scatter(X[index_1,0],X[index_1,1],marker='o',color = 'r',label = '1',s = 15)#show the decision boundaryx1 = np.arange(4,7.5,0.5)x2 = (- classify.W[0] - classify.W[1]*x1) / classify.W[2]plt.plot(x1,x2,color = 'black')plt.xlabel('X1')plt.ylabel('X2')plt.legend(loc = 'upper left')plt.show()
ps:可以看出,最后学习得到的决策边界(分类边界)成功的隔开了两个类别。当然,分类问题还有多分类问题(一对多),还有就是对于非线性分类问题,详情请参见分享的资料。
阅读全文
0 0
- 机器学习:逻辑回归与Python代码实现
- 机器学习 Python实现逻辑回归
- 机器学习:逻辑回归python实现
- 机器学习之逻辑回归python实现
- 机器学习:线性回归与Python代码实现
- 机器学习/逻辑回归(logistic regression)/--附python代码
- 机器学习逻辑回归:算法兑现为python代码
- 【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
- 【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
- 【机器学习算法-python实现】逻辑回归的实现(LogicalRegression)
- 机器学习:逻辑回归原理及实现代码
- 机器学习逻辑回归:原理解析及代码实现
- 二,机器学习算法之逻辑回归(python实现)
- 机器学习——Python实现逻辑回归
- 用Python实现机器学习算法:逻辑回归
- 机器学习|回归问题与python实现
- 机器学习之逻辑回归代码
- Python机器学习实战之逻辑回归
- Android源码解析之应用程序框架层和系统运行库层日志系统
- 工作笔记之遇到的问题总结
- VR开发
- 个人整理的Linux命令
- Opencv入门(播放AVI视频)
- 机器学习:逻辑回归与Python代码实现
- Maven 项目中无法修改Dynamic Web Module无法修改的解决办法
- PHP Redis 缓存实例
- Insert Delete GetRandom O(1)问题及解法
- c#调用winapi为外部程序控件赋值
- Html页面内容的执行顺序
- 在执行目录下找文件
- logback系统异常日志写入文件
- 【美团外卖】广告算法 高级研发工程师/技术专家