3.6. Array Data Types

  • Array can have only one data type (dtype)

  • Type can be "non-primitive" - any class

3.6.1. Signed int

  • Signed (positive and negative)

  • np.int alias for np.int64

  • np.int0 alias for np.int64 - Integer used for indexing

  • np.int8 (max: 127)

  • np.int16 (max: 32,767)

  • np.int32 (max: 2,147,483,647)

  • np.int64 (max: 184,46,744,073,709,551,615)

import numpy as np


a = np.array([1, 2, 3])

type(a)
# <class 'numpy.ndarray'>

a.dtype
# dtype('int64')
import numpy as np


a = np.array([[1., 2., 3.],
              [4., 5., 6.]])

a.astype(int)
# array([[1, 2, 3],
#        [4, 5, 6]])

a.astype(np.int8)
# array([[1, 2, 3],
#        [4, 5, 6]], dtype=int8)

a.astype(np.int64)
# array([[1, 2, 3],
#        [4, 5, 6]])

3.6.2. Unsigned int

  • Unsigned (non-negative only)

  • np.uint0

  • np.uint8

  • np.uint16

  • np.uint32

  • np.uint64

import numpy as np


a = np.array([-1, 0, 1])

type(a)
# <class 'numpy.ndarray'>

a.dtype
# dtype('int64')
import numpy as np


a = np.array([-1, 0, 1])

a.astype(int)
# array([-1, 0, 1])

a.astype(np.uint8)
# array([255, 0, 1], dtype=uint8)

a.astype(np.uint64)
# array([18446744073709551615, 0, 1], dtype=uint64)

3.6.3. float

  • np.float

  • np.float16

  • np.float32

  • np.float64

  • np.float128

import numpy as np


a = np.array([1., 2., 3.])

type(a)
# <class 'numpy.ndarray'>

a.dtype
# dtype('float64')
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a.astype(float)
# array([[1., 2., 3.],
#        [4., 5., 6.]])

a.astype(np.float16)
# array([[1., 2., 3.],
#        [4., 5., 6.]], dtype=float16)

a.astype(np.float32)
# array([[1., 2., 3.],
#        [4., 5., 6.]], dtype=float32)

a.astype(np.float64)
# array([[1., 2., 3.],
#        [4., 5., 6.]])

a.astype(np.float128)
# array([[1., 2., 3.],
#        [4., 5., 6.]], dtype=float128)

3.6.4. complex

  • np.complex

  • np.complex64

  • np.complex128

  • np.complex256

import numpy as np


a = np.array([1+2j])

a.dtype
# dtype('complex128')
import numpy as np


a = np.array([1.1+2.2j])
# array([1.1+2.2j])

a.dtype
# dtype('complex128')

3.6.5. bool

import numpy as np


a = np.array([True, False, True])

a.dtype
# dtype('bool')
import numpy as np


a = np.array([1, 0, 1], bool)

a.dtype
# dtype('bool')

repr(a)
# array([ True, False,  True])

3.6.6. str

import numpy as np


np.array(['a', 'b', 'c'])
# array(['a', 'b', 'c'], dtype='<U1')

np.array(['one', 'two', 'three'])
# array(['one', 'two', 'three'], dtype='<U5')

3.6.7. Assignments

3.6.7.1. As Type

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_astype.py

English
  1. Given a: ndarray (see below)

  2. Convert to int type

  3. The result convert to bool type

  4. What happened in each of those steps?

Polish
  1. Dany a: ndarray (patrz poniżej)

  2. Przekonwertuj do typu int

  3. Rezultat rzutuj na typ bool

  4. Co się stało w każdym z tych kroków?

Input
a = np.array([[-1.1, 0.0, 1.1],
              [ 2.2, 3.3, 4.4]])
The whys and wherefores
  • Defining np.array