Common NumPy Array Functions
There are many NumPy array functions np.array():
available but here are some of the most [1 3 5]
commonly used ones.
np.zeros():
Array [[0. 0. 0.]
Functions
Operations [0. 0. 0.]
[0. 0. 0.]]
np.array(), np.ones():
Array np.zeros(), [[1. 1. 1. 1.]
Creation [1. 1. 1. 1.]]
np.ones(),
Functions
np.empty(), etc. Here,
np.array() - creates an array from a Python
Array np.reshape(), List
Manipulation np.transpose(), np.zeros() - creates an array filled with zeros
Functions etc. of the specified shape
np.ones() - creates an array filled with ones of
the specified shape
np.add(),
Array
np.subtract(),
Mathematical
np.sqrt(), NumPy Array Manipulation Functions
Functions
np.power(), etc. NumPy array manipulation functions allow us
to modify or rearrange NumPy arrays. For
np.median(), example,
Array import numpy as np
np.mean(),
Statistical
np.std(), and # create a 1D array
Functions
np.var(). array1 = np.array([1, 3, 5, 7, 9, 11])
Array Input np.save(), # reshape the 1D array into a 2D array
and Output np.load(), array2 = np.reshape(array1, (2, 3))
Functions np.loadtxt(), etc.
# transpose the 2D array
array3 = np.transpose(array2)
print("Original array:\n", array1)
print("\nReshaped array:\n", array2)
NumPy Array Creation Functions print("\nTransposed array:\n", array3)
Array creation functions allow us to create Output
new NumPy arrays. For example,
import numpy as np Original array:
[ 1 3 5 7 9 11]
# create an array using np.array()
array1 = np.array([1, 3, 5]) Reshaped array:
print("np.array():\n", array1) [[ 1 3 5]
[ 7 9 11]]
# create an array filled with zeros using
np.zeros() Transposed array:
array2 = np.zeros((3, 3)) [[ 1 7]
print("\nnp.zeros():\n", array2) [ 3 9]
[ 5 11]]
# create an array filled with ones using
np.ones() In this example,
array3 = np.ones((2, 4)) np.reshape(array1, (2, 3)) -
print("\nnp.ones():\n", array3)
reshapes array1 into 2D array with shape (2,3)
Output
np.transpose(array2) - transposes 2D
array array2 # find the minimum and maximum marks
min_marks = np.min(marks)
NumPy Array Mathematical Functions print("Minimum marks:", min_marks)
In NumPy, there are tons of mathematical
functions to perform on arrays. For example, max_marks = np.max(marks)
import numpy as np print("Maximum marks:", max_marks)
Output
# create two arrays
array1 = np.array([1, 2, 3, 4, 5]) Mean: 77.2
array2 = np.array([4, 9, 16, 25, 36]) Median: 78.0
Minimum marks: 66
# add the two arrays element-wise Maximum marks: 85
arr_sum = np.add(array1, array2)
Here, computed the mean, median, minimum,
# subtract the array2 from array1 element-wise and maximum of the given array marks.
arr_diff = np.subtract(array1, array2)
NumPy Array Input/Output Functions
# compute square root of array2 element-wise NumPy offers several input/output (I/O)
arr_sqrt = np.sqrt(array2) functions for loading and saving data to and
from files. For example,
import numpy as np
print("\nSum of arrays:\n", arr_sum)
print("\nDifference of arrays:\n", arr_diff) # create an array
print("\nSquare root of first array:\n", arr_sqrt) array1 = np.array([[1, 3, 5], [2, 4, 6]])
Output
# save the array to a text file
Sum of arrays: np.savetxt('data.txt', array1)
[ 5 11 19 29 41]
# load the data from the text file
Difference of arrays: loaded_data = np.loadtxt('array1.txt')
[ -3 -7 -13 -21 -31]
# print the loaded data
Square root of first array: print(loaded_data)
[2. 3. 4. 5. 6.] Output
NumPy Array Statistical Functions [[1. 3. 5.]
NumPy provides us with various statistical [2. 4. 6.]]
functions to perform statistical data analysis.
These statistical functions are useful to find In this example, we first created the 2D array
basic statistical concepts like mean, median, named array1 and then saved it to a text file
variance, etc. It is also used to find the using the np.savetxt() function.
maximum or the minimum element in an We then loaded the saved data using
array. the np.loadtxt() function.
Let's see an example.
import numpy as np
# create a numpy array
marks = np.array([76, 78, 81, 66, 85])
# compute the mean of marks
mean_marks = np.mean(marks)
print("Mean:",mean_marks)
# compute the median of marks
median_marks = np.median(marks)
print("Median:",median_marks)