Skip to content

Tag: numpy

bootstrap numpy 2D array

I am trying to sample with replacement a base 2D numpy array with shape of (4,2) by rows, say 10 times. The final output should be a 3D numpy array. Have tried the code below, it works. But is there a way to do it without the for loop? Answer Here’s one vectorized approach – Basic idea is that we

replace masked with nan in numpy masked_array

I’d like to replace the masked result with nan. Is there a way to do that directly with numpy’s masked_array? Answer filled method replaces the masked values with the fill value: Or as you do, specify the fill value when defining the array: The masked mean method skips over the filled values:

Generating constraints for optimization in scipy using loop

There are lots of constraints which I will use in optimization using scipy; so I need to generate the constraints by loop. Below there’s a sample of my constraints: There constraints are more than three… I use following loop to generate but I couldn’t get the same output. Answer You are upda…

using python to calculate Vector Projection

Is there an easier command to compute vector projection? I am instead using the following: Answer Maybe this is what you really want: This should give the projection of vector x onto vector y – see https://en.wikipedia.org/wiki/Vector_projection. Alternatively, if you want to compute the projection of y…

How is the dtype of a numpy array calculated internally?

I was just messing around with numpy arrays when I realized the lesser known behavior of the dtypes parameter. It seems to change as the input changes. For example, gives dtype(‘int32’) However, gives dtype(‘int64’) So, my first question is: How is this calculated? Does it make the dat…

Finding non-intersection of two pytorch tensors

Thanks everyone in advance for your help! What I’m trying to do in PyTorch is something like numpy’s setdiff1d. For example given the below two tensors: The expected output should be (sorted or unsorted): Ideally the operations are done on GPU and no back and forth between GPU and CPU. Much apprec…