关于Julia中的ndarray创建:额外的堆栈 [英] Regarding ndarray creation in julia: Stacking in extra dimension

查看:55
本文介绍了关于Julia中的ndarray创建:额外的堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下python代码转换为julia:

I would like to convert the following python code into julia:

import numpy as np
x = np.random.random([4,5,6])
y = np.array([[x,  x,  x  ],
              [2*x,3*x,4*x]])
print(y.shape)
-> (2, 3, 4, 5, 6)

在朱莉娅看来,类似的语法是

In julia, the analogous syntax seems to me is

x = rand(4,5,6) 
y = [x x x; 2x 3x 4x]
println(size(y))
-> (8, 15, 6)

这些结果是不同的.你能告诉我怎么做吗?

These results are different. Can you tell me how to do it?

推荐答案

使用随机数和乘数会掩盖您要查找的详细信息.让我们进行连续编号,并尝试让Python和Julia显示相同:

Using random numbers and multipliers obscures the details which you seek. Let's do consecutive numbering and try to get Python and Julia to display alike:

python>>>>>> z = np.reshape(np.array(range(1,121)), [4, 5, 6])
>>> z
array([[[  1,   2,   3,   4,   5,   6],
        [  7,   8,   9,  10,  11,  12],
        [ 13,  14,  15,  16,  17,  18],
        [ 19,  20,  21,  22,  23,  24],
        [ 25,  26,  27,  28,  29,  30]],

       [[ 31,  32,  33,  34,  35,  36],
        [ 37,  38,  39,  40,  41,  42],
        [ 49,  50,  51,  52,  53,  54],
        [ 55,  56,  57,  58,  59,  60]],

       [[ 61,  62,  63,  64,  65,  66],
        [ 67,  68,  69,  70,  71,  72],
        [ 73,  74,  75,  76,  77,  78],
        [ 79,  80,  81,  82,  83,  84],
        [ 85,  86,  87,  88,  89,  90]],

       [[ 91,  92,  93,  94,  95,  96],
        [ 97,  98,  99, 100, 101, 102],
        [103, 104, 105, 106, 107, 108],
        [109, 110, 111, 112, 113, 114],
        [115, 116, 117, 118, 119, 120]]])

julia>z = reshape(1:120, 6, 5, 4)
6×5×4 reshape(::UnitRange{Int64}, 6, 5, 4) with eltype Int64:
[:, :, 1] =
 1   7  13  19  25
 2   8  14  20  26
 3   9  15  21  27
 4  10  16  22  28
 5  11  17  23  29
 6  12  18  24  30

[:, :, 2] =
 31  37  43  49  55
 32  38  44  50  56
 33  39  45  51  57
 34  40  46  52  58
 35  41  47  53  59
 36  42  48  54  60

[:, :, 3] =
 61  67  73  79  85
 62  68  74  80  86
 63  69  75  81  87
 64  70  76  82  88
 65  71  77  83  89
 66  72  78  84  90

[:, :, 4] =
 91   97  103  109  115
 92   98  104  110  116
 93   99  105  111  117
 94  100  106  112  118
 95  101  107  113  119
 96  102  108  114  120

因此,如果您希望在屏幕上类似地打印内容,则需要在Julia和Python之间的数组上交换第一个和最后一个尺寸大小(颠倒尺寸顺序).此外,由于当您将数组放在相同的括号中时Julia会串联它们,而Python只是将其数组嵌套得更深,因此您需要在Python上使用np.reshape或在Julia上重整形以将数组更改为所需的形状.我建议您在连续的整数集上检查结果数组,以确保在返回随机浮点数之前,它们能以相同的方式打印.请记住,访问元素时的索引顺序也不同.考虑

So, if you want things to print similarly on the screen, you need to swap first and last dimension sizes (reverse the order of dimensions) on the arrays between Julia and Python. In addition, since Julia concatenates the arrays when you put them in the same brackets, but Python just nests its arrays in greater depth, you need to use np.reshape on Python or reshape on Julia to change the arrays to the shape you want. I suggest you check the resulting arrays on a consecutive set of integers to be sure they print alike before going back to your random floating point numbers. Remember that the indexing order is different when you access elements, too. Consider

>>> zzz = np.array([[z,z,z], [z,z,z]]) # python

> zzz = reshape([z z z; z z z], 6, 5, 4, 3, 2) # julia

这篇关于关于Julia中的ndarray创建:额外的堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