【问题标题】:Creating a multidimensional array (D>5) from a dictionary..?从字典创建一个多维数组(D>5)..?
【发布时间】:2016-03-18 19:39:07
【问题描述】:

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

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

【问题讨论】:

  • 一个大小为 (7,6,14,6,9,9) 的矩阵将有 7*7*6*14*6*9*9 个条目。
  • @imp9 -- 编辑了我原来的问题来回答你的问题。
  • 你有足够的值来填充一个 6 维矩阵吗?

标签: python arrays dictionary multidimensional-array


【解决方案1】:

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

既然您有索引和数据,请使用 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

【讨论】:

    【解决方案2】:

    基本原理

    最简单、最有效的方法是使用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)
    

    【讨论】:

    • @A.Pung 这就是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多