【机器学习】P10 从头到尾实现一个线性回归案例

news2025/2/27 20:39:10

这里写自定义目录标题

  • (1)导入数据
  • (2)画出城市人口与利润图
  • (3)计算损失值
  • (4)计算梯度下降
  • (5)开始训练
  • (6)画出训练好的模型
  • (7)做出预测
  • (8)完整代码

(1)导入数据

问题引入:假设你是老板,要考虑在不同的城市开一家新店;
x_train 是不同城市的人口数量
y_train 是那个城市一家餐馆的利润

import math
import copy
import numpy as np

x_train = np.array([6.1101, 5.5277, 8.5186, 7.0032, 5.8598, 8.3829, 7.4764, 8.5781, 6.4862, 5.0546, 5.7107, 14.164, 5.734, 8.4084, 5.6407, 5.3794, 6.3654, 5.1301, 6.4296, 7.0708, 6.1891, 20.27, 5.4901, 6.3261, 5.5649, 18.945, 12.828, 10.957, 13.176, 22.203, 5.2524, 6.5894, 9.2482, 5.8918, 8.2111, 7.9334, 8.0959, 5.6063, 12.836, 6.3534, 5.4069, 6.8825, 11.708, 5.7737, 7.8247, 7.0931, 5.0702, 5.8014, 11.7, 5.5416, 7.5402, 5.3077, 7.4239, 7.6031, 6.3328, 6.3589, 6.2742, 5.6397, 9.3102, 9.4536, 8.8254, 5.1793, 21.279, 14.908, 18.959, 7.2182, 8.2951, 10.236, 5.4994, 20.341, 10.136, 7.3345, 6.0062, 7.2259, 5.0269, 6.5479, 7.5386, 5.0365, 10.274, 5.1077, 5.7292, 5.1884, 6.3557, 9.7687, 6.5159, 8.5172, 9.1802, 6.002, 5.5204, 5.0594, 5.7077, 7.6366, 5.8707, 5.3054, 8.2934, 13.394, 5.4369])
y_train = np.array([17.592, 9.1302, 13.662, 11.854, 6.8233, 11.886, 4.3483, 12., 6.5987, 3.8166, 3.2522, 15.505, 3.1551, 7.2258, 0.71618, 3.5129, 5.3048, 0.56077, 3.6518, 5.3893, 3.1386, 21.767, 4.263, 5.1875, 3.0825, 22.638, 13.501, 7.0467, 14.692, 24.147, -1.22, 5.9966, 12.134, 1.8495, 6.5426, 4.5623, 4.1164, 3.3928, 10.117, 5.4974, 0.55657, 3.9115, 5.3854, 2.4406, 6.7318, 1.0463, 5.1337, 1.844, 8.0043, 1.0179, 6.7504, 1.8396, 4.2885, 4.9981, 1.4233, -1.4211, 2.4756, 4.6042, 3.9624, 5.4141, 5.1694, -0.74279, 17.929, 12.054, 17.054, 4.8852, 5.7442, 7.7754, 1.0173, 20.992, 6.6799, 4.0259, 1.2784, 3.3411, -2.6807, 0.29678, 3.8845, 5.7014, 6.7526, 2.0576, 0.47953, 0.20421, 0.67861, 7.5435, 5.3436, 4.2415, 6.7981, 0.92695, 0.152, 2.8214, 1.8451, 4.2959, 7.2029, 1.9869, 0.14454, 9.0551, 0.61705])

(2)画出城市人口与利润图

通过 python 包 matplotlib.pyplot 画图

import matplotlib.pyplot as plt

plt.scatter(x_train, y_train, marker='x', c='g') 

plt.title("利润*10000/人口数量*10000")
plt.ylabel('利润*10000')
plt.xlabel('人口数量*10000')

plt.show()

在这里插入图片描述

(3)计算损失值

已知模型为:
f w ⃗ , b ( x ⃗ ( i ) ) = w ⃗ ⋅ x ⃗ ( i ) + b f_{\vec{w},b}(\vec{x}^{(i)}) = \vec{w}·\vec{x}^{(i)}+b fw ,b(x (i))=w x (i)+b

损失函数为:
c o s t ( i ) = ( f w ⃗ , b ( x ⃗ ( i ) ) − y ( i ) ) 2 cost^{(i)} = (f_{\vec{w},b}(\vec{x}^{(i)})-y^{(i)})^2 cost(i)=(fw ,b(x (i))y(i))2

