ML-Regression
来源:互联网 发布:淘宝产品文案类型 编辑:程序博客网 时间:2024/04/28 00:30
Linearing Regression:
算法:
python实现:
# coding=utf-8from numpy import *import matplotlib.pyplot as pltfilename = 'D:\machinelearninginaction\Ch08\ex0.txt'numLen = len(open(filename).readline().split(('\t'))) - 1dataMat = []labelMat = []fp = open(filename)for line in fp.readlines(): lineAttr = [] print line curLine = line.strip().split('\t') for i in range(numLen): lineAttr.append(float(curLine[i])) dataMat.append(lineAttr) labelMat.append(float(curLine[-1]))print 'X:\n',dataMat,'\n','Y:\n',labelMat#获取到的样本数据和标签数据xMat = mat(dataMat)yMat = mat(labelMat).TxTx = xMat.T*xMatif linalg.det(xTx) == 0: print 'xTx mat is can\'t do inverse' #xTx不可以求逆矩阵else: ws = xTx.I*(xMat.T*yMat) print 'The LinearRegression coef is:\n',ws#现在可以根据获取的coef求出线性回归下的yhatyhat = xMat * wsprint 'new y is:\n ',yhat#绘制线性回归线plt.scatter(xMat[:,1],yMat,color='black',marker='o')plt.scatter(xMat[:,1],yhat,)plt.show()运行后可以得到回归系数ws: [[ 3.00774324] [ 1.69532264]]
也就是说我们得到的回归函数是: y = 3.008X0 + 1.695X1 + e
具体的算法就不多介绍了,网上很多,算法实现图如下:
上图是样本的位置,线性回归之后,可以看到回归线如下:
线性回归的使用与局限:
回归问题一般是对于连续数据的预测,比如预测价格的生长空间等,是很有用的算法。
因为线性回归是寻找到达回归线的个点的距离的平方之和的最小值, 所以理所当然的存在欠拟合和过拟合的问题。如果样本点的噪声过大,比如上图的样本点有几个点离回归线很远,可以认为这类点就是噪声点,这样势必会导致差方之和变大, 这样自然会导致最后的线性回归函数产生误差,导致欠拟合。
Ridge Regression:
算法:
普通线性回归的问题中,最小二乘回归求解的最小化问题是:,这个问题解存在且唯一的条件是 列满秩:, 所以, 如果样本数大于特征数,则普通线性回归就不适用了。原因是xTx不可逆。这时候就需要岭回归出马了。
通过对普通线性回归的基础上加上一个惩罚因子
I 代表的是单位矩阵。通过引入这个惩罚因子能够减少不重要的参数,这种技术价叫做shrinkage(缩减技术)
python实现:
# coding=utf-8from numpy import *import matplotlib.pyplot as pltdef loadDataSet(fileName): #general function to parse tab -delimited floats numFeat = len(open(fileName).readline().split('\t')) - 1 #get number of fields dataMat = []; labelMat = [] fr = open(fileName) for line in fr.readlines(): lineArr =[] curLine = line.strip().split('\t') for i in range(numFeat): lineArr.append(float(curLine[i])) dataMat.append(lineArr) labelMat.append(float(curLine[-1])) return dataMat,labelMatdef ridgeRegress(xMat,yMat,lam = 0.2): xTx = xMat.T*xMat Imat = eye(shape(xMat)[1]) #创建的单位矩阵 punish = lam * Imat punish = xTx + punish if linalg.det(punish) == 0: print 'punish mat is can\'t do inverse' return else: ws = punish.I * (xMat.T * yMat) return ws#归一化处理def normalize(xMat,yMat): ymeans = mean(yMat,0) yMat = yMat - ymeans xmeans = mean(xMat,0) xVar = var(xMat,0) xMat = (xMat - xmeans)/xVar return xMat,yMatabx,aby = loadDataSet('D:\machinelearninginaction\Ch08\\abalone.txt')abx,aby = normalize(mat(abx),mat(aby).T)numtest = 30wMat = zeros((numtest,shape(abx)[1]))for i in range(numtest): print exp(i-10) ws = ridgeRegress(abx,aby,exp(i-10)) wMat[i,:] = ws.Tfig = plt.figure()ax = fig.add_subplot(111)ax.plot(wMat)plt.show()
这里对数据做了归一化处理,处理的目的就是希望对所有的特征都公平对待(当然也可以不用处理,这样通过Ridge可以分析哪些特征可有可无,之后会讨论)。关于归一化的介绍可以看这里。
在这里我对lambda设置了30个值,并且将这30个值得对应洗漱矩阵会话出来如下
X轴代表lambda,测试数据有8个特征,用8种颜色代表可以看出随着lambda的增大,系数最终会优化为0。
Ridge的可以在数据分析的过程中,优化掉不是很重要的参数,当然过度的优化最终导致就是所有的参数的系数都是0,模型也没有意义,所以在中间的某一点达到最优。
从这个图中也能看出哪些 特征值多回归影响比较大。
Ridge通过上述方式补足了对于线性回归欠拟合的处理。
- ML-Regression
- ML:logistic Regression & Regularization
- ML编程作业: Linear Regression
- ML编程作业: Logistic Regression
- ML:aPP:7.线性回归<Linear Regression>
- Stanford ML - Lecture 3 - Logistic regression
- Stanford ML - Logistic Regression 逻辑回归
- ML学习心得(3)---- Logistic Regression & Regularization
- Coursera ML笔记 -----week3 Logistic Regression
- Stanford ML 笔记1-Linear Regression与Logistic Regression
- Stanford ML - Lecture 1 - Linear regression with one variable
- Stanford ML - Lecture 2 - Linear regression with multiple variable
- ML学习心得(2)----Linear Regression 和Regularization
- [ML of Andrew Ng]Week 3 Logistic Regression and Regularization
- [机器学习] Coursera ML笔记 - 逻辑回归(Logistic Regression)
- CNTK入门03:CNTK 101: Logistic Regression and ML Primer
- ML实践——逻辑回归logistic regression
- 我的第一个ML算法(linear regression gradient descent)
- JavaWeb学习总结(二)——Tomcat服务器学习和使用(一)
- OpenCV学习笔记——Mat,图像的新容器
- Linux 中 eval命令的作用
- 分治法-归并排序
- 非平稳时间序列综合分析
- ML-Regression
- Hive的三种Join方式
- app性能测试工具
- 比特币开发专题(区块链钱包安全攻略及备份恢复方法)
- 让Unity3D中的插件窗口EditorWindow保持居中显示
- eclipse在线安装maven插件有效地址
- 【leetcode】561. Array Partition I
- 对话框收缩与扩展
- Matlab 沿三维任意方向切割CT图的仿真计算