Why do I keep getting this error 'RuntimeWarning: overflow encountered in int_scalars'











up vote
3
down vote

favorite
1












I am trying to multiply all the row values and column values of a 2 dimensional numpy array with an explicit for-loop:



product_0 = 1
product_1 = 1
for x in arr:
product_0 *= x[0]
product_1 *= x[1]


I realize the product will blow up to become an extremely large number but from my previous experience python has had no memory problem dealing very very extremely large numbers.

So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.



Any idea how to fix this?



Using non inplace multiplication hasn't helped product_0 = x[0]*product_0










share|improve this question




























    up vote
    3
    down vote

    favorite
    1












    I am trying to multiply all the row values and column values of a 2 dimensional numpy array with an explicit for-loop:



    product_0 = 1
    product_1 = 1
    for x in arr:
    product_0 *= x[0]
    product_1 *= x[1]


    I realize the product will blow up to become an extremely large number but from my previous experience python has had no memory problem dealing very very extremely large numbers.

    So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.



    Any idea how to fix this?



    Using non inplace multiplication hasn't helped product_0 = x[0]*product_0










    share|improve this question


























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I am trying to multiply all the row values and column values of a 2 dimensional numpy array with an explicit for-loop:



      product_0 = 1
      product_1 = 1
      for x in arr:
      product_0 *= x[0]
      product_1 *= x[1]


      I realize the product will blow up to become an extremely large number but from my previous experience python has had no memory problem dealing very very extremely large numbers.

      So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.



      Any idea how to fix this?



      Using non inplace multiplication hasn't helped product_0 = x[0]*product_0










      share|improve this question















      I am trying to multiply all the row values and column values of a 2 dimensional numpy array with an explicit for-loop:



      product_0 = 1
      product_1 = 1
      for x in arr:
      product_0 *= x[0]
      product_1 *= x[1]


      I realize the product will blow up to become an extremely large number but from my previous experience python has had no memory problem dealing very very extremely large numbers.

      So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.



      Any idea how to fix this?



      Using non inplace multiplication hasn't helped product_0 = x[0]*product_0







      python numpy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 17:46









      blue-phoenox

      2,79271435




      2,79271435










      asked Nov 10 at 14:24









      Zero

      183




      183
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).



          When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:



          product_0 = 1
          product_1 = 1
          for x in arr:
          t = int(x[0])
          product_0 = product_0 * t


          and it will not overflow.



          Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:



          I generate first an array that has the same properties of your array:



          arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))


          Then, calculate the geometric mean for each column/row:



          from scipy import stats

          gm_0 = stats.mstats.gmean(arr, axis = 0)
          gm_1 = stats.mstats.gmean(arr, axis = 1)


          gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.



          Hope this solves your problem!






          share|improve this answer























          • The op wanted his array products to be separate , no need to join them
            – Niteya Shah
            Nov 10 at 14:55










          • The text was saying "all the values"
            – giotto
            Nov 10 at 15:21












          • using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
            – Zero
            Nov 10 at 16:11


















          up vote
          0
          down vote













          You say




          So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.




          Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.



          While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.






          share|improve this answer




























            up vote
            0
            down vote













            Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .



            Edit
            After some testing with



            import numpy as np
            x=np.abs(np.random.randn(1000,2)*1000)
            np.max(x)
            prod1=np.dtype('int32').type(1)
            prod2=np.dtype('int32').type(1)
            k=0
            for i,j in x:
            prod1*=i
            prod2*=j
            k+=1
            print(k," ",prod1,prod2)

            1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)


            if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.



            As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that



            from scipy.stats.mstats import gmean
            x=np.abs(np.random.randn(1000,2)*1000)
            print(gmean(x,axis=0))





            share|improve this answer























            • but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
              – Zero
              Nov 10 at 15:16










            • can you try to find out after which value it starts to overflow ?
              – Niteya Shah
              Nov 10 at 15:35










            • i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
              – Zero
              Nov 10 at 16:31













            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',
            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%2f53239890%2fwhy-do-i-keep-getting-this-error-runtimewarning-overflow-encountered-in-int-sc%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).



            When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:



            product_0 = 1
            product_1 = 1
            for x in arr:
            t = int(x[0])
            product_0 = product_0 * t


            and it will not overflow.



            Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:



            I generate first an array that has the same properties of your array:



            arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))


            Then, calculate the geometric mean for each column/row:



            from scipy import stats

            gm_0 = stats.mstats.gmean(arr, axis = 0)
            gm_1 = stats.mstats.gmean(arr, axis = 1)


            gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.



            Hope this solves your problem!






            share|improve this answer























            • The op wanted his array products to be separate , no need to join them
              – Niteya Shah
              Nov 10 at 14:55










            • The text was saying "all the values"
              – giotto
              Nov 10 at 15:21












            • using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
              – Zero
              Nov 10 at 16:11















            up vote
            0
            down vote



            accepted










            Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).



            When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:



            product_0 = 1
            product_1 = 1
            for x in arr:
            t = int(x[0])
            product_0 = product_0 * t


            and it will not overflow.



            Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:



            I generate first an array that has the same properties of your array:



            arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))


            Then, calculate the geometric mean for each column/row:



            from scipy import stats

            gm_0 = stats.mstats.gmean(arr, axis = 0)
            gm_1 = stats.mstats.gmean(arr, axis = 1)


            gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.



            Hope this solves your problem!






            share|improve this answer























            • The op wanted his array products to be separate , no need to join them
              – Niteya Shah
              Nov 10 at 14:55










            • The text was saying "all the values"
              – giotto
              Nov 10 at 15:21












            • using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
              – Zero
              Nov 10 at 16:11













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).



            When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:



            product_0 = 1
            product_1 = 1
            for x in arr:
            t = int(x[0])
            product_0 = product_0 * t


            and it will not overflow.



            Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:



            I generate first an array that has the same properties of your array:



            arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))


            Then, calculate the geometric mean for each column/row:



            from scipy import stats

            gm_0 = stats.mstats.gmean(arr, axis = 0)
            gm_1 = stats.mstats.gmean(arr, axis = 1)


            gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.



            Hope this solves your problem!






            share|improve this answer














            Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).



            When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:



            product_0 = 1
            product_1 = 1
            for x in arr:
            t = int(x[0])
            product_0 = product_0 * t


            and it will not overflow.



            Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:



            I generate first an array that has the same properties of your array:



            arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))


            Then, calculate the geometric mean for each column/row:



            from scipy import stats

            gm_0 = stats.mstats.gmean(arr, axis = 0)
            gm_1 = stats.mstats.gmean(arr, axis = 1)


            gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.



            Hope this solves your problem!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered Nov 10 at 14:50









            giotto

            399




            399












            • The op wanted his array products to be separate , no need to join them
              – Niteya Shah
              Nov 10 at 14:55










            • The text was saying "all the values"
              – giotto
              Nov 10 at 15:21












            • using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
              – Zero
              Nov 10 at 16:11


















            • The op wanted his array products to be separate , no need to join them
              – Niteya Shah
              Nov 10 at 14:55










            • The text was saying "all the values"
              – giotto
              Nov 10 at 15:21












            • using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
              – Zero
              Nov 10 at 16:11
















            The op wanted his array products to be separate , no need to join them
            – Niteya Shah
            Nov 10 at 14:55




            The op wanted his array products to be separate , no need to join them
            – Niteya Shah
            Nov 10 at 14:55












            The text was saying "all the values"
            – giotto
            Nov 10 at 15:21






            The text was saying "all the values"
            – giotto
            Nov 10 at 15:21














            using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
            – Zero
            Nov 10 at 16:11




            using np.shape() my array has a shape of (86507,2).they are integer coordinates in (x,y) fashion and i am trying to get the geometric mean of all the x's and all the y's separately none of the values inside the array should exceed 716 so the only number that should be big is the non numpy normal python variable called product .
            – Zero
            Nov 10 at 16:11












            up vote
            0
            down vote













            You say




            So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.




            Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.



            While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.






            share|improve this answer

























              up vote
              0
              down vote













              You say




              So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.




              Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.



              While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You say




                So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.




                Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.



                While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.






                share|improve this answer












                You say




                So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.




                Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.



                While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 17:54









                user2357112

                146k12149234




                146k12149234






















                    up vote
                    0
                    down vote













                    Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .



                    Edit
                    After some testing with



                    import numpy as np
                    x=np.abs(np.random.randn(1000,2)*1000)
                    np.max(x)
                    prod1=np.dtype('int32').type(1)
                    prod2=np.dtype('int32').type(1)
                    k=0
                    for i,j in x:
                    prod1*=i
                    prod2*=j
                    k+=1
                    print(k," ",prod1,prod2)

                    1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)


                    if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.



                    As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that



                    from scipy.stats.mstats import gmean
                    x=np.abs(np.random.randn(1000,2)*1000)
                    print(gmean(x,axis=0))





                    share|improve this answer























                    • but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                      – Zero
                      Nov 10 at 15:16










                    • can you try to find out after which value it starts to overflow ?
                      – Niteya Shah
                      Nov 10 at 15:35










                    • i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                      – Zero
                      Nov 10 at 16:31

















                    up vote
                    0
                    down vote













                    Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .



                    Edit
                    After some testing with



                    import numpy as np
                    x=np.abs(np.random.randn(1000,2)*1000)
                    np.max(x)
                    prod1=np.dtype('int32').type(1)
                    prod2=np.dtype('int32').type(1)
                    k=0
                    for i,j in x:
                    prod1*=i
                    prod2*=j
                    k+=1
                    print(k," ",prod1,prod2)

                    1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)


                    if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.



                    As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that



                    from scipy.stats.mstats import gmean
                    x=np.abs(np.random.randn(1000,2)*1000)
                    print(gmean(x,axis=0))





                    share|improve this answer























                    • but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                      – Zero
                      Nov 10 at 15:16










                    • can you try to find out after which value it starts to overflow ?
                      – Niteya Shah
                      Nov 10 at 15:35










                    • i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                      – Zero
                      Nov 10 at 16:31















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .



                    Edit
                    After some testing with



                    import numpy as np
                    x=np.abs(np.random.randn(1000,2)*1000)
                    np.max(x)
                    prod1=np.dtype('int32').type(1)
                    prod2=np.dtype('int32').type(1)
                    k=0
                    for i,j in x:
                    prod1*=i
                    prod2*=j
                    k+=1
                    print(k," ",prod1,prod2)

                    1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)


                    if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.



                    As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that



                    from scipy.stats.mstats import gmean
                    x=np.abs(np.random.randn(1000,2)*1000)
                    print(gmean(x,axis=0))





                    share|improve this answer














                    Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .



                    Edit
                    After some testing with



                    import numpy as np
                    x=np.abs(np.random.randn(1000,2)*1000)
                    np.max(x)
                    prod1=np.dtype('int32').type(1)
                    prod2=np.dtype('int32').type(1)
                    k=0
                    for i,j in x:
                    prod1*=i
                    prod2*=j
                    k+=1
                    print(k," ",prod1,prod2)

                    1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)


                    if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.



                    As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that



                    from scipy.stats.mstats import gmean
                    x=np.abs(np.random.randn(1000,2)*1000)
                    print(gmean(x,axis=0))






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 10 at 19:13

























                    answered Nov 10 at 14:58









                    Niteya Shah

                    14418




                    14418












                    • but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                      – Zero
                      Nov 10 at 15:16










                    • can you try to find out after which value it starts to overflow ?
                      – Niteya Shah
                      Nov 10 at 15:35










                    • i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                      – Zero
                      Nov 10 at 16:31




















                    • but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                      – Zero
                      Nov 10 at 15:16










                    • can you try to find out after which value it starts to overflow ?
                      – Niteya Shah
                      Nov 10 at 15:35










                    • i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                      – Zero
                      Nov 10 at 16:31


















                    but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                    – Zero
                    Nov 10 at 15:16




                    but i am not even storing any big numbers in my numpy array the only big number in the program is the product except the product is just a normal python variable not a numpy datatype
                    – Zero
                    Nov 10 at 15:16












                    can you try to find out after which value it starts to overflow ?
                    – Niteya Shah
                    Nov 10 at 15:35




                    can you try to find out after which value it starts to overflow ?
                    – Niteya Shah
                    Nov 10 at 15:35












                    i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                    – Zero
                    Nov 10 at 16:31






                    i put a print(product_0) statement in my for loop and as soon as it runs it gives the aforementioned error for the line product_0 *= x[0] and product_1 *= x[1 after which it prints at first increasingly bigger numbers and then negative and then again positive numbers this cycle repeats for a while after which it prints a whole bunch of zeros. the last non zero number is -2147483648 but the weird thing is there should be no negative numbers the array is actually the coordinates of all the non zero values obtained through np.argwhere(arr1) where arr1 is a seprate numpy array.
                    – Zero
                    Nov 10 at 16:31




















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239890%2fwhy-do-i-keep-getting-this-error-runtimewarning-overflow-encountered-in-int-sc%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python