How to replace NaN values for image data?












7












$begingroup$


My data set has a total of 200 columns, where each column corresponds to the same pixel in all of my images. In total, I have 48,500 rows. The labels for the data range from 0-9.



The data looks something like this:



raw_0   raw_1   raw_2   raw_3   raw_4
0 120.0 133.0 96.0 155.0 66.0
1 159.0 167.0 163.0 185.0 160.0
2 45.0 239.0 66.0 252.0 NaN
3 126.0 239.0 137.0 NaN 120.0
4 226.0 222.0 153.0 235.0 171.0
5 169.0 81.0 100.0 44.0 104.0
6 154.0 145.0 76.0 134.0 175.0
7 77.0 35.0 105.0 108.0 112.0
8 104.0 55.0 113.0 90.0 107.0
9 97.0 253.0 255.0 251.0 141.0
10 224.0 227.0 84.0 214.0 57.0
11 NaN 13.0 51.0 50.0 NaN
12 82.0 213.0 61.0 98.0 59.0
13 NaN 40.0 84.0 7.0 39.0
14 129.0 103.0 65.0 159.0 NaN
15 123.0 128.0 116.0 198.0 111.0


Each column has around 5% missing values and I want to fill in these NaN
values with something meaningful. However, I'm not sure how to go about this. Any suggestions would be welcome.



Thank you!










share|improve this question











