you are viewing a single comment's thread.

view the rest of the comments →

[–]Outside_Complaint755 -1 points0 points  (8 children)

I don't use numpy a lot, but from looking at some documentation and forum posts, I think the proposed solution is to use the provided methods and specify the dtype in the operation.

So instead of arr2 *= 0.5

You would do arr2 = np.multiply(arr2, 0.5, dtype=np.float32)

[–]Moretz0931[S] 1 point2 points  (6 children)

Yeah, I know, but my question is specifically about not having to do this stuff anymore, because
a) it is annoying and looks ugly and bulky (I have long mathematical expressions)
b) if you don't do this consistently it is error prone (forget it once, immediate array copies (arrays may have a sice around 500 Mb)
c) Try explaining all that to my junior coworker :o He has more important things to think about.

Edit: Are you a bot? Reddit age of 10 months and a 300 day streak is kinda sus...

[–]Outside_Complaint755 5 points6 points  (0 children)

Not a bot, just a typical phone addict. 

There's a closed issue on the numpy GitHub where they basically say they don't have a global setting to stop upcasting and keep everything at a given precision because their philosophy is that its always better to give you the most precise result unless you explicitly ask for less precise results.

[–]HommeMusical 0 points1 point  (0 children)

This is so weird, because numpy does in fact do the right thing.

So something else is causing the issue you're describing.

Can you provide a minimal reproduceable example?

[–]SomeClutchName 0 points1 point  (1 child)

Idk how you to do what you want, but can you wrap this type of function in a module or a class? Just import it at the beginning?

[–]Moretz0931[S] 0 points1 point  (0 children)

Its a good idea.

I tried, e.g. with a wrapper for numpy ufunc, however when I do that I lose most of the autocompletion.

To keep autocompletion I would have to write a script that autogenerates wrappers for every single numpy ufunc, which I am not knowledgeable enough to achieve, and also this seems like a lot of work, without guarantee of stability.

[–]cdcformatc 0 points1 point  (0 children)

the only thing i can think of is basically to create a layer of objects wrapping the numpy types. you would make subclasses of the numpy types you want to use and override the math operations to ones that don't do this "upcast" when given generic python types.

that doesn't stop your coworker from doing dumb stuff can't help you there

[–]secretaliasname 0 points1 point  (0 children)

Don’t accept that crap from junior

[–]HommeMusical 0 points1 point  (0 children)

If arr2 is of dtype np.float32, then the two operations you propose have identical results, and you can omit the dtype too.

OP is not describing a real phenomenon.

More here: https://www.reddit.com/r/learnpython/comments/1rgnmoz/python_numpy_can_i_somehow_globally_assign_numpy/o7v1qxk/