Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the L in numpy.shape and 32 in numpy.type of an array?

I'm trying the functions of numpy arrays and below is the code:

import numpy as np

Z =np.array( 
    [[0,4,0,0,0,0],
     [0,0,0,1,0,0],
     [0,1,0,1,0,0],
     [0,0,1,1,0,0],
     [0,0,0,0,0,0],
     [0,0,0,0,0,0]])
print Z
print Z.dtype
print Z.shape

Which gave:

[[0 4 0 0 0 0]
 [0 0 0 1 0 0]
 [0 1 0 1 0 0]
 [0 0 1 1 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]]
int32
(6L, 6L)

It is an integer array with 6 rows and 6 columns. But what is the 32 from numpy.type and the L in numpy.shape?

like image 408
user3211991 Avatar asked Jan 22 '14 13:01

user3211991


People also ask

What is the shape of an array in NumPy?

The shape of an array is the number of elements in each dimension. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.

What is the 32 in the NumPy array type?

It is an integer array with 6 rows and 6 columns. But what is the 32 from numpy.type and the L in numpy.shape? Show activity on this post. The 32 in the array type refers to 32-bit width of the underlying machine integer that stores the array.

What is NumPy shape 0 in Python?

Python numpy shape 0 In this section, we will discuss Python NumPy shape 0. Shape is n.shape is a tuple that always gives dimensions of the array. The shape is a tuple that gives you an indication of the no. of dimensions in the array.

How to include explicitly data types in NumPy arrays?

In numpy arrays we use the same data type, numpy arrays provide numeric data type to draw an array. But numpy provides a facility to include explicitly data types. In the above example, dtype gives the data type of the array as int64. Similarly, it is also giving float 64 as a data type in the next example of numpy.


1 Answers

The 32 in the array type refers to 32-bit width of the underlying machine integer that stores the array. This means that an array with, say, 1 million elements, will take up at least 4 million bytes (32 million bits) of memory.

On the other hand, the suffix L returned by the shape property has nothing to do with the data you can put in your arrays and you should not be concerned with it. If you are interested in the techical details, L denotes the long Python type used to represent integers of unbounded width (not to be confused with the C type of the same name). It doesn't make much sense to represent a small integer such as the number 6 as long, but some code does it anyway for consistency with the same API returning a larger integer. For example, the os.stat call always returns byte sizes in long integers, even if they would fit the regular int type, in order to maintain type invariance of its return value.

like image 126
user4815162342 Avatar answered Oct 06 '22 00:10

user4815162342