What does dtype=object mean while creating a numpy array?

后端 未结 1 1783
甜味超标
甜味超标 2020-12-02 19:06

I was experimenting with numpy arrays and created a numpy array of strings:

ar1 = np.array([\'avinash\', \'jay\'])

As I have read from from

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 19:38

    NumPy arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memory are interpreted as values with that datatype.

    Creating an array with dtype=object is different. The memory taken by the array now is filled with pointers to Python objects which are being stored elsewhere in memory (much like a Python list is really just a list of pointers to objects, not the objects themselves).

    Arithmetic operators such as * don't work with arrays such as ar1 which have a string_ datatype (there are special functions instead - see below). NumPy is just treating the bits in memory as characters and the * operator doesn't make sense here. However, the line

    np.array(['avinash','jay'], dtype=object) * 2
    

    works because now the array is an array of (pointers to) Python strings. The * operator is well defined for these Python string objects. New Python strings are created in memory and a new object array with references to the new strings is returned.


    If you have an array with string_ or unicode_ dtype and want to repeat each string, you can use np.char.multiply:

    In [52]: np.char.multiply(ar1, 2)
    Out[52]: array(['avinashavinash', 'jayjay'], 
          dtype='

    NumPy has many other vectorised string methods too.

    0 讨论(0)
提交回复
热议问题