总损失函数为:
J ( w ⃗ . b ) = 1 2 m ∑ i = 0 m − 1 c o s t ( i ) J(\vec{w}.b) = \frac 1 {2m} \sum ^{m-1} _{i=0} cost^{(i)} J(w .b)=2m1i=0m1cost(i)

程序实现如下:

def compute_cost(x,y,w,b):

	m = x.shape[0]
	total_cost = 0.

	for i in range(m):
		f_wb = np.dot(w,x[i]) + b
		cost = (f_wb - y[i]) ** 2
		total_cost += cost
	total_cost = total_cost / (2 * m)
	
	return total_cost

(4)计算梯度下降

梯度下降公式为:
repeat until convergence:    {    0000 b : = b − α ∂ J ( w , b ) ∂ b    0000 w : = w − α ∂ J ( w , b ) ∂ w    } \begin{align*}& \text{repeat until convergence:} \; \lbrace \newline \; & \phantom {0000} b := b - \alpha \frac{\partial J(w,b)}{\partial b} \newline \; & \phantom {0000} w := w - \alpha \frac{\partial J(w,b)}{\partial w} \; & \newline & \rbrace\end{align*} repeat until convergence:{0000b:=bαbJ(w,b)0000w:=wαwJ(w,b)}

详解 gradient 部分为:
∂ J ( w , b ) ∂ b = 1 m ∑ i = 0 m − 1 ( f w , b ( x ( i ) ) − y ( i ) ) ∂ J ( w , b ) ∂ w = 1 m ∑ i = 0 m − 1 ( f w , b ( x ( i ) ) − y ( i ) ) x ( i ) \frac{\partial J(w,b)}{\partial b} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)}) \\ \frac{\partial J(w,b)}{\partial w} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) -y^{(i)})x^{(i)} bJ(w,b)=m1i=0m1(fw,b(x(i))y(i))wJ(w,b)=m1i=0m1(fw,b(x(i))y(i))x(i)

代码实现 gradient 部分:

def compute_gradient(x,y,w,b):

	m = x.shape[0]
	dj_dw = 0
	dj_db = 0

	for i in range(m):
		f_wb = np.dot(w,x[i]) + b
		dj_dw += (f_wb - y[i]) * x[i]
		dj_db +=  f_wb - y[i]
	dj_dw = dj_dw / m
	dj_db = dj_db / m

	return dj_dw, dj_db

代码实现梯度下降 gradient descent

def gradient_descent(x,y,w_in,b_in,cost_function,gradient_function,alpha,num_iters):
	
	J_history = []
	w_history = []
	w = copy.deepcopy(w_in)
	b = b_in

	for i in range(num_iters):
		dj_dw, dj_db = gradient_function(x,y,w,b)
		w = w - alpha * dj_dw
		b = b - alpha * dj_db
	
		if i<100000:
			cost = cost_function(x,y,w,b)
			J_history.append(cost)
		if i% math.ceil(num_iters/10) == 0:		# math.ceil: 将传入的参数向上取整为最接近的整数
			w_history.append(w)
			print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f}   ")
			
	return w, b, J_history, w_history

(5)开始训练

initial_w = 0.
initial_b = 0.
iterations = 1500
alpha = 0.01

w,b,_,_ = gradient_descent(x_train ,y_train, initial_w, initial_b, compute_cost, compute_gradient, alpha, iterations)
print("w,b found by gradient descent:", w, b)

(6)画出训练好的模型

因为我们已经训练完成模型,所以直接用参数 向量w参数b 的值进行绘图:

m = x_train.shape[0]
predicted = np.zeros(m)

for i in range(m):
    predicted[i] = w * x_train[i] + b

plt.plot(x_train, predicted, c = "b")
plt.scatter(x_train, y_train, marker='x', c='g') 

plt.title("Profits*10000/Population*10000")
plt.ylabel('Profits*10000')
plt.xlabel('Population*10000')

在这里插入图片描述

(7)做出预测

predict1 = 3.5 * w + b
print('For population = 35,000, we predict a profit of $%.2f' % (predict1*10000))

predict2 = 7.0 * w + b
print('For population = 70,000, we predict a profit of $%.2f' % (predict2*10000))

