How could I speed up my written python code: spheres contact detection (collision) using spatial searching

UPDATE: this post answered is now superseded by this new one (which take into account the updates of the question) providing an even faster code based on a different approach. Step 1: better algorithm First of all, building a k-d tree runs in O(n log n) time and doing a query runs in O(log n) … Read more

Feeding .npy (numpy files) into tensorflow data pipeline

It is actually possible to read directly NPY files with TensorFlow instead of TFRecords. The key pieces are and, along with a look at the documentation of the NPY format. For simplicity, let’s suppose that a float32 NPY file containing an array with shape (N, K) is given, and you know the number … Read more

Fitting to Poisson histogram

The problem with your code is that you do not know what the return values of curve_fit are. It is the parameters for the fit-function and their covariance matrix – not something you can plot directly. Binned Least-Squares Fit In general you can get everything much, much more easily: import numpy as np import matplotlib.pyplot … Read more

How to count RGB or HSV channel combination in an image?

You could use np.unique with its new axis argument functionality that does grouping – np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)] Sample run – In [56]: im Out[56]: array([[[255, 255, 255], [255, 0, 0]], [[255, 0, 255], [255, 255, 255]]]) In [57]: np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)] Out[57]: array([[255, 0, 0, 1], [255, 0, 255, 1], [255, 255, 255, 2]])

how set numpy floating point accuracy?

Do you care about the actual precision of the result, or about getting the exact same digits back from your two calculations? If you just want the same digits, you could use np.around() to round the results to some appropriate number of decimal places. However, by doing this you’ll only reduce the precision of the … Read more

Efficiently Creating A Pandas DataFrame From A Numpy 3d array

Here’s one approach that does most of the processing on NumPy before finally putting it out as a DataFrame, like so – m,n,r = a.shape out_arr = np.column_stack((np.repeat(np.arange(m),n),a.reshape(m*n,-1))) out_df = pd.DataFrame(out_arr) If you precisely know that the number of columns would be 2, such that we would have b and c as the last two … Read more

Why do we call .detach() before calling .numpy() on a Pytorch Tensor?

I think the most crucial point to understand here is the difference between a torch.tensor and np.ndarray: While both objects are used to store n-dimensional matrices (aka “Tensors”), torch.tensors has an additional “layer” – which is storing the computational graph leading to the associated n-dimensional matrix. So, if you are only interested in efficient and … Read more