Debugging Numpy VisibleDeprecationWarning (ndarray from ragged nested sequences)

With a function that creates a ragged array: In [60]: def foo(): …: print(‘one’) …: x = np.array([[1],[1,2]]) …: return x …: In [61]: foo() one /usr/local/bin/ipython3:3: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, … Read more

Generate random array of floats between a range

np.random.uniform fits your use case: sampl = np.random.uniform(low=0.5, high=13.3, size=(50,)) Update Oct 2019: While the syntax is still supported, it looks like the API changed with NumPy 1.17 to support greater control over the random number generator. Going forward the API has changed and you should look at https://docs.scipy.org/doc/numpy/reference/random/generated/numpy.random.Generator.uniform.html The enhancement proposal is here: https://numpy.org/neps/nep-0019-rng-policy.html

Add density curve on the histogram

distplot has been removed: removed in a future version of seaborn. Therefore, alternatives are to use histplot and displot. sns.histplot import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np df = pd.DataFrame(np.random.randn(100, 4), columns=list(‘ABCD’)) X = df[‘A’] sns.histplot(X, kde=True, bins=20) plt.show() sns.displot import pandas as pd import matplotlib.pyplot … Read more

calculate distance of 2 list of points in numpy

Using direct numpy broadcasting, you can do this: dist = np.sqrt(((a[:, None] – b[:, :, None]) ** 2).sum(0)) Alternatively, scipy has a routine that will compute this slightly more efficiently (particularly for large matrices) from scipy.spatial.distance import cdist dist = cdist(a, b) I would avoid solutions that depend on factoring-out matrix products (of the form … Read more

Digitizing an analog signal

Here’s a bit of code that might help. from __future__ import division import numpy as np def find_transition_times(t, y, threshold): “”” Given the input signal `y` with samples at times `t`, find the times where `y` increases through the value `threshold`. `t` and `y` must be 1-D numpy arrays. Linear interpolation is used to estimate … Read more

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