dimanche 21 mars 2021

Python pairing lists and drawing samples

I have two lists, x and y, each 2000 elements long. They are plotted together in a scatterplot. I want to join the two lists together into a new list, pairing each x[i] with each y[i] so that I can print out a list that looks like (x[i], y[i]). Then, I want to randomly draw n samples from the new list, with replacement, and plot those samples on the same graph as the scatterplot. Here is what I have so far.

N = 2000
n = 35

x = (np.random.randn(N)/N)
y = x + sigma*(np.random.randn(N))

z = np.random.choice(len(x), size=n, replace=True)
pairs = [(x[i], y[i]) for i in z]

print(z)

plt.plot(x,y,'.')
plt.grid()

Am I on the right track or is there a better way to do this?

After some searching I found a new method that seems to work, but it makes the graph look really strange.

N = 2000
n = 35

x = (np.random.randn(N)/N)
y = x + (np.random.randn(N))
z = [[x, y] for x, y in zip(x, y)]

p = (random.choices(z, k=n))
print(p)

plt.plot(x,y,'.')
plt.plot(p,'.')
plt.grid()

All the dots from the x,y plot are pushed to the side while the dots from the p plot look more like a regular graph. Except that there is also a straight line of dots across the bottom of the graph. I have no idea what the heck is going on. Why is the p graph not plotted in the same area as the x,y plots? What is that straight line of dots across the bottom about?




Aucun commentaire:

Enregistrer un commentaire