vectorization

Iterating over a numpy array and operating on each element

风格不统一 提交于 2019-12-11 15:58:48
问题 I have a numpy array of size 8x8. Here is the numpy array: QuantTable = np.array([[16, 11 ,10, 16, 24, 40, 51, 61], [12, 12, 14, 19, 26, 58, 60, 55], [14, 13, 16, 24, 40, 57, 69, 56], [14, 17, 22, 29, 51, 87, 80, 62], [18, 22, 37, 29, 51, 87, 80, 62], [24, 35, 55, 64, 81, 109, 103, 77], [49, 64, 78, 87, 103, 121, 120, 101], [72, 92, 95, 98, 112, 100, 103, 99]]) I would like to perform the operations on the elements in the array. I have created a function that accepts a scaling factor value

How to implement vectorization over loops of block multiplication via tensor multiplication in Numpy?

二次信任 提交于 2019-12-11 15:54:00
问题 I have a code that runs sample_size sequences of matrix multiplication, and for each sequence there involves seq_length operations of sums of matrices multiplications. But the shortcoming of my code is that as soon as seq_length gets any higher than around 300, the algorithm slows down, and needless to say that as seq_length gets bigger, the whole algorithm becomes slower and slower. So I was wondering if there are any optimization/vectorization that can be implemented with the way I've

Join arrays in PowerShell similar to concatenating DataFrame columns

无人久伴 提交于 2019-12-11 15:22:50
问题 Is there a way to join arrays in PowerShell similar to concatenating DataFrame columns in Python Pandas with concat or in R with cbind rather than iterating through every item? Below is a reproducible example that binds four arrays together as four columns in a PowerShell object. How would I get rid of the for loop and get the same results? $LogicalProcessors = (Get-WmiObject –class Win32_processor -Property NumberOfLogicalProcessors).NumberOfLogicalProcessors; function myTop([String]$SortCol

How to make R foreach loops efficient

99封情书 提交于 2019-12-11 14:06:00
问题 I am trying to compute a 300,000x300,000 matrix in R, my codes are working quite well but it's been running for days now, how can i make it more efficient and time saving? My codes are working well but it has been running for days now, attached are a subset of what I'm working with, the ID extends to 300,000; how can i make the codes run faster in minutes as it has been running for days now. fam <- structure(list(ID = c(1L, 2L, 3L, 4L, 6L, 5L, 7L), dad = c(0L, 0L, 1L, 1L, 1L, 3L, 5L), mum = c

numpy.vectorize of function that returns an array

情到浓时终转凉″ 提交于 2019-12-11 12:19:40
问题 I am trying to vectorize a function which has 2 inputs, and outputs a np.array, of shape =(4,). The function looks like this: def f(a, b): return np.array([a+b, a-b, a, b]) I was able to vectorize the function, using the signature parameter, however it only works if I exclude one of the parameters using the excluded argument of np.vectorize : This works: vec = np.vectorize(f, signature='()->(n)', excluded=[1]) x = np.arange(5) y = 3 vec(x, y) >> output: array([[ 3, -3, 0, 3], [ 4, -2, 1, 3],

Create a block diagonal and subdiagonal with a repeating block in Matlab

隐身守侯 提交于 2019-12-11 11:43:18
问题 Suppose I have an n -by- n matrix A and an n -by- n matrix B . I want to create a block matrix C consisting of N blocks of matrix A as a diagonal and ( N -1) blocks of matrix B as a subdiagonal below the A diagonal. This link gives answers for only the block diagonal case, which are all great. Now I want to add a block subdiagonal, which command in Matlab should I use? Thank you so much for your help. 回答1: If you know how to create a matrix C with A on its diagonal (of size nN -by- nN ), you

Idiomatic way to copy cell values “down” in an R vector [duplicate]

醉酒当歌 提交于 2019-12-11 10:08:59
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: Populate NAs in a vector using prior non-NA values? Is there an idiomatic way to copy cell values "down" in an R vector? By "copying down", I mean replacing NAs with the closest previous non-NA value. While I can do this very simply with a for loop, it runs very slowly. Any advice on how to vectorise this would be appreciated. # Test code # Set up test data len <- 1000000 data <- rep(c(1, rep(NA, 9)), len %/%

How to vectorize Softmax probability of a multi dimensional matrix

送分小仙女□ 提交于 2019-12-11 10:06:40
问题 I am trying to go through the assignment 1 for Stanford cs244n class. Problem 1b highly recommend optimization for the Softmax function. I managed to get the Softmax of the N dimensional vector. I also got the Softmax of the MxN dimensional matrix but used a for loop through the columns. I have the following code: def softmax(x): orig_shape = x.shape # Matrix if len(x.shape) > 1: softmax = np.zeros(orig_shape) for i,col in enumerate(x): softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col

Math library and arrays in Python

ⅰ亾dé卋堺 提交于 2019-12-11 09:47:15
问题 I am to use the Math Library to do some calculations on an array. I tried something like this: import numpy as np import math a = np.array([0, 1, 2, 3]) a1 = np.vectorize(a) print("sin(a) = \n", math.sin(a1)) Unfortunately it does not work. An error occur: "TypeError: must be real number, not vectorize" . How can I use the vectorize function to be able to calculate that kind of things? 回答1: The whole point of numpy is that you don't need any math method or any list comprehension: >>> import

How to Access the Cell array Matrix? [duplicate]

限于喜欢 提交于 2019-12-11 09:39:18
问题 This question already has answers here : How to use cell arrays in Matlab? (2 answers) Closed 5 years ago . Could any one tell me about the indexing of a cell Array? I've tried to google it but I could only find unsatisfied result (may be I'm not good in googling). For matrix indexing I found a good document which can be found here. For my case let take a simple example. a = {ones(10)} and I want to access the first element of a . Something like a(1,1) % this will give a 10 x 10 matrix but i