Librería Numpy

La librería numpy ofrece funciones eficientes para la manipulación y el procesamiento numérico en arrays.

Este tipo de estructuras de almacenamiento númerico no son exactamente listas aunque puedan aparentar ese comportamiento (duck-style). Sus elementos son homogéneos e incluyen operaciones básicas y operaciones más complejas como álgebra lineal.

Numpy forma parte del core de otras librerías como Pandas.

https://numpy.org/doc/stable/index.html

[2]:
%pip freeze | grep numpy
numpy==1.22.3
Note: you may need to restart the kernel to use updated packages.
[2]:
import numpy as np

data = np.array([1,0])
print(data[0])
print(type(data[0]))

data = np.array([[1,0],[2,0],[3,0]])
print(data[0])
print(data[1:2])
print(data[0:,0])
1
<class 'numpy.int64'>
[1 0]
[[2 0]]
[1 2 3]
[6]:
print(data.shape)
print(data.size)
print(data.ndim)
print(data.dtype)
(3, 2)
6
2
int64

Tipos de datos soportados:

  • int: int8, int16, int32, int64

  • uint: uint8, uint16, uint32, uint64

  • bool: Bool

  • float: float16, float32, float64, float128

  • complex: complex64, complex128, complex256

[11]:
data = np.array([[1,0],[2,0]],dtype=np.int32)
data = np.array([[1,0],[2,0]],dtype=np.complex64)
data = np.array([[1,0],[2,0]],dtype=np.float16)
# Conversión
data = np.array(data,dtype=np.uint)
data = data.astype(np.bool_)
print(data)
[[ True False]
 [ True False]]

Generación

No todo es cargar valores. A veces es necesario generar una muestra de puntos aleartoria, un vector neutro, o escala de valores.

[15]:
data = np.array(range(10))
print(data)

data = np.arange(10)
print(data)

data = np.zeros(10)
# https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
print(data)

data = np.ones(10)
print(data)
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[16]:
shape = (10,80) # 10 rows x 80 cols
data = np.zeros(shape)
print(data)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0.]]
[19]:
data = np.linspace(0,1,10)
print(data)
[0.         0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
 0.66666667 0.77777778 0.88888889 1.        ]
[21]:
data = np.logspace(0,2,10) # 10puntos entre 2**0 y 2**2
print(data)
[  1.           1.66810054   2.7825594    4.64158883   7.74263683
  12.91549665  21.5443469   35.93813664  59.94842503 100.        ]
[117]:
data = np.identity(3)
print(data)
print("-"*10)

data = np.eye(3,k=1) # https://numpy.org/doc/stable/reference/generated/numpy.eye.html
print(data)
print("-"*10)

data = np.diag(range(1,4))
print(data)

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
----------
[[0. 1. 0.]
 [0. 0. 1.]
 [0. 0. 0.]]
----------
[[1 0 0]
 [0 2 0]
 [0 0 3]]

Generación o sampling aleatorio

[25]:
data = np.random.rand(3,3)
print(data)
print("-"*20)

data = np.random.randint(1,10,size=(2,2))
print(data)
print("-"*20)

data = np.random.randint(1,10,20).reshape(10,2)
print(data)
print("-"*20)

data = np.array(np.random.rand(20)*10,dtype=int).reshape(10,2)
print(data)
[[0.76201304 0.64290614 0.79994374]
 [0.02751698 0.80432682 0.27843698]
 [0.01754396 0.9867329  0.94234306]]
--------------------
[[3 4]
 [1 2]]
--------------------
[[2 1]
 [5 8]
 [9 1]
 [4 4]
 [6 3]
 [4 9]
 [6 3]
 [6 2]
 [2 1]
 [5 6]]
--------------------
[[9 5]
 [9 9]
 [9 4]
 [1 2]
 [5 2]
 [0 3]
 [5 5]
 [1 2]
 [3 5]
 [9 1]]

Actividades

Actividad 1

Imaginad la siguiente actividad donde tenemos que realizar la siguiente matriz:

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

tip: https://numpy.org/doc/stable/reference/generated/numpy.repeat.html

[93]:
# TODO ACTIVITY

Actividad 2

Generar la siguiente estructura a partir del array([1, 2, 3])

array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])

tip: https://numpy.org/doc/stable/reference/generated/numpy.tile.html

[94]:
# TODO Activity

Carga y volcado de datos

Entrada y salida

¿Cómo cargar datos de un fichero y cómo salvar resultados?

Siempre hay que considerar el tipo de formato con el que se han guardado los datos. El formato influye en el rendimiento de las operaciones (R/W) y la capacidad de almacenamiento utilizada. - CSV suele contener texto, no es eficiente, pero fácil de ingerir en otras herramientas. ¿Tiene sentido un fichero csv para matrices númericas? https://datos.gob.es/es/catalogo

Carga de datos con Pandas a Numpy (solución no-ideal)

Vamos a utilizar cualquier fichero csv (i.e. altura de gabilos) para tener en una estructura de numpy dichos valores de altura.

