I have the following variable for class label in my dataset:
y = np.array([3, 3, 3, 2, 3, 1, 3, 2, 3, 3, 3, 2, 2, 3, 2])
To determine the number of each class, I do:
np.unique(y, return_counts=True)
(array([1, 2, 3]), array([1, 5, 9]))
How then do I manipulate this into a list of tuples for (label, n_samples)
? So that I have:
[ (1,1), (2,5), (3,9) ]
CodePudding user response:
If you want a simple list, use zip
:
out = list(zip(*np.unique(y, return_counts=True)))
Output: [(1, 1), (2, 5), (3, 9)]
Alternatively, you can create an array with:
np.vstack(np.unique(y, return_counts=True)).T
Output:
array([[1, 1],
[2, 5],
[3, 9]])
CodePudding user response:
list_1 = ['a', 'b', 'c']
list_2 = [1, 2, 3]
# option 1
list_of_tuples = list(
map(
lambda x, y: (x, y),
list_1,
list_2
)
)
#option 2
list_of_tuples = [
(list_1[index], list_2[index]) for index in range(len(list_1))
]
# option 3
list_of_tuples = list(zip(list_1, list_2))
print(list_of_tuples)
# output is [('a', 1), ('b', 2), ('c', 3)]