Data types are another important intrinsic aspect of a NumPy array alongside its memory layout and indexing. The data type of a NumPy array can be found by simply checking the dtype
attribute of the array. Try out the following examples to check the data types of different arrays:
In [49]: x = np.random.random((10,10)) In [50]: x.dtype Out[50]: dtype('float64') In [51]: x = np.array(range(10)) In [52]: x.dtype Out[52]: dtype('int32') In [53]: x = np.array(['hello', 'world']) In [54]: x.dtype Out [54]: dtype('S5')
Many array creation functions provide a default array data type. For example, the np.zeros
and np.ones
functions create arrays that are full of floats by default. But it is possible to make them create arrays of other data types too. Consider the following examples that demonstrate how to use the dtype argument to create arrays of arbitrary data types.
In [55]: x = np.ones((10, 10), dtype=np.int) In [56]: x.dtype Out[56]: dtype('int32') In [57]: x = np.zeros((10, 10), dtype='|S1') In [58]: x.dtype Out[58]: dtype('S1')
For a complete list of data types supported by NumPy, refer to http://docs.scipy.org/doc/numpy/user/basics.types.html .
3.145.125.51