Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of generic data.

Arrays in Numpy
A NumPy array (ndarray) is a collection of elements of the same data type stored in a multidimensional structure. Arrays provide an efficient way to store and perform operations on numerical data.
- Number of dimensions in an array is called its rank.
- Size of the array along each dimension is called its shape.
- Elements are accessed using square brackets [] and arrays are commonly created from Python lists.
Creating a Numpy Array
NumPy arrays are created using the np.array() function. They can be created from Python sequences such as lists and tuples.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
arr = np.array((1, 3, 2))
print(arr)
Output
[1 2 3] [[1 2 3] [4 5 6]] [1 3 2]
Explanation:
- np.array([1, 2, 3]) creates a one-dimensional array from a list.
- np.array([[1, 2, 3], [4, 5, 6]]) creates a two-dimensional array from a nested list.
- np.array((1, 3, 2)) creates a one-dimensional array from a tuple.
Accessing the Array Index
Elements in a NumPy array can be accessed using indexing and slicing. Indexing retrieves specific elements, while slicing extracts a range of elements from an array.
import numpy as np
arr = np.array([
[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]
])
arr2 = arr[:2, ::2]
print("First 2 rows and alternate columns:\n", arr2)
arr3 = arr[[1, 1, 0, 3], [3, 2, 1, 0]]
print("Selected elements:", arr3)
Output
First 2 rows and alternate columns: [[-1. 0.] [ 4. 6.]] Selected elements: [0. 6. 2. 3.]
Explanation:
- arr[:2, ::2] selects the first two rows and alternate columns (0 and 2).
- arr[[1, 1, 0, 3], [3, 2, 1, 0]] retrieves elements from the specified row-column index pairs.
Basic Array Operations
NumPy arrays support various mathematical operations that can be performed on individual elements or between multiple arrays. These operations are applied efficiently without using explicit loops.
import numpy as np
a = np.array([[1, 2],
[3, 4]])
b = np.array([[4, 3],
[2, 1]])
print("Adding 1 to every element:\n", a + 1)
print("Subtracting 2 from each element:\n", b - 2)
print("Sum of all array elements:", a.sum())
print("Array sum:\n", a + b)
Output
Adding 1 to every element: [[2 3] [4 5]] Subtracting 2 from each element: [[ 2 1] [ 0 -1]] Sum of all array elements: 10 Array sum: [[5 5] [5 5]]
Explanation:
- a + 1 adds 1 to every element of array a.
- b - 2 subtracts 2 from every element of array b.
- a.sum() returns the sum of all elements in a.
- a + b performs element-wise addition between the two arrays.
Data Types in Numpy
Every NumPy array has a data type (dtype) that determines the type of values stored in the array. NumPy can automatically detect the data type or you can specify it explicitly while creating the array.
import numpy as np
x = np.array([1, 2])
print(x.dtype)
x = np.array([1.0, 2.0])
print(x.dtype)
x = np.array([1, 2], dtype = np.int64)
print(x.dtype)
Output
int64 float64 int64
Explanation:
- np.array([1, 2]) creates an integer array, so its data type is int64.
- np.array([1.0, 2.0]) creates a floating-point array, so its data type is float64.
- dtype=np.int64 explicitly sets the array's data type to int64.
- The dtype attribute is used to view the data type of a NumPy array.
Math Operations on DataType Array
NumPy provides built-in functions for performing mathematical operations on arrays. These operations are applied element-wise and can be performed efficiently on entire arrays at once.
import numpy as np
arr1 = np.array([[4, 7], [2, 6]], dtype=np.float64)
arr2 = np.array([[3, 6], [2, 8]], dtype=np.float64)
print(np.add(arr1, arr2))
print(np.sum(arr1))
print(np.sqrt(arr1))
print(arr1.T)
Output
[[ 7. 13.] [ 4. 14.]] 19.0 [[2. 2.64575131] [1.41421356 2.44948974]] [[4. 2.] [7. 6.]]
Explanation:
- np.add(arr1, arr2) performs element-wise addition of the two arrays.
- np.sum(arr1) returns the sum of all elements in arr1.
- np.sqrt(arr1) calculates the square root of each element.
- arr1.T returns the transpose of the array by swapping rows and columns.
To learn about default numpy methods, refer to Numpy Methods.