# 2.2.1　创建NDArray

```
In [1]: from mxnet import nd

```

```
In [2]: x = nd.arange(12)

x

Out[2]:

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]

<NDArray 12 @cpu(0)>

```

```
In [3]: x.shape

Out[3]: (12,)

```

```
In [4]: x.size

Out[4]: 12

```

```
In [5]: X = x.reshape((3, 4))

X

Out[5]:

[[ 0. 1. 2. 3.]

[ 4. 5. 6. 7.]

[ 8. 9. 10. 11.]]

<NDArray 3x4 @cpu(0)>

```

```
In [6]: nd.zeros((2, 3, 4))

Out[6]:

[[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]]

<NDArray 2x3x4 @cpu(0)>

```

```
In [7]: nd.ones((3, 4))

Out[7]:

[[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]]

<NDArray 3x4 @cpu(0)>

```

```
In [8]: Y = nd.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])

Y

Out[8]:

[[2. 1. 4. 3.]

[1. 2. 3. 4.]

[4. 3. 2. 1.]]

<NDArray 3x4 @cpu(0)>

```

```
In [9]: nd.random.normal(0, 1, shape=(3, 4))

Out[9]:

[[ 2.2122064 0.7740038 1.0434405 1.1839255 ]

[ 1.8917114 -1.2347414 -1.771029 -0.45138445]

[ 0.57938355 -1.856082 -1.9768796 -0.20801921]]

<NDArray 3x4 @cpu(0)>

```

# 2.2.2　运算

NDArray支持大量的 运算符 （operator）。例如，我们可以对之前创建的两个形状为(3, 4)的NDArray做按元素加法。所得结果形状不变。

```
In [10]: X + Y

Out[10]:

[[ 2. 2. 6. 6.]

[ 5. 7. 9. 11.]

[12. 12. 12. 12.]]

<NDArray 3x4 @cpu(0)>

```

```
In [11]: X * Y

Out[11]:

[[ 0. 1. 8. 9.]

[ 4. 10. 18. 28.]

[32. 27. 20. 11.]]

<NDArray 3x4 @cpu(0)>

```

```
In [12]: X / Y

Out[12]:

[[ 0. 1. 0.5 1. ]

[ 4. 2.5 2. 1.75]

[ 2. 3. 5. 11. ]]

<NDArray 3x4 @cpu(0)>

```

```
In [13]: Y.exp()

Out[13]:

[[ 7.389056 2.7182817 54.59815 20.085537 ]

[ 2.7182817 7.389056 20.085537 54.59815 ]

[54.59815 20.085537 7.389056 2.7182817]]

<NDArray 3x4 @cpu(0)>

```

```
In [14]: nd.dot(X, Y.T)

Out[14]:

[[ 18. 20. 10.]

[ 58. 60. 50.]

[ 98. 100. 90.]]

<NDArray 3x3 @cpu(0)>

```

```
In [15]: nd.concat(X, Y, dim=0), nd.concat(X, Y, dim=1)

Out[15]: (

[[ 0. 1. 2. 3.]

[ 4. 5. 6. 7.]

[ 8. 9. 10. 11.]

[ 2. 1. 4. 3.]

[ 1. 2. 3. 4.]

[ 4. 3. 2. 1.]]

<NDArray 6x4 @cpu(0)>,

[[ 0. 1. 2. 3. 2. 1. 4. 3.]

[ 4. 5. 6. 7. 1. 2. 3. 4.]

[ 8. 9. 10. 11. 4. 3. 2. 1.]]

<NDArray 3x8 @cpu(0)>)

```

```
In [16]: X == Y

Out[16]:

[[0. 1. 0. 1.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

<NDArray 3x4 @cpu(0)>

```

```
In [17]: X.sum()

Out[17]:

[66.]

<NDArray 1 @cpu(0)>

```

```
In [18]: X.norm().asscalar()

Out[18]: 22.494442

```

norm(X)等。

# 2.2.3　广播机制

```
In [19]: A = nd.arange(3).reshape((3, 1))

B = nd.arange(2).reshape((1, 2))

A, B

Out[19]: (

[[0.]

[1.]

[2.]]

<NDArray 3x1 @cpu(0)>,

[[0. 1.]]

<NDArray 1x2 @cpu(0)>)

```

```
In [20]: A + B

Out[20]:

[[0. 1.]

[1. 2.]

[2. 3.]]

<NDArray 3x2 @cpu(0)>

```

# 2.2.4　索引

```
In [21]: X[1:3]

Out[21]:

[[ 4. 5. 6. 7.]

[ 8. 9. 10. 11.]]

<NDArray 2x4 @cpu(0)>

```

```
In [22]: X[1, 2] = 9

X

Out[22]:

[[ 0. 1. 2. 3.]

[ 4. 5. 9. 7.]

[ 8. 9. 10. 11.]]

<NDArray 3x4 @cpu(0)>

```

```
In [23]: X[1:2, :] = 12

X

Out[23]:

[[ 0. 1. 2. 3.]

[12. 12. 12. 12.]

[ 8. 9. 10. 11.]]

<NDArray 3x4 @cpu(0)>

```

# 2.2.5　运算的内存开销

```
In [24]: before = id(Y)

Y = Y + X

id(Y) == before

Out[24]: False

```

```
In [25]: Z = Y.zeros_like()

before = id(Z)

Z[:] = X + Y

id(Z) == before

Out[25]: True

```

```
In [26]: nd.elemwise_add(X, Y, out=Z)

id(Z) == before

Out[26]: True

```

```
In [27]: before = id(X)

X += Y

id(X) == before

Out[27]: True

```

# 2.2.6　NDArray和NumPy相互变换

```
In [28]: import numpy as np

P = np.ones((2, 3))

D = nd.array(P)

D

Out[28]:

[[1. 1. 1.]

[1. 1. 1.]]

<NDArray 2x3 @cpu(0)>

```

```
In [29]: D.asnumpy()

Out[29]: array([[1., 1., 1.],

[1., 1., 1.]], dtype=f loat32)

```

• NDArray 是 MXNet 中存储和变换数据的主要工具。
• 可以轻松地对 NDArray 创建、运算、指定索引，并与NumPy 之间相互变换。

（1）运行本节中的代码。将本节中条件判别式X == Y改为X < Y或X > Y，看看能够得到什么样的 NDArray。

（2）将广播机制中按元素运算的两个NDArray替换成其他形状，结果是否和预期一样？

https://www.toutiao.com/a6725904669828186635/

• 博文量
2126
• 访问量
3237619