vectorization

Describing gaps in a time series pandas

家住魔仙堡 提交于 2019-12-23 18:20:30
问题 I'm trying to write a function that takes a continuous time series and returns a data structure which describes any missing gaps in the data (e.g. a DF with columns 'start' and 'end'). It seems like a fairly common issue for time series, but despite messing around with groupby, diff, and the like -- and exploring SO -- I haven't been able to come up with much better than the below. It's a priority for me that this use vectorized operations to remain efficient. There has got to be a more

Loop is not vectorized when variable extent is used

拥有回忆 提交于 2019-12-23 17:13:38
问题 Version A code is not vectorized while version B code is vectorized. How to make version A vectorize and keep the variable extents (without using literal extents)? The nested loop is for multiplication with broadcasting as in numpy library of python and matlab. Description of broadcasting in numpy library is here. Version A code (no std::vector. no vectorization.) This only uses imull (%rsi), %edx in .L169 , which is not a SIMD instruction. gcc godbolt #include <iostream> #include <stdint.h>

Vectorize the pairwise kronecker product in matlab

我怕爱的太早我们不能终老 提交于 2019-12-23 12:37:18
问题 Suppose there are two matrices of the same size, and I want to calculate the summation of their column-wise kronecker product. Due to sometimes the column size is quite large so that the speed could be very slow. Thus, is there anyway to vectorize this function or any function may help reducing the complexity in matlab? Thanks in advance. The corresponding matlab code with a for-loop is provided below, and the answer of d is the interested output: A = rand(3,7); B = rand(3,7); d = zeros(size

Intersection of multiple arrays without for loop in MATLAB

感情迁移 提交于 2019-12-23 12:23:03
问题 I've always been told that almost all for loops can be omitted in MATLAB and that they in general slow down the process. So is there a way to do so here?: I have a cell-array ( tsCell ). tsCell stores time-arrays with varying length. I want to find an intersecting time-array for all time-arrays ( InterSection ): InterSection = tsCell{1}.time for i = 2:length{tsCell}; InterSection = intersect(InterSection,tsCell{i}.time); end 回答1: Here's a vectorized approach using unique and accumarray,

Vectorizing the solution of a linear equation system in MATLAB

为君一笑 提交于 2019-12-23 12:13:49
问题 Summary: This question deals with the improvement of an algorithm for the computation of linear regression. I have a 3D ( dlMAT ) array representing monochrome photographs of the same scene taken at different exposure times (the vector IT ) . Mathematically, every vector along the 3rd dimension of dlMAT represents a separate linear regression problem that needs to be solved. The equation whose coefficients need to be estimated is of the form: DL = R*IT^P , where DL and IT are obtained

“Desort” a vector (undo a sorting)

给你一囗甜甜゛ 提交于 2019-12-23 10:06:46
问题 In Matlab, sort returns both the sorted vector and an index vector showing which vector element has been moved where: [v, ix] = sort(u); Here, v is a vector containing all the elements of u , but sorted. ix is a vector showing the original position of each element of v in u . Using Matlab's syntax, u(ix) == v . My question: How do I obtain u from v and ix ? Of course, I could simply use: w = zero(size(v)); for i = 1:length(v) w(ix(i)) = v(i) end if nnz(w == u) == length(u) print('Success!');

“Desort” a vector (undo a sorting)

♀尐吖头ヾ 提交于 2019-12-23 10:05:36
问题 In Matlab, sort returns both the sorted vector and an index vector showing which vector element has been moved where: [v, ix] = sort(u); Here, v is a vector containing all the elements of u , but sorted. ix is a vector showing the original position of each element of v in u . Using Matlab's syntax, u(ix) == v . My question: How do I obtain u from v and ix ? Of course, I could simply use: w = zero(size(v)); for i = 1:length(v) w(ix(i)) = v(i) end if nnz(w == u) == length(u) print('Success!');

How to vectorize Fisher's exact test?

此生再无相见时 提交于 2019-12-23 09:49:30
问题 Is it possible, and if so how, to optimize this calculation using the vectorization of Fisher's exact test? Runtime is cumbersome when num_cases > ~1000000. import numpy as np from scipy.stats import fisher_exact num_cases = 100 randCounts = np.random.random_integers(100,size=(num_cases,4)) def testFisher(randCounts): return [fisher_exact([[r[0],r[1]],[r[2], r[3]]])[0] for r in randCounts] In [6]: %timeit testFisher(randCounts) 1 loops, best of 3: 524 ms per loop 回答1: Here is an answer using

Vectorize numpy code with operation depending on previous value

陌路散爱 提交于 2019-12-23 09:48:13
问题 The following code models a system that can sample 3 different states at any time, and the constant transition probability between those states is given by the matrix prob_nor . Threrefore, each point in trace depends on the previous state. n_states, n_frames = 3, 1000 state_val = np.linspace(0, 1, n_states) prob = np.random.randint(1, 10, size=(n_states,)*2) prob[np.diag_indices(n_states)] += 50 prob_nor = prob/prob.sum(1)[:,None] # transition probability matrix, # row sum normalized to 1.0

Vectorization: Not a valid collection

两盒软妹~` 提交于 2019-12-23 09:33:24
问题 I wanna vectorize a txt file containing my training corpus for the OneClassSVM classifier. For that I'm using CountVectorizer from the scikit-learn library. Here's below my code: def file_to_corpse(file_name, stop_words): array_file = [] with open(file_name) as fd: corp = fd.readlines() array_file = np.array(corp) stwf = stopwords.words('french') for w in stop_words: stwf.append(w) vectorizer = CountVectorizer(decode_error = 'replace', stop_words=stwf, min_df=1) X = vectorizer.fit_transform