Exp1-LinearRegression

本实验详细介绍了线性回归和岭回归的实现过程,包括单变量和多变量线性回归的梯度下降法、闭式解,以及岭回归的闭式解。通过数据可视化和损失函数计算,帮助理解模型在数据上的工作原理。
摘要由CSDN通过智能技术生成

实验1:线性回归及岭回归


介绍

在本实验中,你将实现线性回归及岭回归并了解其在数据上的工作原理。

本次实验需要用到的数据集包括:

  • ex1data1.txt -单变量的线性回归数据集
  • ex1data2.txt -多变量的线性回归数据集

评分标准如下:

1 单变量线性回归

在该部分实验中,将实现单变量线性回归并用来预测餐车的利润。

假设你是一家餐厅的领导,正在考虑在不同的城市开设新的分店。该连锁店已经在不同的城市有了餐车,并且你能够获得每个城市的人口和利润数据。

现在需要使用这些数据来帮助你选择下一个被扩展的城市。

文件ex1data1.txt包含线性回归问题的数据集。第一列数据对应城市人口,第二列数据对应那座城市的餐车的利润。利润为负时表示亏损。

1.1 绘制数据

在开始进入实验之前,对数据进行可视化通常很有用。对于该数据集,可以使用散点图进行可视化,因为它只有两个属性(人口、利润)。

# 引入所需要的库文件
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os

%matplotlib inline
# 数据存储路径
path = 'ex1data1.txt'

# 读入相应的数据文件
data = pd.read_csv(path, header=None,names=['Population','Profit'])

#查看数据的前五条
data.head(5)
Population Profit
0 6.1101 17.5920
1 5.5277 9.1302
2 8.5186 13.6620
3 7.0032 11.8540
4 5.8598 6.8233
# 数据可视化 
data.plot(kind='scatter', x='Population', y='Profit',c='red', figsize=(9,6))

在这里插入图片描述

1.2 梯度下降

在该部分中,将使用梯度下降来选择合适的线性回归参数 w \mathbf{w} w用以拟合给定数据集。

说明:为编写方便,偏置项 b b b可被作为权重向量 w \mathbf{w} w的第一个分量 w 0 w_0 w0

1.2.1 目标函数

线性回归的目的是最小化目标函数:

E ( w ) = 1 2 m ∑ i = 1 m ( w T x i − y i ) 2 E(\mathbf{w}) = \frac{1}{2m} \sum_{i=1}^m \left( \mathbf{w}^T\mathbf{x}_i - y_i\right)^2 E(w)=2m1i=1m(wTxiyi)2

其中 w \mathbf{w} w为权重参数。上述目标函数也可写成如下矩阵形式:

E ( w ) = 1 2 m ∥ X w − y ∥ 2 2 E(\mathbf{w}) = \frac{1}{2m} \|\mathbf{X}\mathbf{w}-\mathbf{y}\|_2^2

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I'm sorry, as an AI language model, I'm not able to generate visual content. However, I can provide you with instructions on how to create a scatter plot with two non-linear regressed lines using Python and Matplotlib library. First, you need to import the necessary libraries: ```python import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit ``` Then, you need to define the non-linear functions for the two regressed lines. Let's assume they are exponential functions: ```python def f1(x, a, b, c): return a * np.exp(-b * x) + c def f2(x, d, e, f): return d * np.exp(-e * x) + f ``` Next, you need to load your data into two arrays, one for concentration and one for CO2 uptake rate. Let's assume they are called `x` and `y`. ```python x = np.array([concentration data]) y = np.array([CO2 uptake rate data]) ``` Now, you can use the `curve_fit` function to fit the non-linear functions to your data: ```python popt1, pcov1 = curve_fit(f1, x, y) popt2, pcov2 = curve_fit(f2, x, y) ``` Here, `popt` is an array of optimized parameters for the non-linear function, and `pcov` is the covariance matrix for the fit. Finally, you can create the scatter plot with the regressed lines and appropriate labels and legends: ```python plt.scatter(x, y, label='data', color='blue') plt.plot(x, f1(x, *popt1), 'r-', label='regression 1') plt.plot(x, f2(x, *popt2), 'g-', label='regression 2') plt.xlabel('Concentration') plt.ylabel('CO2 Uptake Rate') plt.legend() plt.show() ``` This will create a scatter plot with two non-linear regressed lines and appropriate axis labels and legends.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值