(8)完整代码

import copy
import math
import numpy as np

x_train = np.array([6.1101, 5.5277, 8.5186, 7.0032, 5.8598, 8.3829, 7.4764, 8.5781, 6.4862, 5.0546, 5.7107, 14.164, 5.734, 8.4084, 5.6407, 5.3794, 6.3654, 5.1301, 6.4296, 7.0708, 6.1891, 20.27, 5.4901, 6.3261, 5.5649, 18.945, 12.828, 10.957, 13.176, 22.203, 5.2524, 6.5894, 9.2482, 5.8918, 8.2111, 7.9334, 8.0959, 5.6063, 12.836, 6.3534, 5.4069, 6.8825, 11.708, 5.7737, 7.8247, 7.0931, 5.0702, 5.8014, 11.7, 5.5416, 7.5402, 5.3077, 7.4239, 7.6031, 6.3328, 6.3589, 6.2742, 5.6397, 9.3102, 9.4536, 8.8254, 5.1793, 21.279, 14.908, 18.959, 7.2182, 8.2951, 10.236, 5.4994, 20.341, 10.136, 7.3345, 6.0062, 7.2259, 5.0269, 6.5479, 7.5386, 5.0365, 10.274, 5.1077, 5.7292, 5.1884, 6.3557, 9.7687, 6.5159, 8.5172, 9.1802, 6.002, 5.5204, 5.0594, 5.7077, 7.6366, 5.8707, 5.3054, 8.2934, 13.394, 5.4369])
y_train = np.array([17.592, 9.1302, 13.662, 11.854, 6.8233, 11.886, 4.3483, 12., 6.5987, 3.8166, 3.2522, 15.505, 3.1551, 7.2258, 0.71618, 3.5129, 5.3048, 0.56077, 3.6518, 5.3893, 3.1386, 21.767, 4.263, 5.1875, 3.0825, 22.638, 13.501, 7.0467, 14.692, 24.147, -1.22, 5.9966, 12.134, 1.8495, 6.5426, 4.5623, 4.1164, 3.3928, 10.117, 5.4974, 0.55657, 3.9115, 5.3854, 2.4406, 6.7318, 1.0463, 5.1337, 1.844, 8.0043, 1.0179, 6.7504, 1.8396, 4.2885, 4.9981, 1.4233, -1.4211, 2.4756, 4.6042, 3.9624, 5.4141, 5.1694, -0.74279, 17.929, 12.054, 17.054, 4.8852, 5.7442, 7.7754, 1.0173, 20.992, 6.6799, 4.0259, 1.2784, 3.3411, -2.6807, 0.29678, 3.8845, 5.7014, 6.7526, 2.0576, 0.47953, 0.20421, 0.67861, 7.5435, 5.3436, 4.2415, 6.7981, 0.92695, 0.152, 2.8214, 1.8451, 4.2959, 7.2029, 1.9869, 0.14454, 9.0551, 0.61705])

import matplotlib.pyplot as plt

plt.scatter(x_train, y_train, marker='x', c='g')

plt.title("profits*10000/num(human in a city)*10000")
plt.ylabel('profits*10000')
plt.xlabel('num(human in a city)*10000')

plt.show()


def compute_cost(x, y, w, b):
    m = x.shape[0]
    total_cost = 0.

    for i in range(m):
        f_wb = np.dot(w, x[i]) + b
        cost = (f_wb - y[i]) ** 2
        total_cost += cost
    total_cost = total_cost / (2 * m)

    return total_cost



def compute_gradient(x,y,w,b):

	m = x.shape[0]
	dj_dw = 0
	dj_db = 0

	for i in range(m):
		f_wb = np.dot(w,x[i]) + b
		dj_dw += (f_wb - y[i]) * x[i]
		dj_db +=  f_wb - y[i]
	dj_dw = dj_dw / m
	dj_db = dj_db / m

	return dj_dw, dj_db


def gradient_descent(x, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters):

    J_history = []
    w_history = []
    w = copy.deepcopy(w_in)
    b = b_in

    for i in range(num_iters):
        dj_dw, dj_db = gradient_function(x, y, w, b)
        w = w - alpha * dj_dw
        b = b - alpha * dj_db

        if i < 100000:
            cost = cost_function(x, y, w, b)
            J_history.append(cost)
        if i % math.ceil(num_iters / 10) == 0:  # math.ceil: 将传入的参数向上取整为最接近的整数
            w_history.append(w)
            print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f}   ")

    return w, b, J_history, w_history



initial_w = 0.
initial_b = 0.
iterations = 1500
alpha = 0.01

w,b,_,_ = gradient_descent(x_train ,y_train, initial_w, initial_b, compute_cost, compute_gradient, alpha, iterations)
print("w,b found by gradient descent:", w, b)

m = x_train.shape[0]
predicted = np.zeros(m)

for i in range(m):
    predicted[i] = w * x_train[i] + b

plt.plot(x_train, predicted, c = "b")
plt.scatter(x_train, y_train, marker='x', c='g')

plt.title("Profits*10000/Population*10000")
plt.ylabel('Profits*10000')
plt.xlabel('Population*10000')

plt.show()

predict1 = 3.5 * w + b
print('For population = 35,000, we predict a profit of $%.2f' % (predict1*10000))

predict2 = 7.0 * w + b
print('For population = 70,000, we predict a profit of $%.2f' % (predict2*10000))

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/415635.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

参加Matlab与AI讲座:使用深度强化学习训练走路机器人观后感

时间&#xff1a;2023年4月12日&#xff0c;周三&#xff0c;天气晴 地址&#xff1a;大连理工大学研教楼303 前言&#xff1a;Matlab其实有很多功能&#xff0c;我们所用的只是最基础最简单的部分&#xff0c;例如矩阵计算&#xff0c;画图等等。 随着强化学习的发展&#xff…

一般形式的S曲线公式推导

文章目录一、背景二、目标三、计算3.1 S曲线基本形式3.2 S曲线变换3.3 参数计算3.4 S曲线中心对称条件四、总结五、附件一、背景 S曲线因具备良好可控的平滑性、单调性、连续可导性等优点&#xff0c;常作为各类电机升降速曲线。当前多数S曲线的介绍文章未给出推导过程&#x…

SpringCloud微服务技术栈.黑马跟学(五)

SpringCloud微服务技术栈.黑马跟学 五今日目标1.初识elasticsearch1.1.了解ES1.1.1.elasticsearch的作用1.1.2.ELK技术栈1.1.3.elasticsearch和lucene1.1.4.为什么不是其他搜索技术&#xff1f;1.1.5.总结1.2.倒排索引1.2.1.正向索引1.2.2.倒排索引1.2.3.正向和倒排1.3.es的一些…

SpringMVC基本注解的使用和理解

SpringMVC基本注解的使用和理解 RequestParam注解 使用在方法入参位置&#xff0c;用于指定请求参数名称&#xff0c;将该请求参数绑定到注解参数位置。 属性&#xff1a;name:指定要绑定的请求参数名称&#xff1b; name属性和value属性互为别名。 required 和&#xff1a;指…

Java并发编程(8) —— AQS抽象同步队列详解

上一篇&#xff1a;Java并发编程(7) —— 锁的分类概述 在上一篇中我们提到并发包中的ReentrantLock类是一种可重入独占锁&#xff0c;其锁机制是基于AQS实现的。实际上&#xff0c;并发包java.util.concurrent.locks中的锁都是基于AQS 实现的。 一、AQS是什么 AbstractQueued…

13. unity粒子特效--发射模块、各种发射器形状、粒子渐变(颜色/大小)

1. 发射模块&#xff08;Emission&#xff09; 匀速发射&#xff1a; Rate over Time&#xff1a;每秒钟发射的粒子数 Rate over Distance&#xff1a;每移动一米发射的粒子个数 两者可指定其一&#xff1a;若仅指定Rate over Time&#xff0c;则粒子根据时间的变化进行发射&a…

第三节、语言模型

目录 0、介绍 1、N-gram 模型介绍 2、困惑度 3、N-gram 模型的文本评估 4、N-gram 模型的平滑 5、基于 N-gram 模型的文本生成 6、基于统计的语言模型的缺陷 7、实验总结 0、介绍 首先&#xff0c;我们来思考这样一个问题&#xff1a;随便给你一句话&#xff0c;如何判…

MongoDB中的索引

一、说明 索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可能要花费几十秒甚至几分钟,这对网站的性能是非常致命的。索引是…

Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加

Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加 目录 Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加 0.创建数据库 1. 在resources目录下创建db.properties文件 2. /** * 获取链接与释放资源的工具类--JdbcUtil类 */ 3…

UE-Ueransim-5GC全链路开发记录

目录 1. 系统配置 1.1 Ueransim配置 1.2 UE配置 2. 启动 3. 实际演示 附录 代理1&#xff1a;ueransim-5gc 代理2 ue-ueransim TCPclient TCPserver 1. 系统配置 1.1 Ueransim配置 ueransim的yaml文件如下 version: 3.8 services:ueransim2:container_name: uera…

Ubantu docker学习笔记(六)容器数据卷

文章目录一、容器数据卷二、容器卷挂载2.1 在命令行挂载数据卷2.2 通过dockerfile挂载数据卷三、数据卷容器四、备份数据卷五、数据卷的恢复和迁移5.1 恢复数据卷5.2 迁移数据卷六、管理数据卷6.1 与容器关联&#xff08;1&#xff09;例子一&#xff08;2&#xff09;例子二&a…

【LeetCode】剑指 Offer(28)

目录 题目&#xff1a;剑指 Offer 54. 二叉搜索树的第k大节点 - 力扣&#xff08;Leetcode&#xff09; 题目的接口&#xff1a; 解题思路&#xff1a; 代码&#xff1a; 过啦&#xff01;&#xff01;&#xff01; 题目&#xff1a;剑指 Offer 55 - I. 二叉树的深度 - 力…

MySQL运维11-MySQL的事务隔离级别

文章目录1、MySQL的事务隔离级别2、MySQL事务隔离级别的相关参数和命令2.1、查看事务隔离级别2.2、设置事务隔离级别2.2.1、在会话中设置事务隔离级别2.2.2、在配置文件中设置事务隔离级别3、MySQL的多版本并发控制(MVCC)4、总结1、MySQL的事务隔离级别 事务隔离级别越高&…

Qt5.12实战之规则DLL导出函数使用

1.创建基于MFC的规则DLL工程: 输入工程名,然后点击创建 选择使用共享MFC DLL的常规DLL 创建成功后,解决方案下会多出一个工程 增加导出函数声明 实现导出函数 在模块定义文件def文件中声明导出

YC-B09(原创)基于springboot,vue网上书城

(原创)基于springboot,vue网上书城定制版v4.0 本人原创作品&#xff0c;用户前台、系统管理员后台项目完整&#xff0c;无任何bug。 每行代码都是本人自己写&#xff0c;我在代码上面都写有详细注释 开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk…

代码随想录算法训练营第五十九天-单调栈2| 503.下一个更大元素II 42. 接雨水

503. Next Greater Element II 成环就用取模mod方法 import java.util.Arrays; import java.util.Stack;public class NextGreaterElement2 {public int[] nextGreaterElements(int[] nums) {//边界判断if(nums null || nums.length < 1){return new int[]{-1};}int size …

无需兔魔法!国内手机直接畅玩GPT!

为了用上ChatGPT很多同学都是经历一波三折&#xff0c;闯三关过五将&#xff01;因为使用ChatGPT的门槛很高&#xff0c;尤其是这个kx上网把很多人都挡在了门外&#xff01;有的同学说newbing呢&#xff0c;newbing如果你要用聊天功能&#xff0c;一样有这样门槛&#xff01;很…

Ubuntu docker 基本操作

安装docker&#xff1a; curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 也可以使用国内 daocloud 一键安装命令&#xff1a; curl -sSL https://get.daocloud.io/docker | sh 启动Docker命令&#xff1a; systemctl start docker 查看版本号&#xf…

NKCTF2023 babyrust

这道题目适合科普&#xff1a;rust逆向&#xff0c;xmm指令。 rust逆向的一些注意事项 rust题中&#xff0c;“ida给的main”里的第一个lea是“用户写的main”。 如下图&#xff0c;这是ida标记的main。而用户写的main其实是sub_1400012A0。姑且可以认为“ida给的main”里的…

初识springcloud

认识微服务 单体架构&#xff1a; 简单方便&#xff0c;高度耦合&#xff0c;扩展性差&#xff0c;适合小型项目&#xff0c;例如&#xff0c;学生管理系统。 分布式架构 松耦合&#xff0c;扩展性好&#xff0c;但架构复杂&#xff0c;难度大。适合大型互联网项目。如&#x…