6

Perhaps not such a big deal, but it breaks my heart to follow this:

deltas = data[1:] - data[:-1]

with this:

for i in range(len(deltas)):
        if deltas[i] < 0: deltas[i] = 0
        if deltas[i] > 100: deltas[i] = 0

For this particular example...is there a better way to do the cleansing part?

Question part two: What if the cleansing rules are more complicated, or less complicated than this example. For example, we might just want to change all negative numbers to zero. Or, we might be doing a more complicated mapping.

2 Answers 2

9
import numpy as np
deltas=np.diff(data)
deltas[deltas<0]=0
deltas[deltas>100]=0

Also possible, and a bit quicker is

deltas[(deltas<0) | (deltas>100)]=0
Sign up to request clarification or add additional context in comments.

Comments

1

Try using numpy.vectorize to apply a function to each element of the numpy array.

Comments

Your Answer

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.