This file contains hidden or 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
| # List comprehension to create a list the numbers 1 to 20 squared. | |
| list_of_squares = [x**2 for x in range(1, 20)] | |
| # The above list comprehension is the same as this for-loop: | |
| list_of_squares = [] | |
| for x in range(1, 20): | |
| list_of_squares.append(x**2) |
This file contains hidden or 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
| def list_comp_matrix_multiplication(A, B): | |
| # First, we could check to make sure the matrices can be multiplied together. | |
| # | |
| # (code here) | |
| # Then, we could return the matrix multiplication using a list comprehension. | |
| # | |
| return [ # some list comprehension here ] |
This file contains hidden or 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 | |
| def for_loop_matrix_multiplication(A, B): | |
| # A and B might come in as lists. We'll figure out | |
| # how to deal with this eventually, but for now let's | |
| # convert them to NumPy arrays so that we can transpose | |
| # matrix B. (This is so we can iterate through the columns | |
| # of B, rather than the rows.) | |
| A = np.array(A) | |
| B = np.array(B) |
This file contains hidden or 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 | |
| def for_loop_matrix_multiplication2(A, B): | |
| """Second version of a for loop matrix multiplication. | |
| In this version, we remove the np.dot() function.""" | |
| A = np.array(A) | |
| B = np.array(B) | |
| new_matrix = np.zeros((A.shape[0], B.shape[1])) |
This file contains hidden or 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 | |
| def for_loop_matrix_multiplication3(A, B): | |
| """Third version of a for loop matrix multiplication. | |
| In this version, we replace the NumPy arrays with lists | |
| and figure out how to store the resulting dot product values | |
| in the correct places for the new matrix.""" | |
| # We're going to leave these as NumPy arrays for now because we're | |
| # still transposing B using the "B.T" functionality of np.array. |
This file contains hidden or 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 | |
| def for_loop_matrix_multiplication4(A, B): | |
| """Fourth version of a for loop matrix multiplication. | |
| In this version, we replace B.T with zip(*B) in order to | |
| transpose B without needing to convert it to a NumPy array first. | |
| This means we can remove the opening np.array conversion lines too. | |
| """ | |
| new_matrix = [] |
This file contains hidden or 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 | |
| def for_loop_matrix_multiplication(A, B): | |
| """Fifth and final version of a for loop matrix multiplication.""" | |
| new_matrix = [] | |
| for row in A: | |
| new_row = [] | |
| for col in zip(*B): | |
| new_row.append(sum([x*y for (x, y) in zip(row, col)])) |
This file contains hidden or 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 | |
| def list_comp_matrix_multiplication1(A, B): | |
| """First version of the list comprehension matrix multiplication. | |
| In this version, we create a list comprehension out of our inner for loop. | |
| """ | |
| new_matrix = [] | |
| for row in A: |
This file contains hidden or 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 | |
| def list_comp_matrix_multiplication2(A, B): | |
| """Second version of the list comprehension matrix multiplication. | |
| In this version, we create a list comprehension out of our outer for loop. | |
| """ | |
| # The following line is no longer needed: | |
| # new_matrix = [] | |
This file contains hidden or 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 | |
| def list_comp_matrix_multiplication(A, B): | |
| """Third and final version of the list comprehension matrix multiplication.""" | |
| return [[sum([x*y for (x, y) in zip(row, col)]) for col in zip(*B)] for row in A] | |
| if __name__ == '__main__': | |
| A = [[1, 2, 3], [4, 5, 6]] | |
| B = [[7, 8], [9, 10], [11, 12]] | |
OlderNewer