# 2.2. Array Data Types¶

## 2.2.1. Rationale¶

• Array can have only one data type (dtype)

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

## 2.2.2. Bits and Bytes¶

• Signed and unsigned

• Unsigned cannot be negative

• For negative signed numbers "Two's complement" is used

1       # unsigned

+1      # signed
-1      # signed


3 bit unsigned integers. Values: 8, minimal: 0, maximal: 8:

0       000
1       001
2       010
3       011
4       100
5       101
6       110
7       111


3 bit signed integers. Values: 8, minimal: -4, maximal: 3:

+0      000
+1      001
+2      010
+3      011
−4      100
−3      101
−2      110
−1      111


8 bit signed integers. Values: 256, minimal: -128, maximal: 127:

+0      00000000
+1      00000001
+2      00000010
+126    01111110
+127    01111111
−128    10000000
−127    10000001
−126    10000010
−2      11111110
−1      11111111


32 bit unsigned int. Values: 2,147,483,647, minimal: 0, maximal: 2,147,483,647:

0       0000000000000000000000000000000000
1       0000000000000000000000000000000001
2       0000000000000000000000000000000010
3       0000000000000000000000000000000011
4       0000000000000000000000000000000100
5       0000000000000000000000000000000101
6       0000000000000000000000000000000110
7       0000000000000000000000000000000111


Calculates a two's complement integer from the given input value's bits:

def twos_complement(value: int, num_bits: int) -> int:
mask = 2 ** (num_bits - 1)

# decimal
69

# np.int8
01000101

# np.int16
00000000 01000101

# np.int32
00000000 00000000 00000000 01000101

# np.int64
00000000 00000000 00000000 00000000 00000000 00000000 00000000 01000101


## 2.2.3. Signed int¶

• Signed (positive and negative)

• np.int alias for np.int64

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

• np.int8

• np.int16

• np.int32

• np.int64

Table 2.1. Number of values is calculated with 2 ** bytes

Type

Bits

Number of Values

Minimal

Maximal

np.int8

8

256

-128

127

np.int16

16

65,536

-32,768

32,767

np.int32

32

4,294,967,296

-2,147,483,648

2,147,483,646

np.int64

64

18,446,744,073,709,551,616

-9,223,372,036,854,775,808

9,223,372,036,854,775,807

import numpy as np

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

type(a)
# <class 'numpy.np.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]])


## 2.2.4. Unsigned int¶

• Unsigned (non-negative only)

• np.uint0

• np.uint8

• np.uint16

• np.uint32

• np.uint64

Table 2.2. Number of values is calculated with 2 ** bytes

Type

Bits

Number of Values

Minimal

Maximal

np.uint8

8

256

0

255

np.uint16

16

65,536

0

65,535

np.uint32

32

4,294,967,296

0

4,294,967,295

np.uint64

64

18,446,744,073,709,551,616

0

18,446,744,073,709,551,615

import numpy as np

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

type(a)
# <class 'numpy.np.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)


## 2.2.5. float¶

• np.float

• np.float16

• np.float32

• np.float64

• np.float128

Table 2.3. Number of values is calculated with 2 ** bytes

Type

Bits

Minimal

Maximal

np.float16

16

-65,504

65,504

np.float32

32

±0.000000×10−95

±9.999999×1096

np.float64

64

±0.000000000000000×10−383

±9.999999999999999×10384

np.float128

64

±0.000000000000000000000000000000000×10−6143

±9.999999999999999999999999999999999×106144

import numpy as np

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

type(a)
# <class 'numpy.np.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)


## 2.2.6. 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')


## 2.2.7. 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])


## 2.2.8. 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')


## 2.2.9. Assignments¶

"""
* Assignment: Numpy Dtype Astype
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
1. Given DATA: np.ndarray (see below)
2. Convert to int and save result as result_int
3. Convert to bool and save result as result_bool
4. What happened in each of those steps?
5. Run doctests - all must succeed

Polish:
1. Dany DATA: np.ndarray (patrz sekcja input)
2. Przekonwertuj do typu int i wynik zapisz jako result_int
3. Przekonwertuj do typu bool i wynik zapisz jako result_bool
4. Co się stało w każdym z tych kroków?
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> type(result_int) is np.ndarray
True
>>> type(result_bool) is np.ndarray
True
>>> result_int
array([[-1,  0,  1],
[ 2,  3,  4]])
>>> result_bool
array([[ True, False,  True],
[ True,  True,  True]])
"""

import numpy as np

DATA = np.array([[-1.1, 0.0, 1.1],
[2.2, 3.3, 4.4]])

result_int = ...
result_bool = ...