All Questions
404 questions
1
vote
1
answer
88
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 ...
-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 ...
1
vote
1
answer
45
views
Getting interval cuts between two 2D numpy arrays contining a given range
I have been struggling to write a function to cut up intervals in two numpy arrays (a1,a2) that contain intervals in the full range 0, 6000.
intervals from a1 and a2 can not overlap in any way, if a ...
0
votes
1
answer
60
views
Vectorized way to copy elements from pandas Series to python built-in array
Is there a vectorized way to copy elements from a pandas Series to a python built-in array? For example:
from array import array
import pandas as pd
s = pd.Series(range(0, 10, 2)); s += 0.1
a = array('...
1
vote
1
answer
526
views
Unexpected behavior of JAX `vmap` for multiple arguments
I have found that vmap in JAX does not behave as expected when applied to multiple arguments. For example, consider the function below:
def f1(x, y, z):
f = x[:, None, None] * z[None, None, :] + y[...
1
vote
2
answers
77
views
How to vectorize a function with numpy so that it can be applied to a 3d array, given this function needs to access certain cells of the array?
I have a computation that in which I need go through items of a 3d numpy array and add them to the values in the second dimension of the array (skipping the values in that dimension). It is analogous ...
1
vote
1
answer
50
views
Vectorized method to match and compare elements of two matrices
I have two matrices that contain only 1's and 0's:
A, shape n x m.
B, shape n x o.
Conceptually, the "n" rows represent facilities that contain products "m" and serve customer ...
1
vote
2
answers
107
views
NumPy vectorize pyfunc to expand array into arguments
I have an array of rectangular coordinates with the shape Ax2, where A is an arbitrary number. Here's an example of what I'm talking about:
>>> np.arange(20).reshape(10, 2)
array([[ 0, 1],
...
0
votes
1
answer
35
views
Vectorise Flattend outter product
I have the following function :
def cross(a,b):
shape_a = a.shape[0]
shape_b = b.shape[0]
cross = shape_a * shape_b
return (np.resize(a,(shape_a,1))*np.resize(b,(1,shape_b))).flatten()
Is it ...
1
vote
1
answer
194
views
Use numpy.vectorize on array of arrays
How do I apply numpy.vectorize in order to have it act on an array of arrays where each array is an input to the function?
For instance underneath, I am looking for the returns values to be the list [...
0
votes
2
answers
234
views
Use numpy masked array on an array of arrays without getting a flattened output
Consider the following code
x = np.array([[1, 2, 3], ['NaN', 4, 'NaN'], [7, 8, 9]])
# Convert 'NaN' strings to masked values
mask = np.ma.masked_where(x == 'NaN', x)
# Get a boolean array indicating ...
3
votes
0
answers
94
views
Is there a way to do a complex sum without a new axis in numpy?
I need to perform the sum:
C_ij = sum_k exp(A_ij*B_k)
with numpy, A being a NxN array and B a Nx1 array. Since the product is inside a sum I can't use np.einsum() I think.
For now, I'm doing:
C = np....
0
votes
0
answers
51
views
numpy array in a python function and the correct usage of if condition
let's say that I have this function
def funtion(x, bb, aa):
if x>aa:
res = aa
else:
xxr = x/aa
res = bb*(1.5*xxr-0.5*xxr**3)
return res
If I ...
0
votes
1
answer
73
views
Efficient Matrix construction for a weighted Euclidean distance
I have M points in 2-dimensional Euclidean space, and have stored them in an array X of size M x 2.
I have constructed a cost matrix whereby element ij is the distance d(X[i, :], X[j, :]). The ...