简体   繁体   English

从 numpy 数组中删除小于 1 的元素

[英]Remove elements from numpy array smaller than 1

I am trying to plot large data (50 million values) but I am getting a MemoryError .我正在尝试绘制大数据(5000 万个值),但出现MemoryError Now I am trying to clear my dataset from redundant values.现在我试图从冗余值中清除我的数据集。 In my case, these are all values below 1 and above -1 .就我而言,这些都是低于1和高于-1值。 One thing to keep in mind is that the plot should look the same as the original, but without the noise.要记住的一件事是,情节应该看起来与原作一样,但没有噪音。 Is there a better way to do this than using loops or list comprehensions ?有没有比使用loopslist comprehensions loops更好的方法来做到这一点?

Original Plot:原剧情:

在此处输入图片说明

Edit:编辑:

Thank you for the replies.感谢您的答复。 If I use the proposed approach:如果我使用建议的方法:

daty = daty[(-1 > daty) | (daty > 1)]

It results in this:结果如下:

在此处输入图片说明

如果您的数组名为data

clipped_data = data[(-1 > data) | (data > 1)]

Numpy can do addressing based on an array of booleans, so you can do stuff like this, if you want to set the unwanted values to 0: Numpy 可以根据布尔数组进行寻址,因此如果要将不需要的值设置为 0,您可以执行以下操作:

badIndices = (myArray > 1) | (myArray < -1)
myArray[badIndices]=0

Numpy allows you to apply a mask to any single dimension of an array, resulting in a subset of the unmasked rows (or data points in your case). Numpy 允许您将掩码应用于数组的任何单个维度,从而生成未掩码行(或您的案例中的数据点)的子集。

data = np.array([1.5, 0.2, -5, -0.5])
mask = np.abs(data) > 1
data = data[mask]
print(data) # output: [1.5, -5]

Note that -5 now is at position 1 after the filter.请注意,-5 现在位于过滤器之后的位置 1。

A sample I used on imbedded array我在嵌入式阵列上使用的示例

import numpy as np

data = np.array([[0, 3], [2, 15], [15, 7], [3, 6]])
e1 = (data[:, 0] < 15)
e2 = (data[:, 1] < 15)

print(data[e1 & e2])
# array([[0, 3], [3, 6]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM