Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll talk about how to use NumPy to create and manipulate multi-dimensional
arrays.
NumPy is the fundamental package for scientific computing.
It is a Python library that provides fast operations on multi-dimensional arrays.
These operations include mathematical, logical, shape manipulation, as well as sorting, selecting,
linear algebra, statistics, and simulation.
You may think of it as a free alternative to MATLAB.
NumPy and MATLAB have a lot in common, but they also have many differences, which can
be found on this page.
It's titled NumPy for Matlab users.
At the core of the NumPy package, is the ndarray object.
An ndarray object is a homogeneous multi-dimensional array.
The elements are all of the same type.
NummPy array operations are much faster than Python for and while loops.
Watch this YouTube video titled performance comparison between NumPy and Python.
To use NumPy we need to import it into our program.
This is the recommended way of doing this.
import numpy as np.
We can create a one-dimensional array from a list using the function np.array.
When we show this variable, we see this is an array of 4 elements.
The data type of variable a is numpy.ndarray.
We can also create an array from a tuple.
A common mistake is forgetting to include a pair of parentheses for the tuple.
This doesn't work because it's not a tuple.
It's 4 integer numbers.
We can Create a 2D array from a list of lists like this one.
There are a total of 4 elements.
The first two will be put in the top row, the next two will be put in the bottom row.
Because NumPy arrays contain only homogeneous data, if the data are not of the same type.
NumPy will try to convert the elements into the same type.
In this example, we have an integer and a string.
The integer will be converted to a string.
So, they are of the same type.
However, if NumPy cannot convert them into the same data type, it will generate an error.
For example, this one is too complex for NumPy to convert it into the same data type because
the first element is an integer, the second is a string, and the third one is another
list.
We can fix it in two different ways.
First, we can convert this to a simple list of 4 elements like this.
Then, NumPy will convert every element to a string.
Another way is to convert this to a list of lists like this.
As a result, we will create a 2D array.
Every element is a string.
We can also create an array using a range.
This function is np.arange.
This a is short for array.
The syntax is very similar to the range function.
arange 6 will returns an array of integer numbers from 0 to 6, including 0 but not including
6.
This function will return an array of float numbers from -2 to positive 1, including -2
but not including positive 1, with a step of 0.5.
As a result, this array contains 6 numbers.
The arange function is more flexible than the Python built-in range function.
The range function can only generate integer numbers.
So, this range statement will not work.
This linspace function will create an array with 5 elements equally distributed between
0 and 2, including both 0 and 2.
This is the output.
This function, ones will create an array full of ones.
The shape is determined by this list.
2 means two rows.
3 means three columns.
This the created 2D array.
This function, zeros will create an array full of zeros.
The shape is also determined by this list.
Two rows and three columns.
This the created 2D array.
We can also use tuples in place of lists.
The result is the same.
However, don't forget the parentheses.
This is not a list.
So, it won't work.
If it's just one number and not a tuple or list, it will create a 1D array instead of
a 2D array.
In this example, the zeros function with an argument 3 will just create a 1D array with
3 zeros.
The eye function will create a 2D unit array.
The elements in the diagonal line are ones, and other elements are zeros.
The diag function with an argument a, which is a 1D array, will create a 2D array, d.
The elements in the 1D array will fill the diagonal line of the 2D array.
In this case, 1, 2, and 3, will be put here, 1, 2, and 3.
NumPy arrays have three useful attributes.
ndim stores the number of dimensions.
For array d, it has two dimensions.
The shape attribute stores the length of each dimension.
d is a 3 by 3 array.
3 rows and 3 columns.
So, the shape is this tuple.
The size attribute stores the total number of elements.
3 times 3 is 9.
We can create new arrays by changing the shape of existing arrays.
Let's use the arange function to create a 1D array c.
It contains 6 integer numbers from 0 to 5.
The reshape function with two arguments 2 and 3 will create a new array with 2 rows
and 3 columns like this.
Note that the reshape function is not done in place.
So, after reshaping, c is not changed.
If we want to change c, we need to explicitly assign the reshaped array to c.
Now, c is updated.
We may omit the length of one dimension and let NumPy figure it out for us.
In this example, I just want to reshape it to a 3-row array, the number of columns is
filled with -1, which means unspecified.
After calculation, Python will automatically convert it to a 3-row 2-column array.
Another reshaping operation is called ravel.
It will flatten a high dimensional array to a 1D array.
Flattening is not done in-place.
This is the same as reshaping.
So, c is still the 2D array.
Many arithmetic operations can be applied to NumPy arrays.
These operations are applied to each element.
Let's create an array a.
It has 4 numbers.
Another array b.
It also contains 4 numbers.
a plus b is element-wise addition.
It will create an array of 4 elements.
The first element, 22, is the sum of 20 and 2.
The second element, 33, is the sum of 30 and 3.
And so on.
a minus b is element-wise subtraction.
The first element is equal to 20 minus 2, which is 18.
The second element is 30 minus 3, which is 27.
And so on.
a times b is element-wise multiplication.
a divided by b is element-wise division.
b to the power of 2 or b squared is an element-wise exponentiation.
This is the element-wise sine function.
The exp function is the element-wise exponential with base e, Euler's number, which is about
2.718.
The square root function sqrt is also applied to each element.
To refresh your memory, this is a.
The comparison operation like less than or equal to will also be applied to each element.
20 is less than or equal to 35, so this is True.
So is 30.
But 40 is greater than 35.
So, this is False.
The last one is also False.
Let's compare these operations with lists.
Some of these operations mean totally different things for lists.
And other operations are not applicable for lists.
For example, we have two lists, list a contains 4 numbers.
List b contains another four numbers.
a plus b means list concatenation.
The elements in list b will be appended to the end of list a.
As a result, we have a list of 8 elements.
a minus b will create an error because this operation is not defined for lists.
a times 2 means list repetition.
List a will be repeated twice.
a divided by b and other operations like sine, power, square root will not work for lists.
NumPy can deal with two types of multiplications.
Element-wise product and matrix product.
We have two 2D arrays A and B. They are defined like these.
They are both 2 by 2 arrays.
A times B means element-wise product.
The first element is equal to 1 times 2.
The second element is equal to 1 times 0.
And so on.
The dot function will calculate the matrix product.
The first element is equal to the sum of the elements in the first row of A times the elements
in the first column of B. That is 1 times 2 plus 1 times 3.
The result is 5.
The second element is equal to the sum of the elements in the first row of A times the
elements in the second column of B. That is 1 times 0 plus 1 times 4.
The result is 4.
Do the same for the other two elements.
This is the final output.
Okay, that was a brief introduction to NumPy.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.