Numpy: Arreglos y Matrices

Profesor: Santiago Quiñones

Lenguaje Programación - Ingeniería Industrial

Contenidos

Arreglos numpy

Resumen de listas

• Poderosas
• Colección de valores
• Mantener datos de diferentes tipos
• Cambiar, agregar, remover
• Necesidad de la ciencia de datos
• Operaciones matemáticas sobre colecciones

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 ** 2``
``TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'``

Abrir enlace

Solución: Numpy

• Python numérico
• Alternativa a las Listas de Python: Arreglos Numpy
• Cálculos en arreglos enteros
• Fácil y rápido
• Instalación
• En la terminal: `pip install numpy`

Numpy

``````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 np``

Numpy: 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_list``
``numpy_array + numpy_array``
``array([2, 4, 6])``

Subconjuntos en Numpy

``bmi``
``bmi[1]``
``20.975``
``array([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 columnas``
``````np.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]])     1``````
``np_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]])     1``````
``np_2d[0][2]               ``
``1.71``
``np_2d[0, 2]               ``
``1.71``

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]])     1``````
``np_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]])     1``````
``np_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

By lsantiago

• 68