如何将抖动添加到具有X和Y值的散点图?

问题描述 投票:1回答:1

我已经创建了随机数据,并试图在散点图中添加抖动,但是我不知道如何为X和Y值应用抖动?我有X和Y形式的数据,但没有完整的数据将其传递给seaborn绘图库。

def make_cubic_dataset(m, a=-3.0, b=1.0, c=3.5, d=4, mu=0.0, sigma=0.33):

    x = np.random.uniform(low=-1.0, high=1.0, size=(m,))   
    y =  a*x**3 + b*x**2 + c*x + d + np.random.normal(mu,sigma)
    #generates a random number from the normal distribution with mu and sigma.
    return (x,y)

np.random.seed(42)
x,y = make_cubic_dataset(100)

print(x.shape)
print(y.shape)
print(x[:5])
print(y[:5])

plt.scatter(x, y)
plt.title("Random Artificial Cubic dataset")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

输出:

enter image description here

预期输出

enter image description here

有人可以帮我吗?

python python-3.x matplotlib linear-regression jitter
1个回答
0
投票

您正在向整个y变量添加一个标量随机量,而不是一个随机分布的数字数组。以下将产生具有标准偏差sigma的随机数的正态分布数组:

y =  a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.