NumPy array functions are the built-in functions provided by NumPy that allow us to create and manipulate arrays, and perform different operations on them.
We will discuss some of the most commonly used NumPy array functions.
Common NumPy Array Functions
There are many NumPy array functions available but here are some of the most commonly used ones.
| Array Operations | Functions |
|---|---|
| Array Creation Functions | np.array(), np.zeros(), np.ones(), np.empty(), etc. |
| Array Manipulation Functions | np.reshape(), np.transpose(), etc. |
| Array Mathematical Functions | np.add(), np.subtract(), np.sqrt(), np.power(), etc. |
| Array Statistical Functions | np.median(), np.mean(), np.std(), and np.var(). |
| Array Input and Output Functions | np.save(), np.load(), np.loadtxt(), etc. |
NumPy Array Creation Functions
Array creation functions allow us to create new NumPy arrays. For example,
Output
np.array(): [1 3 5] np.zeros(): [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] np.ones(): [[1. 1. 1. 1.] [1. 1. 1. 1.]]
Here,
np.array()- creates an array from a Python Listnp.zeros()- creates an array filled with zeros of the specified shapenp.ones()- creates an array filled with ones of the specified shape
Note: To learn more about NumPy Array Creation, please visit NumPy Array Creation and NumPy N-d Array Creation.
NumPy Array Manipulation Functions
NumPy array manipulation functions allow us to modify or rearrange NumPy arrays. For example,
Output
Original array: [ 1 3 5 7 9 11] Reshaped array: [[ 1 3 5] [ 7 9 11]] Transposed array: [[ 1 7] [ 3 9] [ 5 11]]
In this example,
np.reshape(array1, (2, 3))- reshapes array1 into 2D array with shape(2,3)np.transpose(array2)- transposes 2D array array2
Note: To learn more about np.reshape() and np.transpose(), please visit NumPy Array Reshaping and NumPy Array Transpose.
NumPy Array Mathematical Functions
In NumPy, there are tons of mathematical functions to perform on arrays. For example,
Output
Sum of arrays: [ 5 11 19 29 41] Difference of arrays: [ -3 -7 -13 -21 -31] Square root of first array: [2. 3. 4. 5. 6.]
Note: To learn more about NumPy Array Mathematical Functions, visit Numpy Arithmetic Array Operations and Numpy Math Functions.
NumPy Array Statistical Functions
NumPy provides us with various statistical functions to perform statistical data analysis.
These statistical functions are useful to find basic statistical concepts like mean, median, variance, etc. It is also used to find the maximum or the minimum element in an array.
Let's see an example.
Output
Mean: 77.2 Median: 78.0 Minimum marks: 66 Maximum marks: 85
Here, computed the mean, median, minimum, and maximum of the given array marks.
Note: To learn more about Numpy Array Statistical Functions, visit Numpy Statistical Functions
NumPy Array Input/Output Functions
NumPy offers several input/output (I/O) functions for loading and saving data to and from files. For example,
Output
[[1. 3. 5.] [2. 4. 6.]]
In this example, we first created the 2D array named array1 and then saved it to a text file using the np.savetxt() function.
We then loaded the saved data using the np.loadtxt() function.
Note: To learn more about NumPy Input/Output Functions, visit NumPy Input and Output.