English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Array addition and deletion operations, common functions are as follows:
Functions | Elements and descriptions |
resize | Return a new array with the specified shape |
append | Add values to the end of the array |
insert | Insert values before the specified index along the specified axis |
delete | Delete a subarray along a specified axis and return the new array after deletion |
unique | Find the unique elements within the array |
The numpy.resize function returns a new array of the specified size.
If the new array size is greater than the original size, it contains copies of the elements from the original array.
numpy.resize(arr, shape)
Parameter description:
arr: The array to be modified in sizeshape: Returns the new shape of the array
import numpy as np a = np.array([[1,2,3,4,5,6,7,8],[4,5,6,7,8,9,10,11]] print('First array:') print(a) print('\n') print('Shape of the first array:') print(a.shape) print('\n') b = np.resize(a, (4,2)) print('The second array:') print(b) print('\n') print('Shape of the second array:') print(b.shape) print('\n') # Note that the first row of a is repeated in b because the size has increased print('Modify the size of the second array:') b = np.resize(a,(5,5)) print(b)
Output result is:
First array: [[ 1 2 3 4 5 6 7 8] [ 4 5 6 7 8 9 10 11]] Shape of the first array: (2, 8) The second array: [[1 2] [3 4] [5 6] [7 8]] Shape of the second array: (4, 2) Modify the size of the second array: [[ 1 2 3 4 5] [ 6 7 8 4 5] [ 6 7 8 9 10] [11 1 2 3 4] [ 5 6 7 8 4]]
The numpy.append function appends values to the end of the array. The append operation allocates the entire array, copying the original array to the new array. In addition, the dimensions of the input array must match, otherwise a ValueError will be generated.
The append function always returns a one-dimensional array.
numpy.append(arr, values, axis=None)
Parameter description:
arr: Input arrayvalues: The values to be added to arr, which need to have the same shape as arr (except for the axis to be added)axis: The default is None. When axis is undefined, it is added horizontally, and the return is always a one-dimensional array! When axis is defined, it is 0 and1when. When axis is defined, it is 0 and1when (the number of columns must be the same). When axis is1At this time, the array is added on the right (the number of rows must be the same).
import numpy as np a = np.array([[1,2,3,4,5,6,7,8],[4,5,6,7,8,9,10,11]] print('First array:') print(a) print('\n') print('Add elements to the array:') print(np.append(a, [7,8,9)) print('\n') print('Add elements along axis 0:') print(np.append(a, [[1,2,3,4,5,6,7,8]], axis = 0)) print('\n') print('Along the axis 1 Add elements:') print(np.append(a, [[5,5,5,5,5,5,5],[7,8,9,7,8,9,1]], axis = 1))
Output result is:
First array: [[ 1 2 3 4 5 6 7 8] [ 4 5 6 7 8 9 10 11]] Add elements to the array: [ 1 2 3 4 5 6 7 8 4 5 6 7 8 9 10 11 7 8 9] Add elements along axis 0: [[ 1 2 3 4 5 6 7 8] [ 4 5 6 7 8 9 10 11] [ 1 2 3 4 5 6 7 8]] Along the axis 1 Add elements: [[ 1 2 3 4 5 6 7 8 5 5 5 5 5 5 5] [ 4 5 6 7 8 9 10 11 7 8 9 7 8 9 1]]
The numpy.insert function inserts values before the given index along the given axis in the input array.
If the type of the value to be inserted is converted, it is different from the input array. The insertion is not in place, and the function returns a new array. In addition, if the axis is not provided, the input array will be flattened.
numpy.insert(arr, obj, values, axis)
Parameter description:
arr: Input arrayobj: The index before which the values are insertedvalues: The values to be insertedaxis: Along which axis to insert, if not provided, the input array will be expanded
import numpy as np a = np.array([[1,2],[3,4],[5,6]] print('First array:') print(a) print('\n') print('The Axis parameter was not passed. The input array will be expanded before insertion.') print(np.insert(a,3print('Broadcasting: ')11,12)) print('\n') print('The Axis parameter was passed. The value array will be broadcasted to match the input array.') print('Along the axis 0: ') print(np.insert(a,1print('Broadcasting: ')11, print('\n') print('Along the axis 1 Broadcasting: ') print(np.insert(a,1,11,axis = 1))
The output result is as follows:
First array: [[1 2] [3 4] [5 6]] The Axis parameter was not passed. The input array will be expanded before insertion. [ 1 2 3 11 12 4 5 6] The Axis parameter was passed. The value array will be broadcasted to match the input array. Broadcasting along axis 0: [[ 1 2] [11 11] [ 3 4] [ 5 6]] Along the axis 1 Broadcasting: [[ 1 11 2] [ 3 11 4] [ 5 11 6]]
The numpy.delete function returns a new array from the input array after removing the specified subarray. Like the insert() function, if the axis parameter is not provided, the input array will be expanded.
Numpy.delete(arr, obj, axis)
Parameter description:
arr: Input arrayobj: Can be sliced, an integer, or an integer array, indicating the subarray to be removed from the input arrayaxis: Along which axis to delete the given subarray, if not provided, the input array will be expanded
import numpy as np a = np.arange(12).reshape(3,4) print('First array:') print(a) print('\n') print('The Axis parameter was not passed. The input array will be expanded before insertion.') print(np.delete(a,5)) print('\n') print('Delete the second column:') print(np.delete(a,1,axis = 1)) print('\n') print('Contains a slice of replacement values removed from the array:') a = np.array([1,2,3,4,5,6,7,8,9,10] print(np.delete(a, np.s_[::2))
Output result is:
First array: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] The Axis parameter was not passed. The input array will be expanded before insertion. [ 0 1 2 3 4 6 7 8 9 10 11] Delete the second column: [[ 0 2 3] [ 4 6 7] [ 8 10 11]] Contains a slice of replacement values removed from the array: [ 2 4 6 8 10]
The numpy.unique function is used to remove duplicate elements from an array.
numpy.unique(arr, return_index, return_inverse, return_counts)
arr: Input array, if it is not a one-dimensional array, it will be expandedreturn_index: If true, returns the positions (indices) of the elements of the new list in the old list and stores them in a listreturn_inverse: If true, returns the positions (indices) of the elements of the old list in the new list and stores them in a listreturn_counts: If true, returns the number of occurrences of elements in the original array in the deduplicated array
import numpy as np a = np.array([5,2,6,2,7,5,6,8,2,9] print('First array:') print(a) print('\n') print('Unique values of the first array:') u = np.unique(a) print(u) print('\n') print('Indices array of the unique array:') u,indices = np.unique(a,return_index=True) print(indices) print('\n') print('We can see the corresponding values of each with the original array index:') print(a) print('\n') print('Indices of the unique array:') u,indices = np.unique(a,return_inverse=True) print(u) print('\n') print('Index is:') print(indices) print('\n') print('Reconstruct the original array using the index:') print(u[indices]) print('\n') print('Return the number of duplicates of the unique elements:') u,indices = np.unique(a,return_counts=True) print(u) print(indices)
Output result is:
First array: [5 2 6 2 7 5 6 8 2 9] Unique values of the first array: [2 5 6 7 8 9] Indices array of the unique array: [1 0 2 4 7 9] We can see the corresponding values of each with the original array index: [5 2 6 2 7 5 6 8 2 9] Indices of the unique array: [2 5 6 7 8 9] Index is: [1 0 2 0 3 1 2 4 0 5] Reconstruct the original array using the index: [5 2 6 2 7 5 6 8 2 9] Return the number of duplicates of the unique elements: [2 5 6 7 8 9] [3 2 2 1 1 1]