Chapter 1 Numpy
Chapter 1 Numpy
Contents
Introduction
Installing and Importing Numpy
Creating Arrays and the NDarray Object
Dimensions in Arrays
The shape of an array
Accessing Array Elements
Array Indexing
Array Slicing
Numpy Random Generators
Joining Numpy Arrays
Filtering & Searching Arrays
Joining Numpy Arrays
Filtering & Searching Arrays
Sorting Arrays
Main Matrix Operations
Bonus Material - غير مطلوب في االمتحان
Introduction
NumPy is short for Numerical Python, it was created by Travis Oliphant in 2005.
It is an Open Source - Free Python library used for working with arrays.
It provides a high-performance multidimensional array object, and tools for working
with arrays.
Numpy aims to provide an array object called NDARRAY that is up to 50x faster than
traditional Python lists.
NumPy is written partially in Python, but most of the parts that require fast computation are
written in C or C++.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 1/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 2/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Installing Numpy
Numpy is not a standard library, therefore, to use numpy, it needs to be installed in our
system.
Once NumPy is installed, we can import it in our applications by using the import keyword.
We can install numpy using Python package manager (PIP Command), as following:
Importing Numpy
After installing NumPy, we can import it and use it in our code.
Numpy alias
alias: In Python alias are an alternate name for referring to the same thing.
Numpy is usually imported under the np alias.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 3/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
We can check the version of numpy library using the version attribute.
1.21.5
Example: Create a Python list of 3 floating point values, then create an ndarray
from the list
[1. 2. 3.]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 4/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Lets find the type of our created array is an object of type ndarray (n
dimensions array):
In [8]: 1 print(type(arr))
<class 'numpy.ndarray'>
Example: Create a numpy array based on a tuple, similar to the list in the
previous example and print its values
[1 2 3 4 5]
Dimensions in Arrays
A dimension is a direction in which you can vary the specification of an array's elements.
An array that holds the sales total for each day of the month has one dimension (the day of
the month).
Nested array are arrays that have arrays as their elements.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 5/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
The last column (the daily sales amounts) represents a 1 dimensional array.
0-D Arrays
<class 'numpy.ndarray'>
1-D Arrays
An array with 1 dimension is called a 1-D array or a Vector.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 6/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
Example: Create a 2-D array containing two arrays with the values 1,2,3 and
4,5,6:
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.
Example: Create a 3-D array with two 2-D arrays, both containing two arrays
with the values 1,2,3 and 4,5,6:
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 7/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
0
1
2
3
[[1 2 3 4]
[5 6 7 8]]
(2, 4)
In [16]: 1 print(arr.ndim)
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 8/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Reshaping Arrays
We can change the shape of an array e.g. from 1-d to 2-d, from 2-d to 5-d...etc using the
reshape method.
The only condition that we need to enforce is:
..................................................
Array shape: (12,)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
..................................................
Array shape: (4, 3) 2
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
..................................................
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usin… 9/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
..................................................
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Array shape: (12,)
..................................................
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
Array shape: (2, 3, 2)
..................................................
[[[ 1 2 3 4 5 6 7 8 9 10 11 12]]]
Array shape: (1, 1, 12)
..................................................
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 10/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[[[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]]
[[[ 8 9]
[10 11]]
[[12 13]
[14 15]]]]
2 x 8 array:
[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]]
8 x 2 array:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]]
In [ ]: 1
We can reshape an array into any new shape as long as the elements required for reshaping
are equal in both shapes.
Flattening arrays
Flattening array means converting a multidimensional array into a 1D array.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 11/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 12/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 13/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Example: Get third and fourth elements from the following array and add them.
Example: Access the 5th element of the 2nd element in the first dimension
5th element (number 10) in the 2nd element ([6,7,8,9,10]) of the first dimens
ion: 10
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 14/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
11
Explaination:
The first number represents the first dimension, which contains two 2-d arrays: [[1, 2, 3], [4,
5, 6]] and [[7, 8, 9], [10, 11, 12]]
Since we selected 1 at the first index location [1,-,-] , we are left with the second array: [[7,
8, 9], [10, 11, 12]]
The second number in our index represents the second dimension [-, 1, -], which also
contains two arrays: [7, 8, 9] and [10, 11, 12]
Since we selected 1, we are left with the second array: [10, 11, 12]
The third number represents the third dimension [-, -, 1], which contains three values: 10
11 and 12
Since we selected 1, we end up with the second element which is 11
Negative Indexing
We can use negative indexing to access array elements using a reverse order.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 15/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Print the last element in the 2nd element of the first dimension:
Array Slicing
Slicing in python refers to extracting a number of elements from a sequence data type
(array, list,tuple..etc).
To slice a number of elements, We use 2 integers to specify the begining and the end of
the sliced elements [start:end]
We can also define a step value to skip a number of elements while slicing array elements
e.g. [start:end:step]
If we don't provide the slice start value, the default value of 0 will be used.
If we don't provide slice end value, the default value of -1 will be used, which refers to the
last element in the array.
[20 30 40 50]
[1 4 7]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 16/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[7 8 9]
From both elements of the first dimension, return the element at index 2:
[3 8]
From both elements in the first dimension, slice index 1 to index 4 (not
included), this will return a 2-D array:
[[ 2 3 4 5]
[ 7 8 9 10]]
Negative Slicing
We can use negative indexing to slice elements using reverse indexing scheme
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 17/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[5 6 7]
[4 5 6]
In [40]: 1 # We can use arange function to create an integer array based on counters
2 x = np.arange(10)
3
4 print("orginal array: ", x)
5 print("Reversed array: ", x[::-1])
6
7 print (x)
orginal array: [0 1 2 3 4 5 6 7 8 9]
Reversed array: [9 8 7 6 5 4 3 2 1 0]
[0 1 2 3 4 5 6 7 8 9]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 18/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
The randint() method also accepts a size parameter where you can specify the shape of an
array.
[51 43 87 48 88]
Example: Generate a 2-D array with 3 rows, each row containing 5 random
integers from 0 to 100:
[[18 3 24 49 71]
[38 39 96 5 21]
[16 8 3 89 25]]
Example: Use the random module of numpy to generate a 1-d array that
contains 5 floating point numbers
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 19/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Heads
Heads
Heads
Tails
Heads
Heads
Heads
Tails
Heads
Heads
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 20/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 21/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
If the value at an index is True that element is contained in the filtered array, if the value at
that index is False that element is excluded from the filtered array.
Create a new array that contains elements in index 0 and 3 only, and filter out
all the rest.
[41 43]
Create a filter array that will return only values higher than 42:
[43 44]
Create a filter array that will return only even elements from the original array:
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 22/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Value found
Value found
Sorting Arrays
Sorting Arrays means putting elements in an ordered sequence.
Ordered sequence is any sequence that has an order corresponding to elements, like
numeric or alphabetical, ascending or descending.
The NumPy ndarray object has a function called sort(), that will sort a specified array.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 23/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[0 1 2 3]
notice that sort is not a inplace operation, the original array remains unchanged
In [57]: 1 print(arr)
[3 2 0 1]
Similar to how we perform operations on numbers, the same logic also works for matrices
and vectors.
However, we need to note that these operations on matrices have restrictions on two
matrices being the same size. This is because they are performed in an element-wise
manner, which is different from matrix dot product.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 24/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
matrix addition
[[3 3]
[3 3]]
matrix substraction
[[1 1]
[1 1]]
matrix multiplication
[[2 2]
[2 2]]
matrix division
[[2. 2.]
[2. 2.]]
Dot product is often being confused with matrix element-wise multiplication (which is
demonstrated above); in fact, it is a more commonly used operation on matrices and
vectors.
Dot product operates by iteratively multiplying each row of the first matrix to the column of
the second matrix one element at a time, therefore the dot product between a j x k matrix
and k x i matrix is a j x i matrix.
Here is an example of how the dot product works between a 3x2 matrix and a 2x3 matrix.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 25/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Dot product operation necessitates the number of columns in the first matrix matching the
number of rows in the second matrix. We use dot() to execute the dot product.
The order of the matrices in the operations is crucial — as indicated below,
matrix2.dot(matrix1) will produce a different result compared to matrix1.dot(matrix2).
Therefore, as opposed to the element-wise multiplication, matrix dot product is not
commutative.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 26/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Matrix Transpose
Transpose swaps the rows and columns of the matrix, so that an j x k matrix becomes k x
j.
To transpose a matrix, we use matrix.T.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 27/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
3 x 3 Identity Matrix:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 28/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
The dot product of the matrix itself (stated as M below) and the inverse of the matrix is the
identity matrix which follows the equation:
There are two things to take into consideration with matrix inverse:
1. The order of the matrix and matrix inverse does not matter even though most matrix
dot products are different when the order changes.
2. Not all matrices have an inverse.
To compute inverse of the matrix, we can use np.linalg.inv().
At this stage, we have only covered some basic concepts in linear algebra that support the
application for data representation.
If you would like to go deeper into more concepts, there is a free helpful book Mathematics
for Machine Learning (https://mml-book.github.io/book/mml-book.pdf) from Deisenroth,
Faisal and Ong that you can examine.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 29/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
We will briefly examine three straightforward use cases of using vectors and matrices in
Numpy:
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 30/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
The first equation states that if the factory produces 2 * x units of product A, 4 * y units of
product B, and 6 * z units of product C in a day, then the total resources used is 18.
The second equation states that if the factory produces 4 * x units of product A, 5 * y units
of product B, and 6 * z units of product C in a day, then the total resources used is 24.
The third equation states that if the factory produces 3 * x units of product A, 1 * y units of
product B, and -2 * z units of product C in a day, then the total resources used is 4.
How many units of each product were produced in a day given the total amount of
resources used?
How much of each resource was used to produce a certain number of units of each
product?
A typical way to compute the value of x,y and z is to eliminate one element at a time, which can
take many steps for tree variables.
An alternative solution is to represent it using the dot product between matrix and vector.
We can package all the coefficients into a matrix and all the variable into a vector, hence we get
following:
Matrix representation gives us a different mindset to solve the equation in one step.
As demonstrated below:
Since the dot product between inverse of the matrix and the matrix itself is the identity matrix,
we can simplify the solution of the linear equation system as the dot product between the
inverse of the coefficient matrix M and the output vector y.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 31/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
2. Linear Regression
The same principle shown in solving the linear equation system can be generalized to linear
regression models in machine learning.
Linear regression is a method for modeling the relationship between one or more independent
variables and a dependent variable.
Linear Regression can be solved using different methods (below), all these methods utilize
matrix operations:
Example:
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 32/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
We can store the feature values in a 1 x (n+1) matrix and the weights are stored in an (n+1) x 1
vector. Then we multiply the element with the same color and add them together to get the
weighted sum.
When the number of instances increase, we naturally think of using for loop to iterate an item at
a time which can be time consuming.
By representing the algorithm in the matrix format, the linear regression optimization process
boils down to solving the coefficient vector [w0, w1, w2 … wn] through linear algebra
operations.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 33/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
As we saw in a previous section, the normal equations define how to calculate w directly:
This can be calculated directly in NumPy using the inv() function for calculating the matrix
inverse.
w = inv(X.T.dot(X)).dot(X.T).dot(y)
Once the coefficients are calculated, we can use them to predict outcomes given X.
yhat = X.dot(w)
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 34/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
[1.00233226]
Additionally, popular Python libraries such as Numpy and Pandas build upon matrix
representation and utilize “vectorization” to speed up the data processing speed.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 35/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
3. Neural Network
Neural network is composed of multiple layers of interconnected nodes, where the outputs of
nodes from the previous layers are weighted and then aggregated to form the input of the
subsequent layers.
If we zoom into the interconnected layer of a neural network, we can see some components of
the regression model.
Take a simple example that we visualize the inner process of the hidden layer i (with node i1,
i2, i3) and hidden layer j (with node j1, j2) from a neural network.
w11 represents the weight of the input node i1 that feeds into the node j1, and w21 represents
the weight of input node i2 that feeds into node j1. In this case, we can package the weights
into 3x2 matrix.
This can be generalized to thousands or even millions of instances which forms the massive
training dataset of neural network models.
Now this process resembles how we represent the linear regression model, except that we we
use a matrix to store the weights instead of a vector, but the principle remains the same.
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 36/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
To take a step further, we can expand this to deep neural networks for deep learning.
This is where Tensor come into play to represent data with more than two dimensions.
For example, in Convolutional Neural Network, we use 3D Tensor for image pixels, as they are
often depicted through three different channels (i.e., red, green, blue color channel).
As you can see, linear algebra act as the building block in machine learning and deep
learning algorithms, and this is just one of the multiple use cases of linear algebra in data
science.
Objective: Build a Neural Network that can map the relationship between inputs and outputs
In [65]: 1 X = numpy.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])
2 y = numpy.array([[0,1,1,0]]).T
3 syn0 = 2*numpy.random.random((3,4)) - 1
4 syn1 = 2*numpy.random.random((4,1)) - 1
5 for j in numpy.arange(60000):
6 l1 = 1/(1+numpy.exp(-(numpy.dot(X,syn0))))
7 l2 = 1/(1+np.exp(-(numpy.dot(l1,syn1))))
8 l2_delta = (y - l2)*(l2*(1-l2))
9 l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1))
10 syn1 += l1.T.dot(l2_delta)
11 syn0 += X.T.dot(l1_delta)
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 37/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Python's default implementation (known as CPython) does some operations very slowly.
Numpy provides an easy and flexible interface to optimized computation with arrays of
data.
The key to making it fast is to use vectorized operations, generally implemented through
NumPy's universal functions (ufuncs).
Lets see an example that compares normal for loop which the UFunc
alternative
- The exampe will compute the reciprocal of elements in an array using a for
loop and then using the Universal Functions.
- We will use timeit to determine the execution time.
1.81 s ± 48.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
The results show that the universal function is much faster than the standard for..loop approach
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 38/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
NumPy's ufuncs feel very natural to use because they make use of Python's native arithmetic
operators. The standard addition, subtraction, multiplication, and division can all be used:
In [*]: 1 x = np.arange(4)
2
3 print("x =", x)
4 print("x + 5 =", x + 5)
5 print("x - 5 =", x - 5)
6 print("x * 2 =", x * 2)
7 print("x / 2 =", x / 2)
8 print("x // 2 =", x // 2) # floor division
9 print("-x = ", -x)
10 print("x ** 2 = ", x ** 2)
11 print("x % 2 = ", x % 2)
12 print(abs(x))
13
14 # Trigonometric functions
15
16 theta = np.linspace(0, np.pi, 3)
17 print("theta = ", theta)
18 print("sin(theta) = ", np.sin(theta))
19 print("cos(theta) = ", np.cos(theta))
20 print("tan(theta) = ", np.tan(theta))
21
22 # Exponents and logarithms
23 x = [1, 2, 3]
24 print("x =", x)
25 print("e^x =", np.exp(x))
26 print("2^x =", np.exp2(x))
27 print("3^x =", np.power(3, x))
28
29 x = [1, 2, 4, 10]
30 print("x =", x)
31 print("ln(x) =", np.log(x))
32 print("log2(x) =", np.log2(x))
33 print("log10(x) =", np.log10(x))
Each of these arithmetic operations are simply convenient wrappers around specific functions
built into NumPy; for example, the + operator is a wrapper for the add function:
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 39/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
In [*]: 1 np.add(x, 2)
Aggregation Functions
For example
In [*]: 1 a = [10,20,30,40,50,60,70,80,90,100,110]
2
3 arr = np.random.randint(20,size=(10))
4 print("unsorted: ", arr)
5 print("sorted: ", np.sort(arr))
6
7 print("Sum: ", np.sum(arr))
8 print("Mean: ", np.mean(arr))
9 print("Median: ", np.median(arr))
10 print("Standard Deviation: ", np.std(arr))
11 print("Variance: ", np.var(arr))
12 print("Minimum: ", np.amin(arr, 0)) #min in the axis
13 print("Maximum: ", np.amax(arr, 0)) #max in the axis
14
15 #percentile()function used to compute the nth percentile
16 #f the given data (array elements) along the specified axis.
17 print("10% percentile: ", np.percentile(arr, 10))
18 print("50% percentile: ", np.percentile(arr, 50))
Additional descritive functions can be incporporated using other Python libraries e.g. SciPy,
Statmodels...etc
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 40/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
In [*]: 1 a + 5
We can think of this as an operation that stretches or duplicates the value 5 into the array
[5, 5, 5], and adds the results.
The advantage of NumPy's broadcasting is that this duplication of values does not actually
take place, but it is a useful mental model as we think about broadcasting.
In [*]: 1 c + a
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 41/42
3/4/23, 11:01 AM chapter_1_numpy - Jupyter Notebook
Here the one-dimensional array a is stretched, or broadcast across the second dimension in
order to match the shape of array c.
While these examples are relatively easy to understand, more complicated cases can involve
broadcasting of both arrays.
In [*]: 1 a = np.arange(3)
2 b = np.arange(3)[:, np.newaxis]
3
4 print(a)
5 print(b)
In [*]: 1 a + b
Just as before we stretched or broadcasted one value to match the shape of the other.
Here we've stretched both a and b to match a common shape, and the result is a two-
dimensional array!
The geometry of these examples is visualized in the following figure (Code to produce this
plot can be found in the appendix, and is adapted from source published in the astroML
documentation. Used by permission).
References
www.w3schools.org (http://www.w3schools.org)
www.tutorialspoint.com (http://www.tutorialspoint.com).
Python Data Science Handbook, Jake VanderPlas, 2017 Link 1
(https://jakevdp.github.io/PythonDataScienceHandbook/) Link 2
(https://github.com/jakevdp/PythonDataScienceHandbook)
How is Linear Algebra Applied for Machine Learning (https://medium.com/towards-data-
science/how-is-linear-algebra-applied-for-machine-learning-d193bdeed268)
localhost:8888/notebooks/OneDrive - UNIVERSITY OF PETRA/Petra/20222/307301 Programming BI-Python II/Chapter 1 Numerical Processing usi… 42/42