vectorization

Concatenate range arrays given start, stop numbers in a vectorized way - NumPy

微笑、不失礼 提交于 2019-12-30 11:07:19
问题 I have two matrices of interest, the first is a "bag of words" matrix, with two columns: the document ID and the term ID. For example: bow[0:10] Out[1]: array([[ 0, 10], [ 0, 12], [ 0, 19], [ 0, 20], [ 1, 9], [ 1, 24], [ 2, 33], [ 2, 34], [ 2, 35], [ 3, 2]]) In addition, I have an "index" matrix, where every row in the matrix contains the index of the first and last row for a given document ID in the bag of words matrix. Ex: row 0 is the first and last index for doc id 0. For example: index[0

Concatenate range arrays given start, stop numbers in a vectorized way - NumPy

折月煮酒 提交于 2019-12-30 11:06:21
问题 I have two matrices of interest, the first is a "bag of words" matrix, with two columns: the document ID and the term ID. For example: bow[0:10] Out[1]: array([[ 0, 10], [ 0, 12], [ 0, 19], [ 0, 20], [ 1, 9], [ 1, 24], [ 2, 33], [ 2, 34], [ 2, 35], [ 3, 2]]) In addition, I have an "index" matrix, where every row in the matrix contains the index of the first and last row for a given document ID in the bag of words matrix. Ex: row 0 is the first and last index for doc id 0. For example: index[0

Add a diagonal of zeros to a matrix in MATLAB

十年热恋 提交于 2019-12-30 08:25:12
问题 Suppose I have a matrix A of dimension Nx(N-1) in MATLAB, e.g. N=5; A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20 ]; I want to transform A into an NxN matrix B , just by adding a zero diagonal, i.e., B=[ 0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]; This code does what I want: B_temp = zeros(N,N); B_temp(1,:) = [0 A(1,:)]; B_temp(N,:) = [A(N,:) 0]; for j=2:N-1 B_temp(j,:)= [A(j,1:j-1) 0 A(j,j:end)]; end B = B_temp; Could you suggest an efficient way to

Vectorization in Apache Mahout

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 05:14:05
问题 I am new to Mahout. I have a requirement to convert a text file to a vector for classification in later stage. Could anybody of of shed some light on these below questions? How to convert a text file to a vector in mahout? The file format is like "username|comment about item|rating" The data will be few TBs. So which algorithm implementable I can use for classification using the vector I suppose to create? Thanks, Arun 回答1: You can check these 2 examples that also somewhat do/explain how to

Loading data for GCC's vector extensions

强颜欢笑 提交于 2019-12-30 04:36:07
问题 GCC's vector extensions offer a nice, reasonably portable way of accessing some SIMD instructions on different hardware architectures without resorting to hardware specific intrinsics (or auto-vectorization). A real use case, is calculating a simple additive checksum. The one thing that isn't clear is how to safely load data into a vector. typedef char v16qi __attribute__ ((vector_size(16))); static uint8_t checksum(uint8_t *buf, size_t size) { assert(size%16 == 0); uint8_t sum = 0; vec16qi

How can I convert a vector to a cell array?

余生长醉 提交于 2019-12-29 08:28:09
问题 I have a column vector I want to convert to a cell array such as: A = rand(10,1); B = cell(10,1); for i=1:10 B{i} = A(i); end B = [0.6221] [0.3510] [0.5132] [0.4018] [0.0760] [0.2399] [0.1233] [0.1839] [0.2400] [0.4173] How can I do this without an explicit for loop? I tried: B{:} = A(:) and [B{:}] = deal(A) with no luck... Also if possible, how can I do the same thing for a matrix, i.e. have each element in a cell by itself? 回答1: Use the function num2cell: B = num2cell(A); Works with

How can I convert a vector to a cell array?

☆樱花仙子☆ 提交于 2019-12-29 08:28:09
问题 I have a column vector I want to convert to a cell array such as: A = rand(10,1); B = cell(10,1); for i=1:10 B{i} = A(i); end B = [0.6221] [0.3510] [0.5132] [0.4018] [0.0760] [0.2399] [0.1233] [0.1839] [0.2400] [0.4173] How can I do this without an explicit for loop? I tried: B{:} = A(:) and [B{:}] = deal(A) with no luck... Also if possible, how can I do the same thing for a matrix, i.e. have each element in a cell by itself? 回答1: Use the function num2cell: B = num2cell(A); Works with

Numpy elementwise product of 3d array

我是研究僧i 提交于 2019-12-29 05:23:26
问题 I have two 3d arrays A and B with shape (N, 2, 2) that I would like to multiply element-wise according to the N-axis with a matrix product on each of the 2x2 matrix. With a loop implementation, it looks like C[i] = dot(A[i], B[i]) Is there a way I could do this without using a loop? I've looked into tensordot, but haven't been able to get it to work. I think I might want something like tensordot(a, b, axes=([1,2], [2,1])) but that's giving me an NxN matrix. 回答1: It seems you are doing matrix

Numpy Broadcast to perform euclidean distance vectorized

冷暖自知 提交于 2019-12-29 03:11:07
问题 I have matrices that are 2 x 4 and 3 x 4. I want to find the euclidean distance across rows, and get a 2 x 3 matrix at the end. Here is the code with one for loop that computes the euclidean distance for every row vector in a against all b row vectors. How do I do the same without using for loops? import numpy as np a = np.array([[1,1,1,1],[2,2,2,2]]) b = np.array([[1,2,3,4],[1,1,1,1],[1,2,1,9]]) dists = np.zeros((2, 3)) for i in range(2): dists[i] = np.sqrt(np.sum(np.square(a[i] - b), axis=1

Improve speed of NORMRND for a family of distributions in MATLAB

浪子不回头ぞ 提交于 2019-12-29 01:50:09
问题 So, I am looking for a way to speed up my code. I have a large vector of normal distributions (i.e. a vector of means and standard deviations) that I need to generate random numbers from. A generic example of my code looks like this: tic N=1e6; mu = rand(N,1); sigma = rand(N,1); temp = zeros(length(mu),1); for i = 1:length(mu) temp(i) = normrnd(mu(i),sigma(i)); end toc This code in its current form has an elapsed time of: Elapsed time is 12.281509 seconds. I normally try to vectorize most of