MLearning
/
Numpy
- 1 Supervised ML 4
-
Classifier S
-
Linear model S
-
Basis expansion S
-
Regularization S
- 2 Matplotlib 2
-
Subplots S
-
Pyplot S
- 3 Datasets 4
-
Iris species S
-
Diabetes S
-
Breast cancer S
-
Simulated data S
- 4 Numpy 7
-
Matrices S
-
Sparse matrices S
-
Vectorize S
-
Average S
-
Standard deviation S
-
Reshape S
-
Multiplication S
- 5 Pandas 5
-
Read data S
-
Data cleaning S
-
Find values S
-
Group rows S
-
Merge data S
- 6 Calculus 2
-
Derivatives S
-
Integrals S
- 7 Algorithms 3
-
K nearest neighbors S
-
Linear regression S
-
Gradient descent S
S
R
Q
ML Numpy Vectorize
Numpy allows operation on arrays, even if their dimension are not the same matrix = np.array([1, 2], [3, 4]) matrix = matrix + 100
Vectorization
p06 Vectorization is essentialy a for loop.
""" Vectorization
It is essentialy a for loop that does not increase performance
"""
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
# Add 100 every element
add_100 = lambda i: i + 100 # create function that add 100
vectorize_add_100 = np.vectorize(add_100) # create vectorized function
vectorized_matrix = vectorize_add_100(matrix) # apply vectorization to all
# One line code
vectorized_matrix = np.vectorize(lambda i: i + 100)(matrix)
print(vectorized_matrix); print()
# [[101 102 103]
# [104 105 106]
# [107 108 109]]
# Brodcasting (different dimensions are allowed)
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
matrix = matrix + 100
print(matrix); print()
# [[101 102 103]
# [104 105 106]
# [107 108 109]]
➥ Questions
Last update: 48 days ago