This project focuses on understanding and applying basic linear algebra concepts using Python. It involves vector and matrix operations, including slicing, element-wise operations, concatenation, and matrix multiplication, leveraging the NumPy library.
- Introduction to Vectors
- What is a Matrix?
- Transpose
- Understanding the dot product
- Matrix Multiplication
- What is the relationship between matrix multiplication and the dot product?
- The Dot Product, Matrix Multiplication, and the Magic of Orthogonal Matrices (advanced)
- numpy tutorial (until Shape Manipulation (excluded))
- numpy basics (until Universal Functions (included))
- array indexing
- numerical operations on arrays
- Broadcasting
- numpy mutations and broadcasting
- numpy.ndarray
- numpy.ndarray.shape
- numpy.transpose
- numpy.ndarray.transpose
- numpy.matmul
At the end of this project, you are expected to be able to explain to anyone, without the help of Google:
- What is a vector?
- What is a matrix?
- What is a transpose?
- What is the shape of a matrix?
- What is an axis?
- What is a slice?
- How do you slice a vector/matrix?
- What are element-wise operations?
- How do you concatenate vectors/matrices?
- What is the dot product?
- What is matrix multiplication?
- What is Numpy?
- What is parallelization and why is it important?
- What is broadcasting?
- Vectors and matrices
- Transpose and shape of a matrix
- Element-wise operations
- Matrix multiplication and dot product
- NumPy library usage
Python Scripts
- Allowed editors: vi, vim, emacs
- All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.9)
- Your files will be executed with numpy (version 1.25.2)
- All your files should end with a new line
- The first line of all your files should be exactly #!/usr/bin/env python3
- A README.md file, at the root of the folder of the project, is mandatory
- Your code should follow pycodestyle (version 2.11.1)
- All your modules should have documentation (python3 -c 'print(import("my_module").doc)')
- All your classes should have documentation (python3 -c 'print(import("my_module").MyClass.doc)')
- All your functions (inside and outside a class) should have documentation (python3 -c 'print(import("my_module").my_function.doc)' and python3 -c 'print(import("my_module").MyClass.my_function.doc)')
- Unless otherwise noted, you are not allowed to import any module
- All your files must be executable
- The length of your files will be tested using wc
- Slice Me Up - Slicing arrays in various ways. Complete the following source code (found below):
arr1
should be the first two numbers ofarr
arr2
should be the last five numbers ofarr
arr3
should be the 2nd through 6th numbers ofarr
- You are not allowed to use any loops or conditional statements
- Your program should be exactly 8 lines
The program should consist of exactly 8 lines.
alexa@ubuntu-xenial:linear_algebra$ cat 0-slice_me_up.py
#!/usr/bin/env python3
arr = [9, 8, 2, 3, 9, 4, 1, 0, 3]
arr1 = # your code here
arr2 = # your code here
arr3 = # your code here
print("The first two numbers of the array are: {}".format(arr1))
print("The last five numbers of the array are: {}".format(arr2))
print("The 2nd through 6th numbers of the array are: {}".format(arr3))
alexa@ubuntu-xenial:linear_algebra$ ./0-slice_me_up.py
The first two numbers of the array are: [9, 8]
The last five numbers of the array are: [9, 4, 1, 0, 3]
The 2nd through 6th numbers of the array are: [8, 2, 3, 9, 4]
alexa@ubuntu-xenial:linear_algebra$ wc -l 0-slice_me_up.py
8 0-slice_me_up.py
alexa@ubuntu-xenial:linear_algebra$
- GitHub repository: holbertonschool-machine_learning
- Directory: math/linear_algebra
- File: 0-slice_me_up.py
- Trim Me Down - Extracting specific columns from a matrix. Complete the following source code (found below):
the_middle
should be a 2D matrix containing the 3rd and 4th columns ofmatrix
- You are not allowed to use any conditional statements
- You are only allowed to use one
for
loop - Your program should be exactly 6 lines
alexa@ubuntu-xenial:linear_algebra$ cat 1-trim_me_down.py
#!/usr/bin/env python3
matrix = [[1, 3, 9, 4, 5, 8], [2, 4, 7, 3, 4, 0], [0, 3, 4, 6, 1, 5]]
the_middle = []
# your code here
print("The middle columns of the matrix are: {}".format(the_middle))
alexa@ubuntu-xenial:linear_algebra$ ./1-trim_me_down.py
The middle columns of the matrix are: [[9, 4], [7, 3], [4, 6]]
alexa@ubuntu-xenial:linear_algebra$ wc -l 1-trim_me_down.py
6 1-trim_me_down.py
alexa@ubuntu-xenial:linear_algebra$
- GitHub repository: holbertonschool-machine_learning
- Directory: math/linear_algebra
- File: 1-trim_me_down.py
- Size Me Please - Calculating the shape of a matrix.
Write a function
def matrix_shape(matrix):
that calculates the shape of a matrix:
- You can assume all elements in the same dimension are of the same type/shape
- The shape should be returned as a list of integers
alexa@ubuntu-xenial:linear_algebra$ cat 2-main.py
#!/usr/bin/env python3
matrix_shape = __import__('2-size_me_please').matrix_shape
mat1 = [[1, 2], [3, 4]]
print(matrix_shape(mat1))
mat2 = [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]],
[[16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30]]]
print(matrix_shape(mat2))
alexa@ubuntu-xenial:linear_algebra$ ./2-main.py
[2, 2]
[2, 3, 5]
alexa@ubuntu-xenial:linear_algebra$
- GitHub repository: holbertonschool-machine_learning
- Directory: math/linear_algebra
- File: 2-size_me_please.py
- Flip Me Over - Transposing a matrix.
Write a function
def matrix_transpose(matrix):
that returns the transpose of a 2D matrix,matrix
:
- You must return a new matrix
- You can assume that
matrix
is never empty - You can assume all elements in the same dimension are of the same type/shape
alexa@ubuntu-xenial:linear_algebra$ cat 3-main.py
#!/usr/bin/env python3
matrix_transpose = __import__('3-flip_me_over').matrix_transpose
mat1 = [[1, 2], [3, 4]]
print(mat1)
print(matrix_transpose(mat1))
mat2 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15],
[16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30]]
print(mat2)
print(matrix_transpose(mat2))
alexa@ubuntu-xenial:linear_algebra$ ./3-main.py
[[1, 2], [3, 4]]
[[1, 3], [2, 4]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30]]
[[1, 6, 11, 16, 21, 26], [2, 7, 12, 17, 22, 27], [3, 8, 13, 18, 23, 28], [4, 9, 14, 19, 24, 29], [5, 10, 15, 20, 25, 30]]
alexa@ubuntu-xenial:linear_algebra$
- GitHub repository: holbertonschool-machine_learning
- Directory: math/linear_algebra
- File: 3-flip_me_over.py
- Line Up - Adding two arrays element-wise.
Write a function
def add_arrays(arr1, arr2):
that adds two arrays element-wise:
- You can assume that
arr1
andarr2
are lists of ints/floats - You must return a new list
- If
arr1
andarr2
are not the same shape, returnNone
alexa@ubuntu-xenial:linear_algebra$ cat 4-main.py
#!/usr/bin/env python3
add_arrays = __import__('4-line_up').add_arrays
arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
print(add_arrays(arr1, arr2))
print(arr1)
print(arr2)
print(add_arrays(arr1, [1, 2, 3]))
alexa@ubuntu-xenial:linear_algebra$ ./4-main.py
[6, 8, 10, 12]
[1, 2, 3, 4]
[5, 6, 7, 8]
None
alexa@ubuntu-xenial:linear_algebra$
- GitHub repository: holbertonschool-machine_learning
- Directory: math/linear_algebra
- File: 4-line_up.py
- Across The Planes - Adding two matrices element-wise.
Write a function
def add_matrices2D(mat1, mat2):
that adds two matrices element-wise:
- You can assume that
mat1
andmat2
are 2D matrices containing ints/floats - You can assume all elements in the same dimension are of the same type/shape
- You must return a new matrix
- If
mat1
andmat2
are not the same shape, returnNone
- Howdy Partner - Concatenating two arrays.
- Gettin’ Cozy - Concatenating two matrices along a specific axis.
- Ridin’ Bareback - Performing matrix multiplication.
- Let The Butcher Slice It - Complex slicing on matrices.
- I’ll Use My Scale - Determining the shape of a NumPy array.
- The Western Exchange - Transposing a NumPy array.
- Bracing The Elements - Element-wise operations on NumPy arrays.
- Cat's Got Your Tongue - Concatenating NumPy arrays along a specific axis.
- Saddle Up - Matrix multiplication using NumPy.
- Slice Like A Ninja (Advanced) - Advanced slicing techniques on NumPy arrays.
- The Whole Barn (Advanced) - Adding matrices of varying dimensions.
- Squashed Like Sardines (Advanced) - Advanced matrix concatenation.