3

I have an np array(series?) with numbers ranging from 1 to 100, I have a second array with some random numbers. Both arrays are the same length. I'd like to create a 3rd array that has a 1 if array2[i] > array1[i], 0 if array2[i] = array1[i] and -1 if array2[i] < array1[i].

I can easily do this with a for loop and some "if" statements but the processing time is more then I can afford.

Please excuse the way I've written this out, I am fairly new to python and asking questions in stackoverflow. Your help is much appreciated.

array1 = [1,2,3,4,5,6,7,8,9,10]
array2 = [5,8,2,4,9,3,0,2,8,5]
array3 = [1,1,-1,0,1,-1,-1,-1,-1,-1]
2
  • Can you add an example for both arrays please? Commented Feb 4, 2017 at 18:59
  • array1 = [1,2,3,4,5,6,7,8,9,10], array2 = [5,8,2,4,9,3,0,2,8,5], array3 should look like this = [1,1,-1,0,1,-1,-1,-1,-1,-1] Commented Feb 4, 2017 at 19:06

4 Answers 4

8

This seems like a good use case for np.sign, which turns positive numbers into 1, zero to 0, and negative to -1:

>>> array1 = np.array([1,2,3,4,5,6,7,8,9,10])
>>> array2 = np.array([5,8,2,4,9,3,0,2,8,5])
>>> np.sign(array2-array1)
array([ 1,  1, -1,  0,  1, -1, -1, -1, -1, -1])
Sign up to request clarification or add additional context in comments.

6 Comments

TIL np.sign is a thing. This is by far the best answer.
We have a winner. :)
I figured it could be this simple, thanks for the help.
I've just tried this answer compared to Christoph Terasa's answer and it takes 0.005 seconds longer this route FYI.
Weird, according to my tests using ipython's %timeit magic this solutions is the fastest.
|
5
import numpy as np

a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.array([5,8,2,4,9,3,0,2,8,5])

r = 1*(b > a) - (b < a) # multiplying by one converts boolean array to int array
print(r) # [ 1  1 -1  0  1 -1 -1 -1 -1 -1]

4 Comments

Ohh this is clever.
Awesome, that looks like it should do the trick, thank you very much for the quick reply!
@Mitch, I liked your solution much more.
@ChristophTerasa See DSM's answer - it is an improved version of mine.
4

You can also use masks:

array1 = [1,2,3,4,5,6,7,8,9,10]
array2 = [5,8,2,4,9,3,0,2,8,5]

result = np.zeros(10)
# use masks
result[array2>array1 ] = 1
result[array2 ==array1 ] = 0
result[array2<array1 ] = -1

print(result) # [1,1,-1,0,1,-1,-1,-1,-1,-1]

Comments

2

Perhaps there is a faster approach, but one option would be to just subtract the first array from the second, then numpy.clip the differences to form your comparison criteria.

>>> arr1 = np.array([1, 5, -3, 2, 7])
>>> arr2 = np.array([-4, 5, 3, 0, 7])
>>> np.clip(arr2-arr1, -1, 1)
array([-1,  0,  1, -1,  0])

Edit: Clipping isn't necessary because of np.sign, see DSM's answer.

1 Comment

This is about 30% faster than my solution.

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.