All Questions
66 questions
2votes
1answer
99views
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 ...
0votes
1answer
139views
Use vectorised functions instead of the loops
My function is such that it takes the minimum over the range of dataframe which keeps increasing in length and maximum over the dataframe which reduces in length with each iteration. The dataframe ...
-2votes
1answer
84views
Python efficiency - nested loop structure with matrices
I am new to python, but have been coding in MATLAB for years. I've been working to code a specific problem, which requires nested for loops for multiple matrices to solve a series of equations. I knew ...
0votes
2answers
41views
Cannot use an 'if statment' on the output of a fucntion, with a single-valued return, inisde a loop. (Use a.any() or a.all() error)
First time asking a question here. The following code is supposed to plot the 'neutron drip line' of nuclei. It represents, fir each specific value of atomic number (Z), the limiting value of neutron ...
0votes
2answers
159views
Efficiently subset a 2D numpy array iteratively
I have a large image on which I want to perform an operation from a moving window over the whole window. Here is a reproducible example: Given an array named image with shape (5, 5), I need to extract ...
0votes
2answers
46views
How to write efficient loops in Python and solve error: "ValueError: operands could not be broadcast together with shapes (6,) (4,)"?
I'm trying to avoid loops as much as possible in Python because I have been told that code runs more efficiently when there are fewer loops. For the same reason, I'm also trying to use "zip"...
1vote
2answers
284views
Numba implementation of nested sorting of list/array
I am trying to use the following function with Numba: @numba.njit def nested_sort(s): return sorted(s, key=lambda x: (x[1], x[2])) s = [[1, 3, 11], [2, 3, 19], [3, 2, 18], [4, 2, 9]] nested_sort(...
0votes
2answers
179views
How to make Python to run 4 nested loops faster?
I have 4 nested loops in my Python code and it takes ages to finish all the loops when the grids are big. For example, here is a piece of my code: from itertools import product T = 50 beta ...
0votes
2answers
91views
Simplifying nested loops using recursion
Let's assume we have a three-dimensional array x. For each k, I want to have the mean of x[:, :, k] stored as out[k]. The task is simple if it is a 3D or 4D matrix, with the for loop like this: x = np....
2votes
3answers
219views
How to avoid nested for loops in NumPy?
I have this code. n_nodes = len(data_x) X = np.zeros((n_nodes, n_nodes)) for i in range(n_nodes): for j in range(n_nodes): X[i, j] = data_x[i] ** j I want to do the same task with no loops ...
2votes
1answer
52views
Replace for loops that exchange attributes of objects with numpy function
I made a bouncing ball simulation in Matplotlib, the update function exchange the move vectors (attributes) of the balls when they collide. Is it possible to replace the for loops in the update ...
1vote
0answers
103views
Is there a function to quickly perform a nested loop in python?
In my code I have a list1 of about length 12000 and a list2 of about length 2000, and I want to create data that is based on how data from both these interact and assign it to each value from list2 ...
0votes
1answer
30views
How can I create np.ndarray from four arrays of different lengths without using loops?
I have 5 arrays, that I need to create two large arrays of shape ((1000,5000)) to use in regression analysis. I'm currently using the following code. # data shapes are as follows bbeta.shape = ((5000,...
1vote
1answer
162views
Computing derivatives using numpy
I'm trying to implement a differential in python via numpy that can accept a scalar, a vector, or a matrix. import numpy as np def foo_scalar(x): f = x * x df = 2 * x return f, df def ...
1vote
1answer
144views
Can numpy.tensordot or ufunc replace this nested for loop?
Note: I know this is very similar to Use numpy.tensordot to replace a nested loop , only that the actual instance we are working on seemed different( I need quadratic form caclculation) and I wasn't ...