Num Py
Num Py
Num Py
25 de noviembre de 2020
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
Desde pip
• Alternativa libre
[https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html]
o tambien...
1 X = [ cc * 9 / 5 + 32 f o r cc i n c v a l u e s ]
2 print (X)
1 def l i b r e r i a ( ) :
2 t1 = time . time ( )
3 X = np . arange ( s i z e o f v e c )
4 Y = np . arange ( s i z e o f v e c )
5 Z = X + Y
6 return time . time ( ) − t1
0.1024632453918457 0.0010013580322265625
Numpy en este ejemplo es 102.32428571428571 más rápido!
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
<class ’numpy.ndarray’>
Ejecutando:
int32
float64
1
1
*¿2?*
1 B = np . a r r a y ( [ [ [ 1 1 1 , 1 1 2 ] , [ 1 2 1 , 1 2 2 ] ] ,
2 [[211 , 212] , [221 , 222]] ,
3 [[311 , 312] , [321 , 322]] ] )
4 print (B)
5 p r i n t ( B . ndim )
[[[111 112]
[121 122]]
[[211 212]
[221 222]]
[[311 312]
[321 322]]]
3
¿Qué pasa?
(6, 3)
(6, 3)
[[67 63 87 77 69 59]
[85 87 99 79 72 71]
[63 89 93 68 92 78]]
[[67 63 87 77 69 59 85 87 99]
[79 72 71 63 89 93 68 92 78]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-22-ce5e4e78d80b> in <module>
----> 1 x.shape = (4, 4)
Más ejemplos:
()
En una multidimensional
1 B = np . a r r a y ( [ [ [ 1 1 1 , 1 1 2 ] , [ 1 2 1 , 1 2 2 ] ] ,
2 [[211 , 212] , [221 , 222]] ,
3 [[311 , 312] , [321 , 322]] ] )
4 p r i n t ( B . shape )
(3, 2, 2)
Hay otras funciones que nos sirven para crear arrays éspeciales’.
[[1. 1. 1.]
[1. 1. 1.]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[1 1 1 1 1]
1 Z = np . z e r o s l i k e ( x )
2 print (Z)
[[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]]
• np.identity
• np.eye
Parámetros:
Ejemplo de la Matriz:
1 np . i d e n t i t y ( 4 )
1 # e q u i v a l e n t e a np . i d e n t i t y ( 4 , i n t )
2 np . i d e n t i t y ( 4 , dtype= i n t )
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
Ejemplo:
1 np . eye ( 5 , 8 , k =1 , dtype = i n t )
array([[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0]])
1 np . l i n s p a c e ( 0 , 2 , 9 )
1 x = np . l i n s p a c e ( 0 , 2 * np . p i , 100 )
2 f = np . s i n ( x )
Representamos f:
1 import m a t p l o t l i b . p y p l o t as p l t
2 %m a t p l o t l i b i n l i n e
3 plt . plot ( f )
4 p l t . show ( )
• zeros like(array),
• ones like(array),
• empty like(array)
random sample([size]),
ranf([size]),
sample([size]),
bytes(length)
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
21
1 B = np . a r r a y ( [ [ [ 1 1 1 , 1 1 2 ] , [ 1 2 1 , 1 2 2 ] ] ,
2 [[211 , 212] , [221 , 222]] ,
3 [[311 , 312] , [321 , 322]] ] )
4 print (B [ 0 ] [ 1 ] [ 0 ] )
121
1 A = np . a r r a y ( [ [ 3 . 4 , 8 . 7 , 9 . 9 ] ,
2 [ 1 . 1 , −7.8 , − 0 . 7 ] ,
3 [ 4 . 1 , 12.3 , 4 . 8 ] ] )
4 print (A [ 1 ] [ 0 ] )
1.1
1.1
Algunos ejemplos:
1 S = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
2 print (S [ 2 : 5 ] )
3 print (S [ : 4 ] )
4 print (S [ 6 : ] )
5 print (S [ : ] )
[2 3 4]
[0 1 2 3]
[6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
En multidimensional:
1 A = np . a r r a y ( [
2 [11 ,12 ,13 ,14 ,15] ,
3 [21 ,22 ,23 ,24 ,25] ,
4 [31 ,32 ,33 ,34 ,35] ,
5 [41 ,42 ,43 ,44 ,45] ,
6 [51 ,52 ,53 ,54 ,55]])
7
8 print (A [ : 3 , 2 : ] )
[[13 14 15]
[23 24 25]
[33 34 35]]
Más:
1 print (A [ 3 : , : ] )
[[41 42 43 44 45]
[51 52 53 54 55]]
1 print (A [ : , 4 : ] )
[[15]
[25]
[35]
[45]
[55]]
[[ 0 1 2 3 4 5 6]
[ 7 8 9 10 11 12 13]
[14 15 16 17 18 19 20]
[21 22 23 24 25 26 27]]
[[ 0 3 6]
[14 17 20]]
1 A = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
2 S = A[2:6]
3
4 S [ 0 ] = 22
5 S [ 1 ] = 23
6
7 p r i n t (A )
[ 0 1 22 23 4 5 6 7 8 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
True
[[42 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
False
False
**DeprecationWarning**
[42 1 2 3 4 5 6 7 8 9 10 11]
[[42 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
[[ 6. 8.]
[10. 12.]]
1 p r i n t ( np . add ( x , y ) )
[[ 6. 8.]
[ 10. 12.]]
1 print ( x − y )
2 p r i n t ( np . s u b t r a c t ( x , y ) )
[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]
[[ 5. 12.]
[ 21. 32.]]
[[ 5. 12.]
[ 21. 32.]]
219
219
Matriz x vector
1 print ( x . dot ( v ) )
[29. 67.]
1 print ( x . dot ( y ) )
[[19. 22.]
[43. 50.]]
1 print ( x / y )
2 p r i n t ( np . d i v i d e ( x , y ) )
[[0.2 0.33333333]
[0.42857143 0.5 ]]
[[0.2 0.33333333]
[0.42857143 0.5 ]]
1 p r i n t ( np . s q r t ( x ) )
[[1. 1.41421356]
[1.73205081 2. ]]
[[1 2]
[3 4]]
[[1 3]
[2 4]]
[1 2 3]
10
1 p r i n t ( np .sum( x4 , a x i s = 0 ) ) # En v e r t i c a l
[4 6]
1 p r i n t ( np .sum( x4 , a x i s = 1 ) ) # En h o r i z o n t a l
[3 7]
Calculamos su histograma
1 ( n , b i n s ) = np . h i s t o g r a m ( v , b i n s =50)
Ahora a pintarlo
1 p l t . p l o t ( . 5 * ( bins [ 1 : ] + bins [ : − 1 ] ) , n )
2 p l t . show ( )
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
Vamos a ver cómo usarlo y como evitar hacer uso de él cuando
no conviene
Esto requiere que los dos arrays tengan el mismo tamaño (forma)
1 a = np . a r r a y ( [ 1 . 0 , 2 . 0 , 3 . 0 ] )
2 b = np . a r r a y ( [ 2 . 0 , 2 . 0 , 2 . 0 ] )
3 a * b
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS
Estos ejemplos:
1 a = np . a r r a y ( [ 2 0 , 3 0 , 4 0 , 5 0 ] )
2 10 * np . s i n ( a )
1 a<35
array([[3, 3, 3],
[3, 3, 3]])
1 b += a
2 b
1 # a += b
2 a = a + b
3 a
1 p r i n t ( a .sum ( ) )
2 p r i n t ( a . min ( ) )
3 p r i n t ( a . max ( ) )
38.263973832073674
6.185226677768423
6.568984400231365
1 b = np . arange ( 1 2 ) . reshape ( 3 , 4 )
2 b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
array([0, 4, 8])
array([[ 0, 1, 3, 6],
[ 4, 9, 15, 22],
[ 8, 17, 27, 38]], dtype=int32)
Aplanar un array
1 a . ravel ()
Traspuesta
1 a . t r a n s p o se ( )
array([[9.45601371, 9.74299607],
[9.20746186, 9.3790752 ],
[9.57922178, 9.66107905]])
1 >>> a . T
3x+y=9
x+2y=8
1 mat = np . a r r a y ( [ [ 3 , 1 ] , [ 1 , 2 ] ] )
2 vec = np . a r r a y ( [ 9 , 8 ] )
3 p r i n t ( np . l i n a l g . s o l v e ( mat , vec ) )
[2. 3.]
Es clase de NumPy
matrix([[1., 2.],
[3., 4.]])
1 type ( A )
numpy.matrix
Transpuesta
1 A.T
matrix([[1., 3.],
[2., 4.]])
Multiplicación de matrices
1 A * A.T
Inversa
1 A. I
matrix([[-2. , 1. ],
[ 1.5, -0.5]])
1 p r i n t ( np . l i n a l g . s o l v e ( mat , vec ) )
[2. 3.]
(Universidad Internacional de Andalucı́a) Big Data I 25 de noviembre de 2020 110 / 122
Scipy
SciPy está dirigido a tareas numéricas de más alto nivel. Por ejemplo:
Integrales numéricas
Ecuaciones diferenciales,
Optimización
y Matrices en general
Creación e Impresión
1 p = np . poly1d ( [ 3 , 4 , 5 ] )
2 print (p)
2
3 x + 4 x + 5
Evaluación
1 print (p ( 4 ) )
2 print (p ([4 , 5 ] ) )
69
[ 69 100]
Integración
1 print ( p . integ ( k =6))
3 2
1 x + 2 x + 5 x + 6
Derivación
1 print (p . deriv ( ) )
6 x + 4
1 Introducción
2 Arrays
3 Índices
4 Funciones
5 Broadcasting
7 RETOS