簡體   English   中英

ctypes-numpy數組沒有形狀?

[英]ctypes - numpy array with no shape?

我正在使用python包裝器來調用c ++ dll庫的函數。 dll庫返回了ctype,我將其轉換為numpy數組

score = np.ctypeslib.as_array(score,1) 

但是,數組沒有形狀?

score
>>> array(-0.019486344729027664)

score.shape
>>> ()

score[0]
>>> IndexError: too many indices for array

如何從分數數組中提取雙精度數?

謝謝。

您可以通過索引[()]訪問0維數組內的數據。

例如, score[()]將檢索數組中的基礎數據。

這個習慣用法實際上是一致的:

# x, y, z are 0-dim, 1-dim, 2-dim respectively
x = np.array(1)
y = np.array([1, 2, 3])
z = np.array([[1, 2, 3], [4, 5, 6]])

# use 0-dim, 1-dim, 2-dim tuple indexers respectively
res_x = x[()]      # 1
res_y = y[(1,)]    # 2
res_z = z[(1, 2)]  # 6

元組似乎是不自然的,因為您不需要在1d和2d情況下顯式使用它們,即y[1]z[1, 2]就足夠了。 該選項不適用於0-dim情況,因此請使用零長度元組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM