Center of mass of a numpy array, how to make less verbose?
By : Gaelle Dayan
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , From what I know of numpy, it's a bad idea to apply an operation to each row of an array one at a time. Broadcasting is clearly the prefered method. Given that, how do I take data with a shape (N,3) and translate it to the center of mass? Below is the 'bad method' I'm using. This works, but I suspect it will have a performance hit for large N: , Try code :
R -= R.sum(0) / len(R)
|
calculating windowed probability mass function on an image using numpy and scipy
By : Mai Tiến Thịnh
Date : March 29 2020, 07:55 AM
Does that help After taking a long, and close look at @Divakar's bincount2D_vectorized, I realized the probability mass function was already in there, and just needed to be reshaped. To replace each pixel in the original image a, with a sum over the its kernel of log(p(x)) where p(x) is the probability of finding the pixel value x in that kernel, we can do: code :
def process(a, ksize=3):
# get the number of times a value occurs in each kernel window
window_shape = (ksize, ksize)
d = math.floor(ksize / 2)
a = np.pad(a, (d, d), 'constant', constant_values=(0, 0))
# get the windowed array of shape (a.height, a.width, ksize, ksize)
b = skimage.util.shape.view_as_windows(a, window_shape)
# process from https://stackoverflow.com/questions/47130141/ @Divakar
ar2D = b.reshape(-1, b.shape[-2] * b.shape[-1])
c = bincount2D_vectorized(ar2D)
# convert to probabilities
p = c / (ksize * ksize)
p[p == 0] = 1 # divide by 0 error
# sum and reshape
return np.sum(np.log(p), axis=1).reshape(a.shape)
|
Wrong center of mass using scipy ndimage.measurements.center_of_mass
By : user2662152
Date : March 29 2020, 07:55 AM
Does that help I am trying to find the centre of mass on binary images using ndimage.measurements.center_of_mass from the Scipy library. , Change the order of the arguments to scatter: code :
plt.scatter(key_point[1], key_point[0], s=20, marker='.', c='lightgreen')
|
'numpy.float64' is not iterable for scipy function centre of mass
By : user3093284
Date : January 02 2021, 04:59 AM
help you fix your problem So what was happening here was that both the obs and obsv variables are stored as xarray.DataArrays - this class is a wrapper around regular numpy arrays. To access the underlying np.ndarray, you will need to call the values from the object: CoM_obsv = ndimage.measurements.center_of_mass(obsv.values)
|
How to use numpy to speed up code that calculates center of mass?
By : user3537169
Date : March 29 2020, 07:55 AM
I hope this helps . I made a small block of code that - given n objects of specified masses and vector coordinates over time - will calculate the center of mass. I think the code looks clunky (it uses 3 for-loops), and was wondering if there were numpy methods to vectorize (or at least speed up) this method. As a note, the use of the class Body could probably be averted for this task, but is used in other relevant code not shown here. code :
center_of_mass = (A.mass * A.position + B.mass * B.position + C.mass * C.position) / total_mass
|