minte9
LearnRemember




Vectors

A vector is an one-dimensional array.
 
import numpy as np
from icecream import ic

row_vector = np.array([1, 2, 3])
column_vector = np.array([[1], [4], [3]])

ic(row_vector)
ic(column_vector)

"""
ic| row_vector: array([1, 2, 3])
ic| column_vector: array([[1],
                          [4],
                          [3]])
"""

Matrices

Numpy main data structure is the multidimensional array.
 
import numpy as np
from icecream import ic

# Three rows, four columns matrix
A = np.array([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
])

ic(A)
ic(A.shape)
ic(A.size)
ic(A.ndim) # array dimension number

"""
    ic| A: array([[ 1,  2,  3,  4],
                [ 5,  6,  7,  8],
                [ 9, 10, 11, 12]])
    ic| A.shape: (3, 4)
    ic| A.size: 12
    ic| A.ndim: 2
"""

Sparse Matrices

A sparse matrix stores only non-zero elements, for computation savings. Compress sparce row (CSR) matrices contain indexes of non-zero values.
 
import numpy as np
from scipy import sparse
from icecream import ic

"""
    Sample matrix (Netfilx movie/users count):
    Every column represents a movie, and every row represents an user
    On values we have how many times each user watched that movie 
    We can see that we have multiple zero values, which is normal.
"""
M = np.array([
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # User vector
    [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
    [3, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
])

# Sparse matrix (CSR)
sparse_matrix = sparse.csr_matrix(M)

ic(sparse_matrix)
print(sparse_matrix)
print("User (index 2) watched the movie (index 0) =", sparse_matrix[2, 0], "times")


"""
ic| sparse_matrix: <3x10 sparse matrix of type '<class 'numpy.int64'>'
                        with 2 stored elements in Compressed Sparse Row format>
  (1, 1)        1
  (2, 0)        3

  User (index 2) watched the movie (index 0) = 3 times
"""

Vectorize

Vectorize in numpy converts a function into another one that can operate on numpy arrays in an element-wise fashion.
 
import numpy as np

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

"""
  Create a vectorized function that adds 100 to each element it receives
  The function is applied to each element of the array A individually
"""
B = np.vectorize(lambda x: x + 100)(A)

# Output result
print(B)

"""
     [[101 102 103]
      [104 105 106]
      [107 108 109]]
"""

Broadcasting

With vectorize we can speed up the code, without a loop. With broadcasting different dimensions are allowed.
 
import numpy as np

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

B = A + 100
print(B)

"""
    [[101 102 103]
     [104 105 106]
     [107 108 109]]
"""



  Last update: 70 days ago