1

I'm trying to write my own machine learning scripts on python (I know there are libraries for that but this is purely for the fun of it - I'm in the process of learning Python). I have the following array;

[array([[[  5,   5,   5, 255],
    [  6,   6,   6, 255],
    [  6,   6,   6, 255],
    ..., 
    [ 12,  12,  12, 255],
    [ 10,  10,  10, 255],
    [ 10,  10,  10, 255]],

   [[  8,   8,   8, 255],
    [ 10,  10,  10, 255],
    [ 14,  14,  14, 255],
    ..., 
    [ 15,  15,  15, 255],
    [ 13,  13,  13, 255],
    [ 13,  13,  13, 255]],

It continues on like this for some time. I've got this array using the follow code:

imagesList = listdir("someaddress")
loadedImages = []
for image in imagesList:
    #img = PImage.open()
    loadedImages.append(misc.imread("someaddress" + image))

My logic here is I want to read in image files as arrays of pixel values for use in a image classification problem. As you can tell from the data above, the images are grey scale. I'd like to remove a dimension from this data and just have a single value for each pixel (eg, ([[[5],[6],[6],[12]...) The 255's are just the alpha values (which I don't care about). I know this is array splicing that I need to use, but boy do I have no idea how to apply it to this problem.

I've tried; loadedImages[:,1]

I get the following error;

TypeError: list indices must be integers, not tuple

The result I really want out of this would look as follows

[array([[  5,
  6,
  6,
..., 
 12,
 10,
 10],

 [  8,
   10,
   14,
    ..., 
   15,
   13,
   13,
user6916458
  • 400
  • 2
  • 7
  • 19

2 Answers2

2

Have you tried removing the comma on your splice, like loadedimages[:1]? For this array arr = [ 5, 5, 5, 255], to remove the 255, you could do arr = arr[:3]. For multi-dimensional arrays, you could nest the for loops as needed so you could iterate over the individual arrays. e.g.

main_array = [[  5,   5,   5, 255],
              [  6,   6,   6, 255],
              [  6,   6,   6, 255]]

for sub_array in main_array:
  spliced_array = sub_array[:3]
  print spliced_array

will yield

[5, 5, 5]
[6, 6, 6]
[6, 6, 6]

Also, in Python, this kind of data structure is called a list. It might be the reason why you're having a hard time finding information about it.

Edit: You could try using list comprehensions as well. It's something that's really useful in python. e.g.

arr = [[  5,   5,   5, 255],
       [  6,   6,   6, 255],
       [  6,   6,   6, 255]]

filtered_arr = [a[0] for a in arr]
filtered_arr
>>>[5,6,6]
jabargas
  • 210
  • 1
  • 3
  • Hey, thanks for the input. All I really want is one number in each row (eg. 5... 6.... 6...) no need to have it all 3 times! – user6916458 Sep 27 '17 at 17:11
  • if that's the case, you could just do arr[:1] or simply arr[0]. It will get the first element of the list. Just use the same logic in the example I gave. – jabargas Sep 27 '17 at 17:14
  • Thanks. How do I then put this back into an array of the same format? I still want the arrays to be of the same form just with the inner most array replaced by a number. – user6916458 Sep 27 '17 at 17:17
2

just use numpy and you can do the kind of extended slicing you want to do:

import numpy as np
a = np.array([[  5,   5,   5, 255],
 [  6,   6,   6, 255],
 [  6,   6,   6, 255]])
# first column:
print(a[:, 0])

in this example, the : represents all rows of the array and the 0 represents the first column

acushner
  • 9,595
  • 1
  • 34
  • 34
  • I have one extra set of square brackets at the start, how would that change the syntax of the *slicing*? :) – user6916458 Sep 27 '17 at 17:23
  • fair question. basically, the ordering goes from outer to inner, so to get the first column, you would want to do `a[:, :, 0]` – acushner Sep 27 '17 at 17:35
  • @acuchner that gives me the error stated in the question (TypeError: list indices must be integers, not tuple) – user6916458 Sep 27 '17 at 18:57
  • you have a list of arrays, if you want to access all of them, make the outermost list an array as well. you'll probably need something like `arrays[:, :, :, 0]` in this case – acushner Sep 27 '17 at 19:02
  • I've made it an array of arrays now (it's initialised with - loadedImages = np.empty(0, dtype=object)) But still no luck. IndexError: too many indices for array – user6916458 Sep 27 '17 at 22:17