Staying up all night has sorted out 11 advanced operations of Numpy, each of which has parameter explanations and small examples to help you.
01 Iteration over an array
NumPy includes an iterator object numpy.nditer. It is a valid multidimensional iterator object that can be used to iterate over an array. Each element of the array can be accessed using Python's standard Iterator interface.
import numpy as np a = np.arange(0, 60, 5) a = a.reshape(3, 4) print(a) for x in np.nditer(a): print(x) [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]] 0 5 10 15 20 25 30 35 40 45 50 55
If two arrays are broadcastable, the nditer composite object can iterate over them simultaneously. Assuming that array a has dimensions 3X4 and there is another array b with dimension 1X4, the following type of iterator is used (array b is broadcast to the size of a).
import numpy as np a = np.arange(0, 60, 5) a = a.reshape(3, 4) print(a) b = np.array([1, 2, 3, 4], dtype=int) print(b) for x, y in np.nditer([a, b]): print(x, y) [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]] [1 2 3 4] 0 1 5 2 10 3 15 4 20 1 25 2 30 3 35 4 40 1 45 2 50 3 55 4
02 Array shape modification function
1.ndarray.reshape
The function modifies the shape without changing the data. The parameters are as follows:
ndarray.reshape(arr, newshape, order)
import numpy as np a = np.arange(8) print(a) b = a.reshape(4, 2) print(b) [0 1 2 3 4 5 6 7] [[0 1] [2 3] [4 5] [6 7]]
2.ndarray.flat
The function returns a one-dimensional iterator over the array, behaving like Python's built-in iterators.
import numpy as np a = np.arange(0, 16, 2).reshape(2, 4) print(a) #Returns the corresponding element of the subscript in the expanded array print(list(a.flat)) [[ 0 2 4 6] [ 8 10 12 14]] [0, 2, 4, 6, 8, 10, 12, 14]
3.ndarray.flatten
The function returns a copy of the array collapsed into one dimension. The function accepts the following parameters:
ndarray.flatten(order)
in:
order: 'C' - by row, 'F' - by column, 'A' - original order, 'k' - order of appearance of elements in memory.
import numpy as np a = np.arange(8).reshape(2, 4) print(a) # default is column-major print(a.flatten()) print(a.flatten(order='F')) [[0 1 2 3] [4 5 6 7]] [0 1 2 3 4 5 6 7] [0 4 1 5 2 6 3 7]
03 Array flip operation function
1.numpy.transpose
The function flips the dimensions of the given array. It returns a view if possible. The function accepts the following parameters:
numpy.transpose(arr, axes)
in:
-
arr: the array to be transposed
-
axes: A list of integers, corresponding to the dimensions, usually all dimensions are flipped.
import numpy as np a = np.arange(24).reshape(2, 3, 4) print(a) b = np.array(np.transpose(a)) print(b) print(b.shape) [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] [[[ 0 12] [ 4 16] [ 8 20]] [[ 1 13] [ 5 17] [ 9 21]] [[ 2 14] [ 6 18] [10 22]] [[ 3 15] [ 7 19] [11 23]]] (4, 3, 2) b = np.array(np.transpose(a, (1, 0, 2))) print(b) print(b.shape [[[ 0 1 2 3] [12 13 14 15]] [[ 4 5 6 7] [16 17 18 19]] [[ 8 9 10 11] [20 21 22 23]]] (3, 2, 4)
-
numpy.ndarray.T
This function is of class ndarray and behaves like numpy.transpose.
import numpy as np a = np.arange(12).reshape(3, 4) print(a) print(a.T) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]
3.numpy.swapaxes
The function swaps the two axes of an array. This function accepts the following parameters:
numpy.swapaxes(arr, axis1, axis2)
in:
-
arr: the input array whose axes are to be swapped
-
axis1: integer corresponding to the first axis
-
axis2: integer corresponding to the second axis
import numpy as np a = np.arange(8).reshape(2, 2, 2) print(a) print(np.swapaxes(a, 2, 0)) [[[0 1] [2 3]] [[4 5] [6 7]]] [[[0 4] [2 6]] [[1 5] [3 7]]]
4.numpy.rollaxis
The numpy.rollaxis() function rolls a specific axis backwards until a specific position. This function accepts three parameters:
numpy.rollaxis(arr, axis, start)
in:
-
arr: input array
-
axis: the axis to scroll backwards, the relative positions of other axes will not change
-
start: Defaults to zero, which means full scrolling. will scroll to a specific position.
import numpy as np a = np.arange(8).reshape(2,2,2) print(a) print(np.rollaxis(a,2)) print(np.rollaxis(a,2,1)) [[[0 1] [2 3]] [[4 5] [6 7]]] [[[0 2] [4 6]] [[1 3] [5 7]]] [[[0 2] [1 3]] [[4 6] [5 7]]]
04 Array modification dimension function
1.numpy.broadcast_to
The function broadcasts the array to the new shape. It returns a read-only view on the original array. It is usually discontinuous. This function may throw a ValueError if the new shape does not conform to NumPy's broadcasting rules. The function accepts the following parameters:
numpy.broadcast_to(array, shape, subok)
import numpy as np a = np.arange(4).reshape(1,4) print(a) print(np.broadcast_to(a,(4,4))) [[0 1 2 3]] [[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]]
2.numpy.expand_dims
The function expands the array shape by inserting new axes at the specified positions. The function requires two parameters:
numpy.expand_dims(arr, axis)
in:
-
arr: input array
-
axis: the position where the new axis is inserted
import numpy as np x = np.array(([1, 2], [3, 4])) print(x) y = np.expand_dims(x, axis=0) print(y) print(x.shape, y.shape) y = np.expand_dims(x, axis=1) print(y) print(x.ndim, y.ndim) print(x.shape, y.shape) [[1 2] [3 4]] [[[1 2] [3 4]]] (2, 2) (1, 2, 2) [[[1 2]] [[3 4]]] 2 3 (2, 2) (2, 1, 2)
3.numpy.squeeze
Function removes one-dimensional entries from the shape of a given array. This function requires two parameters.
numpy.squeeze(arr, axis)
in:
-
arr: input array
-
axis: Integer or tuple of integers to select a subset of single dimension entries in the shape
import numpy as np x = np.arange(9).reshape(1, 3, 3) print(x) y = np.squeeze(x) print(y) print(x.shape, y.shape) [[[0 1 2] [3 4 5] [6 7 8]]] [[0 1 2] [3 4 5] [6 7 8]] (1, 3, 3) (3, 3)
05 Array concatenation operation
The connection functions of arrays in NumPy mainly include the following four:
-
concatenate concatenates data series along existing axes
-
stack joins array sequences along new axes
-
hstack stacks arrays in a sequence horizontally (column direction)
-
vstack stacks arrays in a sequence vertically (row-wise)
1.numpy.stack
The function concatenates a sequence of arrays along a new axis and requires the following arguments:
numpy.stack(arrays, axis)
in:
-
arrays: sequence of arrays of the same shape
-
axis: Returns the axis in the array along which the input arrays are stacked
import numpy as np a = np.array([[1,2],[3,4]]) print(a) b = np.array([[5,6],[7,8]]) print(b) print(np.stack((a,b),0)) print(np.stack((a,b),1)) [[1 2] [3 4]] [[5 6] [7 8]] [[[1 2] [3 4]] [[5 6] [7 8]]] [[[1 2] [5 6]] [[3 4] [7 8]]]
2.numpy.hstack
Is a variant of the numpy.stack() function that stacks to generate a single horizontal array.
import numpy as np a = np.array([[1, 2], [3, 4]]) print(a) b = np.array([[5, 6], [7, 8]]) print(b) print('To stack horizontally:') c = np.hstack((a, b)) print(c) [[1 2] [3 4]] [[5 6] [7 8]] To stack horizontally: [[1 2 5 6] [3 4 7 8]]
3.numpy.vstack
Is a variant of the numpy.stack() function that stacks to generate a single vertical array.
import numpy as np a = np.array([[1, 2], [3, 4]]) print(a) b = np.array([[5, 6], [7, 8]]) print(b) print('Stack vertically:') c = np.vstack((a, b)) print(c) [[1 2] [3 4]] [[5 6] [7 8]] Stack vertically: [[1 2] [3 4] [5 6] [7 8]]
4.numpy.concatenate
The function is used to join two or more arrays of the same shape along a specified axis. The function accepts the following parameters.
numpy.concatenate((a1, a2, ...), axis)
in:
-
a1, a2, ...: sequence of arrays of the same type
-
axis: the axis along which to connect the arrays, defaults to 0
import numpy as np a = np.array([[1,2],[3,4]]) print(a) b = np.array([[5,6],[7,8]]) print(b) print(np.concatenate((a,b))) print(np.concatenate((a,b),axis = 1)) [[1 2] [3 4]] [[5 6] [7 8]] [[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]]
06 Array division operation
The array division functions of arrays in NumPy are mainly as follows:
-
split splits an array into multiple subarrays
-
hsplit splits an array horizontally into multiple subarrays (by columns)
-
vsplit splits an array vertically into subarrays (rows)
1.numpy.split
This function splits an array into subarrays along a specific axis. The function accepts three parameters:
numpy.split(ary, indices_or_sections, axis)
in:
-
ary: the input array to be split
-
indices_or_sections: Can be an integer indicating the number of equal-sized sub-arrays to be created from the input array. If this parameter is a one-dimensional array, its elements indicate the points at which new subarrays are to be created.
-
axis: default is 0
import numpy as np a = np.arange(9) print(a) print('Divide the array into three equal-sized subarrays:') b = np.split(a,3) print(b) print('Divide the array at the indicated position in a one-dimensional array:') b = np.split(a,[4,7]) print(b) [0 1 2 3 4 5 6 7 8] Divide the array into three equal-sized subarrays: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])] Divide the array at the indicated position in a one-dimensional array: [array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
2.numpy.hsplit
A special case of the split() function, where an axis of 1 indicates a horizontal split.
import numpy as np a = np.arange(16).reshape(4,4) print(a) print('Split horizontally:') b = np.hsplit(a,2) print(b) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Split horizontally: [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])]
3.numpy.vsplit
A special case of the split() function, where an axis of 0 indicates a vertical split, regardless of the dimensions of the input array.
import numpy as np a = np.arange(16).reshape(4,4) print(a) print('Split vertically:') b = np.vsplit(a,2) print(b) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Split vertically: [array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])]
07 Array element operations
The array manipulation functions in NumPy are mainly as follows:
-
resize returns a new array of the specified shape
-
append appends the value to the end of the array
-
insert inserts the value along the specified axis before the specified subscript
-
delete returns a new array with subarrays of an axis deleted
-
unique finds the unique element within an array
1.numpy.resize
The function returns a new array of the specified size. If the new size is larger than the original size, contains duplicate copies of the elements in the original array. If it is less than, remove part of the data of the original array. The function accepts the following parameters:
numpy.resize(arr, shape)
in:
-
arr: the input array to resize
-
shape: returns the new shape of the array
import numpy as np a = np.array([[1,2,3],[4,5,6]]) print(a) print(a.shape) b = np.resize(a, (3,2)) print(b) print(b.shape) print('Modify the size of the second array:') b = np.resize(a,(3,3)) print(b) print('Modify the size of the third array:') b = np.resize(a,(2,2)) print(b) [[1 2 3] [4 5 6]] (2, 3) [[1 2] [3 4] [5 6]] (3, 2) Modify the size of the second array: [[1 2 3] [4 5 6] [1 2 3]] Modify the size of the third array: [[1 2] [3 4]]
2.numpy.append
The function adds values to the end of the input array. The append operation is not in-place, but allocates a new array. Also, the dimensions of the input arrays must match or a ValueError will be raised. Function accepts the following functions:
numpy.append(arr, values, axis)
in:
-
arr: input array
-
values: the values to add to arr, e.g. the same shape as arr (except the axis to add)
-
axis: The axis along which the operation is done. If not provided, both arguments will be expanded.
import numpy as np a = np.array([[1,2,3],[4,5,6]]) print(a) print(np.append(a, [[7,8,9]],axis = 0)) print(np.append(a, [[5,5,5],[7,8,9]],axis = 1)) [[1 2 3] [4 5 6]] [[1 2 3] [4 5 6] [7 8 9]] [[1 2 3 5 5 5] [4 5 6 7 8 9]]
3.numpy.insert
The function inserts values in the input array along the given axis before the given index. If the value is typecast to be inserted, it is not the same as the input array. Inserting out of place, the function returns a new array. Also, if no axis is provided, the input array is expanded.
The insert() function accepts the following parameters:
numpy.insert(arr, obj, values, axis)
in:
-
arr: input array
-
obj: the index before which to insert the value
-
values: the values to insert
-
axis: the axis along which to interpolate
import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print(a) print(np.insert(a,3,[11,12])) print(np.insert(a,1,[11],axis = 0)) print(np.insert(a,1,[11],axis = 1)) [[1 2] [3 4] [5 6]] [ 1 2 3 11 12 4 5 6] [[ 1 2] [11 11] [ 3 4] [ 5 6]] [[ 1 11 2] [ 3 11 4] [ 5 11 6]]
4.numpy.delete
The function returns a new array with the specified subarray removed from the input array. As is the case with the insert() function, if no axis argument is provided, the input array will be expanded. The function accepts the following parameters:
Numpy.delete(arr, obj, axis)
in:
-
arr: input array
-
obj: can be sliced, integer or array of integers, indicating the subarray to be removed from the input array
-
axis: the axis along which to delete the given subarray
import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print(a) print(np.delete(a,5)) print(np.delete(a,1,axis = 1)) [[1 2] [3 4] [5 6]] [1 2 3 4 5] [[1] [3] [5]]
5.numpy.unique
The function returns an array of deduplicated elements in the input array. The function can return a tuple containing the deduplicated array and the associated index array. The nature of the index depends on the type of the return parameter in the function call.
numpy.unique(arr, return_index, return_inverse, return_counts)
in:
-
arr: input array, if it is not a one-dimensional array, it will be expanded
-
return_index: if true, returns the index of the element in the input array
-
return_inverse: if true, returns the subscript of the deduplicated array, which can be used to reconstruct the input array
-
return_counts: If true, returns the number of occurrences of elements in the deduplicated array in the original array
import numpy as np a = np.array([5,2,6,2,7,5,6,8,2,9]) u = np.unique(a) print(u) u,indices = np.unique(a, return_index = True) print(u, indices) u,indices = np.unique(a,return_inverse = True) print(u, indices) u,indices = np.unique(a,return_counts = True) print(u, indices) [2 5 6 7 8 9][2 5 6 7 8 9] [1 0 2 4 7 9][2 5 6 7 8 9] [1 0 2 0 3 1 2 4 0 5][2 5 6 7 8 9] [3 2 2 1 1 1]
08 NumPy - String Functions
The following functions are used to perform vectorized string operations on arrays of dtype numpy.string_ or numpy.unicode_. They are based on standard string functions from Python's built-in library. Defined in the character array class (numpy.char)
import numpy as np print(np.char.add(['hello'],[' xyz'])) print(np.char.add(['hello', 'hi'],[' abc', ' xyz'])) print(np.char.multiply('Hello ',3)) print(np.char.center('hello', 20,fillchar = '*')) print(np.char.capitalize('hello world')) print(np.char.title('hello how are you?')) print(np.char.lower(['HELLO','WORLD'])) print(np.char.lower('HELLO')) print(np.char.upper('hello')) print(np.char.upper(['hello','world'])) print(np.char.split ('hello how are you?')) print(np.char.split ('YiibaiPoint,Hyderabad,Telangana', sep = ',')) print(np.char.splitlines('hello\nhow are you?')) print(np.char.splitlines('hello\rhow are you?')) print(np.char.strip('ashok arora','a')) print(np.char.strip(['arora','admin','java'],'a')) print(np.char.join(':','dmy')) print(np.char.join([':','-'],['dmy','ymd'])) print(np.char.replace ('He is a good boy', 'is', 'was')) a = np.char.encode('hello', 'cp500') print(a) print(np.char.decode(a,'cp500')) ['hello xyz'] ['hello abc' 'hi xyz'] Hello Hello Hello *******hello******** Hello world Hello How Are You? ['hello' 'world'] hello HELLO ['HELLO' 'WORLD'] ['hello', 'how', 'are', 'you?'] ['YiibaiPoint', 'Hyderabad', 'Telangana'] ['hello', 'how are you?'] ['hello', 'how are you?'] shok aror ['ror' 'dmin' 'jav'] d:m:y ['d:m:y' 'y-m-d'] He was a good boy b'\x88\x85\x93\x93\x96' hello
09 NumPy - Arithmetic Functions
NumPy includes a large number of functions for various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, complex number processing functions, and more.
-
Trigonometric functions
-
rounding function
-
Arithmetic function
-
NumPy - trigonometric functions
NumPy has standard trigonometric functions that return trigonometric ratios for a given angle in radians. The arcsin, arccos, and arctan functions return the inverse trigonometric functions of sin, cos, and tan for a given angle. The results of these functions can be verified by converting from radians to degrees with the numpy.degrees() function.
import numpy as np a = np.array([0,30,45,60,90]) #Convert to radians by multiplying pi/180 print(np.sin(a*np.pi/180)) print(np.cos(a*np.pi/180)) print(np.tan(a*np.pi/180)) [ 0. 0.5 0.70710678 0.8660254 1. ] [ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17] [ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]
2.NumPy - rounding functions
-
numpy.around() This function returns the value rounded to the desired precision
-
numpy.around(a,decimals) - a input array
-
decimals The number of decimal places to round. The default value is 0. If negative, the integer will be rounded to the left of the decimal point
-
The numpy.floor() function returns the largest integer not larger than the input argument.
-
The numpy.ceil() function returns the upper bound of the input value, which is greater than the smallest integer of the input parameter
import numpy as np a = np.array([1.0, 5.55, 123, 0.567, 25.532]) print(np.around(a)) print(np.around(a, decimals=1)) print(np.floor(a)) print(np.ceil(a)) [ 1. 6. 123. 1. 26.] [ 1. 5.6 123. 0.6 25.5] [ 1. 5. 123. 0. 25.] [ 1. 6. 123. 1. 26.]
3.NumPy - Arithmetic Operations
Input arrays used to perform arithmetic operations such as add(), subtract(), multiply(), and divide() must have the same shape or conform to array broadcasting rules.
-
The numpy.reciprocal() function returns the element-wise reciprocal of the argument.
-
The numpy.power() function takes the element in the first input array as the base and computes it to the power of the corresponding element in the second input array.
-
The numpy.mod() function returns the remainder of the division of the corresponding element in the input array
import numpy as np a = np.array([0.25, 2, 1, 0.2, 100]) print(np.reciprocal(a)) print(np.power(a,2)) a = np.array([10,20,30]) b = np.array([3,5,7]) print(np.mod(a,b)) [ 4. 0.5 1. 5. 0.01] [ 6.25000000e-02 4.00000000e+00 1.00000000e+00 4.00000000e-02. 1.00000000e+04] [1 0 2]
4.NumPy - Statistical Functions
NumPy has many useful statistical functions for finding min, max, percent standard deviation and variance, etc. from a given element in an array.
-
numpy.amin() , numpy.amax() Return the minimum and maximum values along the specified axis from the elements in the given array.
-
The numpy.ptp() function returns the range (max - min) of values along an axis.
-
numpy.percentile() means less than a certain percentage of this worthy observation
-
numpy.percentile(a, q, axis)
-
a input array;
-
q percentile to calculate, between 0 and 100;
-
axis the axis along which it calculates percentiles
-
-
numpy.median() returns the median of a data sample.
-
numpy.mean() returns the arithmetic mean of the elements in an array along the axis.
-
numpy.average() returns the weighted average obtained by multiplying each component by a factor reflecting its importance
import numpy as np a = np.array([[3,7,5],[8,4,3],[2,4,9]]) print(np.amin(a,1)) print(np.amax(a,1)) print(np.ptp(a)) print(np.percentile(a,50)) print(np.median(a)) print(np.mean(a)) print(np.average(a)) print(np.std([1,2,3,4])) #Returns the array standard deviation print(np.var([1,2,3,4])) #Returns the array variance [3 3 2] [7 8 9] 7 4.0 4.0 5.0 5.0 1.11803398875 1.25
10 Sorting, Searching and Counting Functions
Various sorting-related functions are available in NumPy.
-
The numpy.sort() function returns a sorted copy of the input array. numpy.sort(a, axis, kind, order)
-
a array to be sorted;
-
axis is the axis along which to sort the array, if no array is expanded, sort along the last axis;
-
kind defaults to 'quicksort' (quick sort);
-
order if the array contains fields, the field to sort by
-
-
The numpy.argsort() function performs an indirect sort on an input array along a given axis and returns an array of indices of the data using the specified sort type. This index array is used to construct the sorted array.
-
The numpy.lexsort() function performs indirect sorting using key sequences. A key can be thought of as a column in a spreadsheet. This function returns an indexed array with which sorted data can be obtained. Note that the last key happens to be sort 's primary key.
-
The two functions numpy.argmax() and numpy.argmin() return the indices of the largest and smallest elements, respectively, along a given axis.
-
The numpy.nonzero() function returns the indices of nonzero elements in the input array.
-
The numpy.where() function returns the index of the element in the input array that satisfies the given condition.
-
The numpy.extract() function returns elements that satisfy any condition.
import numpy as np a = np.array([[3, 7, 3, 1], [9, 7, 8, 7]]) print(np.sort(a)) print(np.argsort(a)) print(np.argmax(a)) print(np.argmin(a)) print(np.nonzero(a)) print(np.where(a > 3)) nm = ('raju', 'anil', 'ravi', 'amar') dv = ('f.y.', 's.y.', 's.y.', 'f.y.') print(np.lexsort((dv, nm))) [[1 3 3 7] [7 7 8 9]] [[3 0 2 1] [1 3 2 0]] 4 3 (array([0, 0, 0, 0, 1, 1, 1, 1], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3], dtype=int64)) (array([0, 1, 1, 1, 1], dtype=int64), array([1, 0, 1, 2, 3], dtype=int64)) [3 1 0 2]
11 IO file operations
ndarray objects can be saved to and loaded from disk files. The available IO functions are:
-
The numpy.save() file stores the input array in a disk file with the npy extension.
-
numpy.load() rebuilds the array from the npy file.
-
The numpy.savetxt() and numpy.loadtxt() functions store and retrieve array data in a simple text file format.
import numpy as np a = np.array([1,2,3,4,5]) np.save('outfile',a) b = np.load('outfile.npy') print(b) a = np.array([1,2,3,4,5]) np.savetxt('out.txt',a) b = np.loadtxt('out.txt') print(b) [1 2 3 4 5] [ 1. 2. 3. 4. 5.]
-END-
If you want to learn more technology, please get free resources on WeChat below.