机器学习作业1编程作业(python): Linear Regression

英文文档图片均来自原档作业pdf截图 网址https://www.coursera.org/learn/machine-learning/programming/8f3qT/linear-regression

LInear regression with one variable

在这里插入图片描述
在这里插入图片描述
单变量线性回归,使用数据ex1data1
在这里插入图片描述

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

path = r'D:\Ninachen\wg_machinelearning\machine-learning-ex1\ex1\ex1data1.txt'
data = pd.read_csv(path, header=None, names=['population', 'profit'])
data.plot.scatter(x = 'population', y = 'profit', marker = '+')

初步观察原始数据,选择一次函数:
*在这里插入图片描述*
在这里插入图片描述
在这里插入图片描述
X矩阵:每行是一组数据+一个常量–>n组数据k个特征+1个常数列(即x0)->n行(k+1)列矩阵在这里插入图片描述

θ向量:一些系数(个数取决于假设函数的形式)–> 列向量

在这里插入图片描述
代价函数(矩阵向量形式)
在这里插入图片描述
由输入给定迭代次数、学习率,并新建二维数组存储每次的代价函数值:


# cost_table = np.empty([iterations], dtype = float)
# cost_table[0] = cost_function(X, y, theta)

写在一开始:

datamat = data.values #转为矩阵
X = np.column_stack((datamat[:,0], np.ones((len(datamat[:,0]),1)))) #加上一列常数
y = datamat[:,-1] #最后一列

theta = np.zeros((2,1)) #2个系数组成的列向量
iterations = int(input('please input the iterations:')) #给定迭代次数
alpha = float(input('please input the  learning rate alpha:')) #学习率

cost_table = np.empty([iterations]) #后续存放每一步的代价函数值

m = X.shape[0]  #m为数据组数
def cost_function(X, y, theta): #即代价函数J(θ)
    Y = y.reshape((m, 1))
    J_theta = np.sum(np.power((X.dot(theta) - Y), 2)) / (2 * m)
    return J_theta  # J_theta为一个数值

cost_table[0] = cost_function(X, y, theta) #代价函数初值

*注意要用reshape将向量y化为矩阵Y,否则维数不对,矩阵乘法会出错

Y = y.reshape((m, 1))

Y.shape
(97, 1) #是二维数组

y.shape
(97,) #是一维向量

由初值θ计算的代价函数:

cost_function(X, y, theta)
32.072733877455676

再计算θ的更新公式:
在这里插入图片描述
仍然用矩阵计算
在这里插入图片描述

def delta(X, y, theta, alpha):

    Y = y.reshape((m, 1))
    s1 = (X @ theta - Y).T @ X
    return alpha / m * s1.T

迭代更新θ:

for i in range(1, iterations):
    theta = theta - delta(X, y, theta, alpha)
    cost_table[i] = cost_function(X, y, theta)
# 运行结束后,theta即
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值