import random
import numpy as np
class faker_xy_data:
def __init__(self):
self.N = None
self.a = None
self.b = None
self.x_plus = None
# 设置 直线 y= ax +b
self.data = []
def build(self):
self.a = random.uniform(1, 2)
# 系数 a
self.b = random.uniform(10, 20)
# 系数 B
self.x_plus = 0
# x 的起始点
self.N = 100
# 生成数据点个数
for i in range(self.N):
self.next_x()
self.data.append(self.offset())
return self.data
def next_x(self):
'''
生成下一个 x的坐标
基本加数 为 1
偏移量 符合正太分布
'''
self.x_plus += np.random.normal(loc=1)
return self.x_plus
def real_xy(self):
x = self.x_plus
y = self.x_plus * self.a + self.b
return x, y
def offset(self):
"""
对真实的数据点 进行偏移
偏移量符合正态分布
"""
x, y = self.real_xy()
x_x = round(x + np.random.normal(0), 2)
y_y = round(y + np.random.normal(0), 2)
# round 保留两位小数
return x_x, y_y
x1 = faker_xy_data()
data = x1.build()
print(f"A={x1.a} B={x1.b}")
for i in data:
print(i)
生成的一组数据
A=1.7552930636605855 B=15.899836050171853
(1.68, 19.41)
(0.6, 18.83)
(2.89, 21.98)
(3.42, 22.49)
(4.87, 24.07)
(4.24, 25.84)
(6.31, 28.13)
(5.09, 28.73)
(5.6, 27.59)
(6.92, 27.59)
(9.55, 32.1)
(8.18, 32.37)
(12.13, 34.53)
(11.96, 37.34)
(11.02, 40.05)
(15.64, 39.3)
(13.13, 42.11)
(12.4, 40.63)
(16.3, 45.86)
(18.02, 48.05)
(20.92, 47.55)
(18.28, 49.61)
(19.22, 50.23)
(18.89, 51.33)
(18.6, 49.89)
(23.12, 55.21)
(22.74, 56.94)
(23.77, 56.32)
(25.91, 56.26)
(23.32, 59.6)
(26.61, 64.1)
(27.69, 65.48)
(29.15, 65.07)
(28.18, 64.37)
(32.31, 71.24)
(31.76, 73.35)
(32.27, 73.61)
(31.78, 71.86)
(33.23, 70.76)
(30.13, 71.55)
(37.37, 79.89)
(37.58, 81.51)
(38.65, 83.05)
(40.29, 83.85)
(38.07, 83.49)
(42.54, 88.97)
(43.55, 92.71)
(45.46, 93.95)
(46.1, 94.23)
(46.96, 97.61)
(47.35, 99.99)
(46.11, 97.57)
(47.06, 98.88)
(49.24, 102.27)
(51.11, 104.28)
(52.38, 108.75)
(52.82, 110.42)
(55.15, 114.03)
(58.04, 114.65)
(58.04, 119.35)
(58.76, 119.67)
(59.91, 119.15)
(60.59, 122.56)
(60.65, 122.45)
(62.99, 127.08)
(62.91, 126.81)
(64.65, 128.88)
(63.69, 131.39)
(66.52, 134.73)
(70.09, 137.28)
(72.03, 143.61)
(73.53, 145.03)
(72.33, 144.18)
(75.2, 148.04)
(77.52, 150.7)
(77.71, 151.8)
(76.98, 152.45)
(80.9, 155.71)
(81.21, 158.6)
(83.51, 161.62)
(85.97, 166.23)
(88.42, 169.79)
(88.94, 172.16)
(90.12, 176.91)
(93.3, 178.24)
(93.0, 181.51)
(93.31, 179.74)
(94.07, 182.65)
(96.2, 187.75)
(97.03, 187.6)
(98.66, 188.96)
(98.26, 190.44)
(99.9, 192.58)
(101.17, 195.9)
(103.86, 196.18)
(104.58, 200.77)
(106.58, 202.77)
(109.03, 208.29)
(109.71, 211.88)
(108.8, 206.42)
标签:return,Python,self,random,偏移量,plus,拟合,data,def
From: https://www.cnblogs.com/boran/p/16757677.html