numpy矩阵行/列上的函数应用 [英] Function application over numpy's matrix row/column

查看:287
本文介绍了numpy矩阵行/列上的函数应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Numpy将数据存储到矩阵中.从R背景开始,有一种非常简单的方法可以将函数应用于矩阵的行/列或两者.

I am using Numpy to store data into matrices. Coming from R background, there has been an extremely simple way to apply a function over row/columns or both of a matrix.

python/numpy组合是否有类似的东西?编写自己的小实现不是问题,但是在我看来,我想出的大多数版本都将比现有的实现效率低得多/占用更多内存.

Is there something similar for python/numpy combination? It's not a problem to write my own little implementation but it seems to me that most of the versions I come up with will be significantly less efficient/more memory intensive than any of the existing implementation.

我想避免从numpy矩阵复制到局部变量等,这有可能吗?

I would like to avoid copying from the numpy matrix to a local variable etc., is that possible?

我要实现的功能主要是简单的比较(例如,某列中有多少个元素小于数字x,或者其中有多少个绝对值大于y).

The functions I am trying to implement are mainly simple comparisons (e.g. how many elements of a certain column are smaller than number x or how many of them have absolute value larger than y).

推荐答案

几乎所有的numpy函数都在整个数组上运行,并且/或者可以被告知在特定的轴(行或列)上运行.

Almost all numpy functions operate on whole arrays, and/or can be told to operate on a particular axis (row or column).

只要您可以根据作用在numpy数组或数组切片上的numpy函数来定义函数,您的函数将自动在整个数组,行或列上运行.

As long as you can define your function in terms of numpy functions acting on numpy arrays or array slices, your function will automatically operate on whole arrays, rows or columns.

询问如何实现特定功能以获得更具体的建议可能会更有用.

It may be more helpful to ask about how to implement a particular function to get more concrete advice.

Numpy提供 np.vectorize np.frompyfunc 即可将对数字进行操作的Python函数转换为对numpy数组进行操作的函数.

Numpy provides np.vectorize and np.frompyfunc to turn Python functions which operate on numbers into functions that operate on numpy arrays.

例如,

def myfunc(a,b):
    if (a>b): return a
    else: return b
vecfunc = np.vectorize(myfunc)
result=vecfunc([[1,2,3],[5,6,9]],[7,4,5])
print(result)
# [[7 4 5]
#  [7 6 9]]

(当第二个数组较大时,第一个数组的元素将替换为第二个数组的相应元素.)

(The elements of the first array get replaced by the corresponding element of the second array when the second is bigger.)

但是不要太兴奋; np.vectorizenp.frompyfunc只是语法糖.它们实际上并没有使您的代码更快.如果您的基本Python函数一次操作一个值,则np.vectorize一次将其馈入一个项,而整个 操作将会非常慢(与使用numpy函数调用一些底层C或Fortran实现相比).

But don't get too excited; np.vectorize and np.frompyfunc are just syntactic sugar. They don't actually make your code any faster. If your underlying Python function is operating on one value at a time, then np.vectorize will feed it one item at a time, and the whole operation is going to be pretty slow (compared to using a numpy function which calls some underlying C or Fortran implementation).

要计算列x的个元素小于数字y的个数,可以使用以下表达式:

To count how many elements of column x are smaller than a number y, you could use an expression such as:

(array['x']<y).sum()

例如:

import numpy as np
array=np.arange(6).view([('x',np.int),('y',np.int)])
print(array)
# [(0, 1) (2, 3) (4, 5)]

print(array['x'])
# [0 2 4]

print(array['x']<3)
# [ True  True False]

print((array['x']<3).sum())
# 2

这篇关于numpy矩阵行/列上的函数应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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