删除np.array中的选定行

删除np.array中的选定行

问题描述:

我有一个选择来自实验的值,我想删除其他行的某些行。含义:我测量一个场,一个极化和一个极化的误差。现在,执行此测量的机器有时不会在某些行中写入数值。所以,我可能会得到: 场=数据[0]删除np.array中的选定行

field = [1,2,3,3,2,1,nan,4,1,2] 
polarization = [nan, 10,230,13,123,50,102,90,45] 
error = [0.1, 0.1, 0.2, 0.1, 0.1, 0.3, 0.1, 0.1, 0.4, 0.2] 

现在我要删除领域,极化和错误的第一要素,因为偏振态[0] =价值楠。和所有数组的[6]值,因为field [6] = nan。

这是我如何得到我的数据:

class DataFile(object): 
    def __init__(self, filename): 
     self._filename = filename 


    def read_dat_file(self): 
     data = np.genfromtxt(self._filename, delimiter=',', \ 
     usecols=(3,4,5,), skip_header=23, skip_footer=3, unpack=True, converters={\ 
     3: lambda x: self._conv(x), \ 
     4: lambda x: self._conv(x), \ 
     5: lambda x: self._2_conv(x)}) 
     return data 

a = DataFile("DATFILE.DAT") 
print a 

的_conv功能只是做了一些单位换算或写“南”如果值是“”。我试图做类似的事情:

data = data[~np.isnan(data).any(axis=1)] 

但是后来我找回一个数组,事情变得混乱。我的下一个方法是对元素进行计数,从所有数组中删除相同的元素......等等。工程,但它很丑。那么最好的解决方案是什么?

可以遍历行和创建行的面膜,然后用布尔索引来获取通过行的看法:

import numpy as np 

field = [1,2,3,3,2,1,-1,4,1,2] 
polarization = [-1, 10,230,13,123,50,102,90,45,1337] 
error = [0.1, 0.1, 0.2, 0.1, 0.1, 0.3, 0.1, 0.1, 0.4, 0.2] 

#transposition is needed to get expected row-col format 
array = np.array([field, polarization, error]).T 
print(array) 

#create your filter function 
filter = lambda row : row[0] > 0 and row[1] > 0 and row[2] > 0 

#create boolean mask by applying filter 
mask = np.apply_along_axis(filter, 1, array) 
print(mask) 

new_array = array[mask] 
print(new_array) 

尝试使用mask_where命令。

A(非常基本的)例子:

y = np.array([2,1,5,2])       # y axis 
x = np.array([1,2,3,4])       # x axis 
m = np.ma.masked_where(y>5, y)     # filter out values larger than 5 
new_x = np.ma.masked_where(np.ma.getmask(m), x) # applies the mask of m on x 

的好处是,你现在可以将此面膜适用于更多的阵列,而无需通过为他们每个人的屏蔽处理下去。它不会像数数元素一样丑陋。

就你而言,你可能需要仔细检查每个阵列,检查nan,然后在所有其他阵列上应用该掩码。希望有所帮助。

我结合另一个线程和red_tigers回答,我想与分享你: 只是里面的数据运行在你的阵列,这样的功能:

data = np.array([field, polarization, error]).T 

def delete_NaN_rows(self, data): 
    filter = lambda row: ~np.isnan(row[0]) and ~np.isnan(row[1]) and ~np.isnan(row[2]) 
    mask = np.apply_along_axis(filter, 1, data) 
    clean_data = data[mask] 
    return clean_data.T 

我用np.isnan(#element)的倒数(〜)就确定我行与NaN的条目并删除它们。