[27]:
%cat data/"GALIBOS TUNELES MADRID".csv
idelem;ALTURA MAXIMA;NOMBRE TUNEL;POINT_X;POINT_Y;SENTIDO;COMENTARIO- TIPO GALIBO;LATITUD;LONGITUD
8959;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438912,3031;4474612,979;Interior;2 CHAPAS con cadenas final acceso Paseo del Rey;40,41990315;-3,72007491
8954;4;BAILÉN - FERRAZ;439479,3243;4474181,2;E: Bailén;Estructura fija en interior;40,41605513;-3,713352936
8964;4;BAILÉN - FERRAZ;439392,055;4474958,045;E: Ferraz;Estructura fija en interior;40,42304694;-3,714455528
8961;4;BAILÉN - FERRAZ;439482,6262;4474804,58;E: Cuesta de San Vicente;Estructura fija en interior;40,42167106;-3,713373335
8960;4;BAILÉN - FERRAZ;439521,7846;4474874,166;E: Plaza España;Estructura fija en interior;40,42230076;-3,71291839
8962;4;BAILÉN - FERRAZ;439479,502;4474183,125;E: Bailén;CHAPAS con cadenas;40,41607248;-3,713351025
8957;3,9;AVENIDA DEL PLANETARIO BAJO PARQUE ENRIQUE TIERNO GALVÁN;441925,0158;4471575,968;E: Avda Planetario;8 Chapas con cadenas;40,39276008;-3,684290023
8934;3,5;MARÍA DE MOLINA - VELÁZQUEZ - A-2 BAJO LÓPEZ DE HOYOS;441625,8193;4476587,981;E: Po Castellana;BARRA con cadenas;40,4378899;-3,6882751
8935;3,5;MARÍA DE MOLINA - VELÁZQUEZ - A-2 BAJO LÓPEZ DE HOYOS;441618,1062;4476588,674;E: Po Castellana;Exterior (cadenas con chapas pintadas);40,4378956;-3,6883661
8956;3,5;MARÍA DE MOLINA - VELÁZQUEZ - A-2 BAJO LÓPEZ DE HOYOS;442066,6652;4476517,38;E: Velázquez;Exterior (cadenas con chapas pintadas);40,43728473;-3,683071113
8949;4,3;AVENIDA DE PÍO XII - MONFORTE DE LEMOS - SINESIO DELGADO BAJO PASEO DE LA CASTELLANA;442869,3053;4480480,113;E: M-30 (tubo norte);BARRA con cadenas, bajo PMV;40,4730384;-3,6739647
8950;4,5;AVENIDA DE PÍO XII - MONFORTE DE LEMOS - SINESIO DELGADO BAJO PASEO DE LA CASTELLANA;442652,2401;4480464,345;E: Avda Pio XII (tubo norte);BARRA con cadenas, bajo PMV;40,4728814;-3,6765238
8926;3;ALBERTO AGUILERA - SERRANO JOVER BAJO ALBERTO AGUILERA;439558,6536;4475760,808;E: Alberto Aguilera;CHAPAS con cadenas, bajo pórtico;40,4302907;-3,7125681
8927;3;SANTA CRUZ DE MARCENADO BAJO SERRANO JOVER;439300,8839;4475695,577;E: Princesa;CHAPAS con cadenas, bajo pórtico;40,4296843;-3,7156006
8924;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438889,0989;4474423,062;E: Virgen del Puerto;CHAPAS con cadenas;40,4181908;-3,7203326
8925;4;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;439101,5316;4474659,531;E: Cta San Vicente;CHAPAS con cadenas;40,4203439;-3,7178517
8955;4,5;AVENIDA DE PÍO XII - MONFORTE DE LEMOS - SINESIO DELGADO BAJO PASEO DE LA CASTELLANA;442746,4367;4480399,381;E: Avda Pio XII (tubo sur) a M30;BARRA con cadenas, bajo PMV;40,47230268;-3,675406786
8936;3;AZCA;441464,0362;4477835,839;E: H Pinzón N-2;Estructura (y barra con cadenas, bajo frontis;40,4491198;-3,6902975
8937;4;AZCA;441463,4128;4477828,772;E: H Pinzón N-1;BARRA con cadenas en frontis;40,4490561;-3,6903042
8938;3;AZCA;441154,5001;4478216,599;E: G Perón C Haya N-2;Portico;40,452528;-3,6939828
8939;4;AZCA;441099,2385;4478264,333;E: G Perón C Haya N-1;Estructura (exterior) con tres placas colgando;40,4529541;-3,6946389
8940;3;AZCA;441158,5465;4478169,3;E: G Perón Entreplanta;Estructura con CHAPAS con cadenas;40,4521022;-3,6939307
8941;3;AZCA;441141,4101;4478105,872;E: G Perón Entreplanta;Estructura con CHAPAS con cadenas;40,4515296;-3,6941269
8942;4;AZCA;441257,2772;4478216,569;E: Orense N-1;Estructura (exterior) con dos placas colgando;40,452535;-3,6927708
8943;3;AZCA;441187,5315;4478215,751;E: Orense N-2;Estructura (exterior) justo antes del frontis;40,4525227;-3,6935932
8944;2,95;AZCA;440986,71;4477883,444;E: G Moscardó N-1;Barra en frontis;40,4495149;-3,6959305
8945;4;AZCA;441064,0107;4477518,421;E: Agustín Betancourt N-1;BARRA;40,4462321;-3,6949851
8946;4;AZCA;441057,0031;4477414,663;E: Agustín Betancourt N-1;BARRA con cadenas, bajo cartelón direccional y Pintado rayas en frontis;40,4452969;-3,6950581
8947;4;AZCA;441080,0131;4477573,299;E: Agustín Betancourt N-2;E: Desde nivel 1 a nivel 2;40,4467276;-3,6948015
8948;3;AZCA;441101,5086;4477639,757;E: Agustín Betancourt N-1;Chapa con cadenas;40,4473278;-3,6945542
8958;2,95;AZCA;441010,4375;4477877,086;E: G Moscardó N-2;BARRA fija en interior;40,44945931;-3,695650115
8965;2,85;AZCA;441111,5793;4477889,948;Salida Basilica;BARRA con cadenas;40,44958235;-3,694458649
8966;2,85;AZCA;441086,6144;4477890,715;Salida Basilica;Chapas con cadenas;40,44958749;-3,694753106
8967;3;AZCA;441204,42;4478036,222;Bajada al nivel 2;BARRA fija;40,45090662;-3,693377419
8970;3;AZCA;441165,3536;4478160,209;E: Orense al nivel 2;BARRA fija;40,45202079;-3,693849585
8971;3;AZCA;441185,137;4478159,361;E: Orense al nivel 2;BARRA fija;40,45201454;-3,693616213
8972;2,85;AZCA;441101,1363;4477889,271;Salida Basilica desde el nivel 2;BARRA fija;40,44957551;-3,69458173
8973;3;AZCA;441241,8723;4477650,129;S: Paseo de la Castellana;Portico;40,44743117;-3,692900051
8968;3;AZCA;441282,369;4477795,824;S: A nivel 1 Hermanos Pizon;Portico;40,44874652;-3,692436
8969;3;AZCA;441292,4601;4477805,21;E: Desde nivel 1 Hermanos Pizon;Portico;40,44883178;-3,692317876
8974;3;AZCA;441294,0581;4478019,345;Zona norte;Chapas con cadenas;40,45076092;-3,692318828
8975;3;AZCA;441261,9311;4478021,55;Zona norte;BARRA fija;40,45077851;-3,692697879
8980;3;AZCA;441212,5241;4478035,8;Bajada al nivel 2;Chapa con cadenas;40,45090339;-3,693281814
8977;2,85;AZCA;441061,9487;4477893,239;Salida Basilica;BARRA fija;40,44960848;-3,695044198
8978;4;AZCA;441323,6363;4477957,233;Salida sentido Maragall;BARRA fija;40,45020348;-3,691964297
8963;4;AZCA;441324,5433;4477967,923;Salida sentido Maragall;BARRA fija;40,45029984;-3,69195459
8976;3;AZCA;441288,5822;4477792,263;E: A zona privada;BARRA fija;40,44871488;-3,692362406
8981;2,4;AZCA;441093,0476;4477691,231;E: Carril en zona privada;BARRA fija;40,4477909;-3,694658743
8928;4;BAILÉN - FERRAZ;439537,71;4474885,986;E: Plaza España;CHAPAS con cadenas;40,4224084;-3,7127318
8929;4;BAILÉN - FERRAZ;439386,8731;4474964,254;E: Ferraz;CHAPAS con cadenas;40,4231025;-3,7145172
8930;4;BAILÉN - FERRAZ;439471,4819;4474795,195;E: Cuesta de San Vicente;CHAPAS con cadenas;40,4215857;-3,7135038
8932;4,3;SANTA MARÍA DE LA CABEZA BAJO GLORIETA DE SANTA MARÍA DE LA CABEZA;441090,1042;4473112,731;E: Sta M Cabeza;Individual, colgado en cadena y barra;40,4065454;-3,6942692
8933;3,5;MARÍA DE MOLINA - VELÁZQUEZ - A-2 BAJO LÓPEZ DE HOYOS;442067,8213;4476529,635;E: Velázquez;Exterior (Barra con con cadenas);40,4373952;-3,6830586
8931;2,7;ATOCHA - TOLEDO - MAYOR BAJO PLAZA MAYOR;440195,4334;4474017,199;E: Atocha. Individual, colgado de cadena;CHAPAS con cadenas, bajo cartelón;40,4146295;-3,7048974
8982;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438901,4438;4474616,198;Interior;CHAPAS con cadenas;40,41994508;-3,720215203
8983;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438950,9083;4474640,146;Interior;BARRA con cadenas;40,4201315;-3,719622509
8984;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438906,4081;4474619,97;Interior;CHAPAS con cadenas;40,41997369;-3,720152995
8985;2,95;GLORIETA DE SAN VICENTE (ENTRADA PRINCIPAL) RAMPA DE ACCESO Y VIRGEN DEL PUERTO - PASEO DEL REY;438897,2408;4474613,054;Interior;BARRA;40,41991808;-3,720266467
8986;1,95;BAILÉN - FERRAZ;439521,7776;4474784,004;E: Senado;Barra fija;40,42148854;-3,712909899
8979;2,4;AZCA;441092,3825;4477690,674;E: A zona privada;BARRA fija;40,44778584;-3,694666534

Importante No pongais nombres de ficheros con espacios !

[52]:
import pandas as pd
df = pd.read_csv("data/GALIBOS TUNELES MADRID.csv",sep=";")
df.columns
height = df[df.columns[1]]
print(height)
## ACTIVIDAD: Como solucionaomos este error!!!!!!!!

data = np.array(height,np.float32)
print(data)
0     2,95
1        4
2        4
3        4
4        4
5        4
6      3,9
7      3,5
8      3,5
9      3,5
10     4,3
11     4,5
12       3
13       3
14    2,95
15       4
16     4,5
17       3
18       4
19       3
20       4
21       3
22       3
23       4
24       3
25    2,95
26       4
27       4
28       4
29       3
30    2,95
31    2,85
32    2,85
33       3
34       3
35       3
36    2,85
37       3
38       3
39       3
40       3
41       3
42       3
43    2,85
44       4
45       4
46       3
47     2,4
48       4
49       4
50       4
51     4,3
52     3,5
53     2,7
54    2,95
55    2,95
56    2,95
57    2,95
58    1,95
59     2,4
Name: ALTURA MAXIMA, dtype: object
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb Cell 18' in <cell line: 8>()
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000017?line=4'>5</a> print(height)
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000017?line=5'>6</a> ## ACTIVIDAD: Como solucionaomos este error!!!!!!!!
----> <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000017?line=7'>8</a> data = np.array(height,np.float32)
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000017?line=8'>9</a> print(data)

File ~/.pyenv/versions/3.9.7/envs/my397/lib/python3.9/site-packages/pandas/core/series.py:872, in Series.__array__(self, dtype)
    825 def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
    826     """
    827     Return the values as a NumPy array.
    828
   (...)
    870           dtype='datetime64[ns]')
    871     """
--> 872     return np.asarray(self._values, dtype)

ValueError: could not convert string to float: '2,95'

Usando métodos de numpy

[65]:
# https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html
data = np.loadtxt("data/GALIBOS TUNELES MADRID.csv", delimiter=";", usecols = (1), skiprows=1, converters={1: lambda s:float(str(s.decode()).replace(",","."))})
print(data)
print(data.shape)
[2.95 4.   4.   4.   4.   4.   3.9  3.5  3.5  3.5  4.3  4.5  3.   3.
 2.95 4.   4.5  3.   4.   3.   4.   3.   3.   4.   3.   2.95 4.   4.
 4.   3.   2.95 2.85 2.85 3.   3.   3.   2.85 3.   3.   3.   3.   3.
 3.   2.85 4.   4.   3.   2.4  4.   4.   4.   4.3  3.5  2.7  2.95 2.95
 2.95 2.95 1.95 2.4 ]
(60,)

Volviendo a trabajar con datos numéricos

[73]:
data = np.random.uniform(0.01,20.0,size=100000)
print(data[:5])
[ 6.41600405  0.17257063  2.78427297 12.61535684 19.00956934]
[86]:
f = open("data/tmp.npy","wb") # Writing file, in Binary mode
np.save(f,data)
f.close()

with open("data/tmp2.csv","w") as f2: # Writing but in txt
    for n in data:
        f2.write(str(n)+",")

[88]:
%ls -lih data/tmp*
3289744 -rw-r--r--  1 isaac  staff   781K Jun 23 10:17 data/tmp.npy
3290153 -rw-r--r--  1 isaac  staff   1.8M Jun 23 10:17 data/tmp2.csv
[92]:
data = np.random.uniform(0.01,20.0,size=100000)
data2 = np.random.normal(0.3,10,100000)
print(data[:5])
print(data2[:5])

f = open("data/tmp.npy","wb") # Writing file, in Binary mode
np.save(f,data)
np.save(f,data2)
f.close()

print("saved")


with open('data/tmp.npy', 'rb') as f:
    a = np.load(f)
    b = np.load(f)

print(a[:5])
print(b[:5])

[14.60937382 17.14056688  6.61867482 17.25191009 14.61060707]
[ -1.02007071  -3.17804997   4.46352973  -8.90929859 -11.98308117]
saved
[14.60937382 17.14056688  6.61867482 17.25191009 14.61060707]
[ -1.02007071  -3.17804997   4.46352973  -8.90929859 -11.98308117]
[95]:
import pickle

with open("data/tmp3.npy",'wb') as f:
    pickle.dump(a, f)
    pickle.dump(b, f)
[96]:
%ls -lih data/tmp*
3289744 -rw-r--r--  1 isaac  staff   1.5M Jun 23 10:20 data/tmp.npy
3290153 -rw-r--r--  1 isaac  staff   1.8M Jun 23 10:17 data/tmp2.csv
3290903 -rw-r--r--  1 isaac  staff   1.5M Jun 23 10:32 data/tmp3.npy
[102]:
f = "data/tmp3.npy"
ca = pickle.load(open(f,"rb"))
print(ca)
cb = pickle.load(open(f,"rb"))
print(cb)

# Qué paso en este punto?
# ¿Cómo podriamos haber guardado ambas variables dentro del mismo fichero?
[14.60937382 17.14056688  6.61867482 ... 17.88756521  2.51568897
 11.46615691]
[14.60937382 17.14056688  6.61867482 ... 17.88756521  2.51568897
 11.46615691]

Operaciones con series numpy

[105]:
a = np.array([.0,0.1])
b = np.array([1,1])
print(a+b)
print(a-b)
print(a/b)
print(a*b)
print(2**a)
[1.  1.1]
[-1.  -0.9]
[0.  0.1]
[0.  0.1]
[1.         1.07177346]
[106]:
c = np.array([1,1,1])
print(a+c)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb Cell 36' in <cell line: 2>()
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000038?line=0'>1</a> c = np.array([1,1,1])
----> <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000038?line=1'>2</a> print(a+c)

ValueError: operands could not be broadcast together with shapes (2,) (3,)
[118]:
c = np.array([1,1,1,1]).reshape(2,2)
print(a*c)
print("-"*10)
print(a.dot(c)) #https://numpy.org/doc/stable/reference/generated/numpy.dot.html

[[0.  0.1]
 [0.  0.1]]
----------
[0.1 0.1]
[119]:
# Tensor dot
#https://numpy.org/doc/stable/reference/generated/numpy.tensordot.html#numpy.tensordot
a = np.arange(60.).reshape(3,4,5)
b = np.arange(24.).reshape(4,3,2)
c = np.tensordot(a,b, axes=([1,0],[0,1]))
print(c)
print(c.shape)
[[4400. 4730.]
 [4532. 4874.]
 [4664. 5018.]
 [4796. 5162.]
 [4928. 5306.]]
(5, 2)
[121]:
# Einseum
# https://numpy.org/doc/stable/reference/generated/numpy.einsum.html

a = np.arange(25).reshape(5,5)
np.einsum("ii",a)
[121]:
60

https://en.wikipedia.org/wiki/Kronecker_product

\[a \otimes b\]
[130]:
a = np.arange(1,5).reshape(2,2)
print(a)
b = np.array([0,5,6,7]).reshape(2,2)
print(b)
print("-"*10)
k = np.kron(a,b)
print(k)

[[1 2]
 [3 4]]
[[0 5]
 [6 7]]
----------
[[ 0  5  0 10]
 [ 6  7 12 14]
 [ 0 15  0 20]
 [18 21 24 28]]

Actividad

Implementa con operaciones básicas de numpy la multiplicación de Kron. Compara tiempos de ejecución entre tú versión y la ya implementada.

[131]:
#TODO Activity

Funciones sobre series

[133]:
a = np.array(range(10))
print(np.cos(a))
print(np.exp(a))
print(np.log(a))
[ 1.          0.54030231 -0.41614684 -0.9899925  -0.65364362  0.28366219
  0.96017029  0.75390225 -0.14550003 -0.91113026]
[1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01
 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03
 2.98095799e+03 8.10308393e+03]
[      -inf 0.         0.69314718 1.09861229 1.38629436 1.60943791
 1.79175947 1.94591015 2.07944154 2.19722458]
/var/folders/6j/7gfvt_29797dypw8t1wttblw0000gn/T/ipykernel_2569/928567411.py:4: RuntimeWarning: divide by zero encountered in log
  print(np.log(a))
[136]:
print(np.sum(a))
print(np.cumsum(a))
print(np.mean(a))

print("\n",np.cumprod(a))
print(np.min(a))
print(np.argmax(a))

45
[ 0  1  3  6 10 15 21 28 36 45]
4.5

 [0 0 0 0 0 0 0 0 0 0]
0
9
[137]:
print(a.mean())
print(a.min())
print(a.argmax())
4.5
0
9
[140]:
a = a.reshape(2,5)
print(a)
print("-"*10)
print(np.sum(a,axis=1))
print(np.sum(a,axis=0))
[[0 1 2 3 4]
 [5 6 7 8 9]]
----------
[10 35]
[ 5  7  9 11 13]

Actividades

Actividad. 1

¿Cómo calcular la distancia euclidea entre dos vectores?

\[d{v_1,v_2}=\sqrt{\sum_{k=1}^n(x_{1,k}-x_{2,k})^2}\]
[142]:
v1 = np.arange(1,4)
v2 = np.arange(4,7)
#TODO Activity
# Solucion == 5.196152422706632

Actividad. 2

¿Y calcular la distancia de Manhattan?

\[d{v_1,v_2}= \sum_{k=1}^n \mid x_{1,k} - x_{2,k} \mid\]
[143]:
v1 = np.arange(1,4)
v2 = np.arange(4,7)
#TODO Activity
# Solucion == 9

Restructurando la dimensión de una serie

[145]:
a = np.arange(10)
print(a.shape)
print(a.reshape(2,5))
print(a)

(10,)
[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4 5 6 7 8 9]
[147]:
a = a.reshape(2,5)
print(a.T)
print("-"*10)
print(np.hstack(a))  # https://numpy.org/doc/stable/reference/generated/numpy.hstack.html

[[0 5]
 [1 6]
 [2 7]
 [3 8]
 [4 9]]
----------
[0 1 2 3 4 5 6 7 8 9]
[149]:
b = np.arange(10,20).reshape(2,5)
print(b)
print("-"*10)
print(np.hstack((a,b))) # axis-1
print(np.vstack((a,b))) # axis-0

[[10 11 12 13 14]
 [15 16 17 18 19]]
----------
[[ 0  1  2  3  4 10 11 12 13 14]
 [ 5  6  7  8  9 15 16 17 18 19]]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
[152]:
c = np.dstack((a,b)) # axis-2  https://numpy.org/doc/stable/reference/generated/numpy.dstack.html
print(c)
print(c.shape)
[[[ 0 10]
  [ 1 11]
  [ 2 12]
  [ 3 13]
  [ 4 14]]

 [[ 5 15]
  [ 6 16]
  [ 7 17]
  [ 8 18]
  [ 9 19]]]
(2, 5, 2)
[155]:
print(a)
print(np.ravel(a)) # https://numpy.org/doc/stable/reference/generated/numpy.ravel.html

print(np.ravel(a,order="F")) # ‘F’ means to index the elements in column-major,
[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4 5 6 7 8 9]
[0 5 1 6 2 7 3 8 4 9]
[162]:
print(a)
print(np.split(a,2))
c1,c2 = np.split(a,2)

print("-"*10)

print(c1)
print(c1.shape)
print(np.ravel(c1))
print(c2)
[[0 1 2 3 4]
 [5 6 7 8 9]]
[array([[0, 1, 2, 3, 4]]), array([[5, 6, 7, 8, 9]])]
----------
[[0 1 2 3 4]]
(1, 5)
[0 1 2 3 4]
[[5 6 7 8 9]]
[165]:
print(np.concatenate((a,b)))
print("-"*10)
print(np.concatenate((a,b),axis=1))
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
----------
[[ 0  1  2  3  4 10 11 12 13 14]
 [ 5  6  7  8  9 15 16 17 18 19]]
[169]:
# https://pillow.readthedocs.io/en/stable/
[166]:
%sudo pip install pillow
UsageError: Line magic function `%sudo` not found.
[177]:
from PIL import Image

image = Image.open('images/gatito.jpeg')
# summarize some details about the image
print(image.format)
print(image.size)
print(image.mode)

display(image)

JPEG
(1200, 800)
RGB
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_64_1.png
[189]:
data = np.asarray(image)
print(data[0:2])
print(data.size)
print(data.shape)
w,h,_ = data.shape
[[[182 172 163]
  [182 172 163]
  [183 173 164]
  ...
  [203 192 186]
  [203 192 186]
  [203 192 186]]

 [[183 173 164]
  [183 173 164]
  [183 173 164]
  ...
  [202 191 185]
  [203 192 186]
  [203 192 186]]]
2880000
(800, 1200, 3)
[198]:
print(data[w//2,h//2])

data[w//2,h//2] = np.array([255,0,0]) # a red point

img = Image.fromarray(data, 'RGB')

display(img)
[255   0   0]
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_66_1.png
[203]:
center_mask = w//2,h//2
rectangle_size = int(w*0.1)
mask = np.ones(rectangle_size*rectangle_size).reshape(rectangle_size,rectangle_size)
print(mask.shape)

for x in range(w//2,w//2+rectangle_size):
    for y in range(h//2,h//2+rectangle_size):
        data[x,y] =  np.array([255,0,0])


img = Image.fromarray(data, 'RGB')
display(img)

# REALMENTE, el cuadrado está en el centro?

(80, 80)
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_67_1.png

Actividad

Transforma la imagen en tonos grises. Solo con numpy!!!

[244]:
#WAY 1:
#
data = np.asarray(image)

for x in range(data.shape[0]): #no eficiente
     for y in range(data.shape[1]):
         data[x,y] = np.repeat(data[x,y].mean(),3)

img = Image.fromarray(data, 'RGB')
display(img)
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_69_0.png

Operaciones de Slicing

[277]:
a = np.arange(300).reshape(10,10,3)
print(a[:1])
print("-"*10)

print(a[0][0])
print("-"*10)

print(a[:,0])
print("-"*10)

print(a[:,2:4])
print("-"*10)
[[[ 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 28 29]]]
----------
[0 1 2]
----------
[[  0   1   2]
 [ 30  31  32]
 [ 60  61  62]
 [ 90  91  92]
 [120 121 122]
 [150 151 152]
 [180 181 182]
 [210 211 212]
 [240 241 242]
 [270 271 272]]
----------
[[[  6   7   8]
  [  9  10  11]]

 [[ 36  37  38]
  [ 39  40  41]]

 [[ 66  67  68]
  [ 69  70  71]]

 [[ 96  97  98]
  [ 99 100 101]]

 [[126 127 128]
  [129 130 131]]

 [[156 157 158]
  [159 160 161]]

 [[186 187 188]
  [189 190 191]]

 [[216 217 218]
  [219 220 221]]

 [[246 247 248]
  [249 250 251]]

 [[276 277 278]
  [279 280 281]]]
----------
[305]:
a = np.arange(300).reshape(10,10,3)
print(a[:1])
print("-"*10)

print(a[:,:,0])

print("-"*10)

r = 0.2126
print(a[:,:,0]*r)

[[[ 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 28 29]]]
----------
[[  0   3   6   9  12  15  18  21  24  27]
 [ 30  33  36  39  42  45  48  51  54  57]
 [ 60  63  66  69  72  75  78  81  84  87]
 [ 90  93  96  99 102 105 108 111 114 117]
 [120 123 126 129 132 135 138 141 144 147]
 [150 153 156 159 162 165 168 171 174 177]
 [180 183 186 189 192 195 198 201 204 207]
 [210 213 216 219 222 225 228 231 234 237]
 [240 243 246 249 252 255 258 261 264 267]
 [270 273 276 279 282 285 288 291 294 297]]
----------
[[ 0.      0.6378  1.2756  1.9134  2.5512  3.189   3.8268  4.4646  5.1024
   5.7402]
 [ 6.378   7.0158  7.6536  8.2914  8.9292  9.567  10.2048 10.8426 11.4804
  12.1182]
 [12.756  13.3938 14.0316 14.6694 15.3072 15.945  16.5828 17.2206 17.8584
  18.4962]
 [19.134  19.7718 20.4096 21.0474 21.6852 22.323  22.9608 23.5986 24.2364
  24.8742]
 [25.512  26.1498 26.7876 27.4254 28.0632 28.701  29.3388 29.9766 30.6144
  31.2522]
 [31.89   32.5278 33.1656 33.8034 34.4412 35.079  35.7168 36.3546 36.9924
  37.6302]
 [38.268  38.9058 39.5436 40.1814 40.8192 41.457  42.0948 42.7326 43.3704
  44.0082]
 [44.646  45.2838 45.9216 46.5594 47.1972 47.835  48.4728 49.1106 49.7484
  50.3862]
 [51.024  51.6618 52.2996 52.9374 53.5752 54.213  54.8508 55.4886 56.1264
  56.7642]
 [57.402  58.0398 58.6776 59.3154 59.9532 60.591  61.2288 61.8666 62.5044
  63.1422]]
[306]:
# WAY2
# https://e2eml.school/convert_rgb_to_grayscale.html


# TODO

print(data.shape)
img = Image.fromarray(np.uint8(data))
display(img)


(800, 1200, 3)
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_73_1.png
[310]:
# Way 3

rgbcorrection = np.array([0.2989, 0.5870, 0.1140])

data = np.asarray(image)
print(data.shape)
data2 = np.dot(data,rgbcorrection)

print(data2.shape)
img = Image.fromarray(np.uint8(data2))
display(img)


# as a plot
import matplotlib.pyplot as plt
plt.imshow(data2, cmap = plt.get_cmap(name = 'gray'))
plt.show()
(800, 1200, 3)
(800, 1200)
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_74_1.png
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_74_2.png

Funciones propias vectorizadas

[311]:
temperatura = np.random.randint(-10,43,1000)
[314]:
temperatura[temperatura>33]
[314]:
array([42, 36, 40, 34, 35, 34, 42, 37, 36, 35, 41, 40, 34, 42, 42, 36, 35,
       42, 40, 34, 34, 39, 41, 41, 42, 37, 36, 41, 35, 40, 41, 40, 37, 35,
       42, 37, 41, 39, 38, 40, 35, 42, 35, 36, 35, 42, 38, 34, 40, 39, 36,
       40, 41, 42, 37, 36, 38, 38, 37, 42, 38, 37, 38, 35, 37, 35, 42, 39,
       36, 37, 34, 39, 35, 38, 42, 39, 40, 42, 38, 41, 42, 34, 35, 42, 35,
       38, 36, 38, 36, 39, 41, 40, 41, 35, 34, 41, 39, 37, 35, 40, 37, 37,
       41, 42, 37, 37, 40, 37, 37, 37, 39, 37, 40, 36, 35, 38, 39, 36, 38,
       34, 40, 38, 39, 40, 36, 39, 39, 35, 41, 34, 40, 40, 34, 42, 35, 41,
       35, 41, 37, 37, 35, 39, 36, 39, 40, 40, 37, 38, 36, 37, 35, 38, 35,
       37, 35, 35, 37, 36, 35, 39, 40, 37, 37, 37])
[316]:
def isHot(grados):
    if grados>33 and grados<=40:
        return True

def isHot(grados):
    return grados>33 and grados<=40

fHot = np.vectorize(isHot)
print(fHot(temperatura))
[316]:
array([False, False, False,  True, False,  True, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False,  True, False, False, False, False,  True,
       False, False,  True, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False,  True, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False,  True,
       False, False, False, False, False,  True, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False,  True, False,
       False, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False,  True, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False,  True, False, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False,  True, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False,  True, False, False, False, False, False, False,
       False,  True, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False,  True,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
        True, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False,  True, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False,  True, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False,  True, False, False, False,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False, False,  True, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True,  True, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False,  True,  True, False,  True, False, False, False,
       False, False, False,  True, False, False,  True, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False,  True,  True, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False,  True, False,
       False, False,  True, False, False, False,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False,  True, False,
       False, False, False, False, False, False,  True,  True, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False,  True, False, False, False, False,  True,
       False, False, False, False, False, False,  True, False, False,
       False, False, False, False,  True, False, False, False, False,
       False,  True, False, False,  True, False,  True, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False,  True, False,
        True,  True, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False,  True, False,  True, False, False,
       False, False,  True, False, False, False,  True, False, False,
       False, False, False, False, False,  True,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False,  True, False,  True,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False, False, False, False,  True,
       False, False,  True,  True, False, False,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False,  True,
        True, False,  True, False, False,  True, False,  True, False,
       False, False, False,  True, False, False,  True, False, False,
       False,  True, False, False, False, False, False,  True,  True,
       False, False, False, False, False, False,  True, False,  True,
        True,  True, False,  True, False, False,  True, False, False,
       False])
[317]:
temperatura[fHot(temperatura)]
[317]:
array([36, 40, 34, 35, 34, 37, 36, 35, 40, 34, 36, 35, 40, 34, 34, 39, 37,
       36, 35, 40, 40, 37, 35, 37, 39, 38, 40, 35, 35, 36, 35, 38, 34, 40,
       39, 36, 40, 37, 36, 38, 38, 37, 38, 37, 38, 35, 37, 35, 39, 36, 37,
       34, 39, 35, 38, 39, 40, 38, 34, 35, 35, 38, 36, 38, 36, 39, 40, 35,
       34, 39, 37, 35, 40, 37, 37, 37, 37, 40, 37, 37, 37, 39, 37, 40, 36,
       35, 38, 39, 36, 38, 34, 40, 38, 39, 40, 36, 39, 39, 35, 34, 40, 40,
       34, 35, 35, 37, 37, 35, 39, 36, 39, 40, 40, 37, 38, 36, 37, 35, 38,
       35, 37, 35, 35, 37, 36, 35, 39, 40, 37, 37, 37])
[319]:
#Alternativa
# https://numpy.org/doc/stable/reference/routines.logic.html
np.logical_and(temperatura>30,temperatura<=40)
[319]:
array([False, False, False,  True,  True,  True, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False,  True, False,  True, False,
        True, False, False,  True, False, False, False, False,  True,
       False,  True,  True, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False,  True, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False,  True, False, False, False,  True, False,
       False, False, False, False, False, False, False, False,  True,
       False,  True, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False,  True,
       False, False, False, False, False,  True, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False,  True, False,
       False, False, False, False, False, False,  True, False, False,
       False, False,  True, False, False, False, False,  True, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False,  True, False, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False,  True,  True, False,  True, False,  True, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False,  True,
        True, False, False, False, False,  True, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False,  True, False, False, False, False,
       False, False,  True, False, False, False, False, False, False,
       False,  True, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False,  True,  True,
       False, False, False, False, False, False, False, False,  True,
       False, False, False,  True, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False,  True,
       False, False, False, False, False,  True, False, False, False,
        True, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False,  True, False, False, False,  True, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False,  True, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False,  True,  True, False, False,  True, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False, False, False, False, False, False,  True,
       False, False,  True, False, False,  True,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False,  True, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False, False,  True, False, False,  True,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False,  True, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True,  True, False, False, False, False,  True, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False,  True,  True, False,  True, False, False, False,
       False, False, False,  True, False, False,  True, False,  True,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False,  True,  True,  True,  True,
       False, False, False, False, False, False, False, False,  True,
       False,  True, False, False, False, False, False,  True, False,
       False, False,  True, False, False, False,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False, False,  True, False,  True, False,
       False, False, False, False, False, False,  True,  True, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
       False, False, False,  True, False, False, False, False,  True,
       False, False, False, False, False, False,  True, False, False,
       False,  True, False, False,  True, False, False, False,  True,
       False,  True, False, False,  True, False,  True, False, False,
        True, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False,  True, False,
        True,  True, False, False, False, False, False, False, False,
       False, False, False,  True, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False,  True, False,  True, False, False,
       False, False,  True, False, False, False,  True, False,  True,
        True, False, False, False, False,  True,  True,  True, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False, False, False,
       False, False, False, False, False, False,  True, False, False,
       False, False, False, False, False,  True, False, False, False,
       False, False,  True, False, False,  True,  True, False,  True,
       False, False, False, False, False, False, False,  True,  True,
        True,  True, False, False, False, False,  True,  True, False,
       False, False, False, False, False, False, False, False,  True,
       False, False,  True,  True, False, False,  True, False,  True,
       False,  True, False, False, False, False, False, False, False,
       False, False,  True, False,  True, False, False, False,  True,
        True, False,  True, False, False,  True, False,  True, False,
       False, False, False,  True, False, False,  True, False, False,
       False,  True, False, False, False, False, False,  True,  True,
       False, False, False, False, False, False,  True, False,  True,
        True,  True, False,  True, False, False,  True, False, False,
       False])
[323]:
isHot = lambda x: (x>30 and x<=40)
index = list(map(isHot,temperatura))
temperatura[index]
[323]:
array([36, 33, 40, 34, 31, 35, 33, 34, 37, 33, 36, 31, 35, 31, 40, 34, 33,
       36, 35, 40, 34, 34, 39, 37, 36, 31, 35, 40, 40, 37, 35, 37, 32, 32,
       39, 33, 38, 32, 40, 35, 35, 32, 36, 35, 38, 34, 40, 33, 39, 36, 40,
       31, 37, 31, 36, 38, 38, 37, 31, 38, 37, 38, 35, 37, 35, 32, 32, 39,
       32, 36, 37, 34, 31, 39, 33, 35, 38, 39, 31, 40, 31, 38, 34, 35, 35,
       38, 33, 36, 31, 38, 36, 39, 40, 35, 34, 31, 39, 37, 33, 32, 35, 33,
       40, 37, 37, 37, 37, 33, 40, 37, 37, 37, 39, 37, 40, 36, 35, 38, 31,
       39, 33, 36, 38, 34, 33, 40, 38, 39, 40, 36, 39, 39, 35, 34, 40, 32,
       32, 40, 34, 33, 35, 35, 32, 31, 37, 33, 37, 35, 39, 32, 32, 31, 31,
       36, 39, 40, 40, 37, 33, 33, 31, 38, 36, 37, 35, 38, 35, 37, 35, 35,
       37, 36, 35, 39, 40, 37, 37, 37])

Más funciones lógicas

[332]:
np.random.seed(2022)
a = np.random.randint(-90,0,100)

index = np.where(a<-80) # Alerta: Son índices
print(index)
print(a[index])
(array([21, 50, 66, 71, 78, 91, 97]),)
[-88 -88 -81 -83 -86 -86 -88]
[333]:
print(np.logical_or(a<-80,a<-90))

[False False False False False False False False False False False False
 False False False False False False False False False  True False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False  True False False False False False False False False False
 False False False False False False  True False False False False  True
 False False False False False False  True False False False False False
 False False False False False False False  True False False False False
 False  True False False]
[336]:
print(np.logical_not(np.logical_or(a<-40,a<-90)))
[False False  True  True False False False  True False False False False
 False  True False False False  True False False False False  True False
 False  True  True  True False  True  True False False  True False False
  True  True False  True  True False  True False False False  True  True
  True False False  True False False  True  True  True  True  True False
 False False False False  True False False  True False False False False
  True  True False  True False  True False  True False  True False  True
  True False False  True  True  True False False False False  True  True
  True False False  True]

Actividades

¿Cómo podemos conseguir esta transformación?

De

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a

array([ 0, -1,  2, -1,  4, -1,  6, -1,  8, -1])
[ ]:

Operaciones con Grupos

[347]:
np.random.seed(2022)
a = np.random.randint(-30,45,100)
print(a)
[ 15  19  25 -12  -6 -14  23  11   3  -3 -19 -11  18 -11   8  42 -16 -14
 -19 -28 -15  -7   7  26 -18  15 -16 -17  22  12  23  34  15  33  -7   7
  -9  31  26  16 -28  34  16  11  31  31  42  34  21   3  -1  -7  17  19
  25  -2 -21  40  17   1  -4 -23  35  12  32   2 -26 -20  -5  23  32  -7
  -6  34  30   5 -26 -11  19  20  29 -28  -3  41  24 -27  -9  37 -19  15
  37  14  26   3   5  -5  25 -24 -29  -4]
[348]:
9 in a
[348]:
False
[353]:
if -14 in a and not -6 in a:
    print("Something strange")
elif 45 in a:
    print("No 11")
else:
    print("Pues está el -14 y el -6, y no el 45")
Pues está el -14 y el -6, y no el 45
[356]:
#https://numpy.org/doc/stable/reference/generated/numpy.unique.html?highlight=unique#numpy.unique

unique_a = np.unique(a)  # sort but
print(unique_a)
[-29 -28 -27 -26 -24 -23 -21 -20 -19 -18 -17 -16 -15 -14 -12 -11  -9  -7
  -6  -5  -4  -3  -2  -1   1   2   3   5   7   8  11  12  14  15  16  17
  18  19  20  21  22  23  24  25  26  29  30  31  32  33  34  35  37  40
  41  42]
[405]:
unique_a, freq_a = np.unique(a,return_counts=True)
print(a)
print(len(a))
print("-"*10)
print(freq_a)
print(len(freq_a))
print("-"*10)
print(unique_a)
print(len(unique_a))

# ¿Cuántos elementos repetidos hay?
[ 15  19  25 -12  -6 -14  23  11   3  -3 -19 -11  18 -11   8  42 -16 -14
 -19 -28 -15  -7   7  26 -18  15 -16 -17  22  12  23  34  15  33  -7   7
  -9  31  26  16 -28  34  16  11  31  31  42  34  21   3  -1  -7  17  19
  25  -2 -21  40  17   1  -4 -23  35  12  32   2 -26 -20  -5  23  32  -7
  -6  34  30   5 -26 -11  19  20  29 -28  -3  41  24 -27  -9  37 -19  15
  37  14  26   3   5  -5  25 -24 -29  -4]
100
----------
[1 3 1 2 1 1 1 1 3 1 1 2 1 2 1 3 2 4 2 2 2 2 1 1 1 1 3 2 2 1 2 2 1 4 2 2 1
 3 1 1 1 3 1 3 3 1 1 3 2 1 4 1 2 1 1 2]
56
----------
[-29 -28 -27 -26 -24 -23 -21 -20 -19 -18 -17 -16 -15 -14 -12 -11  -9  -7
  -6  -5  -4  -3  -2  -1   1   2   3   5   7   8  11  12  14  15  16  17
  18  19  20  21  22  23  24  25  26  29  30  31  32  33  34  35  37  40
  41  42]
56
[403]:
unique_a,index_a,freq_a = np.unique(a,return_counts=True,return_index=True)
print(freq_a)
print(index_a)
print("-"*10)
print(np.where(freq_a==4))
print(unique_a[17]) # se repite cuatro veces
print(unique_a[33]) # se repite cuatro veces
print(unique_a[50]) # se repite cuatro veces

print(a[np.where(a==-7)])
[1 3 1 2 1 1 1 1 3 1 1 2 1 2 1 3 2 4 2 2 2 2 1 1 1 1 3 2 2 1 2 2 1 4 2 2 1
 3 1 1 1 3 1 3 3 1 1 3 2 1 4 1 2 1 1 2]
[98 19 85 66 97 61 56 67 10 24 27 16 20  5  3 11 36 21  4 68 60  9 55 50
 59 65  8 75 22 14  7 29 91  0 39 52 12  1 79 48 28  6 84  2 23 80 74 37
 64 33 31 62 87 57 83 15]
----------
(array([17, 33, 50]),)
-7
15
34
[-7 -7 -7 -7]
[389]:
np.sort(freq_a)
[389]:
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, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4])
[390]:

print(np.array([0,4,1,2,5,7,9])[::-1]) print(np.argsort(np.array([0,4,1,2,5,7,9]))) print(np.argsort(np.array([0,4,1,2,5,7,9]))[::-1]) print("-"*10) index_sorted = np.argsort(freq_a)[::-1] #https://numpy.org/doc/stable/reference/generated/numpy.argsort.html print(index_sorted)

[9 7 5 2 1 4 0]
[0 2 3 1 4 5 6]
[6 5 4 1 3 2 0]
----------
[33 50 17 43  1  8 26 47 15 44 37 41 19 28 21 20 55 18 31 16 13 11  3 30
 27 48 34 35 52 32 53  2  4  5  6  7 51  9 10 49 12 46 14 29 45 42 40 39
 38 36 22 23 24 25 54  0]
[391]:
unique_a[index_sorted]
[391]:
array([ 15,  34,  -7,  25, -28, -19,   3,  31, -11,  26,  19,  23,  -5,
         7,  -3,  -4,  42,  -6,  12,  -9, -14, -16, -26,  11,   5,  32,
        16,  17,  37,  14,  40, -27, -24, -23, -21, -20,  35, -18, -17,
        33, -15,  30, -12,   8,  29,  24,  22,  21,  20,  18,  -2,  -1,
         1,   2,  41, -29])

Actividades

Actividad 1

  • ¿Cuál es el color más frecuente en la imagen del gatito?

  • Sustituye esos pixeles por un color azul: rgb=(0,0,255)

[404]:
from PIL import Image

image = Image.open('images/gatito.jpeg')
#TODO

[407]:
a = np.arange(10)
b = np.arange(5,15)
print(a)
print(b)
print("-"*10)
print(np.in1d(a,b))
print(np.intersect1d(a,b))
print("-"*10)
print(np.setdiff1d(a,b))
print(np.setdiff1d(b,a))
print("-"*10)
print(np.union1d(a,b))
[0 1 2 3 4 5 6 7 8 9]
[ 5  6  7  8  9 10 11 12 13 14]
----------
[False False False False False  True  True  True  True  True]
[5 6 7 8 9]
----------
[0 1 2 3 4]
[10 11 12 13 14]
----------
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

Actividad 2

¿Cómo podemos conseguir encontrar valores pico, valores mayores sobre sus vecinos?

array([0, 1, 2, 3, 4, 54, 6, 7, 80, 9])
array([5, 8])

Un par de pistas: - np.diff https://numpy.org/doc/stable/reference/generated/numpy.diff.html?highlight=diff#numpy.diff - np.sign https://numpy.org/doc/stable/reference/generated/numpy.sign.html?highlight=sign#numpy.sign -

[409]:
a = np.array([0, 1, 2, 3, 4, 54, 6, 7, 80, 9])
# TODO

Actividad 3

Existe alguna columna o fila que sólo tenga una única incógnita?

sudoku = np.array([[5,3,0,0,7,0,0,0,0],
                 [6,0,0,1,9,5,0,0,0],
                 [1,9,8,0,0,0,0,6,0],
                 [8,0,0,0,6,0,0,0,3],
                 [4,0,0,8,0,3,0,0,1],
                 [7,0,0,0,2,0,0,0,6],
                 [0,6,0,0,0,0,2,8,0],
                 [3,8,0,4,1,9,7,2,5],
                 [4,0,0,0,8,0,0,7,9]])

Funciones de estadística

[416]:
np.random.seed(2022)
temperatures= np.random.normal(loc=17,scale=20,size=100)
print(temperatures)

# https://numpy.org/doc/stable/reference/routines.statistics.html

print(temperatures.mean())

[ 16.98944202  11.5019715   14.21428877  56.69372315  22.64218652
  32.21617316  23.01963212  27.80594538  24.46994575  24.55626788
  15.19573615 -29.11886538  39.85520048 -13.71308571  -0.27504036
  37.3308987   37.67927767   0.51015545  17.37809713   9.33312888
  10.9162905   36.94583012  14.45452319 -12.51771803 -21.81812657
  33.67297847   5.65564224  40.48973914  23.38137663  20.81740856
  24.38540361  14.97704275  -1.83618978 -11.08283418  58.61294024
  14.59367531  32.19583758  53.54864282   3.78545826   0.84387477
  34.75600231  12.65105101  -1.79049033  28.99076633  61.46225393
  37.00010919  39.99349087  13.88847418 -16.01151709 -12.10228336
  23.41523182  33.22699263  12.18390805  20.30242161  16.32998852
  18.75729165  37.68293059  -4.2165312   -3.27168915   8.59535628
  35.64306505  23.34647871  -1.84942499  23.42534598 -10.39107459
  12.73663916  14.7050747   29.44738631  28.10488592 -42.14864475
   2.19048802  35.8916039   38.95975997  26.89916771  42.2200429
  29.7030218   30.14800035   1.38143976   6.62183884   1.35656581
 -11.11610279   3.96857058  29.53034164   7.50493626 -15.81797589
   8.04312544  19.60781124  34.66766311  23.56115827   4.27029087
  28.14971059  32.086131    -5.37259659  23.55482762  35.99593865
   4.89077127  35.4649073   10.24335335  16.06308475  -2.12916391]
16.83709076048648
[425]:
import matplotlib.pyplot as plot

fig, ax = plot.subplots()
ax.plot(np.sort(temperatures))
[425]:
[<matplotlib.lines.Line2D at 0x13f081c10>]
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_111_1.png
[418]:
np.quantile(temperatures,0.5)
[418]:
17.18376957339831
[427]:
np.percentile(a,90) #https://numpy.org/doc/stable/reference/generated/numpy.percentile.html#numpy.percentile
[427]:
56.599999999999994
[434]:
# https://numpy.org/doc/stable/reference/generated/numpy.histogram.html#numpy.histogram
hist, bin_edges = np.histogram(temperatures)
print(hist)
print(bin_edges)

print(np.sum(hist))

[ 1  2  7  8 16 19 22 20  1  4]
[-42.14864475 -31.78755488 -21.42646501 -11.06537515  -0.70428528
   9.65680459  20.01789446  30.37898433  40.74007419  51.10116406
  61.46225393]
100
[432]:
import matplotlib.pyplot as plt
_ = plt.hist(temperatures, bins='auto')  # arguments are passed to np.histogram
plt.title("Histogram with 'auto' bins")
plt.show()
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_115_0.png
[442]:
# https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

space = np.linspace(0,1,len(temperatures))
print(space[:10])
[0.         0.01010101 0.02020202 0.03030303 0.04040404 0.05050505
 0.06060606 0.07070707 0.08080808 0.09090909]
[454]:
import matplotlib.pylab as plt

temperatures_sorted = np.sort(temperatures)
fig, ax = plt.subplots()
ax.plot(temperatures_sorted,space)
#CDF?
[454]:
[<matplotlib.lines.Line2D at 0x169e84130>]
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_117_1.png

Gestión de alertas y errores

[15]:
a , b = 0, 3
c = b/a
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb Cell 8' in <cell line: 2>()
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000007?line=0'>1</a> a , b = 0, 3
----> <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000007?line=1'>2</a> b/a

ZeroDivisionError: division by zero
[17]:
a , b = 0, 3
try:
    c = b/a
except:
    print("error")
finally:
    print("Intento realizar una linea alternativa de ejecucion")
error
Intento realizar una linea alternativa de ejecucion
[20]:
a , b = 0, 3
try:
    c = b/a
except ZeroDivisionError:
    print("error")
finally:
    print("Intento realizar una linea alternativa de ejecucion")
error
Intento realizar una linea alternativa de ejecucion
[21]:
a , b = 0, 3
d={"a":0,"b":-1}
try:
    print(d["c"])
except ZeroDivisionError:
    print("error")
finally:
    print("Intento realizar una linea alternativa de ejecucion")
Intento realizar una linea alternativa de ejecucion
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb Cell 11' in <cell line: 3>()
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000010?line=1'>2</a> d={"a":0,"b":-1}
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000010?line=2'>3</a> try:
----> <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000010?line=3'>4</a>     print(d["c"])
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000010?line=4'>5</a> except ZeroDivisionError:
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000010?line=5'>6</a>     print("error")

KeyError: 'c'
[22]:
a , b = 0, 3
d={"a":0,"b":-1}
try:
    print(d["c"])
except ZeroDivisionError:
    print("hay un cero")
except KeyError:
    print("Key no existente")
finally:
    print("Intento realizar una linea alternativa de ejecucion")
Key no existente
Intento realizar una linea alternativa de ejecucion

Docs : https://docs.python.org/3/tutorial/errors.html

[25]:
a , b = np.arange(10), np.arange(10,20)

c = b/a
print(c)
[        inf 11.          6.          4.33333333  3.5         3.
  2.66666667  2.42857143  2.25        2.11111111]
/var/folders/6j/7gfvt_29797dypw8t1wttblw0000gn/T/ipykernel_2161/475367663.py:3: RuntimeWarning: divide by zero encountered in true_divide
  c = b/a
[26]:
import math
print(math.inf in c)
print(c * np.random.rand(10))
True
[       inf 2.69224968 4.834034   1.42095976 1.47069893 2.04027743
 1.89745307 2.15531551 2.24438896 0.08422341]
[27]:
try:
    c = b/a
except RuntimeWarning:
    print("Capturo warning ? ") # No
/var/folders/6j/7gfvt_29797dypw8t1wttblw0000gn/T/ipykernel_2161/3170614431.py:2: RuntimeWarning: divide by zero encountered in true_divide
  c = b/a
[28]:
# Puedo ignorarlos
import warnings
warnings.filterwarnings("ignore")
c = b/a
print(c)
[        inf 11.          6.          4.33333333  3.5         3.
  2.66666667  2.42857143  2.25        2.11111111]
[30]:
# Puedo gestionarlos como una excepción
np.seterr(all='raise')
c = b/a
print(c)
---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)
/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb Cell 130' in <cell line: 3>()
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000131?line=0'>1</a> # Puedo gestionarlos como una excepción
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000131?line=1'>2</a> np.seterr(all='raise')
----> <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000131?line=2'>3</a> c = b/a
      <a href='vscode-notebook-cell:/Users/isaac/Projects/TxADM_notebooks/docs/notebooks/NumpyNotes/01_numpy_basics.ipynb#ch0000131?line=3'>4</a> print(c)

FloatingPointError: divide by zero encountered in true_divide
[31]:
try:
    c = b/a
except FloatingPointError:
    print("Capturo warning ? ") # Yes
Capturo warning ?

Reflexiones sobre el rendimiento computacional y algorítmico

Principales métricas percibidas por el usuario: - tiempo de respuesta, tiempo de servicio y tiempo de espera

Principales métricas para el sistema: - Productividad (trabajos/tiempo)

Las métricas están influidas porque hay una demanda sobre el servicio. Mayor demanda -> ???

El rendimiento está influido por: - Hardware: tecnología, arquitectura, - Software: sistemas operativos, lenguaje de programación, aplicaciones - Vuestra manera de programar!

[33]:
import time

start = time.time()
# do something
print("Response time: %s seconds"%(time.time()-start))
Response time: 1.9073486328125e-05 seconds
[40]:
serie = np.random.random(10000)

start = time.time()
b = []
for value in serie:
    try:
        b.append(math.sqrt(value))
    except:
        b.append(0)
end1 = time.time()-start
print("Response time: %s seconds"%(end1))
Response time: 0.0015091896057128906 seconds
[41]:
start = time.time()
b = np.sqrt(serie)
end2 = time.time()-start
print("Response time: %s seconds"%(end2))
Response time: 9.608268737792969e-05 seconds
[61]:
speedup = end1/end2
print(speedup)
print("El programa 2 es %0.2f veces más rápido que el programa 1"%speedup)
15.707196029776675
El programa 2 es 15.71 veces más rápido que el programa 1

Si necesitáis 1 hora de ejecución del programa 1, con el segundo solo, 3.8199 minutos. Si necesitáis 24 horas de ejecución del programa 1, con el segundo solo, 1.527 horas.

Atención las métricas de rendimiento suelen seguir una distribución exponencial. NO SON LINEALES!!!!

[80]:
times1 = []
for size in range(10,1000000,1000):
    serie = np.random.random(size)
    start = time.time()
    b = []
    for value in serie:
        try:
            b.append(math.sqrt(value))
        except:
            b.append(0)
    end1 = time.time()-start
    times1.append(end1)

print(".")
times2 = []
for size in range(10,1000000,1000):
    serie = np.random.random(size)
    start = time.time()
    b = np.sqrt(serie)
    end2 = time.time()-start
    times2.append(end2)

.
[81]:
import matplotlib.pyplot as plt
x = list(range(len(times1)))
fig, ax = plt.subplots()
ax.plot(x, times1, label = "programa 1")
ax.plot(x, times2, label = "programa 2")
ax.legend()
plt.show()

# No es suficientemente complejo (o sí -depende de arquitectura) para crear la curva
../../../_images/notebooks_Part2_01_NumpyNotes_01_numpy_basics_139_0.png

Caso A. Traer datos de una BBDD

d833c2a9c53d41d78a151f83e5e52be0

Caso B. Otro paradigma de programación con la explotación de la recursividad

7b4cd3981a6449179719177c86c8b766

Actividad Final

Con estos tres catálogos de datos: - A) https://datos.gob.es/es/catalogo/ea0010587-balears-illes-por-municipios-y-fenomeno-demografico-mnpd-identificador-api-t20-e301-fenom-a2020-l0-23007-px - B) https://data.cityofnewyork.us/Housing-Development/Speculation-Watch-List/adax-9mit - C) https://ec.europa.eu/eurostat/databrowser/view/gov_10a_exp/default/table?lang=en

Debéis analizar o aplicar 4 indicadores mínimos de libre elección. Es decir, según la naturaleza de los datos elegid indicadores estadísticos u otros. Como ejemplo, os proporciono el primero: - Con A) Series por nacidos vivos por residencia materna, Serie ordenada por fallecidos por el luegar de residencia, medía y desviación de los cinco indicadores disponibles por municipio - Con B) ? - Con C) ?

REQUISITOS - Tenéis que analizar los datos EXCLUSIVAMENTE con la librería de NUMPY - Se valorará la no inclusión de valores manuales, es decir, que el código sea robusto y lo más genérico posible. - Se valorará la inclusión de gráficas (plots).

Entrega de la práctica: - Se subirá el enlace de vuestro github personal a la Tarea específica de Auladigital. - Se ha de realizar un notebook por cada catálogo de datos. - Hay que incluir un fichero README.md que contenga el informe de la práctica (indicadores utilizados, y aspectos y conclusiones extraidas de cada uno de ellos) - Se recomienda la siguiente estructura por directorios: /part2/numpy_activity/notebookA….