简体   繁体   English

如何连接 numpy arrays 以创建 2d numpy 数组

[英]How to concatenate numpy arrays to create a 2d numpy array

I'm working on using AI to give me better odds at winning Keno.我正在努力使用 AI 来提高我赢得基诺的几率。 (don't laugh lol) My issue is that when I gather my data it comes in the form of 1d arrays of drawings at a time. (别笑,哈哈)我的问题是,当我收集数据时,它一次以 1d arrays 图纸的形式出现。 I have different files that have gathered the data and formatted it as well as performed simple maths on the data set.我有不同的文件,这些文件收集了数据并对其进行了格式化,并对数据集进行了简单的数学运算。 Now I'm trying to get the data into a certain shape for my Neural Network layers and am having issues.现在,我正在尝试将数据转化为我的神经网络层的特定形状,但遇到了问题。

  formatted_list = file.readlines()     
  #remove newline chars     
  formatted_list = list(filter(("\n").__ne__, formatted_list))     
  #iterate through each drawing, format the ends and split into list of ints     
  for i in formatted_list:         
     i = i[1:]        
     i = i[:-2]         
     i = [int(j) for j in i.split(",")]         
     #convert to numpy array
     temp = np.array(i)
     #t1 = np.reshape(temp, (-1, len(temp))) 
     #print(np.shape(t1))
     #append to master list
     master_list.append(temp)
  print(np.shape(master_list))     

This gives output of "(292,)" which is correct there are 292 rows of data however they contain 20 columns as well.这给出了“(292,)”的 output,这是正确的,有 292 行数据,但它们也包含 20 列。 If I comment in the "#t1 = np.reshape(temp, (-1, len(temp))) #print(np.shape(t1))" it gives output of "(1,20)(1,20)(1,20)(1,20)(1,20)(1,20)(1,20)(1,20)", etc. I want all of those rows to be added together and keep the columns the same (292,20).如果我在“#t1 = np.reshape(temp, (-1, len(temp))) #print(np.shape(t1))”中发表评论,它会给出 output 的“(1,20)(1,20 )(1,20)(1,20)(1,20)(1,20)(1,20)(1,20)”,等等。我希望将所有这些行加在一起并保留列相同 (292,20)。 How can this be accomplished?如何做到这一点?

I've tried reshaping the final list and many other things and had no luck.我试过重塑最终名单和许多其他事情,但没有成功。 It either populates each number in the row and adds it to the first dimension, IE (5840,) I was expecting to be able to append each new drawing to a master list, convert to numpy array and reshape it to the 292 rows of 20 columns.它要么填充行中的每个数字并将其添加到第一个维度,IE(5840,)我期望能够将每个新绘图 append 转换为主列表,转换为 numpy 数组并将其重塑为 292 行 20列。 It just appears that it want's to keep the single dimension.看起来它只是想保持单一维度。 I've tried numpy.concat also and no luck.我也试过 numpy.concat 但没有成功。 Thank you.谢谢你。

You can use vstack to concatenate your master_list .您可以使用vstack连接您的master_list

master_list = []
for array in formatted_list:
    master_list.append(array)

master_array = np.vstack(master_list)

Alternatively, if you know the length of your formatted_list containing the arrays and array length you can just preallocate the master_array .或者,如果您知道包含 arrays 的formatted_list的长度和数组长度,您可以预分配master_array

import numpy as np

formatted_list = [np.random.rand(20)]*292
master_array = np.zeros((len(formatted_list), len(formatted_list[0])))
for i, array in enumerate(formatted_list):
    master_array[i,:] = array

** Edit ** ** 编辑 **

As mentioned by hpaulj in the comments, np.array() , np.stack() and np.vstack() worked with this input and produced a numpy array with shape (7,20).正如 hpaulj 在评论中提到的, np.array()np.stack()np.vstack()使用此输入并生成形状为 (7,20) 的 numpy 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM