Profesor: Santiago Quiñones
Lenguaje Programación - Ingeniería Industrial
Realizar heteroevaluación
Arreglos numpy
Resumen de listas
Ilustración
height = [1.73, 1.68, 1.71, 1.89, 1.79]
print(height)[1.73, 1.68, 1.71, 1.89, 1.79]weight = [65.4, 59.2, 63.6, 88.4, 68.7]
print(weight)[65.4, 59.2, 63.6, 88.4, 68.7]weight / height ** 2TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'Abrir enlace
Solución: Numpy
pip install numpyNumpy
np_height = np.array(height)
print(np_height)array([1.73, 1.68, 1.71, 1.89, 1.79])np_weight = np.array(weight)
print(np_weight)array([65.4, 59.2, 63.6, 88.4, 68.7])bmi = np_weight / np_height ** 2
print(bmi)array([21.85171573, 20.97505669, 21.75028214, 24.7473475, 21.44127836])import numpy as npNumpy: observaciones
np.array([1.0, "is", True])array(['1.0', 'is', 'True'], dtype='<U32')Arreglos numpy: contendrá un solo tipo
Numpy: observaciones
python_list = [1, 2, 3]
numpy_array = np.array([1, 2, 3])[1, 2, 3, 1, 2, 3]Diferentes tipos: ¡comportamiento diferente!
python_list + python_listnumpy_array + numpy_arrayarray([2, 4, 6])Subconjuntos en Numpy
bmibmi[1]20.975array([21.85171573, 20.97505669, 21.75028214, 24.7473475, 21.44127836])Arreglos Numpy 2D
Matrices
Arreglos Numpy 2D
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, 68.7]])
print(np_2d) array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, 68.7]])np_2d.shape (2, 5) # 2 filas, 5 columnasnp.array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, "68.7"]])
array([['1.73', '1.68', '1.71', '1.89', '1.79'],
['65.4', '59.2', '63.6', '88.4', '68.7']])Subconjuntos
0 1 2 3 4
array([[ 1.73, 1.68, 1.71, 1.89, 1.79], 0
[ 65.4, 59.2, 63.6, 88.4, 68.7]]) 1np_2d[0] array([[1.73, 1.68, 1.71, 1.89, 1.79])Subconjuntos
0 1 2 3 4
array([[ 1.73, 1.68, 1.71, 1.89, 1.79], 0
[ 65.4, 59.2, 63.6, 88.4, 68.7]]) 1np_2d[0][2] 1.71np_2d[0, 2] 1.71Subconjuntos
0 1 2 3 4
array([[ 1.73, 1.68, 1.71, 1.89, 1.79], 0
[ 65.4, 59.2, 63.6, 88.4, 68.7]]) 1np_2d[:, 1:3] array([[1.68, 1.71],
[59.2, 63.6]])Subconjuntos
0 1 2 3 3
array([[ 1.73, 1.68, 1.71, 1.89, 1.79], 0
[ 65.4, 59.2, 63.6, 88.4, 68.7]]) 1np_2d[:, 1:3] array([[1.68, 1.71],
[59.2, 63.6]])np_2d[1, :] array([65.4, 59.2, 63.6, 88.4, 68.7])Subconjuntos
np_table
Subconjuntos
np_table
Subconjuntos
np_table
Subconjuntos
np_table
Subconjuntos
np_table
Subconjuntos
np_table
Subconjuntos
np_table
Modificando matrices