vectorization

How to invert a permutation array in numpy

此生再无相见时 提交于 2019-12-17 09:45:16
问题 Given a self-indexing (not sure if this is the correct term) numpy array, for example: a = np.array([3, 2, 0, 1]) This represents this permutation ( => is an arrow): 0 => 3 1 => 2 2 => 0 3 => 1 I'm trying to make an array representing the inverse transformation without doing it "manually" in python, that is, I want a pure numpy solution. The result I want in the above case is: array([2, 3, 1, 0]) Which is equivalent to 0 <= 3 0 => 2 1 <= 2 or 1 => 3 2 <= 0 2 => 1 3 <= 1 3 => 0 It seems so

Fastest way to multiply an array of int64_t?

孤者浪人 提交于 2019-12-17 09:37:10
问题 I want to vectorize the multiplication of two memory aligned arrays. I didn't find any way to multiply 64*64 bit in AVX/AVX2, so I just did loop-unroll and AVX2 loads/stores. Is there a faster way to do this? Note: I don't want to save the high-half result of each multiplication. void multiply_vex(long *Gi_vec, long q, long *Gj_vec){ int i; __m256i data_j, data_i; __uint64_t *ptr_J = (__uint64_t*)&data_j; __uint64_t *ptr_I = (__uint64_t*)&data_i; for (i=0; i<BASE_VEX_STOP; i+=4) { data_i =

numpy elementwise outer product

一世执手 提交于 2019-12-17 07:53:38
问题 I want to do the element-wise outer product of two 2d arrays in numpy. A.shape = (100, 3) # A numpy ndarray B.shape = (100, 5) # A numpy ndarray C = element_wise_outer_product(A, B) # A function that does the trick C.shape = (100, 3, 5) # This should be the result C[i] = np.outer(A[i], B[i]) # This should be the result A naive implementation can the following. tmp = [] for i in range(len(A): outer_product = np.outer(A[i], B[i]) tmp.append(outer_product) C = np.array(tmp) A better solution

Using Numpy Vectorize on Functions that Return Vectors

回眸只為那壹抹淺笑 提交于 2019-12-17 07:27:42
问题 numpy.vectorize takes a function f:a->b and turns it into g:a[]->b[]. This works fine when a and b are scalars, but I can't think of a reason why it wouldn't work with b as an ndarray or list, i.e. f:a->b[] and g:a[]->b[][] For example: import numpy as np def f(x): return x * np.array([1,1,1,1,1], dtype=np.float32) g = np.vectorize(f, otypes=[np.ndarray]) a = np.arange(4) print(g(a)) This yields: array([[ 0. 0. 0. 0. 0.], [ 1. 1. 1. 1. 1.], [ 2. 2. 2. 2. 2.], [ 3. 3. 3. 3. 3.]], dtype=object)

Vectorized way of calculating row-wise dot product two matrices with Scipy

喜夏-厌秋 提交于 2019-12-17 07:26:14
问题 I want to calculate the row-wise dot product of two matrices of the same dimension as fast as possible. This is the way I am doing it: import numpy as np a = np.array([[1,2,3], [3,4,5]]) b = np.array([[1,2,3], [1,2,3]]) result = np.array([]) for row1, row2 in a, b: result = np.append(result, np.dot(row1, row2)) print result and of course the output is: [ 26. 14.] 回答1: Check out numpy.einsum for another method: In [52]: a Out[52]: array([[1, 2, 3], [3, 4, 5]]) In [53]: b Out[53]: array([[1, 2,

NumPy version of “Exponential weighted moving average”, equivalent to pandas.ewm().mean()

时间秒杀一切 提交于 2019-12-17 06:29:39
问题 How do I get the exponential weighted moving average in NumPy just like the following in pandas? import pandas as pd import pandas_datareader as pdr from datetime import datetime # Declare variables ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1)).reset_index(drop=True)['Adj Close'] windowSize = 20 # Get PANDAS exponential weighted moving average ewm_pd = pd.DataFrame(ibm).ewm(span=windowSize, min_periods=windowSize).mean().as_matrix() print(ewm_pd

Efficient evaluation of a function at every cell of a NumPy array

一世执手 提交于 2019-12-17 05:39:25
问题 Given a NumPy array A , what is the fastest/most efficient way to apply the same function, f , to every cell? Suppose that we will assign to A(i,j) the f(A(i,j)) . The function, f , doesn't have a binary output, thus the mask(ing) operations won't help. Is the "obvious" double loop iteration (through every cell) the optimal solution? 回答1: You could just vectorize the function and then apply it directly to a Numpy array each time you need it: import numpy as np def f(x): return x * x + 3 * x -

Bin elements per row - Vectorized 2D Bincount for NumPy

情到浓时终转凉″ 提交于 2019-12-17 04:35:25
问题 I have a NumPy array with integer values. Values of matrix range from 0 to max element in matrix(in other words, all numbers from 0 to max data element presented in it). I need to build effective( effective means fast fully-vectorized solution ) for searching number of elements in each row and encode them according to matrix values. I could not find a similar question, or a question that somehow helped to solve this. So if i have this data in input: # shape is (N0=4, m0=4) 1 1 0 4 2 4 2 1 1 2

Is it possible to vectorize recursive calculation of a NumPy array where each element depends on the previous one?

末鹿安然 提交于 2019-12-17 04:30:45
问题 T(i) = Tm(i) + (T(i-1)-Tm(i))**(-tau(i)) Tm and tau are NumPy vectors of the same length that have been previously calculated, and the desire is to create a new vector T . The i is included only to indicate the element index for what is desired. Is a for loop necessary for this case? 回答1: You might think this would work: import numpy as np n = len(Tm) t = np.empty(n) t[0] = 0 # or whatever the initial condition is t[1:] = Tm[1:] + (t[0:n-1] - Tm[1:])**(-tau[1:]) but it doesn't: you can't

Create a zero-filled 2D array with ones at positions indexed by a vector

戏子无情 提交于 2019-12-17 04:08:40
问题 I'm trying to vectorize the following MATLAB operation: Given a column vector with indexes, I want a matrix with the same number of rows of the column and a fixed number of columns. The matrix is initialized with zeroes and contains ones in the locations specified by the indexes. Here is an example of the script I've already written: y = [1; 3; 2; 1; 3]; m = size(y, 1); % For loop yvec = zeros(m, 3); for i=1:m yvec(i, y(i)) = 1; end The desired result is: yvec = 1 0 0 0 0 1 0 1 0 1 0 0 0 0 1