简体   繁体   English

对 numpy 数组的每一列应用线性变换的最有效方法

[英]Most efficient way to apply linear transformations to each column of a numpy array

I have a random numpy array我有一个随机 numpy 阵列

import numpy as np

a = np.random.randn(10000*5).reshape((10000,5))

and I want to transform as efficiently as possible each column by the function我想通过 function 尽可能有效地转换每一列

def lintransform(interval,x): 
    return (interval[1]-interval[0])*x + interval[0]

Where interval is one of five sorted array of length 2 to transform the columns of a .其中interval是长度为 2 的五个排序数组之一,用于转换a的列。

(for example listofintervals = [[0,3],[1,9],[0.5,3],[4,10],[1,2.7]] ) (例如listofintervals = [[0,3],[1,9],[0.5,3],[4,10],[1,2.7]]

What is the most efficient way to apply each this function for each column respectively and generate a new array, changing the interval used according to its position in the listofintervals ?分别为每列应用每个 function 并生成一个新数组,根据 listofintervals 中的listofintervals更改使用的间隔的最有效方法是什么?

Using numpy vectorization you can do:使用 numpy 矢量化,您可以执行以下操作:

import numpy as np
a = np.random.randn(10000, 5)

intervals = np.array([[0,3],
                      [1,9],
                      [0.5,3],
                      [4,10],
                      [1,2.7]])

r = (intervals[:,1] - intervals[:,0]) * a + intervals[:,0]

Which takes:这需要:

%timeit (intervals[:,1] - intervals[:,0]) * a + intervals[:,0]
131 µs ± 1.35 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Looping through the columns, and applying your function to each row should work:遍历列,并将您的 function 应用于每一行应该可以工作:

for col in range(a.shape[1]):
    a[col] = lintransform(listofintervals[col], a[col])

Output: Output:

a
array([[-5.80231737, -3.1056331 , -1.3878622 ,  3.2891958 , -1.35495844],
       [-7.93085499, 18.46079707, 13.81923528, -3.18486045, -0.31541526],
       [ 1.53477244,  2.61705202, -2.14505552,  0.14751953,  4.70029497],
       ...,
       [ 1.13798389, -0.6765344 , -0.1364982 , -1.0443724 ,  0.06717867],
       [-1.78251012,  0.11171333,  1.28247762,  0.52285423,  0.16057854],
       [-0.59513499, -0.76866946, -0.37233491, -1.08463643, -0.45660967]])

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

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