[英]manipulate tuple into a list of tuples
我的數據集中有 class label 的以下變量:
y = np.array([3, 3, 3, 2, 3, 1, 3, 2, 3, 3, 3, 2, 2, 3, 2])
為了確定每個 class 的數量,我這樣做:
np.unique(y, return_counts=True)
(array([1, 2, 3]), array([1, 5, 9]))
那么我如何將其操作為(label, n_samples)
的元組列表? 這樣我就有了:
[ (1,1), (2,5), (3,9) ]
如果您想要一個簡單的列表,請使用zip
:
out = list(zip(*np.unique(y, return_counts=True)))
Output: [(1, 1), (2, 5), (3, 9)]
或者,您可以創建一個數組:
np.vstack(np.unique(y, return_counts=True)).T
Output:
array([[1, 1],
[2, 5],
[3, 9]])
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)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.