Pythonic方式将一个二维数组移到一个五维数组中?(家谱项目)

huangapple go评论140阅读模式
英文:

Pythonic way to move a 2D array into a 5D array? (Family Tree Project)

问题

我有一个表示家族谱系的5D Numpy数组。第一个索引表示家庭(杰森家族或弗林特斯通家族),第二个是世代(父母或孩子),第三个是他们的性别,第四个是个体,最后是他们的属性(身高、体重、年龄、收入)。

例如,arr[0,0,1,0,:] 会返回 Fred Flinstone 的属性的1D数组,因为他是家庭0、世代0、性别1、个体0。

问题是:我可以以 Pythonic 的方式将一个包含3个父亲和4个他们的属性的2D Father_Array 插入到5D Family_Array 中吗?

我尝试过:

  1. family_array = np.empty((0, 2, 2, 1, 4))
  2. father_array = np.ones((3,4))
  3. family_array = np.append(family_array[:, 0, 0, 0, :], father_array, axis=0)

但这返回一个形状为 (3,4) 而不是 (3,2,2,1,4) 的数组。我想要能够通过以下方式检索父亲的属性:

  1. family_array[i,0,1,0,:]

其中 'i' 代表每个家庭(i = 0 表示弗林特斯通家族,i = 1 表示杰森家族,i = 2 表示鲁宾斯通家族,等等)。

英文:

I have a 5D Numpy array representing family trees. The first index represents the family (the Jetsons or the Flintstones), the second is the generation (parents or children), the third is their gender, the fourth is the individual, and the final their attributes (height, weight, age, income).

For example, arr[0,0,1,0,:] would return a 1D-array of Fred Flinstone's attributes, since he is family 0, generation 0, gender 1, individual 0.

The question is: Can I take a 2D Father_Array (containing 3 fathers and 4 of their attributes), and insert it into the 5D Family_Array in a pythonic manner?

I tried:

  1. family_array = np.empty((0, 2, 2, 1, 4))
  2. father_array = np.ones((3,4))
  3. family_array = np.append(family_array[:, 0, 0, 0, :], father_array, axis=0)

But this returns an array of shape (3,4) rather than (3,2,2,1,4). I want to be able to retrieve father attributes by:

  1. family_array[i,0,1,0,:]

Where 'i' represents each family (i = 0 for Flintstones, i = 1 for Jetsons, i = 2 for Rubbles, etc)

答案1

得分: 1

要创建具有正确尺寸的family_array,您需要一个5D数组,尺寸为(3, 2, 2, 1, 4),其中3是家庭数量,2是代数和性别数量,1是个体数量(因为我们目前只考虑了父亲),4是属性数量。

然后,您可以按如下方式将father_array插入到第一代(0)和男性性别(1)的family_array中:

  1. family_array = np.zeros((3, 2, 2, 1, 4))
  2. father_array = np.ones((3, 4))
  3. family_array[:, 0, 1, 0, :] = father_array

这将father_array插入到family_array的相应位置,然后您可以使用family_array[i, 0, 1, 0, :]检索父亲的属性。

英文:

To create the family_array with the correct dimensions, you need to have a 5D array with dimensions (3, 2, 2, 1, 4), where 3 is the number of families, 2 is the number of generations and genders, 1 is the number of individuals (since we're only considering fathers for now), and 4 is the number of attributes.

You can then insert the father_array into the family_array for the first generation (0) and the male gender (1) as follows:

  1. family_array = np.zeros((3, 2, 2, 1, 4))
  2. father_array = np.ones((3,4))
  3. family_array[:, 0, 1, 0, :] = father_array

This will insert father_array into the corresponding position of family_array, and you'll be able to retrieve the attributes of the fathers using family_array[i,0,1,0,:].

huangapple
  • 本文由 发表于 2023年7月17日 22:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定