scipy

Find most frequent row or mode of a matrix of vectors - Python / NumPy

懵懂的女人 提交于 2021-02-20 19:32:53
问题 I have a numpy array of shape (?,n) that represents a vector of n-dimensional vectors. I want to find the most frequent row. So far it seems that the best way is to just iterate over all the entries and store a count, but it seems obscene that numpy or scipy wouldn't have something builtin to perform this task. 回答1: Here's an approach using NumPy views , which should be pretty efficient - def mode_rows(a): a = np.ascontiguousarray(a) void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a

efficient function to find harmonic mean across different pandas dataframes

两盒软妹~` 提交于 2021-02-20 19:01:49
问题 I have several dataframes with identical shape/types, but slightly different numeric values. I can easily produce a new dataframe with the mean of all input dataframes via: df = pd.concat([input_dataframes]) df = df.groupby(df.index).mean() I want to do the same with harmonic mean (probably the scipy.stats.hmean function). I have attempted to do this using: .groupby(df.index).apply(scipy.stats.hmean) But this alters the structure of the dataframe. Is there a better way to do this, or do I

Interpolation to evenly space trajectory data for different curves

五迷三道 提交于 2021-02-20 02:28:16
问题 I am using the following code (adapted from Resample or normalize trajectory data so points are evenly spaced) to interpolate 2D X & Y positional data (with no time index) so that the points are evenly spaced. From my understanding, the answer for that question assumed that the x values follow a certain curve or pattern (e.g. exponential curve) but that isn't the case for all my trajectories. I believe I need to interpolate X and Y separately. However, this code does not seem to produce

Python: Sample from multivariate normal with N means and same covariance matrix

白昼怎懂夜的黑 提交于 2021-02-19 08:14:11
问题 Suppose I want to sample 10 times from multiple normal distributions with the same covariance matrix (identity) but different means, which are stored as rows of the following matrix: means = np.array([[1, 5, 2], [6, 2, 7], [1, 8, 2]]) How can I do that in the most efficient way possible (i.e. avoiding loops) I tried like this: scipy.stats.multivariate_normal(means, np.eye(2)).rvs(10) and np.random.multivariate_normal(means, np.eye(2)) But they throw an error saying mean should be 1D. Slow

Normalizing vector produces nan in Numpy

可紊 提交于 2021-02-19 07:45:05
问题 I'm getting some strange behavior from scipy/numpy that I suspect is a bug but someone may know better? I've got a pair of long arrays which I'm breaking into frames which are of length 2-4 for debugging purposes. I want to normalize each pair of frames and take the dot product. The code that does it (with some debugging output) is: tf = numpy.copy(t_frame) / norm(t_frame) pf = numpy.copy(p_frame) / norm(p_frame) print "OPF:" print p_frame print "PF: " print pf print "TF norm is: " + str(norm

Normalizing vector produces nan in Numpy

允我心安 提交于 2021-02-19 07:44:20
问题 I'm getting some strange behavior from scipy/numpy that I suspect is a bug but someone may know better? I've got a pair of long arrays which I'm breaking into frames which are of length 2-4 for debugging purposes. I want to normalize each pair of frames and take the dot product. The code that does it (with some debugging output) is: tf = numpy.copy(t_frame) / norm(t_frame) pf = numpy.copy(p_frame) / norm(p_frame) print "OPF:" print p_frame print "PF: " print pf print "TF norm is: " + str(norm

Explain the intuition for the tol paramer in scipy differential evolution

拟墨画扇 提交于 2021-02-19 06:45:05
问题 I am using the differential evolution optimizer in scipy and I don't understand the intuition behind the tol argument. Specifically is say in the documentation: tol: float, optional When the mean of the population energies, multiplied by tol, divided by the standard deviation of the population energies is greater than 1 the solving process terminates: convergence = mean(pop) * tol / stdev(pop) > 1 What does setting tol represent from a user perspective? 回答1: Maybe the formula in the

scipy optimise minimize — parallelisation options

自作多情 提交于 2021-02-19 05:49:05
问题 When running scipy optimize minimum using the L-BFGS-B method, I found that on certain computers, it uses all 8 cpu cores (see photo 1), on others it uses 4 out of 8 cores (see photo 2) and on others it only uses 1 core. I have not used any libraries/code to make it parallel -- it seems to be doing that by default. Is there a way that I can specify how many cores it should use easily? I couldn't find anything online that suggested scipy optimize uses parallelisation by default. fmin = scipy

python scipy.signal.peak_widths --> absolute heigth? (fft -3dB damping)

隐身守侯 提交于 2021-02-19 05:30:28
问题 https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_widths.html I think the linked function can only calculate the peak widths at a relative height. Does anyone know if there is a function that calculates the width at a fixed value (peak_amplitude - x) for all peaks? Currently I am trying to change the original inner function "_peak_widths". Fail already with the cimport. Understand the source code here only partially. I added in the code where I would make a modification.

python scipy.signal.peak_widths --> absolute heigth? (fft -3dB damping)

我只是一个虾纸丫 提交于 2021-02-19 05:29:51
问题 https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_widths.html I think the linked function can only calculate the peak widths at a relative height. Does anyone know if there is a function that calculates the width at a fixed value (peak_amplitude - x) for all peaks? Currently I am trying to change the original inner function "_peak_widths". Fail already with the cimport. Understand the source code here only partially. I added in the code where I would make a modification.