Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
NumPy's ndarray
is its core object, representing a multi-dimensional array of homogeneous data. That means, in a single array, all elements must be of the same type.
An ndarray
is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the number of items in each dimension.
Start with importing the necessary library:
import numpy as np
ndarray
objects:a = np.array([1, 2, 3]) print(a) # Output: [1 2 3] print(type(a)) # Output: <class 'numpy.ndarray'> print(a.shape) # Output: (3,)
zeros_array = np.zeros((2,2)) # Create an array of zeros print(zeros_array) # Output: [[0. 0.] # [0. 0.]] ones_array = np.ones((2,2)) # Create an array of ones print(ones_array) # Output: [[1. 1.] # [1. 1.]]
full_array = np.full((2,2), 7) # Create a constant array print(full_array) # Output: [[7 7] # [7 7]] identity_array = np.eye(2) # Create a 2x2 identity matrix print(identity_array) # Output: [[1. 0.] # [0. 1.]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) b = a[:2, 1:3] # first 2 rows, columns 1 and 2 print(b) # Output: [[2 3] # [6 7]]
a = np.array([[1,2], [3, 4], [5, 6]]) print(a[[0, 1, 2], [0, 1, 0]]) # Output: [1 4 5]
x = np.array([1, 2]) # Let numpy choose the datatype print(x.dtype) # Output: int64 x = np.array([1.0, 2.0]) # Let numpy choose the datatype print(x.dtype) # Output: float64 x = np.array([1, 2], dtype=np.int64) # Force a particular datatype print(x.dtype) # Output: int64
Basic mathematical functions operate element-wise on arrays:
x = np.array([[1,2],[3,4]]) y = np.array([[5,6],[7,8]]) print(np.add(x, y)) # Elementwise sum print(np.subtract(x, y)) # Elementwise difference print(np.multiply(x, y)) # Elementwise product print(np.divide(x, y)) # Elementwise division
The ndarray
is at the heart of the NumPy library. With its efficient operations, you can perform complex computations on data, whether it's for data science, machine learning, or other fields requiring numerical computations on data. Mastering the ndarray
will provide you with a strong foundation for all of NumPy's capabilities.
The NumPy ndarray (n-dimensional array) is a powerful data structure for efficient numerical operations. It provides a flexible container for homogeneous data.
import numpy as np # Creating a 1D ndarray array_1d = np.array([1, 2, 3, 4, 5]) print("1D ndarray:") print(array_1d)
NumPy provides various functions for array creation and manipulation.
# Creating a 2D ndarray array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Manipulating array elements array_2d[1, 2] = 10 array_2d[:, 1] *= 2 print("2D ndarray:") print(array_2d)
Indexing and slicing operations are fundamental for accessing and manipulating ndarray elements.
# Indexing and slicing element = array_2d[0, 1] subarray = array_2d[:, 1:] print("Accessed Element:", element) print("Sliced Subarray:") print(subarray)
NumPy provides functions for creating and initializing arrays with specific patterns.
# Creating arrays with specific patterns zeros_array = np.zeros((2, 3)) ones_array = np.ones((3, 2)) random_array = np.random.random((2, 2)) print("Zeros Array:") print(zeros_array) print("\nOnes Array:") print(ones_array) print("\nRandom Array:") print(random_array)
Vectorized operations in NumPy enable efficient element-wise operations on arrays.
# Vectorized operations array_a = np.array([1, 2, 3]) array_b = np.array([4, 5, 6]) result = array_a + array_b print("Result of Vectorized Operation:") print(result)
NumPy supports multidimensional arrays, enabling efficient manipulation of higher-dimensional data.
# Multidimensional arrays array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print("3D ndarray:") print(array_3d)
NumPy provides advanced array manipulation functions for reshaping, concatenating, and stacking arrays.
# Advanced array manipulation reshaped_array = np.reshape(array_a, (3, 1)) concatenated_array = np.concatenate((array_a, array_b)) stacked_array = np.stack((array_a, array_b)) print("Reshaped Array:") print(reshaped_array) print("\nConcatenated Array:") print(concatenated_array) print("\nStacked Array:") print(stacked_array)
NumPy ndarrays offer advantages over Python lists, such as efficient vectorized operations and memory management.
# NumPy ndarray vs list list_a = [1, 2, 3] list_b = [4, 5, 6] array_a = np.array(list_a) array_b = np.array(list_b) result = array_a + array_b print("Result using ndarray:") print(result) # Comparing with lists result_list = [a + b for a, b in zip(list_a, list_b)] print("\nResult using lists:") print(result_list)