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 Matrices
The main data structure is the multidimensional array Arrays are zero-indexed, first element index is 0 vector = np.array([1, 2, 3]) matrix = np.array([ [1], [2], [3]])
Vector
p01 A vector is an one-dimensional array.
""" Numpy Vectors
One-dimensional arrays
"""
import numpy as np
row = np.array([1, 2, 3]) # row vector
column = np.array([ # column vector
[1],
[4],
[3],
])
print(row) # [1 2 3]
print(column)
# [[1]
# [4]
# [3]]
Matrix
p02 Numpy main data structure is the multidimensional array.
""" Create Matrices
Numpy is the foundation of the Python machine learning stack
The main data structure is the multidimensional array
Arrays are zero-indexed, first element index is 0
Use ':' to select everithing 'up to' or 'after'
"""
import numpy as np
# Matrix
matrix = np.array([ # three rows, two columns
[1, 2],
[1, 2],
[1, 2],
])
print(matrix) # [[1 2] [1 2] [1 2]]
print()
# Describe
matrix = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
])
print(matrix.shape) # (3, 4)
print(matrix.size) # 12
print(matrix.ndim) # 2
# Extract
vector = np.array([1, 2, 3, 4, 5, 6])
print(vector[:]) # [1 2 3 4 5 6]
print(vector[:3]) # [1 2 3]
print(vector[3:]) # [4 5 6]
print(vector[-1]) # 6
print()
assert(vector[:] == [1, 2, 3, 4, 5, 6]) .all() # passed
assert(vector[:3] == [1, 2, 3]) .all() # passed
assert(vector[3:] == [4, 5, 6]) .all() # passed
assert(vector[-1] == 6) # passed
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
print(matrix[1, 1]) # 5
print(matrix[:2, :]) # [[1 2 3] [4 5 6]] # first two rows
print(matrix[:, 1:2]) # [[2] [5] [8]] # all rows, second column
print()
assert(matrix[1, 1] == 5) # passed
assert(matrix[:2, :] == [[1, 2, 3], [4, 5, 6]]) .all() # passed
assert(matrix[:, 1:2] == [[2], [5], [8]]) .all() # passed
➥ Questions
Last update: 47 days ago