[英]numpy generate data from linear function
Say I wanted to generate 100 or so data points from a linear function what's the best way to go about it? 假设我想从线性函数生成100个左右的数据点,那么最好的方法是什么?
An example linear function y = 0.4*x + 3 + delta
线性函数的示例y = 0.4*x + 3 + delta
where delta is a random value drawn from a uniform distribution between -10 and +10 其中delta是从-10和+10之间的均匀分布中抽取的随机值
I want delta to be generated for each data point to give some perturbation to the data. 我希望为每个数据点生成delta,以对数据进行一些扰动。
import numpy as np
d = np.random.uniform(-10, 10)
This seems to fit the bill for delta although I'm unsure exactly how to generate the rest incorporating this. 这似乎符合三角洲的法案,虽然我不确定如何产生其余的纳入这个。
It all rather depends on what x
values you want to evaluate your function. 这完全取决于您想要评估函数的x
值。 Assuming you want to plot from -50 to 50 just use x = np.arange(-50,50)
but then you need d = np.random.uniform(-10, 10, x.size)
. 假设你想从-50到50绘制,只需使用x = np.arange(-50,50)
但是你需要d = np.random.uniform(-10, 10, x.size)
。
Then just run your function: y = 0.4*x + 3 + delta
. 然后运行你的函数: y = 0.4*x + 3 + delta
。
On the other hand if you want a linearly spaced x
you can also use np.linspace
or for logarithmicly spaced x
: np.logspace
. 另一方面,如果你想要一个线性间隔的x
你也可以使用np.linspace
或对数间隔x
: np.logspace
。
In the end it could look like: 最后它看起来像:
x = np.linspace(0, 100, 1000) # 1000 values between 0 and 100
# x = np.arange(-50, 50) # -50, -49, ... 49, 50
delta = np.random.uniform(-10, 10, x.size)
y = 0.4*x + 3 + delta
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.