【问题标题】:Generate matrix from series taking N items at a time?从一次取 N 个项目的系列中生成矩阵?
【发布时间】:2019-11-12 08:11:46
【问题描述】:

我被困在一个问题陈述中,其中我有如下所示的数据:-

数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

现在我想生成如下图所示的矩阵。这是一个有 10 列的矩阵,每行有 10 个先前的值:-

我找不到像这样将序列转换为矩阵的任何最佳或内置方法。任何人都可以帮助我实现这一目标吗?

【问题讨论】:

    标签: python pandas matrix


    【解决方案1】:

    比@jezrael 的技术低得多的方法是使用两个列表推导的组合,如果需要,可以轻松转换为dataframe

    first = [[j if j <= row else 0 for j in range(1, 11)] for row in a[:9]]
    second = [[j for j in range(row - 9, row + 1)] for row in a[9:]]
    first + second
    

    输出:

    [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
     [1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
     [1, 2, 3, 4, 0, 0, 0, 0, 0, 0],
     [1, 2, 3, 4, 5, 0, 0, 0, 0, 0],
     [1, 2, 3, 4, 5, 6, 0, 0, 0, 0],
     [1, 2, 3, 4, 5, 6, 7, 0, 0, 0],
     [1, 2, 3, 4, 5, 6, 7, 8, 0, 0],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
     [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
     [4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
     [5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
     [6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
     [7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
     [8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
     [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
     [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]
    

    【讨论】:

    • @coder 我看到了,但它看起来像是一个错字,因为如果第二行有 1 和 2,那么全为零是没有意义的。
    【解决方案2】:

    我相信您需要 strides0 而不是 NaNs 和 DataFrame 构造函数:

    df = pd.DataFrame({'data':range(1, 21)})
    #print (df)
    
    n = 10
    x = np.concatenate([[0] * (n), df['data'].values])
    
    def rolling_window(a, window):
        shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
        strides = a.strides + (a.strides[-1],)
        return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
    
    arr = rolling_window(x, n)
    arr = np.concatenate([arr[:10, ::-1], arr[10:]])
    

    arr = pd.DataFrame(arr)
    print (arr)
         0   1   2   3   4   5   6   7   8   9
    0    0   0   0   0   0   0   0   0   0   0
    1    1   0   0   0   0   0   0   0   0   0
    2    2   1   0   0   0   0   0   0   0   0
    3    3   2   1   0   0   0   0   0   0   0
    4    4   3   2   1   0   0   0   0   0   0
    5    5   4   3   2   1   0   0   0   0   0
    6    6   5   4   3   2   1   0   0   0   0
    7    7   6   5   4   3   2   1   0   0   0
    8    8   7   6   5   4   3   2   1   0   0
    9    9   8   7   6   5   4   3   2   1   0
    10   1   2   3   4   5   6   7   8   9  10
    11   2   3   4   5   6   7   8   9  10  11
    12   3   4   5   6   7   8   9  10  11  12
    13   4   5   6   7   8   9  10  11  12  13
    14   5   6   7   8   9  10  11  12  13  14
    15   6   7   8   9  10  11  12  13  14  15
    16   7   8   9  10  11  12  13  14  15  16
    17   8   9  10  11  12  13  14  15  16  17
    18   9  10  11  12  13  14  15  16  17  18
    19  10  11  12  13  14  15  16  17  18  19
    20  11  12  13  14  15  16  17  18  19  20
    

    【讨论】:

      【解决方案3】:

      使用 numpy 的另一种解决方案:

      x = np.ones([10,10]) * np.arange(1,11)
      y = np.tril(x,0)
      y[0,0]=0
      z = np.ones(10) * np.arange(1,11).reshape(10,1)
      res = np.concatenate((y,x+z),axis=0)
      

      输出:

      >>> x = np.ones([10,10]) * np.arange(1,11)
      >>> y = np.tril(x,0)
      >>> y[0,0]=0
      >>> z = np.ones(10) * np.arange(1,11).reshape(10,1)
      >>> res = np.concatenate((y,x+z),axis=0)
      >>> res
      array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  0.,  0.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  0.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  6.,  0.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  0.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  0.],
             [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
             [ 2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.],
             [ 3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.],
             [ 4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.],
             [ 5.,  6.,  7.,  8.,  9., 10., 11., 12., 13., 14.],
             [ 6.,  7.,  8.,  9., 10., 11., 12., 13., 14., 15.],
             [ 7.,  8.,  9., 10., 11., 12., 13., 14., 15., 16.],
             [ 8.,  9., 10., 11., 12., 13., 14., 15., 16., 17.],
             [ 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.],
             [10., 11., 12., 13., 14., 15., 16., 17., 18., 19.],
             [11., 12., 13., 14., 15., 16., 17., 18., 19., 20.]])
      

      【讨论】:

        【解决方案4】:
        import numpy as np
        
        zeros = np.zeros((20,10))
        numbers = np.arange(1,21)
        for index, value in enumerate(numbers):
            if index < 10:
                arr = np.pad(numbers[:index],(0, 10-index), mode='constant')
            else:
                arr = numbers[index-10:index]
            zeros[index] = arr
        print(numbers)
        

        这给出了:

        zeros...
        array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  0.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  0.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  6.,  0.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  0.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  0.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  0.],
               [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
               [ 2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.],
               [ 3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.],
               [ 4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.],
               [ 5.,  6.,  7.,  8.,  9., 10., 11., 12., 13., 14.],
               [ 6.,  7.,  8.,  9., 10., 11., 12., 13., 14., 15.],
               [ 7.,  8.,  9., 10., 11., 12., 13., 14., 15., 16.],
               [ 8.,  9., 10., 11., 12., 13., 14., 15., 16., 17.],
               [ 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.],
               [10., 11., 12., 13., 14., 15., 16., 17., 18., 19.]])
        

        它与您的数组略有不同,但我认为这是您想要的?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-15
          • 2014-02-26
          • 2014-10-16
          • 1970-01-01
          • 2020-10-12
          • 1970-01-01
          • 2018-10-13
          相关资源
          最近更新 更多