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 Sparse Matrices
Sparse matrices only store non-zero elements CSR sparse matrices contain indices matrix = sparse.csr_matrix(matrix) print(matrix) # (2, 0) 3
Sparse Matrices
p03 Sparse matrices only store non-zero elements.
""" Sparse matrices
Sparse matrices only store non-zero elements, for computation savings.
Compress sparce row (CSR) matrices contain indices of non-zero values.
Netflix movies/users example:
Columns are every movie on Netflix
Rows are every Netflix user
Values are how many times a user watched that movie
"""
import numpy as np
from scipy import sparse
matrix = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
matrix_sparse = sparse.csr_matrix(matrix) # CSR matrix
print(matrix_sparse)
# (1, 1) 1
# (2, 0) 3
print()
assert matrix_sparse[1, 1] == 1
assert matrix_sparse[2, 0] == 3
# Random
np.random.seed(0)
R1 = np.random.random(3) # generate floats
R2 = np.random.randint(1, 11, 3) # generate integers
print(R1) # [0.5488135 0.71518937 0.60276338]
print(R2) # [4 8 10]
➥ Questions
Last update: 48 days ago