8

I have x0 which is a float64 (64,64) array. Whenever I try this:

    delta = np.random.randn(x0.shape)

It gives the captioned error. This is so basic that I'm wrapping my heads around it. What am I missing out? Thanks

The complete traceback is as follows:

Traceback (most recent call last):

  File "<ipython-input-31-dcd2365ed519>", line 1, in <module>
    delta = np.random.randn(x0.shape)

  File "mtrand.pyx", line 1420, in mtrand.RandomState.randn

  File "mtrand.pyx", line 1550, in mtrand.RandomState.standard_normal

  File "mtrand.pyx", line 167, in mtrand.cont0_array

TypeError: 'tuple' object cannot be interpreted as an integer
5
  • what is type(x0.shape)? Commented Jun 28, 2018 at 17:17
  • @MoxieBall: It's a tuple but I'm wondering why. Commented Jun 28, 2018 at 17:19
  • Why are you wondering? Doesn't your code create this object? In any event, perhaps you can use the unpacking operator *: np.random.randn(*x0.shape) Commented Jun 28, 2018 at 17:20
  • @JohnColeman: Shouldn't it return an array type? Commented Jun 28, 2018 at 17:23
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Commented Jun 28, 2018 at 17:36

3 Answers 3

10

np.random.randn() requires integer arguments, in the form randn(64,64). You are giving np.random.randn() arguments in the form randn((64,64)), which it is not expecting. Instead, if you want to build a 64x64 random array, you will need to pass the number of rows and columns individually, not as a tuple.

Sign up to request clarification or add additional context in comments.

Comments

7

It looks like np.random.randn() expects an integer instead of a tuples: https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html try np.random.randn(x0.shape[0], x0.shape[1])

2 Comments

Thanks, this solved the problem but it should be flexible enough to understand the difference, I guess?
or np.random.randn(*x0.shape)
2

To debug this, I recommend including these line before you try to define delta:

print(x0)
print(type(x0))

x0 is a tuple, but you're trying to use it in the np.random.randn function, which only accepts integers as arguments. If you want a multidimensional array of random numbers (for example, a 3x7x6 array), you can use np.random.randn(3, 7, 6).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.