What is the expected effect of where in numpy's negative?












2















As far as I understand the documentation of numpy's negative function, its where option allows you to leave some array components unnegated:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1., 2.],
[-3., 4.],
[-5., 6.]])


However, when I try it, it seems that those values are (almost) zeroed instead:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1.00000000e+000, 6.92885436e-310],
[-3.00000000e+000, 6.92885377e-310],
[-5.00000000e+000, 6.92885375e-310]])


So how should I see the where option?










share|improve this question























  • Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

    – John Zwinck
    18 hours ago













  • np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

    – hpaulj
    8 hours ago
















2















As far as I understand the documentation of numpy's negative function, its where option allows you to leave some array components unnegated:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1., 2.],
[-3., 4.],
[-5., 6.]])


However, when I try it, it seems that those values are (almost) zeroed instead:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1.00000000e+000, 6.92885436e-310],
[-3.00000000e+000, 6.92885377e-310],
[-5.00000000e+000, 6.92885375e-310]])


So how should I see the where option?










share|improve this question























  • Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

    – John Zwinck
    18 hours ago













  • np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

    – hpaulj
    8 hours ago














2












2








2








As far as I understand the documentation of numpy's negative function, its where option allows you to leave some array components unnegated:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1., 2.],
[-3., 4.],
[-5., 6.]])


However, when I try it, it seems that those values are (almost) zeroed instead:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1.00000000e+000, 6.92885436e-310],
[-3.00000000e+000, 6.92885377e-310],
[-5.00000000e+000, 6.92885375e-310]])


So how should I see the where option?










share|improve this question














As far as I understand the documentation of numpy's negative function, its where option allows you to leave some array components unnegated:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1., 2.],
[-3., 4.],
[-5., 6.]])


However, when I try it, it seems that those values are (almost) zeroed instead:



>>> import numpy as np
>>> np.negative(np.array([[1.,2.],[3.,4.],[5.,6.]]), where=[True, False])
array([[-1.00000000e+000, 6.92885436e-310],
[-3.00000000e+000, 6.92885377e-310],
[-5.00000000e+000, 6.92885375e-310]])


So how should I see the where option?







python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 18 hours ago









equaegheequaeghe

671724




671724













  • Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

    – John Zwinck
    18 hours ago













  • np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

    – hpaulj
    8 hours ago



















  • Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

    – John Zwinck
    18 hours ago













  • np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

    – hpaulj
    8 hours ago

















Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

– John Zwinck
18 hours ago







Good question. Even a simpler example like np.negative(1., where=False) gives a similar wrong-looking, almost-zero result.

– John Zwinck
18 hours ago















np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

– hpaulj
8 hours ago





np.negative(x, where=[True, False], out=x.copy()) will produce an array like x with selected values negated. where does not work meaningfully without an out.

– hpaulj
8 hours ago












1 Answer
1






active

oldest

votes


















4














The documentation describes where like this:




Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.




Let's try an example using the out parameter:



x = np.ones(3)
np.negative(np.array([4.,5.,6.]), where=np.array([False,True,False]), out=x)


This sets x to [1., -5., 1.], and returns the same.



This makes some amount of sense once you realize that "leave the value in the output alone" literally means the output value is "don't care", rather than "same as the input" (the latter interpretation was how I read it the first time, too).



The problem comes in when you specify where but not out. Apparently the "ufunc machinery" (which is not visible in the implementation of np.negative()) creates an empty output array, meaning the values are indeterminate. So the locations at which where is False will have uninitialized values, which could be anything.



This seems pretty wrong to me, but there was a NumPy issue filed about it last year, and closed. It seems unlikely to change, so you'll have to work around it (e.g. by creating the output array yourself using zeros).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250461%2fwhat-is-the-expected-effect-of-where-in-numpys-negative%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    The documentation describes where like this:




    Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.




    Let's try an example using the out parameter:



    x = np.ones(3)
    np.negative(np.array([4.,5.,6.]), where=np.array([False,True,False]), out=x)


    This sets x to [1., -5., 1.], and returns the same.



    This makes some amount of sense once you realize that "leave the value in the output alone" literally means the output value is "don't care", rather than "same as the input" (the latter interpretation was how I read it the first time, too).



    The problem comes in when you specify where but not out. Apparently the "ufunc machinery" (which is not visible in the implementation of np.negative()) creates an empty output array, meaning the values are indeterminate. So the locations at which where is False will have uninitialized values, which could be anything.



    This seems pretty wrong to me, but there was a NumPy issue filed about it last year, and closed. It seems unlikely to change, so you'll have to work around it (e.g. by creating the output array yourself using zeros).






    share|improve this answer




























      4














      The documentation describes where like this:




      Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.




      Let's try an example using the out parameter:



      x = np.ones(3)
      np.negative(np.array([4.,5.,6.]), where=np.array([False,True,False]), out=x)


      This sets x to [1., -5., 1.], and returns the same.



      This makes some amount of sense once you realize that "leave the value in the output alone" literally means the output value is "don't care", rather than "same as the input" (the latter interpretation was how I read it the first time, too).



      The problem comes in when you specify where but not out. Apparently the "ufunc machinery" (which is not visible in the implementation of np.negative()) creates an empty output array, meaning the values are indeterminate. So the locations at which where is False will have uninitialized values, which could be anything.



      This seems pretty wrong to me, but there was a NumPy issue filed about it last year, and closed. It seems unlikely to change, so you'll have to work around it (e.g. by creating the output array yourself using zeros).






      share|improve this answer


























        4












        4








        4







        The documentation describes where like this:




        Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.




        Let's try an example using the out parameter:



        x = np.ones(3)
        np.negative(np.array([4.,5.,6.]), where=np.array([False,True,False]), out=x)


        This sets x to [1., -5., 1.], and returns the same.



        This makes some amount of sense once you realize that "leave the value in the output alone" literally means the output value is "don't care", rather than "same as the input" (the latter interpretation was how I read it the first time, too).



        The problem comes in when you specify where but not out. Apparently the "ufunc machinery" (which is not visible in the implementation of np.negative()) creates an empty output array, meaning the values are indeterminate. So the locations at which where is False will have uninitialized values, which could be anything.



        This seems pretty wrong to me, but there was a NumPy issue filed about it last year, and closed. It seems unlikely to change, so you'll have to work around it (e.g. by creating the output array yourself using zeros).






        share|improve this answer













        The documentation describes where like this:




        Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.




        Let's try an example using the out parameter:



        x = np.ones(3)
        np.negative(np.array([4.,5.,6.]), where=np.array([False,True,False]), out=x)


        This sets x to [1., -5., 1.], and returns the same.



        This makes some amount of sense once you realize that "leave the value in the output alone" literally means the output value is "don't care", rather than "same as the input" (the latter interpretation was how I read it the first time, too).



        The problem comes in when you specify where but not out. Apparently the "ufunc machinery" (which is not visible in the implementation of np.negative()) creates an empty output array, meaning the values are indeterminate. So the locations at which where is False will have uninitialized values, which could be anything.



        This seems pretty wrong to me, but there was a NumPy issue filed about it last year, and closed. It seems unlikely to change, so you'll have to work around it (e.g. by creating the output array yourself using zeros).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 17 hours ago









        John ZwinckJohn Zwinck

        151k16176288




        151k16176288






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250461%2fwhat-is-the-expected-effect-of-where-in-numpys-negative%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre