4
\$\begingroup\$

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.
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 15, 2014 at 5:06
\$\endgroup\$
1
  • \$\begingroup\$ Why do you need that particular output shape? \$\endgroup\$ Commented May 15, 2014 at 15:54

1 Answer 1

2
\$\begingroup\$

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))
answered May 15, 2014 at 7:17
\$\endgroup\$
6
  • \$\begingroup\$ thanks. what about reshaping and transposing? is there ways to improve performance? \$\endgroup\$ Commented May 15, 2014 at 7:33
  • \$\begingroup\$ thanks again. by the way, splitting and stacking are also same in terms of performance? \$\endgroup\$ Commented May 15, 2014 at 8:26
  • 1
    \$\begingroup\$ @alps Actually, reshape docs state that sometimes it does copy the array, for example when reshaping after transposing. I'm not sure about splitting and stacking; I presume stacking involves copying at least in the general case. \$\endgroup\$ Commented May 15, 2014 at 9:53
  • \$\begingroup\$ Where does the 2 come from? \$\endgroup\$ Commented May 15, 2014 at 15:56
  • 1
    \$\begingroup\$ @alps See my comment to Winston above. \$\endgroup\$ Commented May 16, 2014 at 6:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.