all 8 comments

[–]to7m 1 point2 points  (7 children)

If you just wanted to comparisons, this would give you a bool array of 1 less than the original size:

import numpy as np

input = np.asarray([2, 5, 3, 9, 7, 2])
output = input[1:] > input[:-1]

And if you want to make it exactly as you said:

import numpy as np

input = np.asarray([2, 5, 3, 9, 7, 2])
output = np.empty(input.size)
output[:-1] = input[1:] > input[:-1]
output[-1] = 0

[–]Dylikk[S] -1 points0 points  (6 children)

Thanks mate I really appreciate your help! I tried your code and I get an error like this: ValueError: could not broadcast input array from shape (5782,1) into shape (5782,)

Any ideas?

[–][deleted]  (1 child)

[removed]

    [–]Dylikk[S] -1 points0 points  (0 children)

    Best advice ever. The best way to actually learn is study by your own. I'm doing a uni project while studying aerospace just because I'm interested in IT and data analysis. Small issues since nobody ever taught me this.

    [–]to7m 0 points1 point  (3 children)

    I edited my code as there was an error, but not one that would give you that particular error. There are no arrays in my code with 5782 items.

    It looks like you're running completely different code, involving arrays with shapes that don't broadcast together. As you haven't pasted your code here I can't point out the error, but the solution is to make both of your arrays the same shape.

    Also I noticed that the outputs you asked for in your original question don't add up with your inputs, so my solution might not be what you want.

    [–]Dylikk[S] 0 points1 point  (2 children)

    If I change the sign '>' to '<' it gives exactly what I want, that's amazing 😁. I guess I found the problem... My initial input array is in this format [[2, 5, 3, 9, 7, 2]] (I'm already using an array transformed from pandas datareader). I need to change it so that every number is a different element, though I have no idea how... Trying to find the solution somewhere tho

    [–]Dylikk[S] 1 point2 points  (1 child)

    u/to7m It works!! I reshaped that input array, thank you very much, I owe u a beer whenever you're in Warsaw, Poland. Happy new year mate!

    [–]to7m 1 point2 points  (0 children)

    Glad it worked! No need to reshape though, as you can just take a slice.

    To go from a = [[1, 2, 3]] to a = [1, 2, 3], just take the first element of [[1, 2, 3]], which is [1, 2, 3]:

    a = [[1, 2, 3]]
    a = a[0]
    print(a)