Reputation: 1163
I'm using scikit-learn and I have the faces data from fetch_olivetti_faces. And I have the indices of all the faces that wear glasses in the second array. However, In the first array I have all the faces(whether wearing glasses or not). Now I want to train the model to know if a face is wearing a glass or not.
My question is, I want to change the values in the first array(the target array) that contains all the faces, to false(not wearing glasses) and true(wearing glasses) based on the indices that are given in the second array.
This question is generic, you don't need to know about scikit-learn to answer it. Just knowledge about manipulating arrays.
In the first array I have those elements:
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])
In the second array I have the following elements which contains the indices in the first array with people with glasses:
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]
Now, for example, if I pass the first element in array 2 which is 10 as an index to the first array as this:
faces.target[10]
it would give me an image with person that wears a glass. and the same for the rest, 11, 12 ... and so on.
Now, I want to turn all the elements in the first array into false, except the elements that have an index present in the second array to be true. so :
faces.target[10] = true
faces.target[11] = true
...
faces.target[367] = true
and so on
Upvotes: 0
Views: 780
Reputation: 154
To keep it in numpy space you can do:
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
Credit to How to create a numpy array of all True or all False?.
Upvotes: 0
Reputation: 404
You could use a list comprehension:
glasses_label = [index in glasses for index,_ in enumerate(face_target)]
then you can just convert it to a numpy
array if you need with np.array(glasses_label)
Upvotes: 1
Reputation: 365
Try this:
final_list = []
for index, element in enumerate(faces_target):
final_list.append('True') if index in glasses else final_list.append('False')
Upvotes: 1