Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy array of python objects

Since when did numpy allow you to define an array of python objects? Objects array with numpy.

Is there any fundamental difference between these arrays and a python list?

What is the difference between these arrays and say, a python tuple?

There are several handy numpy functions I would like to use, i.e. masks and element-wise operations, on an array of python objects and I would like to use them in my analysis, but I'm worried about using a feature I can't find documentation for anywhere. Is there any documentation for this 'object' datatype?

Was this feature was added in preparation for merging numpy into the standard library?

like image 390
user545424 Avatar asked May 26 '11 16:05

user545424


People also ask

How do you call an array of objects in Python?

An array element can be accessed by indexing, similar to a list i.e. by using the location where that element is stored in the array. The index is enclosed within square brackets [ ], the first element is at index 0, next at index 1 and so on.

Can NumPy array store images?

Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format.

What is NumPy Python array?

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.


1 Answers

The "fundamental" difference is that a Numpy array is fixed-size, while a Python list is a dynamic array.

>>> class Foo:
...  pass
... 
>>> x = numpy.array([Foo(), Foo()])
>>> x.append(Foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'

(You can get around this with numpy.concatenate, but still Numpy arrays aren't meant as a drop-in replacement for list.)

Arrays of object are perfectly well documented, but be aware that you'll have to pass dtype=object sometimes:

>>> numpy.array(['hello', 'world!'])
array(['hello', 'world!'], 
      dtype='|S6')
>>> numpy.array(['hello', 'world!'], dtype=object)
array(['hello', 'world!'], dtype=object)
like image 98
Fred Foo Avatar answered Oct 16 '22 13:10

Fred Foo