0

I'm creating a couple of arrays using numpy and list constructors, and I can't figure out why this is failing. My code is:

import numpy as np
A = np.ndarray( [i for i in range(10)] ) # works fine
B = np.ndarray( [i**2 for i in range(10)] ) # fails, with MemoryError

I've also tried just B = [i**2 for i in range(10)] which works, but I need it to be an ndarray. I don't see why the normal constructor would work but calling a function wouldn't. As far as I understand, the ndarray constructor shouldn't even see the inside of that, it should get a length 10 list with ints in it for both.

3
  • 1
    Use np.array([...]). np.ndarray is an advanced constructor with different parameters. np.array is the one we normally use. Commented Sep 17, 2016 at 19:26
  • Or use np.zeros (or ones or empty) if trying to create an array with a list of dimensions. Commented Sep 17, 2016 at 19:30
  • As @hpaulj suggests, normally you should use np.array() to create a new array from a list. The first argument of ndarray is the shape of the array, not the data to put in the array. So you are requesting an array with shape [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]. It is interesting that it generates a MemoryError. With that 0 in there, the total size of the array would actually be 0. Commented Sep 17, 2016 at 19:35

1 Answer 1

2

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html is a low-level method that we normally don't use. Its first argument is the shape.

In [98]: np.ndarray([x for x in range(3)])
Out[98]: array([], shape=(0, 1, 2), dtype=float64)
In [99]: np.ndarray([x**2 for x in range(3)])
Out[99]: array([], shape=(0, 1, 4), dtype=float64)

Normally use zeros or ones to construct a blank array of a given shape:

In [100]: np.zeros([x**2 for x in range(3)])
Out[100]: array([], shape=(0, 1, 4), dtype=float64)

Use np.array if you want to turn a list into an array:

In [101]: np.array([x for x in range(3)])
Out[101]: array([0, 1, 2])
In [102]: np.array([x**2 for x in range(3)])
Out[102]: array([0, 1, 4])

You can generate the range numbers, and then perform the math on the whole array (without iteration):

In [103]: np.arange(3)**2
Out[103]: array([0, 1, 4])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I guess I knew the type was ndarray so I assumed that would be the constructor.

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.