7️⃣

3.7 NumPy 2: Linear Algebra

Linear Algebra is the subject involving vector and matrix operations. NumPy makes Linear Algebra a lot easier, so let's discuss how to use it to do what we discussed in the previous lesson.

Addition/Subtraction -

u = np.array([0, 1, 2])
v = np.array([1, 2, 3])
w = u + v
x = u - v

The above code will create two NumPy arrays, and their matrix addition is stored in w, while their matrix subtraction is stored in x. If you had to do this without NumPy, things would be a lot harder - trust me.

In any case, the same principle applies to a 2D NumPy arrays.

Scalar Multiplication -

z = 3*np.arrau([1, 2, 3])

The above code stores the NumPy array [3, 6, 9] in z. NumPy arrays do scalar multiplication easily.

Dot Product -

u = np.array([1,2])
v = np.array([2,3])
z = np.dot(u,v)
z: 8

The dot product is done by np in-built function, np.dot(x,y)

Broadcasting -

u = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
z = u+1
z: [2, 3, 4, 5, 6, 7, 8, 9, 10]

This is a bit of a unique operation that NumPy allows. Broadcasting adds the constant to every element of the NumPy array.

Matrix Multiplication -

np.dot(a,b): a*b

The dot function from the NumPy library would just multiply inputs if you enter constants. However, if you enter 1D NumPy arrays, you will get the dot product we discussed earlier. But if you enter 2D NumPy arrays, the

np.dot()

function just performs matrix multiplication.

Universal Functions -

The NumPy arrays have some useful statistical functions as well.

Mean -

If numpyArray is a NumPy Array: The average value of all the elements in it is

numpyArray.mean()

Maximum and Minimum -

numpyArray.min()

produces the minimum element in numpyArray.

numpyArray.max()

produces the maximum element in numpyArray.

Sampling -

This is not exactly what you think of when you hear the word "Sampling". The np.linspace() function produces a NumPy array that contains all the samples.

np.linspace(a,b,n)

a represents the initial value and b represents the end value. n represents the number of samples. Let's see a practical example of what it does:

np.linspace(1,5,4): [1, 2.3333, 3.6667, 5]

However, linspace has a provision for an open interval as well. This means that the samples taken don't include the endpoint:

np.linspace(1,5,4,True): [1, 2.3333, 3.6667, 5]
np.linspace(1,5,4,False): [1, 2, 3, 4]

We'll often use linspace for plotting.

Transpose -

Finally, to take the transpose of an array, you can use the following code:

numpyArray.T

Provides the transpose of the NumpyArray

Random.rand -

We will be using this in splitting up our data. The

np.random.rand(n)

function takes the dimensions of an array, and produces an array of the same size filled with random values between 0 and 1. We will use this as a way to split up data into slightly randomized categories.

And that's it! NumPy functionalities are not very easy to remember either, but you can refer back to this later.

Previous Section

Next Section

Add a page link block here (if there is no next section, delete this column)

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.