📜  如何删除多行 NumPy 数组?

📅  最后修改于: 2022-05-13 01:54:22.293000             🧑  作者: Mango

如何删除多行 NumPy 数组?

NumPy 是用于处理数组的Python库。在Python,有一些列表可以用作数组,但速度很慢。因此,NumPy 为我们提供了比传统Python列表快得多的数组对象。它们更快的原因是它们将数组存储在内存中的一个连续位置,不像列表那样使过程访问和操作更加高效。

有多种方法可以删除 NumPy 数组中的多行。他们在下面给出:-

  • numpy.delete() – numpy.delete() 是Python中的一个函数,它返回一个新数组,其中删除了子数组以及提到的轴。通过将轴的值保持为零,有两种可能的方法可以使用 numphy.delete() 删除多行。

使用整数数组,语法: np.delete(x, [ 0, 2, 3], axis=0)

Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5) 
  
print(geek.delete(x, [0, 1, 2], axis=0))


Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
print(geek.delete(x, slice(0, 3), axis=0))


Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Removing all rows before the 4th row
result = x[4:]
print(result)


Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# Fancy indexing
print(x[[0, 1, 2]])


Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# applying numpy.take() function
print(geek.take(x, [0, 2, 6], axis=0))


Python3
import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Performing Boolean Indexing
mask_array = x[:, 0] < 10
print(x[mask_array])


输出:

[[15 16 17 18 19]                                                                                                
[20 21 22 23 24]                                                                                                
[25 26 27 28 29]          
[30 31 32 33 34]]

使用切片对象—— slice()函数允许我们指定如何对序列进行切片。



slice函数的语法: slice(start, stop, step index)

蟒蛇3

import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
print(geek.delete(x, slice(0, 3), axis=0))

输出:

[[15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]]                                                                                              
  • 基本索引——这是删除多行 NumPy 数组的最简单方法之一。
    基本索引的语法:数组对象(开始:停止:步骤)

蟒蛇3

import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Removing all rows before the 4th row
result = x[4:]
print(result)

输出:

[[20 21 22 23 24]                                                                                                
[25 26 27 28 29]                                                                                                
[30 31 32 33 34]]   
  • 花式索引- 这是我们使用数组索引数组的方法,允许我们通过引用它们的索引号一次访问多个数组元素。
    语法:数组对象[行号]

蟒蛇3

import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# Fancy indexing
print(x[[0, 1, 2]])  

输出:

[[ 0  1  2  3  4]                                                                                                
[ 5  6  7  8  9]                                                                                               
[10 11 12 13 14]]
  • numpy.take() – numpy.take() 是Python中的一个函数,用于从沿提到的轴和索引的数组中返回元素。现在,如果有人提到轴的值为空/零,那么它能够为我们提供所需的索引并以类似于花式索引的方式工作。

蟒蛇3

import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)  
  
# applying numpy.take() function
print(geek.take(x, [0, 2, 6], axis=0)) 

输出:

[[ 0  1  2  3  4]                                                                                                
[10 11 12 13 14]                                                                                                
[30 31 32 33 34]] 
  • 布尔索引——这是一个非常方便的方法,特别是当我们为删除设置一些条件时。
    例如,我们要删除以大于 10 的值开头的行。然后我们可以使用布尔索引来完成。

蟒蛇3

import numpy as geek
  
# Defining the array
x = geek.arange(35).reshape(7, 5)
  
# Performing Boolean Indexing
mask_array = x[:, 0] < 10
print(x[mask_array])

输出:

[[0 1 2 3 4]
 [5 6 7 8 9]]