过滤一个numpy数组的行? [英] Filter rows of a numpy array?

查看:239
本文介绍了过滤一个numpy数组的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个函数应用到numpy数组的每一行。如果这个函数的计算结果是真的,我会保持这一行,否则我会放弃它。例如,我的函数可能是:

pre $ code def f(row):
如果sum(row)> 10 :return True
else:return False

我想知道是否有类似的东西:

  np.apply_over_axes()

它将函数应用到numpy数组的每一行并返回结果。我希望得到如下的东西:

  np.filter_over_axes()

这将对numpy数组的每一行应用一个函数,并且只返回函数返回true的行。有没有这样的事情?或者我应该只使用for循环?理想情况下,你将能够实现你的函数的矢量化版本,并使用那要做布尔索引。对于绝大多数问题,这是正确的解决方案。 Numpy提供了很多功能,可以在不同的轴上执行以及所有的基本操作和比较,所以最有用的条件应该是可以进行矢量化的。

 <$ (20,3)
x_new = x [np.sum(x,axis = 1)> .5]

如果您确定无法完成上述操作,建议使用列表理解(或 np.apply_along_axis
$ b

  def myfunc(row):
)创建一个bools数组来索引。返回总和(行)> 。

bool_arr = np.array([myfunc(row)for x)]
x_new = x [bool_arr]

这将以相对干净的方式完成工作,但会比矢量化版本慢得多。一个例子:

  x = np.random.randn(5000,200)

%timeit x [np.sum(x,axis = 1)> .5]
#100循环,最好的3:每循环5.71毫秒

%timeit x [np.array([myfunc(row)for x]]
#1循环,最好3:每个循环217毫秒


I am looking to apply a function to each row of a numpy array. If this function evaluates to true I will keep the row, otherwise I will discard it. For example, my function might be:

def f(row):
    if sum(row)>10: return True
    else: return False

I was wondering if there was something similar to:

np.apply_over_axes()

which applies a function to each row of a numpy array and returns the result. I was hoping for something like:

np.filter_over_axes()

which would apply a function to each row of an numpy array and only return rows for which the function returned true. Is there anything like this? Or should I just use a for loop?

Ideally, you would be able to implement a vectorized version of your function and use that to do boolean indexing. For the vast majority of problems this is the right solution. Numpy provides quite a few functions that can act over various axes as well as all the basic operations and comparisons, so most useful conditions should be vectorizable.

import numpy as np

x = np.random.randn(20, 3)
x_new = x[np.sum(x, axis=1) > .5]

If you are absolutely sure that you can't do the above, I would suggest using a list comprehension (or np.apply_along_axis) to create an array of bools to index with.

def myfunc(row):
    return sum(row) > .5

bool_arr = np.array([myfunc(row) for row in x])
x_new = x[bool_arr]

This will get the job done in a relatively clean way, but will be significantly slower than a vectorized version. An example:

x = np.random.randn(5000, 200)

%timeit x[np.sum(x, axis=1) > .5]
# 100 loops, best of 3: 5.71 ms per loop

%timeit x[np.array([myfunc(row) for row in x])]
# 1 loops, best of 3: 217 ms per loop

这篇关于过滤一个numpy数组的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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