What is the use of the numpy.insert () function
This article is about what numpy.insert() function is used for. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Numpy function
insert function
numpy.insert can have three parameters (arr, obj, values) or four parameters (arr, obj, values, axis):
The first argument arr is an array, which can be one-dimensional or multidimensional, and inserts elements on top of arr
The second parameter obj is where the element is inserted
The third parameter values is the value to insert
The fourth parameter axis indicates which axis to insert at the corresponding insertion position
If the fourth argument is not given, then the default is to flatten arr into a one-dimensional array, and then insert the corresponding value in the corresponding position.
The following are examples:
arr2=np.array ([1,2,3,4,5])arr3=arr2.reshape(-1,1)arr=np.insert (arr3,1,[[0],[0]],axis=0)#arr=np.insert (arr3,slice(1,2),[[0],[0]],axis=0) where slice means slice, as in numpy print (arr3)print(arr3)arr.flatten()[[1][2][3][4][5][1][0][2][3][4][5] array ([1, 0, 0, 2, 3, 4, 5])arr=np.insert (arr,4,[[0],[0]],axis=0)#where 4 indicates the position obj, insert valuearr=np.insert after several positions (arr,7,[[0],[0]],axis=0)arr=np.insert(arr,10,[[0],axis=0)print(arr.flatten())[1 0 2 0 0 3 0 0 4 0 5] Thank you for reading! About "numpy.insert() function what to use" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!