简体   繁体   中英

Lack of randomness in numpy.random

Although the input data I'm working is randomly generated, when I used matplotlib to graph it, I got only a few different distinct points! I used the expression

[[numpy.random.randint(0,20) + numpy.random.random() for i in xrange(100)] for j in xrange(2)]

to generate the data I expected something that would resemble a surface. Also, I did not add any randomness to the output, as I wanted to ensure that the fit worked before I did.

The outputs are also suspicious, as they should have been generated with the equation

z = 112x/2 + 2^.15y + 109

Any help would be appreciated.

Here are some views of the plot:

数字数字数字

There's nothing wrong with my numpy. It would be good if you shared your code, as the error is probably there, since the following works just fine:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

x, y = np.array([[np.random.randint(0,20) + np.random.random()
                  for i in xrange(100)] for j in xrange(2)])
z = 112*x/2 + 2**.15*y + 109

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

在此输入图像描述

As other have noted, the right way of generating your numbers would be:

x, y = np.random.rand(2, 100) * 20

or even

x, y = np.random.randint(20, size=(2, 100)) + np.random.rand(2, 100)

but that has no effect on the outcome.

This distribution looks OK to me. It doesn't look like a surface because you're not generating real numbers, just integers, which is why those "bars" are being formed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM