Disclaimer: Everything I write here is from my personal experience. I learned programming mostly by practicing and through working. There can be many other ways to do this. I feel comfortable doing stuffs in these ways. This doesn't mean this is the best approach. Infact, I think in most cases there are better ways to do these. But, I hope these will be helpful for many beginner programmers.
Import python numpy library to your local machine. Adding "as np" simply ensures that you don't need to write "numpy" each time you use numpy, rather writing "np" would be enough.
import numpy as np
if you want to create a new matrix and you know the size of the matrix, the best way is to create a matrix where all values are zero. writing np.zeros((nyumber of elements in first axis, number of elements in second axis, number of elements in third axis, .......)) is the way to do this. Here, I am showing a 2d matrix first
aMatrix = np.zeros((5,5))
print(aMatrix)
[[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]]
Similarly we can create a matrix where all the values are "1".
bMatrix = np.ones((7,7))
print(bMatrix)
[[1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1.]]
We can create a matrix with "zeros" and where shapes are similar to bMatrix by calling "np.zeros_like(matrix_name)". A similar function exists for "ones" also: "np.ones_like(matrix name)
cMatrix = np.zeros_like(bMatrix)
print(cMatrix)
[[0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0.]]
dMatrix = np.ones_like(aMatrix)
print(dMatrix)
[[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]
If we want to know the shape of the matrix we can call "shape". It would be like "matrix_name.shape"
print(dMatrix.shape)
(5, 5)
If we just want to get number of rows we can use "dMatrix.shape[0]", to get column number we have to write "dMatrix.shape[1]", if there is a third axis we can write "dMatrix.shape[2]"
print("Number of rows: ", dMatrix.shape[0])
print("Number of columns: ", dMatrix.shape[1])
Number of rows: 5 Number of columns: 5
Now if we want, we can reshape the matrix with "reshape" attribute. For example we want to reshape the dMatrix in (25, 1) shape.
dMatrix = dMatrix.reshape(25, 1)
print(dMatrix)
print("shape of dMatrix: ", dMatrix.shape)
[[1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.]] shape of dMatrix: (25, 1)
Transposing a matrix is rather simple. just write "matrix_name.T"
dMatrix = dMatrix.T
print(dMatrix)
print("shape of dMatrix: ", dMatrix.shape)
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]] shape of dMatrix: (1, 25)
lets create a matrix where each value equals to 5. you can simply do that by adding 5 to a zero matrix or multiplying a "one" matrix with 5. This is known as "Broadcasting"
print("All 5")
all_5 = np.zeros((5, 5)) + 5
print(all_5)
print("All 7")
all_7 = np.ones((5, 5)) * 7
print(all_7)
All 5 [[5. 5. 5. 5. 5.] [5. 5. 5. 5. 5.] [5. 5. 5. 5. 5.] [5. 5. 5. 5. 5.] [5. 5. 5. 5. 5.]] All 7 [[7. 7. 7. 7. 7.] [7. 7. 7. 7. 7.] [7. 7. 7. 7. 7.] [7. 7. 7. 7. 7.] [7. 7. 7. 7. 7.]]
now if we want to add these two matrix we can write "all_5 + all_7". we can also subtract, multiply or divide in this way.
add = all_5 + all_7
print("add: ", add)
subtract = all_5 - all_7
print("subtract: ", subtract)
multiply = all_5 * all_7
print("multiply: ", multiply)
divide = all_5 / all_7
print("divide: ", divide)
add: [[12. 12. 12. 12. 12.] [12. 12. 12. 12. 12.] [12. 12. 12. 12. 12.] [12. 12. 12. 12. 12.] [12. 12. 12. 12. 12.]] subtract: [[-2. -2. -2. -2. -2.] [-2. -2. -2. -2. -2.] [-2. -2. -2. -2. -2.] [-2. -2. -2. -2. -2.] [-2. -2. -2. -2. -2.]] multiply: [[35. 35. 35. 35. 35.] [35. 35. 35. 35. 35.] [35. 35. 35. 35. 35.] [35. 35. 35. 35. 35.] [35. 35. 35. 35. 35.]] divide: [[0.71428571 0.71428571 0.71428571 0.71428571 0.71428571] [0.71428571 0.71428571 0.71428571 0.71428571 0.71428571] [0.71428571 0.71428571 0.71428571 0.71428571 0.71428571] [0.71428571 0.71428571 0.71428571 0.71428571 0.71428571] [0.71428571 0.71428571 0.71428571 0.71428571 0.71428571]]
If the shape of the matrices are different such operation will not work. However, we can do operation with two matrices if one of them have same length on either axis and other axis have length 1.
all_8 = np.ones((7, 7)) * 8
all_5+all_8
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-37-1565c473dabf> in <module> 1 all_8 = np.ones((7, 7)) * 8 2 ----> 3 all_5+all_8 ValueError: operands could not be broadcast together with shapes (5,5) (7,7)
col_8 = np.ones((5, 1)) * 8
print(all_5+col_8)
[[13. 13. 13. 13. 13.] [13. 13. 13. 13. 13.] [13. 13. 13. 13. 13.] [13. 13. 13. 13. 13.] [13. 13. 13. 13. 13.]]
We can initiate a matrix with values from 1 to 100 in single row by calling "np.arange(first number, last number, step size)"
arange_100 = np.arange(1,101)
print(arange_100)
print("print only odd numbers")
arange_100_with_step_2 = np.arange(1,101, 2)
print(arange_100_with_step_2)
[ 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100] print only odd numbers [ 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99]
Reshape the 100 numbers in 10 by 10 form.
arange_100_in_10by10 = arange_100.reshape(10,10)
print(arange_100_in_10by10)
[[ 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] [ 31 32 33 34 35 36 37 38 39 40] [ 41 42 43 44 45 46 47 48 49 50] [ 51 52 53 54 55 56 57 58 59 60] [ 61 62 63 64 65 66 67 68 69 70] [ 71 72 73 74 75 76 77 78 79 80] [ 81 82 83 84 85 86 87 88 89 90] [ 91 92 93 94 95 96 97 98 99 100]]
We want to randomly initiate a 10 by 10 matrix. call np.random.rand(size of first axis, size of second axis, .....)
random_init = np.random.rand(10,10)
print(random_init)
[[0.21789767 0.84449787 0.76840226 0.08467654 0.93437151 0.88633764 0.69986724 0.38168991 0.10893449 0.82426403] [0.02786849 0.70596521 0.27929803 0.68108055 0.45990907 0.04362285 0.64541993 0.66076678 0.98439573 0.94122548] [0.5612729 0.38233805 0.14056796 0.96346433 0.90813321 0.79394989 0.83079724 0.30189563 0.89795683 0.95075021] [0.04605033 0.0448031 0.33140918 0.86288464 0.5174089 0.05959838 0.48501169 0.39468165 0.37704519 0.79565281] [0.63075334 0.47892303 0.10204925 0.99554195 0.11755351 0.07861717 0.83145368 0.72791591 0.1789244 0.8782639 ] [0.52928461 0.32380447 0.81121166 0.001948 0.08135151 0.13474819 0.39944006 0.71698854 0.49709545 0.92568986] [0.80712886 0.7153766 0.36061093 0.98551363 0.59841251 0.89160713 0.75363293 0.6310706 0.5023445 0.95303643] [0.62572551 0.13270236 0.26968496 0.23722339 0.95804876 0.3576835 0.72763154 0.03407235 0.09247004 0.05656004] [0.79608301 0.94723959 0.79854401 0.96642042 0.9340482 0.06475777 0.18756258 0.14304726 0.5267342 0.11266936] [0.65951866 0.27084273 0.17108096 0.98947774 0.56907415 0.43747298 0.02992767 0.30866577 0.68009097 0.36111711]]
If we want to create a matrix with only integer values, we need to call "np.random.randint(lowest number, highest number, size of array)"
random_integer = np.random.randint(0,100,(10,10))
print(random_integer)
[[83 4 61 3 38 59 48 92 54 76] [33 47 31 23 57 93 76 84 79 75] [78 22 61 83 41 46 55 39 7 1] [81 88 41 72 61 26 53 51 50 55] [76 75 81 78 6 35 16 48 68 4] [50 76 93 77 47 61 76 27 45 16] [13 48 8 80 6 68 20 95 76 98] [13 13 98 3 56 44 66 96 99 76] [32 60 29 78 96 41 75 42 58 29] [70 2 33 91 87 5 34 58 3 96]]