Programación

“Primero resuelve el problema. Entonces, escribe el código.” — John Johnson

viernes, 19 de octubre de 2018

Lo de NumPY

import numpy as np

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print("forma")
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"

print("Matriz")
a1 = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a1)
# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:

row_r1 = a1[0, :]   # Rank 1 view of the second row of a
row_r2 = a1[1:2, :]  # Rank 2 view of the second row of a
print("renglon 0")

print(row_r1, row_r1.shape)  # Prints "[5 6 7 8] (4,)"
print("renglon 1")
print(row_r2, row_r2.shape)  # Prints "[[5 6 7 8]] (1, 4)"

# We can make the same distinction when accessing columns of an array:
col_r1 = a1[:, 0]
col_r2 = a1[:, 1:2]
print("columna 0")
print(col_r1, col_r1.shape)  # Prints "[ 2  6 10] (3,)"
print("columna 1")
print(col_r2, col_r2.shape)  # Prints "[[ 2]
                             #          [ 6]
                             #          [10]] (3, 1)"

No hay comentarios:

Publicar un comentario