3

I have created random data and trying to add jitter in scatter plot but I can't figure out how to apply jitter for the X and Y values? I have data in the form of X and Y but not the whole as a data to pass it to the seaborn plotting library.

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()

Output:

enter image description here

Expected output

enter image description here

Can anyone help me with this?

Mayur Potdar
  • 451
  • 1
  • 8
  • 18

1 Answers1

3

You're adding a single scalar random amount to your entire y variable, rather than an array of randomly distributed numbers. The following will produce a normally distributed array of random numbers with a standard deviation sigma:

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

Result:

enter image description here

Nick Martin
  • 731
  • 3
  • 17
  • Thanks for your help. I'm trying to calculate `Intercept`, `slop`, and `Determination coefficient` but because of that change in code, I can't perform any operation. The error is `Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.` – Mayur Potdar Oct 20 '19 at 21:20
  • @MayurPotdar Please accept this answer and ask a new question with your new error. – Nick Martin Oct 20 '19 at 21:23
  • can you please tell me how to add that straight line to the given scatter plot? – Mayur Potdar Oct 20 '19 at 21:33
  • Just add this under your `plt.scatter`: `plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))`. – Nick Martin Oct 20 '19 at 21:38
  • Much appreciated. This helps me to understand the plot. And I was able to resolve the above-mentioned comment. Thanks again. – Mayur Potdar Oct 20 '19 at 21:44