Search code examples
pythonrandomsamplepairing

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?


Solution

  • You may use zip twice

    • to pair x and y to be able to use random.choices (i'd suggest random.sample to get uniques)
    • to pair in the other way all values to get a sample_x and sample_y

    And use plt.scatter


    N = 2000
    n = 35
    x = np.random.randn(N) / N
    y = x + np.random.randn(N)
    
    # plot all
    plt.scatter(x, y, marker='.')
    
    # generate and plot sample
    z = list(zip(x, y))
    p = random.choices(z, k=100)
    sample_x, sample_y = zip(*p)
    plt.scatter(sample_x, sample_y, marker='+')
    
    plt.grid()
    plt.show()