NumPy 数组操作 numpy.delete

NumPy 数组操作 numpy.delete

此函数从输入数组中删除指定的子数组,并返回一个新数组。与insert()函数一样,如果未使用axis参数,则输入数组会被展平。该函数接受以下参数:

Numpy.delete(arr, obj, axis)

其中,

序号 参数和描述
1 arr 输入数组
2 obj 可以是一个切片、一个整数或一个整数数组,表示要从输入数组中删除的子数组
3 axis 要删除给定子数组的轴。如果未给出,则将 arr 拉平

示例

import numpy as np 
a = np.arange(12).reshape(3,4) 

print 'First array:' 
print a 
print '\n'  

print 'Array flattened before delete operation as axis not used:' 
print np.delete(a,5) 
print '\n'  

print 'Column 2 deleted:'  
print np.delete(a,1,axis = 1) 
print '\n'  

print 'A slice containing alternate values from array deleted:' 
a = np.array([1,2,3,4,5,6,7,8,9,10]) 
print np.delete(a, np.s_[::2])

它的输出将如下所示 –

First array:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Array flattened before delete operation as axis not used:
[ 0 1 2 3 4 6 7 8 9 10 11]

Column 2 deleted:
[[ 0 2 3]
 [ 4 6 7]
 [ 8 10 11]]

A slice containing alternate values from array deleted:
[ 2 4 6 8 10]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程