温馨提示×

NumPy数组压缩的方法是什么

小亿
138
2024-05-13 12:47:13
栏目: 编程语言

NumPy数组压缩的方法是使用numpy.compress()函数。该函数接受两个参数,第一个参数是一个布尔数组,用于指示哪些元素需要被压缩,第二个参数是要进行压缩的数组。压缩后的数组将只包含布尔数组中为True的元素。例如:

import numpy as np arr = np.array([1, 2, 3, 4, 5]) condition = np.array([True, False, True, False, True]) compressed_arr = np.compress(condition, arr) print(compressed_arr) 

输出结果将是[1 3 5],只包含布尔数组中为True的元素。

0