Chapter 2 Numpy Module

In Python we have lists that serve the purpose of arrays, but they are slow to process.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

2.1 Numpy Library

NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier transform, and matrices.

NumPy stands for Numerical Python.

A numpy array is similar to a list. It’s usually fixed in size and each element is of the same type. We can cast a list to a numpy array by first importing numpy:

[ speed and memory advantages]

  1. the basics and array creation

  2. Indexing and slicing

  3. Basic Operations

  4. Universal Functions

import before use

import numpy as np

print(np.__version__)
## 1.26.4

2.1.1 A. One dimensional array

2.1.1.1 the basics and array creation

Create an array from a list object

x = np.array([0, 1, 2, 3, 4]) 
type(x)  
## <class 'numpy.ndarray'>

Create an array from a tuple object

np.array( (1, 2, 3, 4) )
## array([1, 2, 3, 4])

2.1.1.2 Attributes of an array

x = np.array( [1, 2, 3, 4] )


print("type of the object --->", type(x))
## type of the object ---> <class 'numpy.ndarray'>
print("type of values in object --->", x.dtype)
## type of values in object ---> int64
print("size of the array --->", x.size)
## size of the array ---> 4
print("dimension of the object --->", x.ndim )
## dimension of the object ---> 1
print("size and dimension --->", x.shape)
## size and dimension ---> (4,)

2.1.1.3 Indexing and Slicing

slicing: array[start : stop : step]

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# Get elements from index 2 to 5 (exclusive)
print(arr[2:5])  # Output: [2, 3, 4]
## [2 3 4]
# Get every other element starting from index 1
print(arr[1::2])  # Output: [1, 3, 5, 7]
## [1 3 5 7 9]
# Get all elements in reverse order
print(arr[::-1])  # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
## [9 8 7 6 5 4 3 2 1 0]

2.1.1.4 Basic Vector Operations

Addition: a + b or np.add(a, b) - Element-wise addition of two arrays. Subtraction: a - b or np.subtract(a, b) - Element-wise subtraction of two arrays. Multiplication: a * b or np.multiply(a, b) - Element-wise multiplication of two arrays. Division: a / b or np.divide(a, b) - Element-wise division of two arrays. Power: a ** b or np.power(a, b) - Element-wise exponentiation of two arrays.

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b)  # Output: [5, 7, 9]
## [5 7 9]
print(a - b)  # Output: [-3, -3, -3]
## [-3 -3 -3]
print(a * b)  # Output: [ 4, 10, 18]
## [ 4 10 18]
print(a / b)  # Output: [0.25, 0.4, 0.5 ]
## [0.25 0.4  0.5 ]
print(a ** b)  # Output: [  1, 32, 729]
## [  1  32 729]
print(np.add(a, b))
## [5 7 9]
print(np.subtract(a, b))
## [-3 -3 -3]
print(np.multiply(a, b))
## [ 4 10 18]
print(np.divide(a, b))
## [0.25 0.4  0.5 ]
print(np.power(a, b))
## [  1  32 729]

2.1.1.5 Other Vector Operations

Sum: a.sum() or np.sum(a) - Calculate the sum of all elements in the array.

Mean: a.mean() or np.mean(a) - Calculate the mean of all elements in the array.

Median: np.median(a) - Calculate the median of all elements in the array.

Standard Deviation: a.std() or np.std(a) - Calculate the standard deviation of all elements in the array.

Dot Product: a.dot(b) - Calculate the dot product of two arrays.

numpy pre-defined functions

a = np.array([1, 2, 3, 4, 5])

print(np.sum(a))  # Output: 15
## 15
print(np.mean(a))  # Output: 3.0
## 3.0
print(np.median(a))  # Output: 3.0
## 3.0
print(np.std(a))  # Output: 1.58113883046
## 1.4142135623730951

python pre-defined functions

a = np.array([1, 2, 3, 4, 5])

print(a.sum())  # Output: 15
## 15
print(sum(a))
## 15
print(a.std())  
## 1.4142135623730951

User defined functions

a = np.array([1, 2, 3, 4, 5])

# Define the function
def my_func(x):
  
    return -2*x**2 + 5*x + 20
  
  
my_func(a)
## array([23, 22, 17,  8, -5])

2.1.1.6 Matrix Multiplication

Dot Product: a @ b or np.dot(a, b) - Matrix multiplication of two arrays.

Matrix Product: a.dot(b) - Matrix multiplication of two arrays (alternative syntax).

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

print(a @ b)  # Output: [[19 22], [43 50]]
## [[19 22]
##  [43 50]]
print(np.dot(a, b))  # Output: [[19 22], [43 50]]
## [[19 22]
##  [43 50]]
print(a.dot(b))  # Output: [[19 22], [43 50]]
## [[19 22]
##  [43 50]]