NumPy:如何过滤矩阵线 [英] NumPy: How to filter matrix lines

查看:113
本文介绍了NumPy:如何过滤矩阵线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是numpy的新手,在尝试过滤样本子集时遇到麻烦.

I'm new to numpy and having trouble trying to filter a subset of a sample.

我有一个形状为(1000, 12)的矩阵.也就是说,一千个样本,每个样本中有12个数据列.我愿意创建两个矩阵,一个矩阵包含样本中的所有异常值,另一个矩阵包含所有非异常值的元素.所得矩阵应具有以下形状:

I've got a matrix with the shape (1000, 12). That is, a thousand samples, with 12 data columns in each. I'm willing to create two matrices, one with all the outliers in the sample, and the other with all the elements which are not outliers; The resulting matrices should have this shape:

norm.shape     = (883, 12)
outliers.shape = (117, 12)

要识别异常值,我正在使用以下条件:

To identify an outlier, I'm using this condition:

cond_out  = (dados[0:,RD_EVAL] > _max_rd) | (dados[0:,DUT_EVAL] > _max_dut)

也就是说,对于矩阵中的每一行,我正在寻找两列的值.如果其中之一高于某个阈值,则该线被认为是离群值.关键是,此条件的形状为(1000,),因此当我压缩原始矩阵时,得到的结果为(117,).我该如何过滤矩阵,使结果为(117,12),也就是一个矩阵,其中的所有行都是异常值,而每个行中都有所有数据列?

That is, for each line in the matrix, I'm looking for the values of two columns. If one of them is above a certain threshold, then the line is considered an outlier. The point is, this condition has a shape (1000,), so when I compress the original matrix, I get a (117,) result. How could I filter the matrix so the result would be (117,12), that is, a matrix with all the lines that are outliers, but with all the data columns in each of them?

推荐答案

import numpy as np

d=np.random.randn(4,4)

array([[ 1.16968447, -0.07650322, -0.30519481, -2.09278839],
       [ 0.53350868, -0.8004209 ,  0.38477468,  1.31876924],
       [ 0.06461366,  0.82204993,  0.42034665,  0.30473843],
       [ 1.13469745, -1.47969242,  2.36338208, -0.33700972]])

让我们过滤第二列中所有小于零的行:

Lets filter all the lines that are less than zero in the second column:

d[:,1]<0
array([ True,  True, False,  True], dtype=bool)

您会看到,您将获得一个逻辑数组,可用于选择所需的行:

You see, you get a logical array that you can use to select the desired rows:

d[d[:,1]<0,:]

array([[ 1.16968447, -0.07650322, -0.30519481, -2.09278839],
       [ 0.53350868, -0.8004209 ,  0.38477468,  1.31876924],
       [ 1.13469745, -1.47969242,  2.36338208, -0.33700972]])

这篇关于NumPy:如何过滤矩阵线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