2015-09-25 50 views
2

我一直在尝试使用ndarray的numpy ndarray来变换数组的数组。将数组变换为NumPy的ndarray中的示例

这是我的D型:

dt = 'i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,f8,i8,i8,f8,f8,f8,a50,a50,a50,a50' 

这是我的数据:

# data array reduced to one row for sake of readability 
data = [[45608L, 0L, 46115L, 11952L, 11952L, 0, 0, 0, 0, 0, 11951L, 11951L, 46176L, 9.0, 0, 1, 1407340577.0, 1407340577.0, 0, 'Simulation Movement', 'planned', '', ''],] 

我已经尝试了这些方法:

np.array(data, dt) 
np.array([np.array(row, dt) for row in data]) 

但是当我运行这两个我得到:

TypeError: expected a readable buffer object

Buuuuut,如果我叫np.array以与阵列仅包含我行的每个单独的元件,并使用适当的数据类型(这样做是使用具有枚举回路和分裂dt),它的工作原理。是这样的:

for row in data: 
    for index, value in enumerate(row): 
    np.array([value,], dt.split(',')[index]) 

任何想法,请?

回答

1

似乎这个工作,你需要将内部列表转换为元组。示例 -

import numpy as np 

dt = 'i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,f8,i8,i8,f8,f8,f8,a50,a50,a50,a50' 

data = [[45608L, 0L, 46115L, 11952L, 11952L, 0, 0, 0, 0, 0, 11951L, 11951L, 46176L, 9.0, 0, 1, 1407340577.0, 1407340577.0, 0, 'Simulation Movement', 'planned', '', ''],] 

result = np.array(map(tuple, data),dt) 

演示运行here。但有了这个,你可以得到一个1元素的数组012返回shape = (1,)(1元素是元组)。


您还可以使用'object'为D型,Example -

result1 = np.array(data,'object') 

即使这样做导致与正确形状的阵列,有些事情可能不是因为混合类型的工作(但我猜你期望的)。