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]
the basics and array creation
Indexing and slicing
Basic Operations
Universal Functions
import before use
## 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
## <class 'numpy.ndarray'>
Create an array from a tuple object
## array([1, 2, 3, 4])
2.1.1.2 Attributes of an array
## type of the object ---> <class 'numpy.ndarray'>
## type of values in object ---> int64
## size of the array ---> 4
## dimension of the object ---> 1
## 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]
## [1 3 5 7 9]
## [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.
## [5 7 9]
## [-3 -3 -3]
## [ 4 10 18]
## [0.25 0.4 0.5 ]
## [ 1 32 729]
## [5 7 9]
## [-3 -3 -3]
## [ 4 10 18]
## [0.25 0.4 0.5 ]
## [ 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
## 15
## 3.0
## 3.0
## 1.4142135623730951
python pre-defined functions
## 15
## 15
## 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]]
## [[19 22]
## [43 50]]
## [[19 22]
## [43 50]]