Numpy delete 删除数组的行和列
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts * Notebook - Multi-Engine 2.0 (python3) * JupyterLab - Notebook - Conda-python3 * numpy 1.19.1Numpy delete 删除数组的行和列import numpy as nparr = np.arra...
所属的课程名称及链接
环境信息
- * ModelArts
- * Notebook - Multi-Engine 2.0 (python3)
- * JupyterLab - Notebook - Conda-python3
- * numpy 1.19.1
- * JupyterLab - Notebook - Conda-python3
- * Notebook - Multi-Engine 2.0 (python3)
Numpy delete 删除数组的行和列
import numpy as np
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print("arr",arr,"\n")
print("arr.flatten()",arr.flatten(),"\n")
print("arr.flatten()[2]",arr.flatten()[2],"\n")
print("np.delete(arr,obj=2)",np.delete(arr,obj=2),"\n") # axis为None。先 arr.flatten(),再移除arr[obj]元素)
print("np.delete(arr,obj=0,axis=0)",np.delete(arr,obj=0,axis=0),"\n") # axis=0 行
print("np.delete(arr,obj=[0,1],axis=0)",np.delete(arr,obj=[0,1],axis=0),"\n") # axis=0 行,一次删除多行
print("np.delete(arr,obj=0,axis=1)",np.delete(arr,obj=2,axis=1),"\n") # axis=1 列
arr [[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
arr.flatten() [ 1 2 3 4 5 6 7 8 9 10 11 12]
arr.flatten()[2] 3
np.delete(arr,obj=2) [ 1 2 4 5 6 7 8 9 10 11 12]
np.delete(arr,obj=0,axis=0) [[ 5 6 7 8]
[ 9 10 11 12]]
np.delete(arr,obj=[0,1],axis=0) [[ 9 10 11 12]]
np.delete(arr,obj=0,axis=1) [[ 1 2 4]
[ 5 6 8]
[ 9 10 12]]
help
help(np.delete)
Help on function delete in module numpy:
delete(arr, obj, axis=None)
Return a new array with sub-arrays along an axis deleted. For a one
dimensional array, this returns those entries not returned by
`arr[obj]`.
Parameters
----------
arr : array_like
Input array.
obj : slice, int or array of ints
Indicate indices of sub-arrays to remove along the specified axis.
.. versionchanged:: 1.19.0
Boolean indices are now treated as a mask of elements to remove,
rather than being cast to the integers 0 and 1.
axis : int, optional
The axis along which to delete the subarray defined by `obj`.
If `axis` is None, `obj` is applied to the flattened array.
Returns
-------
out : ndarray
A copy of `arr` with the elements specified by `obj` removed. Note
that `delete` does not occur in-place. If `axis` is None, `out` is
a flattened array.
See Also
--------
insert : Insert elements into an array.
append : Append elements at the end of an array.
Notes
-----
Often it is preferable to use a boolean mask. For example:
>>> arr = np.arange(12) + 1
>>> mask = np.ones(len(arr), dtype=bool)
>>> mask[[0,2,4]] = False
>>> result = arr[mask,...]
Is equivalent to `np.delete(arr, [0,2,4], axis=0)`, but allows further
use of `mask`.
Examples
--------
>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> np.delete(arr, 1, 0)
array([[ 1, 2, 3, 4],
[ 9, 10, 11, 12]])
>>> np.delete(arr, np.s_[::2], 1)
array([[ 2, 4],
[ 6, 8],
[10, 12]])
>>> np.delete(arr, [1,3,5], None)
array([ 1, 3, 5, 7, 8, 9, 10, 11, 12])
备注
1. 感谢老师的教学与课件
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)