$endgroup$

















    7












    $begingroup$


    My data set has a total of 200 columns, where each column corresponds to the same pixel in all of my images. In total, I have 48,500 rows. The labels for the data range from 0-9.



    The data looks something like this:



    raw_0   raw_1   raw_2   raw_3   raw_4
    0 120.0 133.0 96.0 155.0 66.0
    1 159.0 167.0 163.0 185.0 160.0
    2 45.0 239.0 66.0 252.0 NaN
    3 126.0 239.0 137.0 NaN 120.0
    4 226.0 222.0 153.0 235.0 171.0
    5 169.0 81.0 100.0 44.0 104.0
    6 154.0 145.0 76.0 134.0 175.0
    7 77.0 35.0 105.0 108.0 112.0
    8 104.0 55.0 113.0 90.0 107.0
    9 97.0 253.0 255.0 251.0 141.0
    10 224.0 227.0 84.0 214.0 57.0
    11 NaN 13.0 51.0 50.0 NaN
    12 82.0 213.0 61.0 98.0 59.0
    13 NaN 40.0 84.0 7.0 39.0
    14 129.0 103.0 65.0 159.0 NaN
    15 123.0 128.0 116.0 198.0 111.0


    Each column has around 5% missing values and I want to fill in these NaN
    values with something meaningful. However, I'm not sure how to go about this. Any suggestions would be welcome.



    Thank you!










    share|improve this question











    $endgroup$















      7












      7








      7


      3



      $begingroup$


      My data set has a total of 200 columns, where each column corresponds to the same pixel in all of my images. In total, I have 48,500 rows. The labels for the data range from 0-9.



      The data looks something like this:



      raw_0   raw_1   raw_2   raw_3   raw_4
      0 120.0 133.0 96.0 155.0 66.0
      1 159.0 167.0 163.0 185.0 160.0
      2 45.0 239.0 66.0 252.0 NaN
      3 126.0 239.0 137.0 NaN 120.0
      4 226.0 222.0 153.0 235.0 171.0
      5 169.0 81.0 100.0 44.0 104.0
      6 154.0 145.0 76.0 134.0 175.0
      7 77.0 35.0 105.0 108.0 112.0
      8 104.0 55.0 113.0 90.0 107.0
      9 97.0 253.0 255.0 251.0 141.0
      10 224.0 227.0 84.0 214.0 57.0
      11 NaN 13.0 51.0 50.0 NaN
      12 82.0 213.0 61.0 98.0 59.0
      13 NaN 40.0 84.0 7.0 39.0
      14 129.0 103.0 65.0 159.0 NaN
      15 123.0 128.0 116.0 198.0 111.0


      Each column has around 5% missing values and I want to fill in these NaN
      values with something meaningful. However, I'm not sure how to go about this. Any suggestions would be welcome.



      Thank you!










      share|improve this question











      $endgroup$




      My data set has a total of 200 columns, where each column corresponds to the same pixel in all of my images. In total, I have 48,500 rows. The labels for the data range from 0-9.



      The data looks something like this:



      raw_0   raw_1   raw_2   raw_3   raw_4
      0 120.0 133.0 96.0 155.0 66.0
      1 159.0 167.0 163.0 185.0 160.0
      2 45.0 239.0 66.0 252.0 NaN
      3 126.0 239.0 137.0 NaN 120.0
      4 226.0 222.0 153.0 235.0 171.0
      5 169.0 81.0 100.0 44.0 104.0
      6 154.0 145.0 76.0 134.0 175.0
      7 77.0 35.0 105.0 108.0 112.0
      8 104.0 55.0 113.0 90.0 107.0
      9 97.0 253.0 255.0 251.0 141.0
      10 224.0 227.0 84.0 214.0 57.0
      11 NaN 13.0 51.0 50.0 NaN
      12 82.0 213.0 61.0 98.0 59.0
      13 NaN 40.0 84.0 7.0 39.0
      14 129.0 103.0 65.0 159.0 NaN
      15 123.0 128.0 116.0 198.0 111.0


      Each column has around 5% missing values and I want to fill in these NaN
      values with something meaningful. However, I'm not sure how to go about this. Any suggestions would be welcome.



      Thank you!







      machine-learning python pandas numpy image-preprocessing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 4 at 20:05









      n1k31t4

      7,3462427




      7,3462427










      asked May 4 at 10:23









      Amer FarooqAmer Farooq

      383




      383






















          4 Answers
          4






          active

          oldest

          votes


















          8












          $begingroup$

          Given you have images stretched out as columns in a table with ~48,500 rows, I am assuming you have the raw images that are 220x220 in dimension.



          You can use a function available via OpenCV called inpaint, which will restore missing pixel values (for example black pixels of degraded photos).



          Here is an image example. Top-left shows the image with missing values (in black). Top-right shows just the missing values (the mask). Bottom-left and bottom-right are the final output, comparing two different algorithms for filling the images.



          restored image



          I would suggest trying both methods on your images to see what looks best.



          Have a look at the Documentation for more details on the algorithms themselves. Here is the documentation of the actual function.



          As for code, it will look something like this:



          import opencv as cv    # you will need to install OpenCV

          dst = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)



          • the first argument is your image with missing values

          • the second is the mask, with locations of where missing pixels are, i.e. which pixels should be filled/interpolated.

          • third is the radius around missing pixels to fill

          • fourth is the flag for the algorithm to use (see link above for two alternatives)


          For each image, you can generate the mask with something like this:



          mask = image[image == np.nan]





          share|improve this answer











          $endgroup$













          • $begingroup$
            Thank you for the suggestion! Looks promising.
            $endgroup$
            – Amer Farooq
            May 4 at 16:43



















          2












          $begingroup$

          There are multiple ways to go after this. You can do mean imputation, median imputation, mode imputation or most common value imputation. Calculate one of the above value for either rows or columns depending on how your data is structured. One of the simplest ways to fill Nan's are df.fillna in pandas






          share|improve this answer









          $endgroup$





















            2












            $begingroup$

            for any (x,y) if NAN you can impute to average of surrounding pixels as:



            if((x==0  & y==0):
            return (x+1)+(y+1))/2

            else if(x==x_max & y==y_max):
            return (x-1)+(y-1))/2

            else if(x==0 & y==y_max):
            return (x+1)+(y-1))/2

            else if(x==x_max & y==0):
            return (x-1)+(y+1))/2

            else if(x==0):
            return ((x+1)+(y-1)+(y+1))/3

            else if(x==x_max):
            return ((x-1)+(y-1)+(y+1))/3

            else if(y==0):
            return ((x+1)+(x-1)+(y+1))/3

            else if(y==y_max):
            return ((x-1)+(x+1)+(y-1))/3

            else :
            return ((x-1)+(x+1)+(y-1)+(y+1))/4





            share|improve this answer











            $endgroup$





















              1












              $begingroup$

              If adjacent rows are adjacent pixels that I'd just use the average value of the adjacent pixels. That seems like it would make sense for an image, and would certainly be hard for the human eye to see.






              share|improve this answer









              $endgroup$














                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "557"
                };
                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: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                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%2fdatascience.stackexchange.com%2fquestions%2f51375%2fhow-to-replace-nan-values-for-image-data%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                8












                $begingroup$

                Given you have images stretched out as columns in a table with ~48,500 rows, I am assuming you have the raw images that are 220x220 in dimension.



                You can use a function available via OpenCV called inpaint, which will restore missing pixel values (for example black pixels of degraded photos).



                Here is an image example. Top-left shows the image with missing values (in black). Top-right shows just the missing values (the mask). Bottom-left and bottom-right are the final output, comparing two different algorithms for filling the images.



                restored image



                I would suggest trying both methods on your images to see what looks best.



                Have a look at the Documentation for more details on the algorithms themselves. Here is the documentation of the actual function.



                As for code, it will look something like this:



                import opencv as cv    # you will need to install OpenCV

                dst = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)



                • the first argument is your image with missing values

                • the second is the mask, with locations of where missing pixels are, i.e. which pixels should be filled/interpolated.

                • third is the radius around missing pixels to fill

                • fourth is the flag for the algorithm to use (see link above for two alternatives)


                For each image, you can generate the mask with something like this:



                mask = image[image == np.nan]





                share|improve this answer











                $endgroup$













                • $begingroup$
                  Thank you for the suggestion! Looks promising.
                  $endgroup$
                  – Amer Farooq
                  May 4 at 16:43
















                8












                $begingroup$

                Given you have images stretched out as columns in a table with ~48,500 rows, I am assuming you have the raw images that are 220x220 in dimension.



                You can use a function available via OpenCV called inpaint, which will restore missing pixel values (for example black pixels of degraded photos).



                Here is an image example. Top-left shows the image with missing values (in black). Top-right shows just the missing values (the mask). Bottom-left and bottom-right are the final output, comparing two different algorithms for filling the images.



                restored image



                I would suggest trying both methods on your images to see what looks best.



                Have a look at the Documentation for more details on the algorithms themselves. Here is the documentation of the actual function.



                As for code, it will look something like this:



                import opencv as cv    # you will need to install OpenCV

                dst = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)



                • the first argument is your image with missing values

                • the second is the mask, with locations of where missing pixels are, i.e. which pixels should be filled/interpolated.

                • third is the radius around missing pixels to fill

                • fourth is the flag for the algorithm to use (see link above for two alternatives)


                For each image, you can generate the mask with something like this:



                mask = image[image == np.nan]





                share|improve this answer











                $endgroup$













                • $begingroup$
                  Thank you for the suggestion! Looks promising.
                  $endgroup$
                  – Amer Farooq
                  May 4 at 16:43














                8












                8








                8





                $begingroup$

                Given you have images stretched out as columns in a table with ~48,500 rows, I am assuming you have the raw images that are 220x220 in dimension.



                You can use a function available via OpenCV called inpaint, which will restore missing pixel values (for example black pixels of degraded photos).



                Here is an image example. Top-left shows the image with missing values (in black). Top-right shows just the missing values (the mask). Bottom-left and bottom-right are the final output, comparing two different algorithms for filling the images.



                restored image



                I would suggest trying both methods on your images to see what looks best.



                Have a look at the Documentation for more details on the algorithms themselves. Here is the documentation of the actual function.



                As for code, it will look something like this:



                import opencv as cv    # you will need to install OpenCV

                dst = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)



                • the first argument is your image with missing values

                • the second is the mask, with locations of where missing pixels are, i.e. which pixels should be filled/interpolated.

                • third is the radius around missing pixels to fill

                • fourth is the flag for the algorithm to use (see link above for two alternatives)


                For each image, you can generate the mask with something like this:



                mask = image[image == np.nan]





                share|improve this answer











                $endgroup$



                Given you have images stretched out as columns in a table with ~48,500 rows, I am assuming you have the raw images that are 220x220 in dimension.



                You can use a function available via OpenCV called inpaint, which will restore missing pixel values (for example black pixels of degraded photos).



                Here is an image example. Top-left shows the image with missing values (in black). Top-right shows just the missing values (the mask). Bottom-left and bottom-right are the final output, comparing two different algorithms for filling the images.



                restored image



                I would suggest trying both methods on your images to see what looks best.



                Have a look at the Documentation for more details on the algorithms themselves. Here is the documentation of the actual function.



                As for code, it will look something like this:



                import opencv as cv    # you will need to install OpenCV

                dst = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)



                • the first argument is your image with missing values

                • the second is the mask, with locations of where missing pixels are, i.e. which pixels should be filled/interpolated.

                • third is the radius around missing pixels to fill

                • fourth is the flag for the algorithm to use (see link above for two alternatives)


                For each image, you can generate the mask with something like this:



                mask = image[image == np.nan]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 21 at 20:12

























                answered May 4 at 15:44









                n1k31t4n1k31t4

                7,3462427




                7,3462427












                • $begingroup$
                  Thank you for the suggestion! Looks promising.
                  $endgroup$
                  – Amer Farooq
                  May 4 at 16:43


















                • $begingroup$
                  Thank you for the suggestion! Looks promising.
                  $endgroup$
                  – Amer Farooq
                  May 4 at 16:43
















                $begingroup$
                Thank you for the suggestion! Looks promising.
                $endgroup$
                – Amer Farooq
                May 4 at 16:43




                $begingroup$
                Thank you for the suggestion! Looks promising.
                $endgroup$
                – Amer Farooq
                May 4 at 16:43











                2












                $begingroup$

                There are multiple ways to go after this. You can do mean imputation, median imputation, mode imputation or most common value imputation. Calculate one of the above value for either rows or columns depending on how your data is structured. One of the simplest ways to fill Nan's are df.fillna in pandas






                share|improve this answer









                $endgroup$


















                  2












                  $begingroup$

                  There are multiple ways to go after this. You can do mean imputation, median imputation, mode imputation or most common value imputation. Calculate one of the above value for either rows or columns depending on how your data is structured. One of the simplest ways to fill Nan's are df.fillna in pandas






                  share|improve this answer









                  $endgroup$
















                    2












                    2








                    2





                    $begingroup$

                    There are multiple ways to go after this. You can do mean imputation, median imputation, mode imputation or most common value imputation. Calculate one of the above value for either rows or columns depending on how your data is structured. One of the simplest ways to fill Nan's are df.fillna in pandas






                    share|improve this answer









                    $endgroup$



                    There are multiple ways to go after this. You can do mean imputation, median imputation, mode imputation or most common value imputation. Calculate one of the above value for either rows or columns depending on how your data is structured. One of the simplest ways to fill Nan's are df.fillna in pandas







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 4 at 13:00









                    karthikeyan mgkarthikeyan mg

                    348111




                    348111























                        2












                        $begingroup$

                        for any (x,y) if NAN you can impute to average of surrounding pixels as:



                        if((x==0  & y==0):
                        return (x+1)+(y+1))/2

                        else if(x==x_max & y==y_max):
                        return (x-1)+(y-1))/2

                        else if(x==0 & y==y_max):
                        return (x+1)+(y-1))/2

                        else if(x==x_max & y==0):
                        return (x-1)+(y+1))/2

                        else if(x==0):
                        return ((x+1)+(y-1)+(y+1))/3

                        else if(x==x_max):
                        return ((x-1)+(y-1)+(y+1))/3

                        else if(y==0):
                        return ((x+1)+(x-1)+(y+1))/3

                        else if(y==y_max):
                        return ((x-1)+(x+1)+(y-1))/3

                        else :
                        return ((x-1)+(x+1)+(y-1)+(y+1))/4





                        share|improve this answer











                        $endgroup$


















                          2












                          $begingroup$

                          for any (x,y) if NAN you can impute to average of surrounding pixels as:



                          if((x==0  & y==0):
                          return (x+1)+(y+1))/2

                          else if(x==x_max & y==y_max):
                          return (x-1)+(y-1))/2

                          else if(x==0 & y==y_max):
                          return (x+1)+(y-1))/2

                          else if(x==x_max & y==0):
                          return (x-1)+(y+1))/2

                          else if(x==0):
                          return ((x+1)+(y-1)+(y+1))/3

                          else if(x==x_max):
                          return ((x-1)+(y-1)+(y+1))/3

                          else if(y==0):
                          return ((x+1)+(x-1)+(y+1))/3

                          else if(y==y_max):
                          return ((x-1)+(x+1)+(y-1))/3

                          else :
                          return ((x-1)+(x+1)+(y-1)+(y+1))/4





                          share|improve this answer











                          $endgroup$
















                            2












                            2








                            2





                            $begingroup$

                            for any (x,y) if NAN you can impute to average of surrounding pixels as:



                            if((x==0  & y==0):
                            return (x+1)+(y+1))/2

                            else if(x==x_max & y==y_max):
                            return (x-1)+(y-1))/2

                            else if(x==0 & y==y_max):
                            return (x+1)+(y-1))/2

                            else if(x==x_max & y==0):
                            return (x-1)+(y+1))/2

                            else if(x==0):
                            return ((x+1)+(y-1)+(y+1))/3

                            else if(x==x_max):
                            return ((x-1)+(y-1)+(y+1))/3

                            else if(y==0):
                            return ((x+1)+(x-1)+(y+1))/3

                            else if(y==y_max):
                            return ((x-1)+(x+1)+(y-1))/3

                            else :
                            return ((x-1)+(x+1)+(y-1)+(y+1))/4





                            share|improve this answer











                            $endgroup$



                            for any (x,y) if NAN you can impute to average of surrounding pixels as:



                            if((x==0  & y==0):
                            return (x+1)+(y+1))/2

                            else if(x==x_max & y==y_max):
                            return (x-1)+(y-1))/2

                            else if(x==0 & y==y_max):
                            return (x+1)+(y-1))/2

                            else if(x==x_max & y==0):
                            return (x-1)+(y+1))/2

                            else if(x==0):
                            return ((x+1)+(y-1)+(y+1))/3

                            else if(x==x_max):
                            return ((x-1)+(y-1)+(y+1))/3

                            else if(y==0):
                            return ((x+1)+(x-1)+(y+1))/3

                            else if(y==y_max):
                            return ((x-1)+(x+1)+(y-1))/3

                            else :
                            return ((x-1)+(x+1)+(y-1)+(y+1))/4






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 4 at 13:32









                            Mark.F

                            1,1491822




                            1,1491822










                            answered May 4 at 12:49









                            AnshAnsh

                            191




                            191























                                1












                                $begingroup$

                                If adjacent rows are adjacent pixels that I'd just use the average value of the adjacent pixels. That seems like it would make sense for an image, and would certainly be hard for the human eye to see.






                                share|improve this answer









                                $endgroup$


















                                  1












                                  $begingroup$

                                  If adjacent rows are adjacent pixels that I'd just use the average value of the adjacent pixels. That seems like it would make sense for an image, and would certainly be hard for the human eye to see.






                                  share|improve this answer









                                  $endgroup$
















                                    1












                                    1








                                    1





                                    $begingroup$

                                    If adjacent rows are adjacent pixels that I'd just use the average value of the adjacent pixels. That seems like it would make sense for an image, and would certainly be hard for the human eye to see.






                                    share|improve this answer









                                    $endgroup$



                                    If adjacent rows are adjacent pixels that I'd just use the average value of the adjacent pixels. That seems like it would make sense for an image, and would certainly be hard for the human eye to see.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered May 4 at 12:13









                                    bstrainbstrain

                                    17616




                                    17616






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Data Science Stack Exchange!


                                        • 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.


                                        Use MathJax to format equations. MathJax reference.


                                        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%2fdatascience.stackexchange.com%2fquestions%2f51375%2fhow-to-replace-nan-values-for-image-data%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

                                        Bruad Bilen | Luke uk diar | NawigatsjuunCommonskategorii: BruadCommonskategorii: RunstükenWikiquote: Bruad

                                        Modern approach to radio buttons

                                        A word for worn pool table felt