Basic array operations
Contents
Basic array operations#
All arithmetic operators on numpy arrays apply elementwise.
Elementwise operations are really powerful and we will be using them a lot!!! Let’s illustrate this with some examples:
import numpy as np # run this cell or the numpy commands won't run
array1 = np.array([10, 20, 13])
array2 = np.arange(3)
print(f"array1 is {array1}, array2 is {array2}")
print("The (elementwise) sum of these two arrays")
print(array1 + array2)
print("The (elementwise) difference of these two arrays")
print(array1 - array2)
array1 is [10 20 13], array2 is [0 1 2]
The (elementwise) sum of these two arrays
[10 21 15]
The (elementwise) difference of these two arrays
[10 19 11]
We can take the (elementwise) products:
print("The (elementwise) product of these two arrays")
print(array1 * array2)
The (elementwise) product of these two arrays
[ 0 20 26]
Do you see what this is doing?? Rather than having to loop over both arrays and multiply, or add, each pair of element together, we can do all of it in one command.
So:
array_3 = array_1 * array_2
Using numpy arrays is the same as doing:
array_3 = []
for idx in range(len(array_1)):
array_3.append(array_1[idx] * array_2[idx])
Which is easier to read and quicker to write?
You can also do things like:
array_1 = array_1 * 3
or even
array_1 *= 3
to multiply all values of array_1 by 3, rather than having to use a for loop for this.
Exercise 2.1#
In the cell below create two numpy arrays of the same size. One array should be created either by using np.arange or np.linspace. Then add and subtruct them together (element-wise). Print the resulting arrays.
Exercise 2.2#
In the cell below create one array by using np.linspace, it should contain 5 equally spaced points in the interval \([0, 1]\) (so the first value should be 0, and the last value 1). Then raise this array to the power of three (element-wise) (ie. compute value**3 for all values in the array). Print the resulting array. Then repeat the process but raise the array to the power of two (element-wise).
Special functions#
We can use numpy mathematical functions to compute things like sin or the square root directly on all elements of an array, no loops required!
array1 = np.linspace(0,4,20)
print("The (elementwise) square root of array1 is")
print(np.sqrt(array1))
print("The (elementwise) exp of array1 is")
print(np.exp(array1))
print("The (elementwise) sin of array1 is")
print(np.sin(array1))
The (elementwise) square root of array1 is
[0. 0.45883147 0.64888568 0.79471941 0.91766294 1.02597835
1.12390297 1.21395396 1.29777137 1.3764944 1.4509525 1.52177182
1.58943883 1.65434038 1.71679015 1.77704663 1.83532587 1.89181061
1.94665705 2. ]
The (elementwise) exp of array1 is
[ 1. 1.23432754 1.52356446 1.88057757 2.32124868 2.86518116
3.53657199 4.36528819 5.38819541 6.65079796 8.20926306 10.13291944
12.50734147 15.43815597 19.055741 23.52102582 29.03264982 35.83579909
44.23311356 54.59815003]
The (elementwise) sin of array1 is
[ 0. 0.20897462 0.40872137 0.59041986 0.74604665 0.86872962
0.95305133 0.99528832 0.9935755 0.9479885 0.86054034 0.73509255
0.57718464 0.39378948 0.19300541 -0.01630136 -0.2248883 -0.42354465
-0.60349817 -0.7568025 ]
Exercise 2.3#
In the cell below create an array of \(100\) equally spaced points from the interval \([0,10]\). Then print the array that contains the values of the function \(\cos\) evaluated on all points of this array.
Example:
If we have an array of \([0, 1, 2]\) then the array that contains the values of the function \(\cos\) evaluated at this array is \([\cos(0), \cos(1), \cos(2)]\).
Other important functions (including some from statistics)
The sum
array_for_stats = np.array([3.4, 5.6, 7.8])
print("The sum of the above array is")
print(np.sum(array_for_stats))
print("or equivalently")
print(array_for_stats.sum())
The sum of the above array is
16.8
or equivalently
16.8
Exercise 2.4#
In the cell below, create an array of the first 100 positive integers. Then print its sum.
The mean
print("The mean of the above array is")
print(np.mean(array_for_stats))
print("or equivalently")
print(array_for_stats.mean())
The mean of the above array is
5.6000000000000005
or equivalently
5.6000000000000005
Exercise 2.5#
In the cell below, create an array of 10 numbers (float-s). Then print their mean.
Then find the standard deviation of the array you have created:
print("The standard deviation of the above array is")
print(np.std(name_of_array)) #Complete this line of code with the name of your array
print("or equivalently")
print(name_of_array.std()) #Complete this line of code
The standard deviation of the above array is
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [7], in <module>
1 print("The standard deviation of the above array is")
----> 2 print(np.std(name_of_array)) #Complete this line of code with the name of your array
3 print("or equivalently")
4 print(name_of_array.std()) #Complete this line of code
NameError: name 'name_of_array' is not defined
Exercise 2.7#
In the cell below, create an array of 100 ones. Then calculate its standard deviation. Do you know why the standard deviation is equal to \(0\)?
The largest (maximum) element of an array
print("The maximum of the above array is")
print(np.max(name_of_array))
print("or equivalently")
print(name_of_array.max())
The maximum of the above array is
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [8], in <module>
1 print("The maximum of the above array is")
----> 2 print(np.max(name_of_array))
3 print("or equivalently")
4 print(name_of_array.max())
NameError: name 'name_of_array' is not defined
Exercise 2.8#
In the cell below create two arrays
first one having the elements
[3.14, 6.28, 12.56].second one having the elements
[2.72, 6.81, 20.43].
Divide the first array by the second array (element-wise), then find the maximum of the resulting array.
Now fine the lowest (minimum) element of an array
print("The minimum of the above array is")
print(np.min(name_of_array)) #remember to change this
print("or equivalently")
print(name_of_array.min()) #remember to change this
The minimum of the above array is
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <module>
1 print("The minimum of the above array is")
----> 2 print(np.min(name_of_array)) #remember to change this
3 print("or equivalently")
4 print(name_of_array.min()) #remember to change this
NameError: name 'name_of_array' is not defined
Exercise 2.9#
In the cell below create two arrays
first one having the elements
[3.14, 2.28, 4.26].second one having the elements
[1.72, 2.81, 0.43].
Then, ultiply the first array by the second array (element-wise), then find the minimum of the resulting array.
Rounding an array#
In most applications, we don’t want to state numbers to many decimal places. Often, 2 or 3 decimal places is precise enough. We can round our values to a certain number of decimal places.
array_lots_decim = np.array([2.33333333333, 3.555555555, 6.88685756464])
n = 2
print(f"Rounding to {n} decimal places")
print(np.round(array_lots_decim, n))
Rounding to 2 decimal places
[2.33 3.56 6.89]
Exercise 2.10#
In the cell below create 17 equally spaced points from the interval [0, 1]. Round the elements of the array to 3 decimal places and print the rounded array.