ai-2、机器学习之线性回归

发布于:2025-03-02 ⋅ 阅读:(112) ⋅ 点赞:(0)

1、机器学习

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、线性回归

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
####所以y可以当成我们需要的结果,根据公式可以求的y一撇的值更小,所以更接近需要的结果,所以y一撇拟合性更好

2.1、梯度下降法

在这里插入图片描述
在这里插入图片描述

已知:
J = f ( ( (p ) ) ) = 3.5p 2 ^2 2-14p+14
p i _i i = 0.5 , α \alpha α = 0.01
p i + 1 _{i+1} i+1= ??

∵ \because 一元二次函数 f ( ( (p ) ) ) = ap 2 ^2 2-bp+c的幂函数求导公式
f ( ( (x ) ) ) = x a ^a a -> f ′ f\prime f( x x x) = ax a − 1 ^{a-1} a1

∵ \because
3.5p 2 ^2 2其导数为2*3.5p ( 2 − 1 ) ^{(2-1)} (21) = 7p

-14p其导数为-14*p ( 1 − 1 ) ^{(1-1)} (11)=-14

14为常数项导数为0

∵ \because 3.5p 2 ^2 2-14p+14的导数是7p-14

∴ \therefore α \alpha α δ δ p i \frac{\delta}{\delta p_i} δpiδf ( ( (p i _{i} i ) ) ) = 7p-14

∵ \because p i _i i = 0.5

∴ \therefore 代入 α \alpha α δ δ p i \frac{\delta}{\delta p_i} δpiδf ( ( (p i _{i} i ) ) )=7*0.5-14=-10.5

∴ \therefore 损失函数J = 0.5-0.01*(-10.5) = 0.605

∴ \therefore 损失函数梯度值为0.605
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3、python下调用scikit-learn

在这里插入图片描述
https://scikit-learn.org/stable/

在这里插入图片描述
可以用简短的代码求解模型

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

import numpy as np
from sklearn.linear_model import LinearRegression
# 获取数据
x = [1,2,3,4,5,6,7,8,9,10]
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
X = x.reshape(-1, 1)
y = np.array([7,9,11,13,15,17,19,21,23,25])
# 寻找a、b(y = ax + b)
lr_model = LinearRegression()
lr_model.fit(X,y)

# 展示a、b
a = lr_model.coef_
b = lr_model.intercept_

#打印系数a和截距b
print("系数a",a)
print("截距b",b)

#对新数据进行预测
x_new = np.array([11,12])
X_new = x_new.reshape(-1, 1)
predictions = lr_model.predict(X_new)

#打印预测数据
print("预测数据是:",predictions)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

import numpy as np
from sklearn.linear_model import LinearRegression
# 获取数据
x = [1,2,3,4,5,6,7,8,9,10]
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
X = x.reshape(-1, 1)
y = np.array([7,9,11,13,15,17,19,21,23,25])
# 寻找a、b(y = ax + b)
lr_model = LinearRegression()
lr_model.fit(X,y)

# 展示a、b
a = lr_model.coef_
b = lr_model.intercept_

#打印系数a和截距b
print("系数a",a)
print("截距b",b)

#对新数据进行预测
x_new = np.array([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,11])
X_new = x_new.reshape(-1, 1)
predictions = lr_model.predict(X_new)

#打印预测数据
print("预测数据是:",predictions)

from sklearn.metrics import mean_squared_error,r2_score
MSE = mean_squared_error(y,predictions)
R2 = r2_score(y,predictions)

#打印均方差,r方值
print("MSE:",MSE)
print("R2:",R2)

#画图对比y/ 和y可视化模型表现
from matplotlib import pyplot as plt
plt.scatter(y,predictions)

在这里插入图片描述
matplotlib绘制散点图
在这里插入图片描述

#绘制散点图

import matplotlib.pyplot as plt

# 生成示例数据
np.random.seed(42)
x_values = np.random.rand(100)
y_values = x_values + np.random.randn(100) * 0.1  

# 绘制散点图
plt.scatter(x_values, y_values, color='blue', marker='o')  # color参数定义点的颜色,marker定义点的形状

# 设置matplotlib的字体为微软雅黑,确保你的系统中已安装此字体,避免中文乱码问题
plt.rcParams['font.family'] = 'Microsoft YaHei'
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 用于正常显示中文标签
# 解决保存图像时负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False

 
# 添加标题和标签
plt.title('散点图')  # 添加标题
plt.xlabel('X轴')  # X轴标签
plt.ylabel('Y轴')  # Y轴标签
 
# 显示图表
plt.show()

多张散点图展示

#散点图多张图同时展示

import matplotlib.pyplot as plt

# 生成示例数据
np.random.seed(42)
x_values = np.random.rand(100)
y_values = x_values + np.random.randn(100) * 0.1

# 如果需要四张图则创建一个2x2的子图布局
for i in range(1, 5):  # 因为是从1开始计数,所以要到5(包括)
    plt.subplot(2, 2, i)  # 2行2列的布局,当前是第i个子图
    plt.scatter(x_values, y_values, color='blue', marker='p')

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到