NumPy Array Functions
Last Updated :
24 Jan, 2025
Improve
NumPy array functions are a set of built-in operations provided by the NumPy library that allow users to perform various tasks on arrays. With NumPy array functions, you can create, reshape, slice, sort, perform mathematical operations, and much more—all while taking advantage of the library's speed and efficiency.
Table of Content
This article explores some of the most important NumPy array functions with examples to help you harness their power.
Array Creation Functions
- np.array(): Converts a Python list, tuple, or sequence into an array.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
- np.zeros(): Creates an array filled with zeros.
import numpy as np
zeros_array = np.zeros((2, 3))
print(zeros_array)
- np.ones(): Creates an array filled with ones.
import numpy as np
ones_array = np.ones((3, 2))
print(ones_array)
- np.arange(): Generates an array with values in a specified range.
import numpy as np
range_array = np.arange(0, 10, 2)
print(range_array)
- np.linspace() : Generates an array of evenly spaced numbers over a specified range.
import numpy as np
linear_array = np.linspace(0, 1, 5)
print(linear_array)
-
np.random
Functions: Generates arrays with random values.
import numpy as np
random_array = np.random.rand(2, 3)
print(random_array)
Array Manipulation Functions
- np.reshape(): Reshapes an array without changing its data.
import numpy as np
reshaped = np.reshape(np.arange(6), (2, 3))
print(reshaped)
- np.flatten(): Flattens a multi-dimensional array into one dimension.
import numpy as np
flattened = reshaped.flatten()
print(flattened)
- np.transpose(): Transposes the dimensions of an array.
import numpy as np
transposed = reshaped.T
print(transposed)
- np.concatenate(): Joins two or more arrays along an axis.
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
concatenated = np.concatenate((a, b))
print(concatenated)
Mathematical and Statistical Functions
- np.sum() : Computes the sum of array elements.
import numpy as np
array = np.array([1, 2, 3])
total = np.sum(array)
print(total)
- np.mean(): Computes the mean of array elements.
import numpy as np
mean_value = np.mean(array)
print(mean_value)
import numpy as np
max_val = np.max(array)
min_val = np.min(array)
print(max_val)
print(min_value)
- np.sqrt(): Computes the square root of each element in an array.
import numpy as np
sqrt_array = np.sqrt(array)
print(sqrt_array)
Indexing and Slicing Functions
- Indexing: Access specific elements
import numpy as np
array = np.array([1, 2, 3, 4])
element = array[2]
print(element)
- Slicing: Access subsets of arrays
import numpy as np
subset = array[1:3]
print(subset)
Sorting and Searching Functions
- np.sort(): Sorts an array.
import numpy as np
sorted_array = np.sort(np.array([3, 1, 2]))
print(sorted_array)
- np.argsort() : Returns the indices of the sorted elements.
import numpy as np
indices = np.argsort(np.array([3, 1, 2]))
print(indices)
- np.where(): Returns the indices of elements that satisfy a condition.
import numpy as np
array = np.array([1, 2, 3, 4])
indices = np.where(array > 2)
print(indices)
Input/Output Functions
import numpy as np
np.save('array.npy', array)
loaded_array = np.load('array.npy')
print(loaded array)
- np.savetxt() and np.loadtxt() : Save and load arrays in text format.
import numpy as np
np.savetxt('array.txt', array)
loaded_array = np.loadtxt('array.txt')
print(loaded_array)