【问题标题】:array of arrays. How can I use the items in one array to match the items in another array using NumPy. ML数组数组。如何使用 NumPy 使用一个数组中的项目来匹配另一个数组中的项目。机器学习
【发布时间】:2022-10-06 03:36:28
【问题描述】:

我有一个 (n,1) 维数组,其中包含我的 id。另一个数组是 (n,p) 维的。我想使用第一个数组的每个项目来匹配我的第二个数组的项目。

例子。

输入 Arr_1 = ([[100], [200], [300]])

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

输出

Arr_3 = ([[100],[1,2,3]], [[200],[4,5,6]], [[300][7,8,9]]])

在我的代码中,\'Arr_1\'(8000, 1) 对应于 user_id,\'Arr_2\'(8000, 1000) 对应于现在是 np 数组的标记化文本数据。这两个数组都是我的 NN 模型的 X 输入。

  • Arr_3 = list(zip(Arr1, Arr2))?
  • zip 将生成可能不是所需的列表元组。
  • 还是单线:Arr_3 = list(map(list, zip(a, b)))

标签: python keras numpy-ndarray


【解决方案1】:
x = [[100], [200], [300]]
y = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

z = [[i, j] for i, j in zip(x, y)]
print(z)
# [[[100], [1, 2, 3]], [[200], [4, 5, 6]], [[300], [7, 8, 9]]]

【讨论】:

    【解决方案2】:

    我看不出这是如何通过理解来完成的。如果 Arr_1 可以修改,则此方法有效:

    for a in Arr_1:
        a.append(next(iter(Arr_2)))
    

    否则,首先制作 Arr_1 的深层副本。

    【讨论】:

      【解决方案3】:

      如果您希望此数据用于 NN 模型,您可以将其与Y 数据一起放入Dataset

      from tensorflow.data import Dataset
      
      Arr_1 = [100, 200, 300]
      Arr_2 = [[1,2,3], [4,5,6], [7,8,9]]
      Y = [5, 6, 7]
      
      dataset = Dataset.from_tensor_slices(((Arr_1, Arr_2), Y)).batch(1)
      

      您可以打印出一些值:

      for x, y in dataset.take(3):
          print(f'x = {x}')
          print(f'y = {y}')
      

      输出:

      x = (<tf.Tensor: shape=(1,), dtype=int32, numpy=array([100], dtype=int32)>, <tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[1, 2, 3]], dtype=int32)>)
      y = [5]
      x = (<tf.Tensor: shape=(1,), dtype=int32, numpy=array([200], dtype=int32)>, <tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[4, 5, 6]], dtype=int32)>)
      y = [6]
      x = (<tf.Tensor: shape=(1,), dtype=int32, numpy=array([300], dtype=int32)>, <tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[7, 8, 9]], dtype=int32)>)
      y = [7]
      

      数据集包含XY,因此您只需提供数据集(不带y 参数)即可运行fit 方法:

      model.fit(dataset, epochs=2000)
      

      完整示例:

      import tensorflow.keras.layers as L
      from tensorflow.keras import Model
      from tensorflow.data import Dataset
      
      Arr_1 = [100, 200, 300]
      Arr_2 = [[1,2,3], [4,5,6], [7,8,9]]
      Y = [5, 6, 7]
      
      dataset = Dataset.from_tensor_slices(((Arr_1, Arr_2), Y)).batch(1)
      
      for x, y in dataset.take(3):
          print(f'x = {x}')
          print(f'y = {y}')
      
      input_1 = L.Input(shape=(1,))
      input_2 = L.Input(shape=(3,))
      concat = L.Concatenate(axis=1)([input_1, input_2])
      output = L.Dense(1)(concat)
      
      model = Model(inputs=[input_1, input_2], outputs=output)
      model.compile(loss='mse', optimizer='Adam')
      model.fit(dataset, epochs=2000)
      
      x, y = next(iter(dataset))
      print(f'x = {x}')
      print(f'y_true = {y}')
      print(f'model prediction: {model.predict(x)}')
      

      【讨论】:

        【解决方案4】:
        for i in range(len(Arr1)):
            Arr2[i].append(Arr1[i])
        
        display(Arr2)
        [[1, 2, 3, [100], [100]], [4, 5, 6, [200], [200]], [7, 8, 9, [300]]]
        

        希望这可以帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-13
          • 2020-08-31
          • 2015-03-10
          • 1970-01-01
          • 1970-01-01
          • 2017-03-12
          • 1970-01-01
          相关资源
          最近更新 更多