我一直在將這種方法用于范圍。我無法為固定串列/陣列制定等效方法。
# What I've been using & OUTPUT I'm looking for.
degrees = np.arange(10,50,10)
ITER = np.array(degrees)
for i in range( 4):
x1 = np.sin(np.radians(ITER))
y1 = np.cos(np.radians(ITER ))
XY = np.column_stack((np.asarray(x1),np.asarray(y1)))
print(XY)
錯誤代碼:
# appending to array has seen many failures.
# appended array always prints empty. must have a false assumption
xy1 = np.array([])
degrees = np.array([10, 20, 30, 40])
for degree in np.nditer(degrees):
x1 = np.sin(np.radians(degree))
y1 = np.cos(np.radians(degree))
#np.append(xy1,[x1,y1]).reshape(2,1)
#ugh = np.asarray([x1,y1])
#a = np.append(xy1,[[x1,y1]],axis =0).reshape(2,-1)
#a = np.append(xy1,[[x1],[y1]],axis =0)#.reshape(2,-1)
#np.append(xy1,ugh, axis =0).reshape(2,1)
#np.append(xy1,ugh, axis =0)
#a = np.append(xy1,[ugh])
XY = np.column_stack((np.asarray(x1),np.asarray(y1)))
print(xy1)
# OUTPUT should be same as working example above
事后看來,我會使用串列......但現在我希望以此為學習機會。
更新: @hpaulj提供的
# Iterate through Range
degrees = np.arange(10,50,10)
x1 = np.sin(np.radians(degrees))
y1 = np.cos(np.radians(degrees ))
XY = np.column_stack((x1, y1))
# Iterate through fixed list
degrees = np.array([10, 20, 30, 40])
XY = np.zeros((0,2))
for rad in np.radians(degrees):
XY = np.append(XY, [[np.sin(rad), np.cos(rad)]], axis=0)
我的主要錯誤是將陣列初始化為錯誤的形狀。
XY = np.zeros((0,2))
degrees = np.array([10, 20, 30, 40])
for rads in np.radians(degrees):
x1 = np.sin(rads)
y1 = np.cos(rads)
XY = np.append(XY, [[x1,y1]]).reshape(-1,2)
uj5u.com熱心網友回復:
您的第一個代碼運行;你為什么要寫別的東西?
但我認為你需要更好地理解第一個。讓我們運行它:
In [449]: degrees = np.arange(10,50,10)
...: ITER = np.array(degrees)
...: for i in range( 4):
...: x1 = np.sin(np.radians(ITER))
...: y1 = np.cos(np.radians(ITER ))
...: XY = np.column_stack((np.asarray(x1),np.asarray(y1)))
In [450]: degrees
Out[450]: array([10, 20, 30, 40])
In [451]: ITER
Out[451]: array([10, 20, 30, 40])
arange產生一個陣列(閱讀檔案);那為什么要額外np.array(degrees)打電話呢?它沒有任何改變;它只是制作了另一個副本。
In [452]: XY
Out[452]:
array([[0.17364818, 0.98480775],
[0.34202014, 0.93969262],
[0.5 , 0.8660254 ],
[0.64278761, 0.76604444]])
degrees是(4,)形狀;x1也是,并且XY是 (4,2),連接兩個一維陣列作為列。
為什么要迭代range(4)?只是為了通過重復sin計算讓代碼運行得更慢?你做同樣的事情 4 次,沒有積累任何東西。它只是使用最后一次運行來制作XY. 并且x1已經是一個陣列;為什么要額外np.array(x1)包裝?
In [453]: x1,y1
Out[453]:
(array([0.17364818, 0.34202014, 0.5 , 0.64278761]),
array([0.98480775, 0.93969262, 0.8660254 , 0.76604444]))
In [454]: np.sin(np.radians(ITER))
Out[454]: array([0.17364818, 0.34202014, 0.5 , 0.64278761])
不知道你是粗心大意,還是不了解Python迭代的基礎知識。
這就是您所需要的:
degrees = np.arange(10,50,10)
x1 = np.sin(np.radians(ITER))
y1 = np.cos(np.radians(ITER ))
XY = np.column_stack((x1, y1))
第二次嘗試
我剛剛注意到你使用np.nditer. 為什么?如果要迭代,請直接使用
for degree in degress:
....
nditer不是更快的迭代方式;檔案在這方面可能會產生誤導。它實際上僅用作在cython. python 版本很慢 - 對于大多數用戶來說過于復雜。
正如第一個代碼所示,您不需要迭代來計算所有的 sin/cos degrees。但如果你必須迭代,這里有一個簡單清晰的版本:
In [457]: degrees = np.arange(10,50,10)
...: x1, y1 = [], []
...: for degree in degrees:
...: x1.append(np.sin(np.radians(degree)))
...: y1.append(np.cos(np.radians(degree)))
...: XY = np.column_stack((np.array(x1), np.array(y1)))
In [458]: x1
Out[458]:
[0.17364817766693033,
0.3420201433256687,
0.49999999999999994,
0.6427876096865393]
x1,y1是串列??;list append 比較快,也簡單。 np.append速度慢且難以正確使用。不要使用它(因為nditer它需要更強的免責宣告,甚至可能需要洗掉)。
這是一個np.append有效的迭代版本;我不推薦它,但它說明了如何np.append作業:
In [461]: degrees = np.arange(10,50,10)
...: XY = np.zeros((0,2))
...: for rad in np.radians(degrees):
...: XY = np.append(XY, [[np.sin(rad), np.cos(rad)]], axis=0)
我只做一次np.radians轉換。無需重復或在迭代中執行。
我初始XY為 (0,2) 陣列 - 并在每次迭代時向其添加一個 (1,2) 陣列。 np.append帶軸只是
XY = np.concatenate((XY, [[np.sin...]]), axis=0)
您失敗的嘗試有各種問題。 np.array([])具有形狀 (0,)。您不能將 (2,) 與axis) 連接起來。 np.append回傳一個新陣列;它不能就地作業。你的嘗試都沒有改變xy1。
仔細查看第二個代碼塊,我覺得您只是粗心大意。您混合xy1, x1, y1, a,XY而沒有注意它們可能如何相關或可能不相關。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/535719.html
標籤:Python麻木的
上一篇:帶周期的numpy插值
