我正在使用scikit-learn,并且有fetch_olivetti_faces中的faces数据。我在第二个数组中具有戴眼镜的所有面孔的索引。但是,在第一个数组中,我具有所有面孔(无论是否戴着眼镜)。现在,我想训练模型以了解人脸是否戴着眼镜。
我的问题是,我想根据第二个数组中给出的索引,将包含所有面孔的第一个数组(目标数组)中的值更改为false(不戴眼镜)和true(戴眼镜)。
这个问题是通用的,您不需要了解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
解决方案
尝试这个:
final_list = []
for index, element in enumerate(faces_target):
final_list.append('True') if index in glasses else final_list.append('False')