3 ms·
Here, I'll use logical indexing to pull out all the positive even numbers from this array of random integers: Numpy: a = np.random.random_integers(low = -10
by oiuswv 11y ago
Here, I'll use logical indexing to pull out all the positive even numbers from this array of random integers:
Numpy:
a = np.random.random_integers(low = -100, high = 100, size = (100,))
a[np.logical_and(a % 2 == 0, a > 0)
(or a[(a % 2 == 0) * (a > 0)])
Logical indexing is used everywhere in numerical computing. Using functions like logical_and() or boolean multiplication or addition is more difficult to follow than Matlab or Julia.
Julia:
a = rand(-100:100, 100)
a[(a % 2 .== 0) & (a .> 0)]
Well, that's beautiful. Element-wise operations are prefixed with "."
- jdreaver 11y agoYou have made a very verbose numpy version :) Here is a slightly better one: a = np.random.randint(-100, 100, size=100) a[(a & 2 == 0) & (a > 0)] Python does indeed have logical boolean operators. I think this is clearer than your Julia example because: - The numpy version makes it clear that you are using random integers, not floating point values. - The keyword argument for "size" makes it clear what that second 100 is for. (The use of the first two numbers, -100 and 100, is pretty clear from context.) - In numpy, most operations are element-wise by default, because the result would be ambiguous or not useful otherwise. This removes the line noise of the extra "." before operations. Don't get me wrong, I think Julia is awesome. I just think you've constructed a very poor example for numpy.
- timholy 11y ago> The numpy version makes it clear that you are using random integers, not floating point values. That's also completely clear in the Julia version, if you learn a little Julia. > In numpy, most operations are element-wise by default, because the result would be ambiguous or not useful otherwise. This is why I think the Julia approach is better. If I write `a == 7`, am I testing whether `a` is 7 or whether any of the elements of `a` are 7?
- jdreaver 11y ago> That's also completely clear in the Julia version, if you learn a little Julia. In every language I've used, the default is for a "rand" function to return random floats between 0 and 1, and given arguments it returns floats between the arguments. I don't think it has to do with learning Julia, it is just that including "integer" in the function name makes it clear the function returns integers. > This is why I think the Julia approach is better. If I write `a == 7`, am I testing whether `a` is 7 or whether any of the elements of `a` are 7? I think this is more of a comment about mixing arrays and scalars in a dynamic language. I made my comment assuming you are performing operations on arrays. If you are comparing two arrays, I think the default of element-wise operations makes more sense.
- KenoFischer 11y agoIn julia the rand function is more general in that it samples from a distribution, which you can pass as the first argument (defaulting to uniform on [0,1]). Since the first argument is an integer range, you get an integer value.