HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculating minima values for each column of given data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
eachcolumncalculatingminimaforvaluesdatagiven

Problem

Can anyone help make this perform faster?

import numpy as np
from scipy import signal

d0 = np.random.random_integers(10, 12, (5,5))
d1 = np.random.random_integers(1, 3, (5,5))
d2 = np.random.random_integers(4, 6, (5,5))
d3 = np.random.random_integers(7, 9, (5,5))
d4 = np.random.random_integers(1, 3, (5,5))
d5 = np.random.random_integers(13, 15, (5,5))

data = np.array([d0,d1,d2,d3,d4,d5])  ####THIS SHAPE IS GIVEN, WHICH CAN NOT BE CHANGED.
data = data.reshape(6,25)

data = data.T
minimas=[]
for x in data:
    minima = signal.argrelmin(x, axis=0)
    minimas.append(x[minima])

print minimas    ####THIS SHAPE IS GIVEN, WHICH CAN NOT BE CHANGED.

Solution

Note: this only works if there are exactly two minima in each row.

You can avoid the loop by computing minima along axis 1 of the data array.

minima = signal.argrelmin(data, axis=1)
minimas = list(data[minima].reshape(-1,2))

Code Snippets

minima = signal.argrelmin(data, axis=1)
minimas = list(data[minima].reshape(-1,2))

Context

StackExchange Code Review Q#49799, answer score: 2

Revisions (0)

No revisions yet.