我有一個維度矩陣n x d和一個標簽向量1 x n。
n = 3
d = 2
data = np.random.rand((n,d))
labels = np.random.choice(1, n)
我想同時遍歷ith 列data和ith 元素labels。到目前為止,我有:
for i in range(n):
x = data[i]
y = labels[i]
... do something with them ..
我試過np.nditer這樣做,但是很難讓向量和矩陣很好地協同作業:
for x, y in np.nditer([data, labels]):
...
ValueError: operands could not be broadcast together with shapes (3,2) (3,)
uj5u.com熱心網友回復:
In [15]: n = 3
...: d = 2
...: data = np.random.rand(n, d)
...: labels = np.random.choice(10, n)
In [16]: data, labels
Out[16]:
(array([[0.87013539, 0.66778321],
[0.63311902, 0.74640742],
[0.76874321, 0.43470357]]),
array([6, 8, 1]))
直截了當的拉鏈:
In [17]: for i, j in zip(data, labels):
...: print(i, j)
[0.87013539 0.66778321] 6
[0.63311902 0.74640742] 8
[0.76874321 0.43470357] 1
一個作業的nditer(不是我推薦的):
In [18]: for x, y in np.nditer([data, labels[:, None]]):
...: print(x, y)
0.8701353861218606 6
0.6677832101171755 6
0.6331190219218099 8
0.7464074205732978 8
0.7687432095639312 1
0.43470357108767144 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429369.html
上一篇:NumpyWherewithdate-將日期轉換為長整數
下一篇:多個資料集和擬合線
