通过Python中的索引删除数组列(Remove column of array via indexing in Python)

编程入门 行业动态 更新时间:2024-10-28 12:20:46
通过Python中的索引删除数组列(Remove column of array via indexing in Python)

我试图通过索引删除数组中的观察。 我所拥有的是:

import numpy as np test = np.ones([1, 1001])

我想要做的是返回一个与test相同的数组,但是删除了第5个观察结果(即test [0:4 AND 6:])。 有一个简单的方法吗?

I'm trying to remove an observation in an array via indexing. What I have is:

import numpy as np test = np.ones([1, 1001])

What I want to do is return an array that is the same as test, but having removed the 5th observation (ie, test[0:4 AND 6:]). Is there a simple way to do this?

最满意答案

你可以使用切片和hstack :

In [18]: test_ex5 = np.hstack((test[:,:5],test[:,6:])) In [19]: test.shape Out[19]: (1, 1001) In [20]: test_ex5.shape Out[20]: (1, 1000)

请注意,您的索引关闭一: test[0:4 AND 6:]会删除两个元素而不是一个元素。

You could use slicing and hstack:

In [18]: test_ex5 = np.hstack((test[:,:5],test[:,6:])) In [19]: test.shape Out[19]: (1, 1001) In [20]: test_ex5.shape Out[20]: (1, 1000)

Note that your indexing is off by one: test[0:4 AND 6:] would delete two elements instead of one.

更多推荐

本文发布于:2023-07-24 13:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1246279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   Python   Remove   indexing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!