我有一些無法理解爲什麼下面不工作:numpy的D型錯誤 - (結構數組創建)
np.dtype(dict(names="10", formats=np.float64))
我一直在苦苦掙扎,是因爲我想獲得的recfunctions
功能numpy
到工作,但由於與numpy.dtype
問題,我沒有成功。這是我目前收到的錯誤:
dtype = np.dtype(dict(names=names, formats=formats))
ValueError: all items in the dictionary must have the same length.
我想要得到的數據結構將包含每個分配領域中的一個類型記錄陣列與多列數據的 - 類似於字典,其中每個值是一個二維數組或幾列數據。通常情況下,數據可能最終爲〜6列,每個密鑰或記錄約〜2000行,約200條記錄。
這是我曾嘗試在一個完整的腳本:(雖然仍給予同樣的錯誤)
import numpy as np
from numpy.lib import recfunctions
# Just function to make random data
def make_data(i, j):
# some arbitrary function to show that the number of columns may change, but rows stay the same length
if i%3==0:
data = np.array([[i for i in range(0,1150)]*t for t in range(0,3)])
else:
data = np.array([[i for i in range(0,1150)]*t for t in range(0,6)])
return data
def data_struct(low_ij, high_ij):
"""
Data Structure to contain several columns of data for different combined values between "low ij" and "high ij"
Key: "(i, j)"
Value: numpy ndarray (multidimensional)
"""
for i in range(0,low_ij+1):
for j in range(0,high_ij+1):
# Get rid of some of the combinations
# (unimportant)
if(i<low_ij and j<low_ij):
break
elif(i<j):
break
# Combinations of interest to create structure
else:
names = str(i)+str(j)
formats = np.float64
data = np.array(make_data(i, j))
try:
data_struct = recfunctions.append_fields(base=data_struct, names=names, data=data, dtypes=formats)
# First loop will assign data_struct using this exception,
# then proceed to use the try statement to add on the rest of the data
except UnboundLocalError:
dtype = np.dtype(dict(names=names, formats=formats))
data_struct = np.array(data, dtype=dtype)
return data_struct