# -*- coding: utf-8 -*-
"""
Created on Tue May 19 09:47:11 2020
numpy实现最小二乘法
@author: 86186
"""
import numpy as np
import matplotlib.pyplot as plt
def loss(w, b, x, y):
total_loss = 0.0
differ = w*x + b - y
for i in range(len(x)):
total_loss += differ[i] ** 2
return total_loss/len(x)
#根据公式计算参数w和b
def compute_parameters(x, y):
numerator = 0.0 #分子
denominator = 0.0 #分母
x_mean = np.mean(x)
y_mean = np.mean(y)
for i in range(len(x)):
numerator += (x[i] - x_mean) * (y[i] - y_mean)
denominator += (x[i] - x_mean) ** 2
w = numerator / denominator
b = y_mean - w * x_mean
return w, b
x = np.linspace(1,100,200) #在[1,100]区间里生成均匀分布的200个数字
noise = np.random.normal(loc=0.0, scale=20, size=200)#生成均值为0,标准差为20的正态分布数据(200个)
y = 0.8 * x + noise
w, b = compute_parameters(x, y)
plt.figure()
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] #plt处理中文,字体为微软雅黑
y_pred = w * x + b
plt.scatter(x, y, c='r') #画散点图
plt.title("x与y的拟合函数")
plt.plot(x, y_pred, c='b') #画直线图
plt.xlabel('x')
plt.ylabel('y')
plt.show()
#矩阵求解法
#计算公式params = (X.T dot X)^-1 dot X.T dot Y
x = np.array(x)
y = np.array(y)
x_features = []
for i in range(len(x)):
x_features.append(np.array([x[i], 1]).T) #将输入特征加上偏置向并转置成列向量
x_features = np.array(x_features) #[200, 2]
xTx = np.dot(x_features.T, x_features) #[2, 2]
xTx_inv = np.linalg.inv(xTx) #[2, 2]
xTx_inv_xT = np.dot(xTx_inv, x_features.T)
params = np.dot(xTx_inv_xT, y)
w,b = params[0], params[1]
plt.figure()
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] #plt处理中文,字体为微软雅黑
y_pred = w * x + b
plt.scatter(x, y, c='r') #画散点图
plt.title("x与y的拟合函数")
plt.plot(x, y_pred, c='b') #画直线图
plt.xlabel('x')
plt.ylabel('y')
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
最小二乘法_使用Python+Numpy实现的最小二乘法.zip (1个子文件)
最小二乘法_使用Python+Numpy实现的最小二乘法
main.py 2KB
共 1 条
- 1
资源评论
__AtYou__
- 粉丝: 1570
- 资源: 409
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
- WordPress后台美化插件QuarterAdmin分享
- PCB_Project单片机绘制 (2024-5-11 22-22-13).zip
- nccl-local-repo-ubuntu2204-2.21.5-cuda12.4-1.0-1-amd64
- STM32移植LVGL源码工程 stm32移植GUI-Guider源码 LVGL移植源码
- Screenshot_20240522_084328_com.tencent.mm.jpg
- 附件计算机专业课选课说明-1.xlsx
- 基于TypeScript的ahousepet-admin-web管理系统设计源码
- 《广东开放大学学习指引》期末考核要求0522.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功