如何在Python中切片数组

时间:2021-10-02 15:39:41

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;

我正在尝试在python上编写我自己的机器学习脚本(我知道有这样的库,但这纯粹是为了它的乐趣 - 我正在学习Python)。我有以下数组;

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

我的逻辑是,我想在图像文件中读取像素值数组,以用于图像分类问题。从上面的数据可以看出,图像是灰度级的。我想从这些数据中删除一个维度,每个像素只有一个值(例如,([[[5],[6],[6],[12] ...)255只是alpha值(我不关心)。我知道这是我需要使用的数组拼接,但是男孩我不知道如何将它应用于这个问题。

I've tried; loadedImages[:,1]

我试过了; 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,

2 个解决方案

#1


1  

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.

您是否尝试删除拼接中的逗号,如loadedimages [:1]?对于这个数组arr = [5,5,5,255],要删除255,你可以做arr = arr [:3]。对于多维数组,您可以根据需要嵌套for循环,以便迭代各个数组。例如

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.

此外,在Python中,这种数据结构称为列表。这可能是您很难找到相关信息的原因。

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

编辑:您也可以尝试使用列表推导。它在python中非常有用。例如

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]

#2


1  

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

只需使用numpy就可以进行你想要做的扩展切片:

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

在此示例中,:表示数组的所有行,0表示第一列

#1


1  

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.

您是否尝试删除拼接中的逗号,如loadedimages [:1]?对于这个数组arr = [5,5,5,255],要删除255,你可以做arr = arr [:3]。对于多维数组,您可以根据需要嵌套for循环,以便迭代各个数组。例如

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.

此外,在Python中,这种数据结构称为列表。这可能是您很难找到相关信息的原因。

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

编辑:您也可以尝试使用列表推导。它在python中非常有用。例如

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]

#2


1  

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

只需使用numpy就可以进行你想要做的扩展切片:

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

在此示例中,:表示数组的所有行,0表示第一列