- Addition/Subtraction -
- Scalar Multiplication -
- Dot Product -
- Broadcasting -
- Matrix Multiplication -
- Universal Functions -
- Mean -
- Maximum and Minimum -
- Sampling -
- Transpose -
- Random.rand -
- Previous Section
- Next Section

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.

**Univers****al Functions -**

**Univers**

**al 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.*