前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >机器学习|第一周:单变量线性回归例子

机器学习|第一周:单变量线性回归例子

作者头像
机器视觉CV
发布2019-07-15 19:28:32
4760
发布2019-07-15 19:28:32
举报
文章被收录于专栏:机器视觉CV机器视觉CV

本篇是吴恩达机器学习课程单变量线性回归作业ex1 Python 版本参考程序

程序是从jupyter notebook 转换过来的

导入要使用的模块

代码语言:javascript
复制
1%matplotlib notebook # jupyter notebook 的魔法方法
2import numpy as np
3import matplotlib.pyplot as plt
4import pandas as pd
代码语言:javascript
复制
1# 加载数据
2data = pd.read_csv('ex1data1.txt', header=None, names=['Population', 'Profit'])
代码语言:javascript
复制
1# 数据查看
2data.describe() # jupyter notebook 直接用来显示数据的,以下非Python 语法是 jupyter notebook 用来显示数据的

数据基本信息

代码语言:javascript
复制
1# 数据可视化,绘制散点图
2data.plot(kind='scatter', x='Population', y='Profit', figsize=(12, 8))

代价函数

代码语言:javascript
复制
1def costFunction(X, y, theta):
2    inner = np.power((X*theta.T)-y, 2)
3    return np.sum(inner) / (2*X.shape[0])
代码语言:javascript
复制
1# 在第 0 列插入一列数据 1 标签是 'Ones'
2data.insert(0, 'Ones', 1)
3data.head() # 查看前5 行数据
代码语言:javascript
复制
1# 设置训练集数据
2cols = data.shape[1] # 获取列数下标为 1 ;行数下标为 0 
3X = data.iloc[:,0:-1] #  取所有行,去掉所有一列
4y = data.iloc[:,cols-1:cols] # 取所有行,最后一列
5# 数据集转化为矩阵
6X = np.matrix(X.values)
7y = np.matrix(y.values)
8theta = np.matrix(np.array([0, 0]))
代码语言:javascript
复制
1#shape 属性是查看维数的
2X.shape, y.shape, theta.shape

((97, 2), (97, 1), (1, 2))

代码语言:javascript
复制
1#测试
2theta_1 = np.matrix(np.array([-1, 2]))
3J = costFunction(X, y, theta_1)
4print('\nWith theta = [-1 ; 2]\nCost computed =', J)
5print('Expected cost value (approx) 54.24\n');

With theta = [-1 ; 2] Cost computed = 54.24245508201238 Expected cost value (approx) 54.24

梯度下降更新

代码语言:javascript
复制
1def gradientDescent(X, y, theta, alpha, iteration):
2    J_history = np.zeros(iteration)
3    for iter in range(iteration):
4        h = X*theta.T 
5        theta = theta - alpha * (h - y).T*X / y.shape[0]
6        J_history[iter] = costFunction(X, y, theta)#记录
7    return theta, J_history
代码语言:javascript
复制
1alpha = 0.01
2iteration = 1000
3trained_theta, J = gradientDescent(X, y, theta, alpha, iteration)
代码语言:javascript
复制
1trained_theta[0, 1],J.shape

(1.127294202428184, (1000,))

拟合结果可视化

代码语言:javascript
复制
 1x_axis = np.linspace(data.Population.min(), data.Population.max(), 100)#设置x轴
 2y_axis = trained_theta[0, 0] + (trained_theta[0, 1] * x_axis)#设置Y轴
 3fig, ax = plt.subplots(figsize=(12,8))
 4ax.plot(x_axis, y_axis, 'r', label='Prediction')      # 绘制拟合曲线
 5ax.scatter(data.Population, data.Profit, label='Traning Data')#绘制散点图
 6ax.legend(loc=2)
 7ax.set_xlabel('Population')
 8ax.set_ylabel('Profit')
 9ax.set_title('Predicted Profit vs. Population Size')
10plt.show()

代价函数图像

代码语言:javascript
复制
1fig1, ax1 = plt.subplots()
2x=np.linspace(1, iteration, iteration)
3ax1.plot(x, J)

代价函数随着迭代次数增加,不断下降。 说明,梯度下降算法适合用来优化代价函数


您的关注和转发是对我最大的鼓励!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器视觉CV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导入要使用的模块
  • 代价函数
  • 梯度下降更新
  • 拟合结果可视化
  • 代价函数图像
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档