(斯坦福机器学习笔记)线性回归练习
来源:互联网 发布:linux ntp配置文件 编辑:程序博客网 时间:2024/06/06 01:16
代码均使用python3.x
题目是:
import numpy as npimport randomimport matplotlib.pyplot as pltF64 = '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 npimport randomimport matplotlib.pyplot as pltF64 = '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
0 0
- (斯坦福机器学习笔记)线性回归练习
- (斯坦福机器学习课程笔记)局部加权线性回归练习
- (斯坦福机器学习课程笔记)多项式回归练习
- (斯坦福机器学习课程笔记)用广义线性模型推导逻辑回归模型
- 斯坦福机器学习笔记 第1周 二、单变量线性回归(一)
- 斯坦福机器学习笔记 第1周 二、单变量线性回归(二)
- 斯坦福机器学习公开课笔记(一)--单变量线性回归
- 斯坦福机器学习公开课笔记(二)--多变量线性回归
- 斯坦福机器学习课程笔记week2&3 线性/逻辑斯蒂回归
- 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent
- 斯坦福机器学习视频笔记 Week2 多元线性回归 Linear Regression with Multiple Variables
- 斯坦福机器学习笔记 第2周 四、多变量线性回归
- 《机器学习》学习笔记(一):线性回归、逻辑回归
- 机器学习笔记-线性回归
- 机器学习笔记:线性回归
- 斯坦福《机器学习》Lesson1-3感想-------2、线性回归
- 斯坦福《机器学习》Lesson1-3感想-------3、线性回归二
- 斯坦福机器学习3之局部线性回归建模
- 如何知道自己的硬件是否支持64位系统?
- 【NOIP2017提高组模拟12.10】幻魔皇
- 2016-12-06学习内容
- Linux 常规文件编辑
- [51nod 1594]Gcd and Phi
- (斯坦福机器学习笔记)线性回归练习
- Java学习日记1
- 字符串逆序输出
- iOS之实现图片裁剪的几种方式
- Java中登录页面 记住密码
- 文章标题
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- You are using pip version 8.1.1, however version 9.0.1 is available.
- leetcode--Happy Number