(斯坦福机器学习笔记)线性回归练习
2016-12-12 22:13
405 查看
代码均使用python3.x
题目是:import numpy as np import random import matplotlib.pyplot as plt F64 = 'float64' def gen_linear_dot_sample(num_point): x = list(range(num_point)) for i in range(num_point): x[i] = x[i] / num_point y = x.copy() for i in range(num_point): y[i]=y[i]+random.uniform(-0.08,0.08) return [x,y] [x,y]=gen_linear_dot_sample(50)
生成了50个样本点,如下图
试用线性回归求出最佳拟合的直线
=====================分割线=======================================
代码如下:
import numpy as np import random import matplotlib.pyplot as plt F64 = 'float64' def gen_linear_dot_sample(num_point): x = list(range(num_point)) for i in range(num_point): x[i] = x[i] / num_point y = x.copy() for i in range(num_point): y[i]=y[i]+random.uniform(-0.08,0.08) return [x,y] def show_point(p_x,p_y): plt.figure(1) plt.plot(p_x,p_y,'ob') plt.xlim(-0.2,1.2) plt.ylim(-0.2,1.2) plt.xlabel('x') plt.ylabel('y') plt.show() def show_point_and_line(p_x,p_y,l_x,l_y): plt.figure(1) plt.plot(p_x,p_y,'ob') plt.plot(l_x,l_y,'r') plt.xlim(-0.2,1.2) plt.ylim(-0.2,1.2) plt.xlabel('x') plt.ylabel('y') plt.show() def linear_re(x,y,study_rate,times_iteration): y=y.reshape(y.shape[0],1).astype(F64) if x.ndim == 1: x=x.reshape((x.shape[0],1)) one_mat = np.ones((x.shape[0],1),dtype=F64) x_add_one = np.hstack((one_mat,x)) theta = np.random.random((x.shape[1]+1)) theta=theta.astype(F64).reshape(x.shape[1]+1,1) theta_init=theta print('初始化后,theta的值是:','b=',theta[0,0],'k=',theta[1,0]) for i in range(times_iteration): diff_cost = ((np.dot(x_add_one,theta)-y)*x_add_one) diff_cost=diff_cost.sum(0,dtype=F64,keepdims=1).transpose() diff_cost_mean = diff_cost/x.shape[0] theta = theta-diff_cost_mean*study_rate print('迭代后得到的theta的值是:','b=',theta[0,0],'k=',theta[1,0]) return [theta_init,theta] [x,y]=gen_linear_dot_sample(50) [theta_init,theta]=linear_re(np.asarray(x,dtype=F64),np.asarray(y,dtype=F64),0.25,200) show_point_and_line(x,y,x,theta_init[0]+theta_init[1]*x) show_point_and_line(x,y,x,theta[0]+theta[1]*x)
初始化后,得到的直线是这样
线性回归后,得到直线
初始化后,theta的值是: b= 0.134382446993 k= 0.553982228195
迭代后得到的theta的值是: b= 0.00249451906385 k= 0.973867674154
相关文章推荐
- (斯坦福机器学习课程笔记)局部加权线性回归练习
- (斯坦福机器学习课程笔记)多项式回归练习
- 斯坦福机器学习课程笔记week2&3 线性/逻辑斯蒂回归
- 【机器学习-斯坦福】学习笔记23——偏最小二乘法回归(Partial Least Squares Regression)
- 机器学习推导笔记1--机器学习的任务、步骤、线性回归、误差、梯度下降
- 【Stanford|斯坦福-机器学习:线性回归-单特征梯度下降+动态图】python3实现
- 【学习笔记】斯坦福大学公开课(机器学习) 之一:线性回归、梯度下降
- 斯坦福机器学习视频笔记 Week3 逻辑回归和正则化 Logistic Regression and Regularization
- 斯坦福机器学习公开课学习笔记(3)—拟合问题以及局部权重回归、逻辑回归
- 机器学习与R笔记之线性回归
- Machine Learning(Stanford)| 斯坦福大学机器学习笔记--第一周(5.线性回归的梯度下降)
- # 机器学习笔记2——参数学习、非参数学习、局部加权线性回归、线性回归的概率解释、logistics回归
- 机器学习笔记(一)线性回归
- Stanford机器学习笔记-1.线性回归
- 【机器学习-斯坦福】学习笔记18——线性判别分析(Linear Discriminant Analysis)(一)
- 斯坦福《机器学习》Lesson1-3感想-------3、线性回归二
- 斯坦福大学机器学习课程笔记:线性回归以及梯度下降
- 机器学习笔记01:线性回归(Linear Regression)和梯度下降(Gradient Decent)
- 机器学习笔记 - 线性模型与线形回归
- (斯坦福机器学习课程笔记)用广义线性模型推导逻辑回归模型