import numpy as np
L = list(range(15))
a = np.array(L)
a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
type(L)
list
type(a)
numpy.ndarray
tableau=np.ones(4)
tableau
array([1., 1., 1., 1.])
np.ones(shape=4)
array([1., 1., 1., 1.])
tableau[2]=5
tableau
array([1., 1., 5., 1.])
matrice=np.ones((3,2))
matrice
array([[1., 1.], [1., 1.], [1., 1.]])
matrice[1][1]=5
matrice[1, 0]=9
matrice
array([[1., 1.], [9., 5.], [1., 1.]])
cube=np.ones((4,3,2))
cube
array([[[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]]])
cube[1][1][1]=5
cube[1, 1, 0]=9
cube
array([[[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [9., 5.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]]])
np.sctypes
{'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int64], 'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64], 'float': [numpy.float16, numpy.float32, numpy.float64, numpy.float128], 'complex': [numpy.complex64, numpy.complex128, numpy.complex256], 'others': [bool, object, bytes, str, numpy.void]}
tab=np.array([1, 5, 100], dtype=np.int8)
tab
array([ 1, 5, 100], dtype=int8)
tab.dtype
dtype('int8')
tab.itemsize
1
tab=np.array([1, 5.5, 128], dtype=np.int8)
tab
array([ 1, 5, -128], dtype=int8)
type(np.nan)
float
# instruction qui bogue
# tab=np.array([1, 5.5, np.nan], dtype=np.int8)
tab
array([ 1, 5, -128], dtype=int8)
tab=np.array([1, 5, np.nan], dtype=np.float16)
tab
array([ 1., 5., nan], dtype=float16)
np.sctypes['others']
[bool, object, bytes, str, numpy.void]
tab = np.array(['spam', 'bean'], dtype=str)
tab
array(['spam', 'bean'], dtype='<U4')
tab[0].dtype
dtype('<U4')
tab.nbytes
32
tab2 = np.array(['spam', 'beans'], dtype=str)
tab2
array(['spam', 'beans'], dtype='<U5')
tab2[0].dtype
dtype('<U4')
tab2[0].nbytes
16
tab2[1].dtype
dtype('<U5')
tab2.nbytes
40
tab3 = np.array(['spam', 'beans'], dtype=(str, 3))
tab3
array(['spa', 'bea'], dtype='<U3')
tab3[0].dtype
dtype('<U3')
tab3[1].dtype
dtype('<U3')
tab3.nbytes
24