UNIT 2
NUMPY LIBRARAY Ricky
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 was created in 2005 by Travis Oliphant. It is an open source
project and you can use it freely.
NumPy stands for Numerical Python.
The need of numpy:
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.
1. Installation of NumPy
NumPy is a powerful library for numerical computing in Python. It must be
installed before usage. Install it using pip:
pip install numpy
2. Importing NumPy
After installing NumPy, it must be imported into your Python script. It’s a
common practice to import NumPy as np to save time when referencing the
library:
import numpy as np
3. Using numpy as np
Using import numpy as np is a convention to make code concise and
readable.
All NumPy operations can be done using the alias np.
Example:
array = np.array([1, 2, 3])
print(array)
4. NumPy as ndarray
An ndarray is the core data structure in NumPy, representing N-dimensional
arrays. It can hold elements of the same type (integers, floats, etc.) and
provides efficient operations on large datasets.
Example:
array = np.array([1, 2, 3])
print(type(array)) # Output: <class 'numpy.ndarray'>
ndarray is useful for operations like matrix manipulations, element-wise
arithmetic, and statistical functions.
5. Dimensions in Arrays
When creating an array in NumPy, several parameters can be used:
object: The input data (like a list, tuple, or another array).
dtype: The desired data type of the array, e.g., int, float.
copy: If True, the data is copied. If False, changes in the original object will
reflect in the array.
order: Memory layout for the array. 'C' is row-major (C-style), 'F' is column-
major (Fortran-style).
subok: If True, subclasses of ndarray are passed. If False, the base class is
forced.
ndmin: Specifies the minimum number of dimensions for the output array.
Example:
array = np.array([1, 2, 3], dtype='float', ndmin=2)
print(array)
# Output: [[1. 2. 3.]] (2D array)
In this example, the ndmin parameter forces the array to have at least two
dimensions.
6. Creating an Array Using a List
You can create a NumPy array from a Python list. This is the most basic way
to create a NumPy array:
my_list = [1, 2, 3, 4]
array = np.array(my_list)
print(array)
# Output: [1 2 3 4]
Here, a Python list is passed to np.array() to create a 1D array.
7. 0D Array (Zero-Dimensional Array)
A 0D array is also known as a scalar. It represents a single value.
Example:
array = np.array(42)
print(array.ndim) # Output: 0
Here, the array has no dimensions, as it holds a single value.
8. 1D Array, 2D Array, and 3D Array in NumPy
1D Array: A simple array with a single list of elements.
array = np.array([1, 2, 3])
print(array.ndim) # Output: 1
2D Array: An array that consists of rows and columns (a matrix).
array = np.array([[1, 2, 3], [4, 5, 6]])
print(array.ndim) # Output: 2
3D Array: An array containing multiple 2D matrices (a cube of values).
array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(array.ndim) # Output: 3
The ndim attribute tells you the number of dimensions of the array.
9. Check the Number of Dimensions
You can check the number of dimensions in an array using .ndim. This
attribute returns an integer representing the number of axes or dimensions.
array = np.array([1, 2, 3])
print(array.ndim) # Output: 1
10. Higher-Dimensional Arrays
NumPy supports arrays of more than 3 dimensions. For instance, you can
create a 5D array using the ndmin parameter.
Example:
array = np.array([1, 2, 3], ndmin=5)
print(array)
print(array.ndim) # Output: 5
11. Anatomy of an Array
Arrays in NumPy consist of several key elements:
Axis: Each dimension of the array is called an axis.
Axis 0 refers to the rows.
Axis 1 refers to the columns.
Shape: The shape of an array is a tuple that tells how many elements are in
each dimension.
array = np.array([[1, 2], [3, 4], [5, 6]])
print(array.shape) # Output: (3, 2)
The array has 3 rows and 2 columns, so the shape is (3, 2).
Q: Describe the Axis in Python
In NumPy, axis specifies the dimension along which operations are
performed:
Axis 0 runs vertically (rows).
Axis 1 runs horizontally (columns).
For example, summing along axis 0 adds up all the elements in each column:
array = np.array([[1, 2], [3, 4]])
print(np.sum(array, axis=0)) # Output: [4 6] (column sum)
Summing along axis 1 adds up all the elements in each row:
print(np.sum(array, axis=1)) # Output: [3 7] (row sum)
12. Reshaping Arrays
Reshaping arrays means changing the number of dimensions while keeping
the same data. The total number of elements must remain the same.
Example:
array = np.array([1, 2, 3, 4, 5, 6])
reshaped = array.reshape(2, 3)
print(reshaped)
# Output:
# [[1 2 3]
# [4 5 6]]
In this case, the 1D array [1, 2, 3, 4, 5, 6] is reshaped into a 2D array with 2
rows and 3 columns.
Reshape Notes:
The new shape must be compatible with the number of elements in the
original array.
Use -1 in reshape() to automatically calculate one dimension based on the
size of the other dimensions:
reshaped = array.reshape(3, -1)
print(reshaped)
# Output:
# [[1 2]
# [3 4]
# [5 6]]