首页下载资源后端线性回归阶梯上升数据构建程序

ZIP线性回归阶梯上升数据构建程序

xuyinqi1.27KB需要积分:1

资源文件列表:

线性回归阶梯上升数据构建程序.zip 大约有1个文件
  1. 线性回归阶梯上升数据构建程序.py 3.19KB

资源介绍:

进行线性数据回归分析经常需要用到波动上升的随机数据,本程序给出了使用python构建的由线性数据+随机数据+正弦数据的波动上升数据并绘制散点图的代码和效果展示。该数据共5段100个可用于进行线性回归数据分析。
# -*- coding: utf-8 -*- #导入第三方库 import random import pandas as pd import numpy as np import matplotlib.pyplot as plt import openpyxl from openpyxl import Workbook #构造用于线性回归分析使用的波动上升随机数据并绘制散点图 #构造正弦函数(数量20个、幅值0.6)点 m = np.linspace(0, 2*np.pi, 20) n = 0.6 * np.sin(m) #构建1-20序号 numbers_x1 = list(range(1, 21)) #生成20个[(序号/20)+(±0.2)+正弦函数点]的随机数据 numbers_y1 = np.add(np.add([item / 20 for item in numbers_x1],np.random.uniform(-0.2, 0.2, 20).tolist()),n) #构建21-40序号 numbers_x2 = list(range(21, 41)) #生成20个[(序号/20)+(±0.2)+正弦函数点]的随机数据 numbers_y2 = np.add(np.add([item / 20 for item in numbers_x2],np.random.uniform(-0.2, 0.2, 20).tolist()),n) #构建41-60序号 numbers_x3 = list(range(41, 61)) #生成20个[(序号/20)+(±0.2)+正弦函数点]的随机数据 numbers_y3 = np.add(np.add([item / 20 for item in numbers_x3],np.random.uniform(-0.2, 0.2, 20).tolist()),n) #构建61-80序号 numbers_x4 = list(range(61, 81)) #生成20个[(序号/20)+(±0.2)+正弦函数点]的随机数据 numbers_y4 = np.add(np.add([item / 20 for item in numbers_x4],np.random.uniform(-0.2, 0.2, 20).tolist()),n) #构建81-100序号 numbers_x5 = list(range(81, 101)) #生成20个[(序号/20)+(±0.2)+正弦函数点]的随机数据 numbers_y5 = np.add(np.add([item / 20 for item in numbers_x5],np.random.uniform(-0.2, 0.2, 20).tolist()),n) #创建excel文件 wb = Workbook() #选择当前工作表 ws = wb.active #工作表命名 ws.title = "线性回归数据" #新建工作表并命名 ws2 = wb.create_sheet("原始数据") #表格A1写入x ws['A1'] = 'x' #表格B1写入y ws['B1'] = 'y' #将20个x值写入A列 for i, x1 in enumerate(numbers_x1, start=2): cell = 'A{}'.format(i) ws[cell] = x1 #将50个y值写入B列 for i, y1 in enumerate(numbers_y1, start=2): cell = 'B{}'.format(i) ws[cell] = y1 #将20个x值写入A列 for i, x2 in enumerate(numbers_x2, start=22): cell = 'A{}'.format(i) ws[cell] = x2 #将50个y值写入B列 for i, y2 in enumerate(numbers_y2, start=22): cell = 'B{}'.format(i) ws[cell] = y2 #将20个x值写入A列 for i, x3 in enumerate(numbers_x3, start=42): cell = 'A{}'.format(i) ws[cell] = x3 #将50个y值写入B列 for i, y3 in enumerate(numbers_y3, start=42): cell = 'B{}'.format(i) ws[cell] = y3 #将20个x值写入A列 for i, x4 in enumerate(numbers_x4, start=62): cell = 'A{}'.format(i) ws[cell] = x4 #将50个y值写入B列 for i, y4 in enumerate(numbers_y4, start=62): cell = 'B{}'.format(i) ws[cell] = y4 #将20个x值写入A列 for i, x5 in enumerate(numbers_x5, start=82): cell = 'A{}'.format(i) ws[cell] = x5 #将50个y值写入B列 for i, y5 in enumerate(numbers_y5, start=82): cell = 'B{}'.format(i) ws[cell] = y5 #保存excel wb.save('回归数据.xlsx') #读取excel数据 data = pd.read_excel('回归数据.xlsx',sheet_name='线性回归数据') #获取x列 x = data['x'] #获取y列 y = data['y'] #绘制散点图 plt.scatter(x,y,color = 'b') #绘图显示 plt.show()
100+评论
captcha