如何根据另一个数组的信息将数组中的值更改为 true 和 false



我正在使用scikit learn,我有来自fetch_olivetti_faces的人脸数据。我有第二个阵列中所有戴眼镜的人脸的索引。然而,在第一个阵列中,我有所有的脸(无论是否戴眼镜)。现在我想训练模特知道一张脸上是否戴着眼镜。

我的问题是,我想根据第二个数组中给出的索引,将包含所有人脸的第一个数组(目标数组)中的值更改为false(不戴眼镜)和true(戴眼镜)。

这个问题是一般性的,你不需要了解scikit,学会回答它。只需要了解操作数组的知识。

在第一个数组中,我有以下元素:

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

试试这个:

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

您可以使用列表理解:

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

如果需要np.array(glasses_label),则可以将其转换为numpy阵列

要将其保存在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

这要归功于如何创建一个全True或全False的numpy数组?。

相关内容

  • 没有找到相关文章

最新更新