【问题标题】:Extract sub-arrays from numpy-array and get desired 3D-shape从 numpy-array 中提取子数组并获得所需的 3D 形状
【发布时间】:2018-03-06 08:45:20
【问题描述】:

给定一个数组:

array([[[1], [2], [3]],
       [[4], [5], [6]],
       [[7], [8], [9]]])

我想创建三个新数组(encodedecode_indecode_out)。

encode 数组应如下所示:

array([array([[1]]),
       array([[1], [2]]),
       array([[4]]),
       array([[4], [5]]),
       array([[7]]),
       array([[7], [8]])])

decode_in 数组应该是encodeinverse,每行添加一个零,因此该数组的形状与下面的decode_out 相同,即:

array([array([[2], [3], [0]]),
       array([[3], [0]]),
       array([[5], [6], [0]]),
       array([[6], [0]]),
       array([[8], [9], [0]]),
       array([[9], [0]])])

decode_out 应该是 decode_in,但移动了一步。目前我通过添加一个零而不是附加来解决这个问题,就像上面所做的那样。

array([array([[0], [2], [3]]),
       array([[0], [3]]),
       array([[0], [5], [6]]),
       array([[0], [6]]),
       array([[0], [8], [9]]),
       array([[0], [9]])])

下面的代码可以解决问题:

def convert_to_encodings_and_decodings(training_data):                                                                                                       

  encode = list()                                                                                                                                          
  decode_in = list()                                                                                                                                       
  decode_out = list()                                                                                                                                      

  for i in range(training_data.shape[0]):                                                                                                                  
      for j in range(training_data.shape[1]-1):                                                                                                            
          encode.append(training_data[i, :j+1])                                                                                                            
          d_in = np.concatenate((training_data[i, j+1:], np.zeros((1, 1))))                                                                                
          d_out = np.concatenate((np.zeros((1, 1)), training_data[i, j+1:]))                                                                               
          decode_in.append(d_in)                                                                                                                           
          decode_out.append(d_out)                                                                                                                         

  encode = np.asarray(encode)                                                                                                                              
  decode_in = np.asarray(decode_in)                                                                                                                        
  decode_out = np.asarray(decode_out)                                                                                                                      

  return encode, decode_in, decode_out

但生成的数组具有形状:(119920,),这与我的模型(用 Keras 编写)不兼容,它需要 3D 输入。 (nb_samplesnb_timestepsnb_features)。我希望最终的解决方案会输出形状数组:(119920, max_sub_list_length, 1),这并不理想,因为我想避免不必要的填充——但如果这就是它所需要的——那就这样吧。

我尝试将我的解决方案基于 numpy 数组(即encode = np.zeros(desired shape)),并尝试使用以下方法填充数据:

  def pad_data(data, pad_to_length):

      padded_data = list()                                                                                                                                   

      for d in data:                                                                                                                                     
          pd = np.pad(route, ((0, pad_to_length - route.shape[0]), (0, 0)), "constant")                                                              
          padded_data.append(pd)                                                                                                                   

      return np.asarray(padded_data)  

但这会导致 MemoryError

非常感谢任何帮助。

【问题讨论】:

  • 你有一个不同长度的数组,不可能有一个适合你的模型的形状。

标签: python arrays numpy keras


【解决方案1】:

您至少需要两个不同的数组。如果单个数组中有不同的长度,则不能有形状。

length1 = 1
length2 = 2

originalShape = originalArray.shape
arrayWithZeros = np.zeros((shape[0], shape[1]+1, shape[2]))
arrayWithZeros[:, :shape[1], :] = originalArray    


encode1 = orignalArray[:,:length1,:]
encode2 = originalArray[:,:length2,:]

decode_in1 = originalArray[:,length1:,:]
decode_in2 = originalArray[:,length2:,:]

decode_out1 = np.zeros(decode_in1.shape)
decode_out2 = np.zeros(decode_in2.shape)
decode_out1[:,1:,:] = decode_in1[:,:-1,:]
decode_out2[:,1:,:] = decode_in2[:,:-1,:]

像这样使用两个不同的数组将要求模型的长度为None,如input_shape=(None,1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多