当前位置: 首页 > news >正文

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

这里写自定义目录标题

  • (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))

相关文章:

  • 四个常见的Linux面试问题
  • 【面试】TCP、UDP、Socket、HTTP网络编程面试题
  • Mysql查询截取分析_慢查询日志
  • Qt 中的信息输出机制:QDebug、QInfo、QWarning、QCritical 的简单介绍和用法
  • 50 Projects 50 Days - Expanding Cards 学习记录
  • Go语言精修(尚硅谷笔记)第十一章
  • 【EHub_tx1_tx2_E100】不止科技NVISTAR ROC 300激光雷达Ubuntu18.04+ROS1ROS2 评测
  • WebRTC API
  • 大数据之Spark开发环境准备
  • 什么是Java运算?Java运算好学吗?
  • abaqus子程序vumat安装使用
  • 【Java项目】Minio的安装部署以及SpringBoot整合Minio
  • 架构设计第一讲:架构设计相关面试题汇总
  • LeetCode笔记:Biweekly Contest 101
  • 【python实操】马上毕业了,你还不懂什么是守护线程、线程、进程?(附12306抢票程序-源代码)
  • Asm.js的简单介绍
  • Cumulo 的 ClojureScript 模块已经成型
  • es6要点
  • es的写入过程
  • Java基本数据类型之Number
  • PAT A1017 优先队列
  • PhantomJS 安装
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • select2 取值 遍历 设置默认值
  • session共享问题解决方案
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • Xmanager 远程桌面 CentOS 7
  • 从零搭建Koa2 Server
  • 从伪并行的 Python 多线程说起
  • 基于web的全景—— Pannellum小试
  • 每天10道Java面试题,跟我走,offer有!
  • 入口文件开始,分析Vue源码实现
  • 使用 5W1H 写出高可读的 Git Commit Message
  • PostgreSQL之连接数修改
  • ​业务双活的数据切换思路设计(下)
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (libusb) usb口自动刷新
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (第二周)效能测试
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (一)插入排序
  • (转载)Google Chrome调试JS
  • ../depcomp: line 571: exec: g++: not found
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NetCore部署微服务(二)
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • :=
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [c#基础]DataTable的Select方法
  • [HarmonyOS]第一课:从简单的页面开始