Introducción a NumPy — 14:24 min

  • 14:24 min | Última modificación: Octubre 12, 2021

[1]:
#
# Importación de la libreria y creación de un
# array
# ===============================================
#
import numpy as np

some_list = [1, 2, 3, 4, 5, 6]
type(some_list)
[1]:
list
[2]:
#
# Creación de un array a partir de uan lista
# ===============================================
#
some_array = np.array(some_list)

display(
    type(some_array),
    some_array,
    some_array.shape,
)
numpy.ndarray
array([1, 2, 3, 4, 5, 6])
(6,)
[3]:
#
# Función ones()
# ===============================================
#
ones_array = np.ones(5)
ones_array
[3]:
array([1., 1., 1., 1., 1.])
[4]:
np.ones((3, 4))
[4]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
[5]:
#
# Función arange()
# ===============================================
#
range_array = np.arange(10)
range_array
[5]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[6]:
#
# Función linspace()
# ===============================================
#
linspace_array = np.linspace(
    start=1,
    stop=5,
    num=20,
    endpoint=True,
)

linspace_array
[6]:
array([1.        , 1.21052632, 1.42105263, 1.63157895, 1.84210526,
       2.05263158, 2.26315789, 2.47368421, 2.68421053, 2.89473684,
       3.10526316, 3.31578947, 3.52631579, 3.73684211, 3.94736842,
       4.15789474, 4.36842105, 4.57894737, 4.78947368, 5.        ])
[7]:
#
# Números Aleatorios
# ===============================================
#
display(
    np.random.random(
        size=(3, 4),
    ),
    "",
    np.random.uniform(
        low=1,
        high=2,
        size=(3, 4),
    ),
    "",
    np.random.normal(
        loc=1,
        scale=2,
        size=(3, 4),
    ),
)
array([[0.7841071 , 0.08279329, 0.85304076, 0.31961308],
       [0.14432893, 0.44994109, 0.04584197, 0.47972086],
       [0.12858044, 0.59402962, 0.44033422, 0.3265233 ]])
''
array([[1.80534142, 1.76071563, 1.47248309, 1.57283928],
       [1.85959762, 1.45114157, 1.45125685, 1.67641946],
       [1.37704914, 1.28532614, 1.38548571, 1.54240142]])
''
array([[-0.61405818, -0.92727512,  1.38932549, -0.10729285],
       [ 2.99013011, -1.0328996 , -0.60144859,  0.62906101],
       [ 1.49205984,  2.11208538,  1.27538646, -2.01684845]])
[8]:
#
# Selección de elementos
# ===============================================
#
some_array = np.arange(10)

display(
    some_array[0],
    some_array[-1],
    some_array[2:7],
)
0
9
array([2, 3, 4, 5, 6])
[24]:
#
# Selección de la primera columna
#
some_matrix = np.array(
    [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ]
)
some_matrix[:, 0]
[24]:
array([1, 4, 7])
[10]:
#
# Segunda fila
#
some_matrix[1, :]
[10]:
array([4, 5, 6])
[11]:
#
# Segunda columna
#
some_matrix[:, 1]
[11]:
array([2, 5, 8])
[12]:
#
# Método reshape()
# ===============================================
#
display(
    range_array.reshape(2, 5),
    "---",
    range_array.reshape(5, 2),
    "---",
    range_array.reshape(5, 2).shape,
)
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
'---'
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
'---'
(5, 2)
[13]:
#
# Método reshape()
# ===============================================
#
display(
    range_array,
    "---",
    range_array.reshape(-1, 1),
)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
'---'
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
[14]:
#
# Función column_stack()
# ===============================================
#
a = np.array((1, 2, 3))
b = np.array((2, 3, 4))
ab = np.column_stack((a, b))
ab
[14]:
array([[1, 2],
       [2, 3],
       [3, 4]])
[15]:
#
# Funciones estadísticas
# ===============================================
#
display(
    np.mean(range_array),
    np.median(range_array),
    np.var(range_array),
    np.std(range_array),
)
4.5
4.5
8.25
2.8722813232690143
[16]:
#
# Operaadores aritméticos
# ===============================================
#
first_array = np.random.random(4)
second_array = np.random.random(4)

display(
    first_array + second_array,
    first_array / second_array ** 2,
)
array([0.92214097, 0.92085579, 0.32797461, 0.47011169])
array([3.79065793e-01, 6.25521253e+03, 3.73954472e+01, 3.69668464e-01])
[17]:
#
# Operaciones matriciales
# ===============================================
#
first_matrix = np.random.uniform(
    low=1,
    high=2,
    size=(3, 3),
)

second_matrix = np.random.uniform(
    low=10,
    high=11,
    size=(3, 3),
)

first_matrix + second_matrix
[17]:
array([[11.63139833, 12.01795548, 12.65783554],
       [12.26671018, 12.68652686, 11.44699822],
       [12.45564177, 12.40830808, 11.7421942 ]])
[18]:
#
# Operadores lógicos
# ===============================================
#
np.arange(10) > 5
[18]:
array([False, False, False, False, False, False,  True,  True,  True,
        True])
[19]:
#
# Selección lógica
# ===============================================
#
some_array = np.arange(10)
bool_array = some_array > 5
some_array[bool_array]
[19]:
array([6, 7, 8, 9])
[20]:
array_gt_3 = some_array > 3
array_lt_8 = some_array < 8
array_gt_3_and_lt_8 = np.logical_and(array_gt_3, array_lt_8)
array_gt_3_and_lt_8
[20]:
array([False, False, False, False,  True,  True,  True,  True, False,
       False])
[21]:
array_not_gt_3_or_not_lt_8 = np.logical_or(
    np.logical_not(array_gt_3), np.logical_not(array_lt_8)
)
array_not_gt_3_or_not_lt_8
[21]:
array([ True,  True,  True,  True, False, False, False, False,  True,
        True])
[22]:
#
# Iteración sobre arrays
# ===============================================
#
for value in range_array:
    print(value)
0
1
2
3
4
5
6
7
8
9
[23]:
#
# Iteración sobre una matriz
# ===============================================
#

display(first_matrix)

print("")

for value in first_matrix:
    print(value)

print("")

for value in np.nditer(first_matrix):
    print(value)
array([[1.51209191, 1.37778694, 1.74292753],
       [1.47442418, 1.77997925, 1.38107561],
       [1.46678555, 1.45803285, 1.530237  ]])

[1.51209191 1.37778694 1.74292753]
[1.47442418 1.77997925 1.38107561]
[1.46678555 1.45803285 1.530237  ]

1.5120919101970627
1.3777869434537025
1.7429275260775094
1.4744241820936903
1.779979249861668
1.381075613619974
1.4667855496143267
1.4580328518115055
1.5302369970895575