Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy – list of lists to flat array

I am in the processing phase of my machine learning algorithm where I need to see if a cat is going outside or not.
Currently my images are presented by multiple lists like below.
(this is how 1 image is shown, an array containing an array that represents 1 row of pixels in the image)

[[125  71 116 ... 255 255 255]
 [102 128 137 ... 255 255 255]
 [101  96  82 ... 255 255 255]
 ...
 [148 151 149 ...  55  51  49]
 [150 149 147 ...  52  50  49]
 [143 143 147 ...  50  50  50]]

But I want them in this format (Where the inner lists are gone, but you still can show the image):

array([  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   3.,  18.,
        18.,  18., 126., 136., 175.,  26., 166., 255., 247., 127.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
        30.,  36.,  94., 154., 170., 253., 253., 253., 253., 253., 225.,
       172., 253., 242., 195.,  64.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,  49., 238., 253., 253., 253., 253.,
       253., 253., 253., 253., 251.,  93.,  82.,  82.,  56.,  39.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
        18., 219., 253., 253., 253., 253., 253., 198., 182., 247., 241.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,  80., 156., 107., 253.,
       253., 205.,  11.,   0.,  43., 154.,   0.,   0.,   0.,   0.,   0.,
...
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.])

These are the numbers of another picture, so the numbers does not matter, but the format does. Is there a way to get my format of 2 lists in a format like above.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently my fit refuses to work because of this.

This is how I get my images into the lists at the moment.

X = []
y = []
def make_arrays():

  inside = os.path.join("input", "classificatie", "aanwezig")
  outside = os.path.join("input", "classificatie", "buiten")
  nothing = os.path.join("input", "classificatie", "niets")

  images = glob.glob(f"{inside}/*")

  for i in images:
      img = cv2.imread(i)
      x = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      X.append(x)
      y.append("inside")

  images = glob.glob(f"{outside}/*")

  for i in images:
      img = cv2.imread(i)
      x = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      X.append(x)
      y.append("outside")

  images = glob.glob(f"{nothing}/*")

  for i in images:
      img = cv2.imread(i)
      x = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      X.append(x)
      y.append("nothing")
  
  

make_arrays()

>Solution :

Try this:

np.array(x).ravel()

where x is the nested list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading