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

利用sklearn 实现线性回归、非线性回归

代码:

import pandas as pd
import numpy as np
import matplotlib
import random
from matplotlib import pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression# 创建虚拟数据
x = np.array(range(30))
temp_y = 10 + 2 * x + x ** 2 + x ** 3
y = temp_y + 1500 * np.random.normal(size=30)  # 添加噪声
x = x.reshape(30, 1)
y = y.reshape(30, 1)# 线性回归
clf1 = LinearRegression()
clf1.fit(x, y)
y_l = clf1.predict(x)  # 线性回归预测值# 非线性回归
ployfeat = PolynomialFeatures(degree=3)  # 根据degree的值转换为相应的多项式(非线性回归)
x_p = ployfeat.fit_transform(x)
clf2 = LinearRegression()
clf2.fit(x_p, y)print("线性回归方程为: y = {} + {}x".format(clf1.intercept_[0],clf1.coef_[0,0]))
print("非线性回归曲线方程为 y = {}+{}x+{}x^2+{}x^3".format(clf2.intercept_[0],clf2.coef_[0,1],clf2.coef_[0,2],clf2.coef_[0,3]))def f(x):return clf2.intercept_[0]+clf2.coef_[0,1]*(x**1)+clf2.coef_[0,2]*(x**2)+clf2.coef_[0,3]*(x**3)
font={"family":"FangSong",'size':12}
matplotlib.rc("font",**font)plt.plot(x, clf2.predict(x_p),label="非线性回归_调用预测")
plt.plot(x, f(x),label="非线性回归_写函数拟合预测")
plt.plot(x, y_l,label="线性回归")
plt.scatter(x, y,label="数据")
plt.legend()
plt.show()

http://www.lryc.cn/news/104066.html

相关文章:

  • Java课题笔记~ MyBatis入门
  • Activity的自启动模式
  • 53数组的扩展
  • Rust调试【三】
  • uniApp 对接安卓平板刷卡器, 读取串口数据
  • Go new 与 make
  • centos系统离线安装k8s v1.23.9最后一个版本并部署服务,docker支持的最后一个版本
  • (学习笔记-内存管理)如何避免预读失效和缓存污染的问题?
  • 【arthas】入门与实战(一)
  • vim、awk、tail、grep的使用
  • vue拖拽改变宽度
  • 华为数通HCIA-ARP(地址解析协议)详细解析
  • 【Python机器学习】实验04(1) 多分类(基于逻辑回归)实践
  • 【ChatGLM_01】ChatGLM2-6B本地安装与部署(大语言模型)
  • 谷歌Tsunami(海啸)扫描器搭建扩展使用教程
  • 诚迈科技承办大同首届信息技术产业峰会,共话数字经济崭新未来
  • 【Python】Python使用TK实现动态爱心效果
  • Unity3d C#快速打开萤石云监控视频流(ezopen)支持WebGL平台,替代UMP播放视频流的方案(含源码)
  • 【Android】APP启动优化学习笔记
  • docker的使用
  • iOS使用Rust调研
  • 抖音引流推广的几个方法,抖音全自动引流脚本软件详细使用教学
  • k8s概念-DaemonSet
  • Mac 终端快捷键设置:如何给 Mac 中的 Terminal 设置 Ctrl+Alt+T 快捷键快速启动
  • VR 变电站事故追忆反演——正泰电力携手图扑
  • fpga开发——蜂鸣器
  • 【Liux下6818开发板(ARM)】触摸屏
  • 苍穹外卖day11——数据统计图形报表(Apache ECharts)
  • 在制作PC端Game Launcher游戏启动器时涉及到的技术选型
  • SQL力扣练习(九)