All Questions
1,734 questions
2
votes
1
answer
82
views
Python Vectorized Mask Generation (Numpy) [closed]
I have an arbitrary Matrix M which is (N x A). I have a column vector V (N x 1) which has on each row the amount of entries I would like to keep from the original M <= A (starting from the leftmost)...
1
vote
1
answer
89
views
How to populate a 2-d numpy array with values from a third dimension?
New Post: Processing satellite conjunctions with numpy efficiently
Original Post:
I have a numpy array of shape n x m x r, where the n axis represents an object, the m axis represents a timestep and ...
0
votes
1
answer
55
views
Vectorizing a Battery SOC Update with Recursive Charge/Discharge Constraints in NumPy
I have a battery simulation script where the battery’s state-of-charge (SOC) is updated iteratively. The charge and discharge values (how much energy is added/taken from the battery) at each timestep ...
2
votes
1
answer
99
views
Is there a smart way to vectorize a nested for-loop where the inner index is limited by the outer index?
Is there a smart way to vectorize a nested for loop of inner products, where the inner index is lower bound by the outer index?
Here's a simple example. Say that arr1 and arr2 are numpy arrays each ...
3
votes
1
answer
103
views
How to extract sub arrays from a larger array with two start and two stop 1-D arrays in Python?
I am looking for a way to vectorize the following code,
# Let cube have shape (N, M, M)
sub_arrays = np.empty(len(cube), 3, 3)
row_start = ... # Shape (N,) and are integers in range [0, M-2]
row_end ...
4
votes
5
answers
162
views
Efficiently draw random samples without replacement from an array in python
I need to draw random samples without replacement from a 1D NumPy array. However, performance is critical since this operation will be repeated many times.
Here’s the code I’m currently using:
import ...
0
votes
1
answer
83
views
Manipulation of a Pandas dataframe most time- and memory-efficiently
Please imagine I have a dataframe like this:
df = pd.DataFrame(index=pd.Index(['1', '1', '2', '2'], name='from'), columns=['to'], data= ['2', '2', '4', '5'])
df:
Now, I would like to calculate a ...
0
votes
1
answer
41
views
Summarize higher dimensions in numpy
I have a numpy array that holds board game states for all possible moves, and I want to summarize some of those moves. I'm struggling to vectorize that code and avoid a for loop when I choose which ...
1
vote
1
answer
66
views
How to efficiently compute and process 3x3x3 voxel neighborhoods in a 3D NumPy array?
I am working on a function to process 3D images voxel-by-voxel. For each voxel, I compute the difference between the voxel value and its 3x3x3 neighborhood, apply distance-based scaling, and determine ...
0
votes
2
answers
103
views
Vectorization with if/else conditions Python
I am somehow new to python and would like to get rid of use of for loop as it makes the program very slow. So I would like vectorize it. however, I am not sure how would I use it with two conditions
...
1
vote
1
answer
62
views
is there a faster way to solve time interval comparison in pandas?
i am trying to implement a solution that finds consequential alarms for an alarm within the given timeframe. i have created an intervalIndex from the dataframe and used np.vectorize to compare ...
1
vote
0
answers
58
views
Only calculate a numpy array at certain indexes given by a mask
Inside a large project I needed to process a 2D-numpy array in a certain way. Originally I implemented it using a quadruple-nested for-loop, with an if-test that checks if a condition is met for the ...
-1
votes
1
answer
74
views
Can't vectorize this function - works with constants but returns ValueError operands could not be broadcast together
I wrote a python function I would expect to allow vectorization, using np.where and np.maximum. However, when attempting to call that function by passing dataframe columns, I get the error "...
5
votes
1
answer
58
views
Numpy vectorize with if-statement: inconsistent with order of elements
Using the following two simple functions:
def x(t):
return 0 if t < 0 else 1
def h(t):
return 0 if t < 0 else np.exp(-t)
after applying x = np.vectorize(x) and h = np.vectorize(h) the ...
0
votes
0
answers
63
views
Is there a way of improving my solution by using Numpy vectorization?
I have the following exercise. Given a set of coordinates in a 3x4 matrix:
coord = np.array([
[2,3],
[0,1],
[2,2],
[2,3],
[1,1],
[2,3],
[1,1]
])
Given a 3x4 matrix total ...