我想將點存盤在一個陣列中,但我不確定如何遍歷它。
x = np.dot(weights, corners)
x_points = np.zeros([100, 2])
for i in range(101):
x_points = (x corners[np.random.randint(3)]) / 2
x = x_points
我試影像這樣迭代我的陣列:
x = np.dot(weights, corners)
x_points = np.zeros([100, 2])
for i in range(101):
x_points[i:,] = (x corners[np.random.randint(3)]) / 2
x = x_points
我如何存盤陣列中的每個點?
uj5u.com熱心網友回復:
您的 for 回圈旨在迭代您的點數,但您從未專門從原始x陣列中選擇第 i 行進行處理;相反,您只需在每次迭代中使用整個陣列。這是你應該做的:
for i in range(101):
x_points[i,:] = (x[i,:] ...) / 2
(請注意,如果您愿意,您甚至可以省略第二個索引:x[i]與 相同x[i,:]。)
此外,您在x_points陣列中存盤的索引是錯誤的。我假設你打算x_points[i,:]像我上面那樣寫,而不是x_points[i:,]. 后者沿第一個軸將陣列從第 i 行到末尾切片,因此您的代碼實際上一次修改的不僅僅是一行x_points。
最后,我不明白您為什么要x = x_points在每次回圈迭代中進行分配。這樣,在回圈第一次運行后,您的原始x陣列不再存在。如果要“重命名” finalized x_points,則需要將此行放在 for 回圈之后。
uj5u.com熱心網友回復:
使用 Numpy
假設您只有 10 個點來保持這里的可讀性,我已經展示了如何使用 Numpy 創建陣列并訪問其元素。
>>> weights = np.random.randn(10,4)
>>> corners = np.random.rand(4,2)
>>> x=np.dot(weights, corners)
>>> x
array([[ 0.41183813, 0.18830351],
[ 0.10599174, 0.76246711],
[ 0.50235149, 2.76642114],
[ 0.17072047, 0.67126034],
[-0.25400796, -1.20589651],
[-0.04360992, -0.06102023],
[ 0.0446144 , 0.48355716],
[ 0.8501312 , 1.93899732],
[ 0.44656254, 1.05180096],
[ 0.00397146, 0.19248246]])
>>> x_points = np.zeros([10, 2])
現在您有一組 x_points,初始化為 0.0,以及 x 中權重和角點的點積。
for i in range(len(x_points)):
x_points[i] = (x[i] corners[np.random.randint(3)]) / 2
所有隨機初始化的點,考慮到 x 值和角點都被寫入 x_points。
>>> x_points
array([[ 0.33206919, 0.25078735],
[ 0.17914599, 0.53786916],
[ 0.46472124, 1.74498146],
[ 0.29890573, 0.69740106],
[-0.08596056, -0.17476289],
[ 0.01923846, 0.39767525],
[ 0.14845732, 0.39841418],
[ 0.46610902, 1.39768402],
[ 0.43682676, 0.88767137],
[ 0.04302915, 0.52442659]])
對于簡單的 Python 2D 陣列
您創建一個二維陣列如下:
matrix = [[0 for x in range(num_cols)] for y in range(num_rows)]
然后,您可以像訪問帶有索引的串列一樣訪問它:
matrix[2][3] = 23
for i in range(num_rows):
for j in len(num_cols):
print(f"The element in the {i}th row, {j}th col is {matrix[i][j]}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/352370.html
上一篇:將字符插入字符陣列
