【问题标题】:How to change values in the array to true and false based on another array's information如何根据另一个数组的信息将数组中的值更改为真假
【发布时间】:2019-06-25 21:10:26
【问题描述】:

我正在使用 scikit-learn,并且我有来自 fetch_olivetti_faces 的人脸数据。我有第二个数组中所有戴眼镜的面孔的索引。但是,在第一个数组中,我有所有的面孔(无论是否戴眼镜)。现在我想训练模型知道一张脸是否戴了玻璃。

我的问题是,我想根据在第二个数组。

这个问题是通用的,你不需要知道 scikit-learn 来回答它。只是关于操作数组的知识。

在第一个数组中我有这些元素:

faces.target
array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,
        3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  5,
        5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,
        6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,
        8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10,
       10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
       11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,
       13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15,
       15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
       17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
       18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20,
       20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
       22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
       23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
       25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27,
       27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28,
       28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30,
       30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32,
       32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
       34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35,
       35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37,
       37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39,
       39, 39, 39, 39, 39, 39, 39, 39, 39])

在第二个数组中,我有以下元素,其中包含第一个数组中戴眼镜的人的索引:

print(glasses)
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31, 32, 37, 38, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 63, 64, 69, 120, 121, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 160, 161, 164, 165, 166, 167, 168, 169, 180, 181, 182, 185, 189, 190, 191, 192, 194, 196, 197, 198, 199, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369]

现在,例如,如果我将数组 2 中的第一个元素 10 作为索引传递给第一个数组,如下所示:

faces.target[10]

它会给我一个戴眼镜的人的图像。其余的也一样,11、12……等等。

现在,我想将第一个数组中的所有元素都设置为 false,除了第二个数组中存在索引的元素为 true。所以:

faces.target[10] = true
faces.target[11] = true 
...
faces.target[367] = true
and so on

【问题讨论】:

  • 分享一个数据示例,您对代码的尝试将非常有帮助,请考虑分享相同的内容

标签: python arrays scikit-learn


【解决方案1】:

试试这个:

final_list = []

for index, element in enumerate(faces_target):
   final_list.append('True') if index in glasses else final_list.append('False')

【讨论】:

    【解决方案2】:

    您可以使用列表推导:

    glasses_label  = [index in glasses for index,_ in enumerate(face_target)]
    

    如果您需要 np.array(glasses_label),您可以将其转换为 numpy 数组

    【讨论】:

      【解决方案3】:

      要将其保存在 numpy 空间中,您可以这样做:

      import numpy as np
      
      all_faces=np.zeros(4)
      is_face_glassed=np.full((4), False)
      glassed_faces=np.array([1,3])
      for glassed_face in glassed_faces:
          is_face_glassed[glassed_face]=True
      

      归功于How to create a numpy array of all True or all False?

      【讨论】:

        猜你喜欢
        • 2014-06-27
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多