Last active
October 19, 2022 09:50
-
-
Save neubig/86c7d1730016fe1337b0d3577f1845dc to your computer and use it in GitHub Desktop.
Examples of linear algebra in numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import sys | |
################# Explanation ################## | |
# This is a function to calculate house prices h(x) = -40 + 0.25x | |
# The first term (-40) is the base price, and "x" is the number of square feet in the house | |
################################################ | |
# Set up the function | |
my_function = np.array([-40, 0.25]) | |
# Read the data from the file | |
with open(sys.argv[1], "r") as my_file: | |
my_data = np.loadtxt(my_file) | |
# Take the dot product | |
my_result = my_data.dot(my_function) | |
# Print the result | |
for x in my_result: | |
print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a numpy implementation of the linear algebra tutorial from | |
# Andrew Ng's machine learning class: | |
# https://class.coursera.org/ml-005/lecture | |
import numpy as np | |
##### Matricies and Vectors | |
# Define two arrays, one 4x3, one 3x2 | |
A = np.array([[1402, 191], [1371, 821], [949, 1437], [147, 1448]]) | |
B = np.array([[1, 2, 3], [4, 5, 6]]) | |
# Print the dimensions of each | |
print(A.shape) | |
print(B.shape) | |
# Print some of the elements | |
# NOTE: Elements are zero-indexed, so the first element will be 0,0 | |
print(A[0,0]) | |
print(A[0,1]) | |
print(A[2,1]) | |
print(A[3,0]) | |
# Define a 4 x 1 vector | |
y = np.array([460, 232, 315, 178]) | |
##### Addition and Scalar Multiplication | |
# Define two arrays | |
A = np.array([[1, 0 ], [2, 5], [3, 1]]) | |
B = np.array([[4, 0.5], [2, 5], [0, 1]]) | |
# Add them together | |
print(A+B) | |
# Multiply/divide by a scalar | |
print(3*A) | |
print(A/4) | |
##### Matrix/vector Multiplication | |
# Define an matrix and a vector | |
A = np.array([[1, 3], [4, 0], [2, 1]]) | |
y = np.array([1, 5]) | |
# Perform matrix/vector multiplication | |
print(A.dot(y)) | |
# Calculate the function | |
data = np.array([[1, 2104], [1, 1416], [1, 1534], [1, 852]]) | |
func = np.array([-40, 0.25]) | |
print(data.dot(func)) | |
##### Matrix/matrix Multiplication | |
# Define matrices and multiply them | |
A = np.array([[1, 3, 2], [4, 0, 1]]) | |
B = np.array([[1, 3], [0, 1], [5, 2]]) | |
print(A.dot(B)) | |
##### Matrix properties | |
# Test the commutative property | |
A = np.array([[1, 1], [0, 0]]) | |
B = np.array([[0, 0], [2, 0]]) | |
print(A.dot(B)) | |
print(B.dot(A)) | |
# Test the associative property | |
C = np.array([[1, 2, 3], [4, 5, 6]]) | |
print(A.dot(B.dot(C))) | |
print(A.dot(B).dot(C)) | |
# Create and use the identity matrix | |
I = np.identity(2) | |
print(A.dot(I)) | |
print(I.dot(A)) | |
##### Inverse and transpose | |
# Test the inverse | |
A = np.array([[1, 2], [3, 4]]) | |
Ainv = np.linalg.inv(A) | |
print(Ainv) | |
print(Ainv.dot(A)) | |
A = np.array([[1, 1], [0, 0]]) | |
Ainv = np.linalg.inv(A) | |
# Transpose | |
A = np.array([[1, 2], [3, 4]]) | |
print(np.transpose(A)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import numpy as np | |
##### Compare 1000x1000 matrix-matrix multiplication speed | |
# Set up the variables | |
SIZE = 200 | |
A = np.random.rand(SIZE,SIZE) | |
B = np.random.rand(SIZE,SIZE) | |
# Try the naive way in Python | |
start = timeit.default_timer() | |
# --> Matrix multiplication in pure Python | |
out2 = np.zeros((SIZE,SIZE)) | |
for i in range(SIZE): | |
for j in range(SIZE): | |
for k in range(SIZE): | |
out2[i,k] += A[i,j]*B[j,k] | |
time_spent = timeit.default_timer() - start | |
print(time_spent) | |
# Try using numpy | |
start = timeit.default_timer() | |
# --> Matrix multiplication in numpy | |
out1 = A.dot(B) | |
time_spent = timeit.default_timer() - start | |
print(time_spent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment