简体   繁体   English

append 使用与另一个 ndarray 索引匹配的索引到列表的第 i 个值

[英]append ith value to a list using index that matches another ndarray index

I am trying to subsample the cifar100 dataset to train and test on one subclass from each superclass.我正在尝试对 cifar100 数据集进行子采样,以对每个超类的一个子类进行训练和测试。 I have it set up so that if a value in y_full (the subclass label for each image) matches my list of subclasses that I want, the index of that element is used to grab a value from X_full (the images) with the same index.我进行了设置,以便如果 y_full 中的值(每个图像的子类 label)与我想要的子类列表匹配,则该元素的索引用于从 X_full(图像)中获取具有相同索引的值.

This is my code so far:到目前为止,这是我的代码:

from sklearn.model_selection import train_test_split
cifar100 = keras.datasets.cifar100
(X_full, y_full), (X_test_full, y_test_full) = cifar100.load_data(label_mode="fine")

classes = [0,1,2,3,4,5,6,8,9,12,15,22,23,26,27,34,36,41,47,54]

X_tr_full = []
y_tr_full = []
X_test = []
y_test = []

for i in y_full:
  if i in classes:
    X_tr_full.append(X_full[np.where(y_full==i)])
    y_tr_full.append(i)

for i in y_test_full:
  if i in classes:
    X_test.append(X_test_full[np.where(y_test_full==i)])
    y_test.append(i)

The problem with my code is in the np.where(y_full==i) .我的代码的问题在于np.where(y_full==i) This sends back a tuple of ALL of the indices in y_full that have a value that matches a class in my list, which then adds ALL images from X_full with those indices into one entry.这会发回 y_full 中所有索引的元组,其值与我的列表中的 class 匹配,然后将来自 X_full 的所有图像与这些索引添加到一个条目中。 Instead I want to iterate through the entirety of y_full, if the class label matches my class list, I want the index of that element to be used to append the value from X_full with that same index for every value in y_full. Instead I want to iterate through the entirety of y_full, if the class label matches my class list, I want the index of that element to be used to append the value from X_full with that same index for every value in y_full. Sorry if I'm not clear enough, it's hard to explain what I'm trying to do, but hopefully someone can help!抱歉,如果我不够清楚,很难解释我要做什么,但希望有人能提供帮助!

I think I got it figured out.我想我明白了。 It was pretty simple once I figured out how to call each index separate from each other:一旦我弄清楚如何将每个索引彼此分开调用,这非常简单:

for n in range(y_full.size):
  if y_full[n] in classes:
    X_tr_full.append(X_full[n])
for i in y_full:
  if i in classes:
    y_tr_full.append(i)

for n in range(y_test_full.size):
  if y_test_full[n] in classes:
    X_test.append(X_test_full[n])
for i in y_test_full:
  if i in classes:
    y_test.append(i)

To illustrate my comment, I'll use a simple example of modulus testing为了说明我的评论,我将使用一个简单的模数测试示例

In [224]: arr = np.arange(10); alist = []
In [225]: for i in [2,3]:
     ...:     alist.append(arr[arr%i>0])
     ...:     
In [226]: alist
Out[226]: [array([1, 3, 5, 7, 9]), array([1, 2, 4, 5, 7, 8])]

I get a list of arrays, which can be joined into one array with:我得到一个 arrays 的列表,它可以加入一个数组:

In [227]: np.hstack(alist)
Out[227]: array([1, 3, 5, 7, 9, 1, 2, 4, 5, 7, 8])

Alternatively with extend :或者使用extend

In [228]: arr = np.arange(10); alist = []    
In [229]: for i in [2,3]:
     ...:     alist.extend(arr[arr%i>0])
     ...:         
In [230]: alist
Out[230]: [1, 3, 5, 7, 9, 1, 2, 4, 5, 7, 8]    
In [231]: np.array(alist)
Out[231]: array([1, 3, 5, 7, 9, 1, 2, 4, 5, 7, 8])

extend replaces your iterative append . extend替换您的迭代append

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM