vectorization

Vectorised method to append dataframe rows to columns and vice-versa

霸气de小男生 提交于 2020-02-24 04:48:46
问题 My dataframe is as follows: df = pd.DataFrame({'a': {'d': 1, 'e': 0, 'f': 1, 'g': 1}, 'b': {'d': 0, 'e': 0, 'f': 0, 'g': 1}, 'c': {'d': 0, 'e': 1, 'f': 1, 'g': 0}}) which gives: >>> df a b c d 1 0 0 e 0 0 1 f 1 0 1 g 1 1 0 For every row in the dataframe, I would like to add a new column of 0 s , and for every column in the dataframe, I would like to add a new row of 0 s. I've attempted to solve this problem so far in the following way: edges = df.columns for i in df.index: df[i] = [0 for _ in

Increment and insert values based on a specific value of another array

怎甘沉沦 提交于 2020-02-22 12:12:11
问题 I have the following code with the given arrays a and b . import numpy as np # Parts of interest are highlighted with ^ ... a = np.array([0,2,9,12,18,19]) # ^^ ^^ b = np.array([1,1,1,2,1,3] # ^ ^ # Should result in an array like assert result == np.array([0,2,9,12,13,18,19,20,21]) # ^^ ^^ ^^ ^^ ^^ The values in b define how many increments of the value in a (at the same index) should be inserted in the result. Ones in b don't affect the result. I think that I could do some splitting/joining

Find the Hamming distance between string sequences

我只是一个虾纸丫 提交于 2020-02-05 05:52:46
问题 I have a dataset of 3156 DNA sequences, each of which has 98290 characters (SNPs), comprising the (usual) 5 symbols : A, C, G, T, N (gap). What is the optimal way to find the pairwise Hamming distance between these sequences? Note that for each sequence, I actually want to find the reciprocal of the number of sequences (including itself), where the per-site hamming distance is less than some threshold (0.1 in this example). So far, I have attempted the following: library(doParallel)

Efficient way to vectorize R for loops

大兔子大兔子 提交于 2020-02-05 04:00:15
问题 How can the following R code be vectorized to reduce computing time? q = matrix(0,n,p) for(u in 1 : n){ q1 <- matrix(0,p,1) for(iprime in 1 : n){ for(i in 1 : n){ if(cause[iprime]==1 & cause[i]>1 & (time[i]<time[u]) & (time[u] <= time[iprime])){ q1 = q1 + (covs[i,] - S1byS0hat[iprime,])*G[iprime]/G[i]*expz[i]/S0hat[iprime] } } } q[u,] = q1/(m*m) } Following values could be used as an example: n = 2000 m = 500 p=3 G = runif(n) time = runif(n,0.01,5) cause = c(rep(0,600),rep(1,1000),rep(2,400))

Is there a way to speed up Numpy array calculations when they only contain values in upper/lower triangle?

浪尽此生 提交于 2020-02-04 11:27:04
问题 I'm doing some matrix calculations (2d) that only involve values in the upper triangle of the matrices. So far I've found that using Numpy's triu method ("return a copy of a matrix with the elements below the k-th diagonal zeroed") works and is quite fast. But presumably, the calculations are still being carried out for the whole matrix, including unnecessary calculations on the zeros. Or are they?... Here is an example of what I tried first: # Initialize vars N = 160 u = np.empty(N) u[0] =

Possible Vectorization using If Statements in MATLAB

隐身守侯 提交于 2020-02-04 09:34:57
问题 Suppose I have the following column vectors as res1 = -0.81 res2 = 0.61 0.1 -0.4 -0.91 0.62 0.2 -0.56 0.63 -0.72 and I have two fixed constant D = 0.5 . Now suppose an element of res1 is called X and an element of res2 is called Y . I have the following conditions if (X > D && Y < -D) output = 1 elseif (X < -D && Y > D) output = -1 else output = 0 end My question is this: Is it possible to "vectorize" these conditions to iterate over the entire vectors res1 and res2 , such that my output

Possible Vectorization using If Statements in MATLAB

杀马特。学长 韩版系。学妹 提交于 2020-02-04 09:32:29
问题 Suppose I have the following column vectors as res1 = -0.81 res2 = 0.61 0.1 -0.4 -0.91 0.62 0.2 -0.56 0.63 -0.72 and I have two fixed constant D = 0.5 . Now suppose an element of res1 is called X and an element of res2 is called Y . I have the following conditions if (X > D && Y < -D) output = 1 elseif (X < -D && Y > D) output = -1 else output = 0 end My question is this: Is it possible to "vectorize" these conditions to iterate over the entire vectors res1 and res2 , such that my output

Vectorisation of code for insertion of 2x2 matrix along the diagonal of a large matrix

我与影子孤独终老i 提交于 2020-02-02 12:22:17
问题 I am trying to do an elementwise insertion of a small matrix (2x2) along the diagonal of a large matrix (10x10 say). Overlapping values are added, and the small matrix is only inserted where it can fit fully inside the large matrix. I have achieved this using a for loop, but am curious whether the process can be vectorised. function M = TestDiagonal() N = 10; miniM = [1, -1; -1, 1]; M = zeros(N); for i = 1:N-1 M(i:i+1,i:i+1) = M(i:i+1,i:i+1) + miniM; end end Giving the desired matrix 1 -1 0 0

Vectorisation of code for insertion of 2x2 matrix along the diagonal of a large matrix

落花浮王杯 提交于 2020-02-02 12:20:05
问题 I am trying to do an elementwise insertion of a small matrix (2x2) along the diagonal of a large matrix (10x10 say). Overlapping values are added, and the small matrix is only inserted where it can fit fully inside the large matrix. I have achieved this using a for loop, but am curious whether the process can be vectorised. function M = TestDiagonal() N = 10; miniM = [1, -1; -1, 1]; M = zeros(N); for i = 1:N-1 M(i:i+1,i:i+1) = M(i:i+1,i:i+1) + miniM; end end Giving the desired matrix 1 -1 0 0

C# - How to convert byte array of image pixels data to grayscale using vector SSE operation

偶尔善良 提交于 2020-01-24 22:12:07
问题 I have a problem with converting the image data stored in byte[] array to grayscale. I want to use vector SIMD operations because in future a need to write ASM and C++ DLL files to measure operations time. When I read about SIMD I found that SSE command is operation on 128-bit registers so there is a problem because I need to convert my byte[] array into few Vector<T> stored into List<T>. Image is four channels RGBA JPEG so I need also to know how to create vectors with R, G, B data based on