【问题标题】:Aggregate the data of multiple numpy files into one将多个numpy文件的数据聚合为一个
【发布时间】:2020-02-04 21:15:24
【问题描述】:

我有一个列表,其中包含一个数据集的 6 个不同的子数据集。我想进行 6 折交叉验证。因此,在每次 6 个步骤的 for 循环中,将我的数据集分成 2 组(将包含 5 个子数据集和测试集的训练 - 包含留下一个子数据集)。我的代码如下:

编辑(考虑到 cmets):

sets = ['datasets/1.pickle', 'datasets/2.pickle', ..., 'datasets/6.pickle']
for i in range(0,7):
  train_set = sets[:i]+sets[i+1:]
  test_data, test_lbls = crossValidFiles(sets[i]) # returns the data for a specific sub-samlpe, returns two numpy arrays.
  for item in train_set:
      train_set = [(train_data, train_lbls) for crossValidFiles(item) in train_set]
      train_data = np.concatenate([a for (a,b) in train_set], axis = 0)
      train_lbls = np.concatenate([b for (a,b) in train_set], axis = 0)
      #train_data, train_lbls = crossValidFiles(item) # that returns one file at time.

如何汇总我为训练集返回的文件?

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    你可以使用np.concatenate()np concatenate

    例如

    import numpy as np
    t1 = np.array([[1,2,3],[4,5,6]])
    t2 = np.array([[7,8,9],[10,11,12]])
    train array = np.concatenate((t1,t2), axis=0)
    

    为了处理您的文件,我将提取 train_datatrain_lbls 以获取您的数据,然后将每个列表连接起来。例如:

    import numpy as np
    t1 = [np.array([[1,2,3],[4,5,6]]), np.array(['train_lbls'])]
    t2 = [np.array([[7,8,9],[10,11,12]]), np.array(['train_lbls'])]
    train_set = [t1,t2]
    train_set = [(train_data, train_lbls) for crossValidFiles(item) in train_set]
    train_data = np.concatenate([a for (a,b) in train_set], axis=0)
    train_lbls = np.concatenate([b for (a,b) in train_set], axis=0)
    

    【讨论】:

    • 我可以在 for 循环中这样做吗?
    • 我收到消息无法分配给函数调用以使用 crossValidFiles。
    • 你能把你的代码和错误编辑成问题吗?
    • 我修改了问题!
    • 等等,它现在对你有用吗?还是您仍然遇到错误?
    【解决方案2】:

    作为Mason's 答案的替代方案,您可以在 crossValidFiles 函数中使用 np.concatenate ,以便在聚合的测试数据上运行其中的任何代码。

    import numpy as np
    
    def crossValidFiles(input_file):
        data, labels = some_load_function(input_file)
        return data, labels
    
    def some_load_function(input_file):
        # Check if the input file is a string or list-like
        if isinstance(input_file, str):
            train_array = some_load_function_2(input_file)
        else:
            train_array = np.concatenate([some_load_function_2(f) for f in input_file], axis=0)
    
        # rest of your code to create variables 'data' and 'labels'
        return data, labels
    
    
    

    Link.

    【讨论】:

    • 我的 some_load_function 返回两个 numpy 数据和标签数组。因此,当我运行“train_array = np.concatenate([some_load_function(f) for f in input_file], axis=0)”时,我收到以下错误: ValueError: Could not broadvast input array from shape () to shape () .
    • 在这种情况下,您可以将 if-else 块放在 some_load_function 定义中(编辑了我的答案)。我遗漏了很多,因为我不知道你的功能是什么样的。梅森的答案也应该有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2018-09-26
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多