NumPy stands for Numerical Python. It is a popular Python library used for numerical computing, arrays, mathematical operations and scientific data processing.
ndarray data structure and many efficient
functions for working with numerical data.
NumPy is an open-source Python library designed for efficient numerical computations.
It is widely used in data science, machine learning, scientific computing, engineering and data analysis.
NumPy can be installed using pip.
pip install numpy
You can also use:
python -m pip install numpy
NumPy is commonly imported using the alias np.
import numpy as np
The alias makes NumPy functions shorter and easier to write.
The np.array() function creates a NumPy array.
import numpy as np
numbers = np.array([10, 20, 30, 40])
print(numbers)
[10 20 30 40]
Python lists and NumPy arrays can both store collections of values, but NumPy arrays are designed for efficient numerical operations and multidimensional data.
numbers = np.array([1, 2, 3, 4])
print(numbers * 2)
[2 4 6 8]
NumPy performs the operation element by element.
NumPy arrays can have different numbers of dimensions.
0-D Array:
a = np.array(10)
1-D Array:
a = np.array([10, 20, 30])
2-D Array:
a = np.array([
[1, 2],
[3, 4]
])
3-D Array:
a = np.array([
[
[1, 2],
[3, 4]
]
])
The ndim attribute returns the number of dimensions of an array.
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
print(a.ndim)
2
The shape attribute returns the size of the array along each
dimension.
import numpy as np
a = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(a.shape)
(2, 3)
This means the array contains 2 rows and 3 columns.
The size attribute returns the total number of elements.
a = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(a.size)
6
The dtype attribute tells you the data type of the array elements.
a = np.array([10, 20, 30])
print(a.dtype)
The exact displayed dtype depends on the values and NumPy/platform details.
The np.arange() function creates evenly spaced values within
a specified range.
import numpy as np
numbers = np.arange(1, 6)
print(numbers)
[1 2 3 4 5]
The stop value is not included.
The np.zeros() function creates an array filled with zeros.
import numpy as np
numbers = np.zeros(5)
print(numbers)
[0. 0. 0. 0. 0.]
The np.ones() function creates an array filled with ones.
numbers = np.ones(5)
print(numbers)
[1. 1. 1. 1. 1.]
The np.empty() function creates an array without explicitly
initializing its elements to a particular value. The initial values are
not guaranteed to be zero.
numbers = np.empty(5)
print(numbers)
empty() should not be treated as
meaningful initial data. Assign values before using them.
The np.linspace() function creates a specified number of
evenly spaced values over an interval.
numbers = np.linspace(0, 10, 5)
print(numbers)
[ 0. 2.5 5. 7.5 10. ]
NumPy arrays use zero-based indexing.
numbers = np.array([10, 20, 30, 40])
print(numbers[0])
print(numbers[2])
10 30
Negative indexes access elements from the end of the array.
numbers = np.array([10, 20, 30, 40])
print(numbers[-1])
print(numbers[-2])
40 30
Array slicing is used to select a range of elements.
numbers = np.array([10, 20, 30, 40, 50])
print(numbers[1:4])
[20 30 40]
For a 2-D array, indexes can be used for both row and column.
numbers = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(numbers[0, 1])
print(numbers[1, 2])
20 60
NumPy allows arithmetic operations to be performed element by element.
a = np.array([10, 20, 30])
print(a + 5)
print(a * 2)
print(a - 3)
[15 25 35] [20 40 60] [ 7 17 27]
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(a + b)
print(a * b)
[11 22 33] [10 40 90]
NumPy provides many mathematical functions.
numbers = np.array([1, 4, 9, 16])
print(np.sqrt(numbers))
[1. 2. 3. 4.]
Other useful functions include np.sin(), np.cos(),
np.exp() and np.log().
numbers = np.array([10, 20, 30, 40])
print(np.sum(numbers))
print(np.min(numbers))
print(np.max(numbers))
100 10 40
numbers = np.array([10, 20, 30, 40])
print(np.mean(numbers))
print(np.std(numbers))
The mean() function calculates the arithmetic average and
std() calculates the standard deviation.
The reshape() method changes the shape of an array without
changing its data.
numbers = np.array([1, 2, 3, 4, 5, 6])
matrix = numbers.reshape(2, 3)
print(matrix)
[[1 2 3] [4 5 6]]
The flatten() method returns a flattened copy of an array.
matrix = np.array([
[1, 2],
[3, 4]
])
numbers = matrix.flatten()
print(numbers)
[1 2 3 4]
The np.concatenate() function joins arrays along an existing
axis.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.concatenate((a, b))
print(result)
[1 2 3 4 5 6]
NumPy provides functions such as vstack() and
hstack() for combining arrays along different dimensions.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
[[1 2 3] [4 5 6]]
The np.sort() function returns a sorted copy of an array.
numbers = np.array([40, 10, 30, 20])
result = np.sort(numbers)
print(result)
[10 20 30 40]
Boolean conditions can be used to filter NumPy arrays.
numbers = np.array([10, 20, 30, 40, 50])
result = numbers[numbers > 25]
print(result)
[30 40 50]
NumPy provides a random number module for generating random values.
import numpy as np
numbers = np.random.randint(
1,
10,
size=5
)
print(numbers)
The values will vary each time the program runs.
numbers = np.random.rand(5)
print(numbers)
This generates five random floating-point values in the interval from 0 up to but not including 1.
NumPy also provides functions for linear algebra operations.
a = np.array([
[1, 2],
[3, 4]
])
print(np.linalg.det(a))
The numpy.linalg module provides functions for matrix and
linear algebra calculations.
NumPy is an important foundation for the Python data science ecosystem. Many libraries use NumPy arrays or concepts built around numerical arrays.
np.empty() initializes an array with zeros.import numpy as np
numbers = np.array([
10, 20, 30, 40, 50
])
print("Array:", numbers)
print("Sum:", np.sum(numbers))
print("Average:", np.mean(numbers))
print("Maximum:", np.max(numbers))
print("Minimum:", np.min(numbers))
print("Greater than 25:",
numbers[numbers > 25])
Array: [10 20 30 40 50] Sum: 150 Average: 30.0 Maximum: 50 Minimum: 10 Greater than 25: [30 40 50]
np.np.array() creates NumPy arrays.ndim returns the number of dimensions.shape returns the dimensions of an array.size returns the total number of elements.dtype shows the element data type.arange() creates evenly spaced values using a step.linspace() creates a specified number of evenly spaced values.zeros() creates arrays filled with zeros.ones() creates arrays filled with ones.reshape() changes the shape of an array.Question: Which function is commonly used to create a NumPy array?