繁体   English   中英

从字典创建多维数组(D> 5)..?

[英]Creating a multidimensional array (D>5) from a dictionary..?

我正在尝试使用不同长度的矢量构建一个多维数组来绘制问题的“过程空间”。 我首先将值存储在字典的键中:

d = {'width' : [1,2,3,5,3,5,3],
 'height' : [1,2,3,5,5,3],
 'length' : [1,3,3,7,8,0,0,7,2,3,6,3,2,3],
 'composition' : [1,2,3,5,5,3],
 'year' : [7,5,3,2,1,6,4,9,11],
 'efficiency' : [1,1,2,3,5,8,13,21,34]}

是否可以使用这些键来构造大小的多维(6D)矩阵

(7,6,14,6,9,9) (也就是说,每个字典键将表示为最终数组的单独维度)

编辑:我想使用此矩阵作为查看数据的横截面的一种方法。 例如,我希望能够说,“这里的所有效率值都是'长度'的函数,给出:

width = 4
height = 2
composition = 3
year = 7

我认为您将列命名为维度。

由于您有索引和数据,请使用pandas DataFrames

from pandas import Series, DataFrame
d = {'width' : [1,2,3,5,3,5,3],
 'height' : [1,2,3,5,5,3],
 'length' : [1,3,3,7,8,0,0,7,2,3,6,3,2,3],
 'composition' : [1,2,3,5,5,3],
 'year' : [7,5,3,2,1,6,4,9,11],
 'efficiency' : [1,1,2,3,5,8,13,21,34]}

由于缺少数据,您需要一个中间步骤,直到您可以将其转换为DataFrame。

intermediate=dict()
for x in d:
    intermediate[x]=Series(d[x])

data=DataFrame(intermediate)

然后您可以使用普通的pandas语法查询数据。

data[data.length>5]

    composition  efficiency  height  length  width  year
3             5           3       5       7      5     2
4             5           5       5       8      3     1
7           NaN          21     NaN       7    NaN     9
10          NaN         NaN     NaN       6    NaN   NaN

基本原则

简单而有效的方法是使用NumPy

d = {'width' : [1,2,3,5,3,5,3],
     'height' : [1,2,3,5,5,3],
     'length' : [1,3,3,7,8,0,0,7,2,3,6,3,2,3],
     'composition' : [1,2,3,5,5,3],
     'year' : [7,5,3,2,1,6,4,9,11],
     'efficiency' : [1,1,2,3,5,8,13,21,34]}

您需要订购您的名字:

names = ['width' ,'height', 'length' ,'composition', 'year','efficiency']

导入NumPy:

import numpy as np

找到形状:

shape = tuple(len(d[name]) for name in names)

shape是:

(7, 6, 14, 6, 9, 9)

创建一个零数组:

lookup = np.zeros(shape, dtype=np.uint16)

我使用非常小的无符号整数来节省空间。 如果需要,您可以使用更大的数字:

现在lookup可以像这样使用:

>>> lookup[0, 0, 0, 0, 0, 0]
0
>>> lookup[0, 0, 0, 0, 0, 0] = 12
>>> lookup[0, 0, 0, 0, 0, 0]
12

查找efficiency所有值:

>>> lookup[0, 0, 0, 0, 0, :]
array([12,  0,  0,  0,  0,  0,  0,  0,  0], dtype=uint16)

yearefficiency所有值:

>>> lookup[0, 0, 0, 0, :, :]
array([[12,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
   [ 0,  0,  0,  0,  0,  0,  0,  0,  0]], dtype=uint16)

有用的课程

为方便起见,包装成一个类:

class Lookup(object):
    def __init__(self, dims, dtype=np.uint16):
        self.names = [item[0] for item in dims]
        self.shape = [item[1] for item in dims]
        self.repr = np.zeros(self.shape, dtype=dtype)

    def _make_loc(self, coords):
        return [coords.get(name, slice(None)) for name in self.names]

    def get_value(self, coords):
        return self.repr.__getitem__(self._make_loc(coords))

    def set_value(self, coords, value):
        return self.repr.__setitem__(self._make_loc(coords), value)

指定尺寸:

dims = [('width', 7),
        ('year', 9),
        ('composition', 6),
        ('height', 6),
        ('efficiency', 9),
        ('length', 14)]

制作一个实例:

lookup = Lookup(dims)

设置一个值:

coords1 = {'width': 3,
          'height': 1,
          'composition': 2,
          'year': 6, 
          'length': 3}

lookup.set_value(coords1, 11)

获取价值:

coords2 = {'width': 3,
          'height': 1,
          'composition': 2,
          'year': 6}

lookup.get_value(coords2)

给你 :

array([[ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]], dtype=uint16)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM