NumPy的思考……
?
問題:
為什么第一次輸出矩陣形式的數據,第二次輸出list形式的數據?
詳見代碼:
a = np.array([[1, 2], [3, 4]]) print(a) print('ndim :', a.ndim)控制臺輸出:
[[1 2][3 4]] ndim : 2然而,代碼修改一下:
b = np.array([[11, 12], [14, 16, 17]]) print(b) print(type(b))?控制臺輸出:
[list([11, 12]) list([14, 16, 17])] <class 'numpy.ndarray'>答案:
第二次輸出元素 [ [11, 12], [14, 16, 17] ] 在形式上不能用矩陣形式輸出,不對稱。
?
?
問題:
array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0)?
order : {'K', 'A', 'C', 'F'}, optionalSpecify the memory layout of the array. order參數用于:指定數組的內存布局。order的作用體現在哪里?
怎么才能看出數組的內存布局?
?
?答案:
ndarray.flags屬性, 查看ndarray對象的內存信息
C_CONTIGUOUS : True c_contiguous(連續的)F_CONTIGUOUS : False f_contiguousOWNDATA : True own dataWRITEABLE : True writeableALIGNED : True aligned(對齊)WRITEBACKIFCOPY : False write back if copyUPDATEIFCOPY : False update if copy?
?
?
sin():
Parameters----------x : array_likeAngle, in radians (:math:`2 \pi` rad equals 360 degrees).?
2 \pi = 2π
360°等于2π弧度
在數學和物理中,弧度是角的量度單位.它是由國際單位制導出的單位,單位縮寫是rad.(radians)
弧度定義:弧長等于圓半徑的弧所對的圓心角為1弧度
根據定義,一周的弧度數為2πr/r=2π, 360°角=2π弧度,因此,1弧度約為57.3°,即57°17'44.806'',1°為π/180弧度,近似值為0.01745弧度,周角為2π弧度, 平角(即180°角)為π弧度, 直角為π/2弧度.
在具體計算中,角度以弧度給出時,通常不寫弧度單位,直接寫值.最典型的例子是三角函數,如sin 8π、tan (3π/2).
弧長公式:
l rad=nπr/180
在這里,n就是角度數.
?
?
np.repeat()使用技巧
c = np.array([[1, 2, 3]]) c = c.reshape(-1, 3).repeat(2, 0) 輸出c: [[1, 2, 3],[1, 2, 3]] ===================== cc = c.reshape(-1, 3) # -1代表不管行數,只是確定列數為3# repeat用法 d = c.reshape(-1, 3) d.repeat(2, 0) 可以 d.repeat(preats=2, axis=0) 可以 np.repeat(d, 2, 0) 也可以?
?
?
問題:
同樣是order排序,傳入'C','F'不同,則打印不同?
代碼:
import numpy as np a = np.arange(0, 60, 5).reshape((3, 4)) print(a) for x in np.nditer(a,flags=['external_loop'], order='C'):print(x, end=', ')?
控制臺輸出:
[[ 0 5 10 15][20 25 30 35][40 45 50 55]] [ 0 5 10 15 20 25 30 35 40 45 50 55],?
然而,代碼:
import numpy as np a = np.arange(0, 60, 5).reshape((3, 4)) print(a) for x in np.nditer(a,flags=['external_loop'], order='F'):print(x, end=', ')?
控制臺輸出:
[[ 0 5 10 15][20 25 30 35][40 45 50 55]] [ 0 20 40], [ 5 25 45], [10 30 50], [15 35 55],?
答案:
迭代器遍歷對應于每列,并組合為一維數組。(默認)
?
?
問題:
a.T在nditer中迭代不應該輸出
[[0 3][1 4][2 5]]?
嗎?
代碼:
a = np.arange(6).reshape(2, 3) print(a) print(a.T) print(a.T.flags) for x in np.nditer(a.T):print(x, end=', ') print('\n')?
控制臺輸出:
[[0 1 2][3 4 5]][[0 3][1 4][2 5]]C_CONTIGUOUS : FalseF_CONTIGUOUS : TrueOWNDATA : FalseWRITEABLE : TrueALIGNED : TrueWRITEBACKIFCOPY : FalseUPDATEIFCOPY : False0, 1, 2, 3, 4, 5,?
答案:
np.nditer(..., order='K') :
order參數默認‘K’
源碼中,as close sth. as possible : 盡可能靠近
'K' means as close to the order the array elements appear in memory as possible.?
翻譯:'K' 意味著,(順序要)盡可能靠近內存中出現的的數組的元素順序。
?
?
?
?
?
?
?
?
?
?
?
?
=============================================
?
轉載于:https://www.cnblogs.com/daemonFlY/p/10029667.html
總結
以上是生活随笔為你收集整理的NumPy的思考……的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: luogu4407 [JSOI2009]
- 下一篇: MySQL--更新自增列的潜在风险