Skip to content

Commit 13add20

Browse files
committed
(翻译英文)基础知识部分章节
1 parent bc81d60 commit 13add20

File tree

4 files changed

+140
-320
lines changed

4 files changed

+140
-320
lines changed

docs/user/basics/broadcasting.md

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,71 @@
1-
# Broadcasting
1+
# 广播(Broadcasting
22

3-
::: tip See also
3+
::: tip 另见
44

5-
[``numpy.broadcast``](https://numpy.org/devdocs/reference/generated/numpy.broadcast.html#numpy.broadcast)
6-
7-
[Array Broadcasting in Numpy](theory.broadcasting.html#array-broadcasting-in-numpy)
5+
- [``numpy.broadcast``](/reference/generated/numpy.broadcast.html#numpy.broadcast)
6+
- [Numpy中的数组广播](https://numpy.org/devdocs/user/theory.broadcasting.html#array-broadcasting-in-numpy)
87

98
:::
109

11-
::: tip Note
10+
::: tip 注意
1211

13-
See [this article](https://numpy.org/devdocs/user/theory.broadcasting.html)
14-
for illustrations of broadcasting concepts.
12+
有关广播概念的说明,请参阅[此文章](https://numpy.org/devdocs/user/theory.broadcasting.html)
1513

1614
:::
1715

18-
The term broadcasting describes how numpy treats arrays with different
19-
shapes during arithmetic operations. Subject to certain constraints,
20-
the smaller array is “broadcast” across the larger array so that they
21-
have compatible shapes. Broadcasting provides a means of vectorizing
22-
array operations so that looping occurs in C instead of Python. It does
23-
this without making needless copies of data and usually leads to
24-
efficient algorithm implementations. There are, however, cases where
25-
broadcasting is a bad idea because it leads to inefficient use of memory
26-
that slows computation.
16+
术语广播(Broadcasting)描述了 numpy 如何在算术运算期间处理具有不同形状的数组。受某些约束的影响,较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。广播提供了一种矢量化数组操作的方法,以便在C而不是Python中进行循环。它可以在不制作不必要的数据副本的情况下实现这一点,通常导致高效的算法实现。然而,有些情况下广播是一个坏主意,因为它会导致内存使用效率低下,从而减慢计算速度。
2717

28-
NumPy operations are usually done on pairs of arrays on an
29-
element-by-element basis. In the simplest case, the two arrays must
30-
have exactly the same shape, as in the following example:
18+
NumPy 操作通常在逐个元素的基础上在数组对上完成。在最简单的情况下,两个数组必须具有完全相同的形状,如下例所示:
3119

3220
``` python
33-
>>> a = np.array([1.0, 2.0, 3.0])
21+
>>>>>> a = np.array([1.0, 2.0, 3.0])
3422
>>> b = np.array([2.0, 2.0, 2.0])
3523
>>> a * b
3624
array([ 2., 4., 6.])
3725
```
3826

39-
NumPy’s broadcasting rule relaxes this constraint when the arrays’
40-
shapes meet certain constraints. The simplest broadcasting example occurs
41-
when an array and a scalar value are combined in an operation:
27+
当数组的形状满足某些约束时,NumPy的广播规则放宽了这种约束。当一个数组和一个标量值在一个操作中组合时,会发生最简单的广播示例:
4228

4329
``` python
44-
>>> a = np.array([1.0, 2.0, 3.0])
30+
>>>>>> a = np.array([1.0, 2.0, 3.0])
4531
>>> b = 2.0
4632
>>> a * b
4733
array([ 2., 4., 6.])
4834
```
4935

50-
The result is equivalent to the previous example where ``b`` was an array.
51-
We can think of the scalar ``b`` being *stretched* during the arithmetic
52-
operation into an array with the same shape as ``a``. The new elements in
53-
``b`` are simply copies of the original scalar. The stretching analogy is
54-
only conceptual. NumPy is smart enough to use the original scalar value
55-
without actually making copies so that broadcasting operations are as
56-
memory and computationally efficient as possible.
36+
结果等同于前面的示例,其中``b``是数组。我们可以将在算术运算期间``b``*拉伸* 的标量想象成具有相同形状的数组``a``。新元素
37+
``b``只是原始标量的副本。拉伸类比只是概念性的。NumPy足够聪明,可以使用原始标量值而无需实际制作副本,因此广播操作尽可能具有内存和计算效率。
5738

58-
The code in the second example is more efficient than that in the first
59-
because broadcasting moves less memory around during the multiplication
60-
(``b`` is a scalar rather than an array).
39+
第二个示例中的代码比第一个示例中的代码更有效,因为广播在乘法期间移动的内存较少(``b``是标量而不是数组)。
6140

62-
## General Broadcasting Rules
41+
## 一般广播规则
6342

64-
When operating on two arrays, NumPy compares their shapes element-wise.
65-
It starts with the trailing dimensions and works its way forward. Two
66-
dimensions are compatible when
43+
在两个数组上运行时,NumPy会逐元素地比较它们的形状。它从尾随尺寸开始,并向前发展。两个尺寸兼容时
6744

68-
1. they are equal, or
69-
1. one of them is 1
45+
1. 他们是平等的,或者
46+
1. 其中一个是1
7047

71-
If these conditions are not met, a
72-
``ValueError: operands could not be broadcast together`` exception is
73-
thrown, indicating that the arrays have incompatible shapes. The size of
74-
the resulting array is the maximum size along each dimension of the input
75-
arrays.
48+
如果不满足这些条件,则抛出 ``ValueError: operands could not be broadcast together`` 异常,指示数组具有不兼容的形状。结果数组的大小是沿输入的每个轴不是1的大小。
7649

77-
Arrays do not need to have the same *number* of dimensions. For example,
78-
if you have a ``256x256x3`` array of RGB values, and you want to scale
79-
each color in the image by a different value, you can multiply the image
80-
by a one-dimensional array with 3 values. Lining up the sizes of the
81-
trailing axes of these arrays according to the broadcast rules, shows that
82-
they are compatible:
50+
数组不需要具有相同 *数量* 的维度。例如,如果您有一个``256x256x3``RGB值数组,并且希望将图像中的每种颜色缩放不同的值,则可以将图像乘以具有3个值的一维数组。根据广播规则排列这些数组的尾轴的大小,表明它们是兼容的:
8351

8452
``` python
8553
Image (3d array): 256 x 256 x 3
8654
Scale (1d array): 3
8755
Result (3d array): 256 x 256 x 3
8856
```
8957

90-
When either of the dimensions compared is one, the other is
91-
used. In other words, dimensions with size 1 are stretched or “copied”
92-
to match the other.
58+
当比较的任何一个尺寸为1时,使用另一个尺寸。换句话说,尺寸为1的尺寸被拉伸或“复制”以匹配另一个尺寸。
9359

94-
In the following example, both the ``A`` and ``B`` arrays have axes with
95-
length one that are expanded to a larger size during the broadcast
96-
operation:
60+
在以下示例中,``A````B``数组都具有长度为1的轴,在广播操作期间会扩展为更大的大小:
9761

9862
``` python
9963
A (4d array): 8 x 1 x 6 x 1
10064
B (3d array): 7 x 1 x 5
10165
Result (4d array): 8 x 7 x 6 x 5
10266
```
10367

104-
Here are some more examples:
68+
以下是一些例子:
10569

10670
``` python
10771
A (2d array): 5 x 4
@@ -125,7 +89,7 @@ B (2d array): 3 x 1
12589
Result (3d array): 15 x 3 x 5
12690
```
12791

128-
Here are examples of shapes that do not broadcast:
92+
以下是不广播的形状示例:
12993

13094
``` python
13195
A (1d array): 3
@@ -135,10 +99,10 @@ A (2d array): 2 x 1
13599
B (3d array): 8 x 4 x 3 # second from last dimensions mismatched
136100
```
137101

138-
An example of broadcasting in practice:
102+
实践中广播的一个例子:
139103

140104
``` python
141-
>>> x = np.arange(4)
105+
>>>>>> x = np.arange(4)
142106
>>> xx = x.reshape(4,1)
143107
>>> y = np.ones(5)
144108
>>> z = np.ones((3,4))
@@ -182,12 +146,10 @@ array([[ 1., 2., 3., 4.],
182146
[ 1., 2., 3., 4.]])
183147
```
184148

185-
Broadcasting provides a convenient way of taking the outer product (or
186-
any other outer operation) of two arrays. The following example shows an
187-
outer addition operation of two 1-d arrays:
149+
广播提供了一种方便的方式来获取两个数组的外积(或任何其他外部操作)。以下示例显示了两个1-d数组的外积操作:
188150

189151
``` python
190-
>>> a = np.array([0.0, 10.0, 20.0, 30.0])
152+
>>>>>> a = np.array([0.0, 10.0, 20.0, 30.0])
191153
>>> b = np.array([1.0, 2.0, 3.0])
192154
>>> a[:, np.newaxis] + b
193155
array([[ 1., 2., 3.],
@@ -196,6 +158,5 @@ array([[ 1., 2., 3.],
196158
[ 31., 32., 33.]])
197159
```
198160

199-
Here the ``newaxis`` index operator inserts a new axis into ``a``,
200-
making it a two-dimensional ``4x1`` array. Combining the ``4x1`` array
201-
with ``b``, which has shape ``(3,)``, yields a ``4x3`` array.
161+
这里 ``newaxis`` 索引操作符插入一个新轴 ``a`` ,使其成为一个二维 ``4x1`` 数组。将 ``4x1`` 数组与形状为 ``(3,)````b`` 组合,产生一个``4x3``数组。
162+

docs/user/basics/byteswapping.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
# Byte-swapping
1+
# 字节交换
22

3-
## Introduction to byte ordering and ndarrays
3+
## 字节排序和ndarrays简介
44

5-
The ``ndarray`` is an object that provide a python array interface to data in memory.
5+
``ndarray``是一个为内存中的数据提供python数组接口的对象。
66

7-
It often happens that the memory that you want to view with an array is not of the same byte ordering as the computer on which you are running Python.
7+
经常发生的情况是,要用数组查看的内存与运行Python的计算机的字节顺序不同。
88

9-
For example, I might be working on a computer with a little-endian CPU - such as an Intel Pentium, but I have loaded some data from a file written by a computer that is big-endian. Let’s say I have loaded 4 bytes from a file written by a Sun (big-endian) computer. I know that these 4 bytes represent two 16-bit integers. On a big-endian machine, a two-byte integer is stored with the Most Significant Byte (MSB) first, and then the Least Significant Byte (LSB). Thus the bytes are, in memory order:
9+
例如,我可能正在使用带有 little-endian CPU 的计算机 - 例如Intel Pentium,但是我已经从一个由 big-endian计算机 编写的文件中加载了一些数据。假设我已经从Sun(big-endian)计算机写入的文件中加载了4个字节。我知道这4个字节代表两个16位整数。在 big-endian 机器上,首先以最高有效字节(MSB)存储双字节整数,然后存储最低有效字节(LSB)。因此字节按内存顺序排列:
1010

11-
1. MSB integer 1
12-
1. LSB integer 1
13-
1. MSB integer 2
14-
1. LSB integer 2
11+
1. MSB整数1
12+
1. LSB整数1
13+
1. MSB整数2
14+
1. LSB整数2
1515

16-
Let’s say the two integers were in fact 1 and 770. Because 770 = 256 * 3 + 2, the 4 bytes in memory would contain respectively: 0, 1, 3, 2. The bytes I have loaded from the file would have these contents:
16+
假设两个整数实际上是1和770.因为770 = 256 * 3 + 2,内存中的4个字节将分别包含:0,1,3,2。我从文件加载的字节将包含这些内容:
1717

1818
``` python
1919
>>> big_end_buffer = bytearray([0,1,3,2])
2020
>>> big_end_buffer
2121
bytearray(b'\x00\x01\x03\x02')
2222
```
2323

24-
We might want to use an ``ndarray`` to access these integers. In that case, we can create an array around this memory, and tell numpy that there are two integers, and that they are 16 bit and big-endian:
24+
我们可能需要使用 ``ndarray`` 来访问这些整数。在这种情况下,我们可以围绕这个内存创建一个数组,并告诉numpy有两个整数,并且它们是16位和Big-endian
2525

2626
``` python
2727
>>> import numpy as np
@@ -32,21 +32,21 @@ We might want to use an ``ndarray`` to access these integers. In that case, we c
3232
770
3333
```
3434

35-
Note the array ``dtype`` above of ``>i2``. The ``>`` means ‘big-endian’ (``<`` is little-endian) and i2 means ‘signed 2-byte integer’. For example, if our data represented a single unsigned 4-byte little-endian integer, the dtype string would be ``<u4``.
35+
注意上面的数组``dtype > i2````>`` 表示 ``big-endian``( ``<`` ``Little-endian`` ),``i2`` 表示‘有符号的2字节整数’。例如,如果我们的数据表示单个无符号4字节小端整数,则dtype字符串将为 ``<u4``
3636

37-
In fact, why don’t we try that?
37+
事实上,为什么我们不尝试呢?
3838

3939
``` python
4040
>>> little_end_u4 = np.ndarray(shape=(1,),dtype='<u4', buffer=big_end_buffer)
4141
>>> little_end_u4[0] == 1 * 256**1 + 3 * 256**2 + 2 * 256**3
4242
True
4343
```
4444

45-
Returning to our ``big_end_arr`` - in this case our underlying data is big-endian (data endianness) and we’ve set the dtype to match (the dtype is also big-endian). However, sometimes you need to flip these around.
45+
回到我们的 ``big_end_arr`` - 在这种情况下我们的基础数据是big-endian(数据字节序),我们设置dtype匹配(dtype也是big-endian)。但是,有时你需要翻转它们。
4646

4747
::: danger 警告
4848

49-
Scalars currently do not include byte order information, so extracting a scalar from an array will return an integer in native byte order. Hence:
49+
标量当前不包含字节顺序信息,因此从数组中提取标量将返回本机字节顺序的整数。因此:
5050

5151
``` python
5252
>>> big_end_arr[0].dtype.byteorder == little_end_u4[0].dtype.byteorder
@@ -55,64 +55,64 @@ True
5555

5656
:::
5757

58-
## Changing byte ordering
58+
## 更改字节顺序
5959

60-
As you can imagine from the introduction, there are two ways you can affect the relationship between the byte ordering of the array and the underlying memory it is looking at:
60+
从介绍中可以想象,有两种方法可以影响数组的字节顺序与它所查看的底层内存之间的关系:
6161

62-
- Change the byte-ordering information in the array dtype so that it interprets the underlying data as being in a different byte order. This is the role of ``arr.newbyteorder()``
63-
- Change the byte-ordering of the underlying data, leaving the dtype interpretation as it was. This is what ``arr.byteswap()`` does.
62+
- 更改数组dtype中的字节顺序信息,以便将基础数据解释为不同的字节顺序。这是作用 ``arr.newbyteorder()``
63+
- 更改基础数据的字节顺序,保留dtype解释。这是做什么的 ``arr.byteswap()``
6464

65-
The common situations in which you need to change byte ordering are:
65+
您需要更改字节顺序的常见情况是:
6666

67-
1. Your data and dtype endianness don’t match, and you want to change the dtype so that it matches the data.
68-
1. Your data and dtype endianness don’t match, and you want to swap the data so that they match the dtype
69-
1. Your data and dtype endianness match, but you want the data swapped and the dtype to reflect this
67+
1. 您的数据和dtype字节顺序不匹配,并且您希望更改dtype以使其与数据匹配。
68+
1. 您的数据和dtype字节顺序不匹配,并且您希望交换数据以使它们与dtype匹配
69+
1. 您的数据和dtype字节顺序匹配,但您希望交换数据和dtype来反映这一点
7070

71-
### Data and dtype endianness don’t match, change dtype to match data
71+
### 数据和dtype字节顺序不匹配,更改dtype以匹配数据
7272

73-
We make something where they don’t match:
73+
我们制作一些他们不匹配的东西:
7474

7575
``` python
7676
>>> wrong_end_dtype_arr = np.ndarray(shape=(2,),dtype='<i2', buffer=big_end_buffer)
7777
>>> wrong_end_dtype_arr[0]
7878
256
7979
```
8080

81-
The obvious fix for this situation is to change the dtype so it gives the correct endianness:
81+
这种情况的明显解决方法是更改​​dtype,以便它给出正确的字节顺序:
8282

8383
``` python
8484
>>> fixed_end_dtype_arr = wrong_end_dtype_arr.newbyteorder()
8585
>>> fixed_end_dtype_arr[0]
8686
1
8787
```
8888

89-
Note the array has not changed in memory:
89+
请注意,内存中的数组未更改:
9090

9191
``` python
9292
>>> fixed_end_dtype_arr.tobytes() == big_end_buffer
9393
True
9494
```
9595

96-
### Data and type endianness don’t match, change data to match dtype
96+
### 数据和类型字节顺序不匹配,更改数据以匹配dtype
9797

98-
You might want to do this if you need the data in memory to be a certain ordering. For example you might be writing the memory out to a file that needs a certain byte ordering.
98+
如果您需要内存中的数据是某种顺序,您可能希望这样做。例如,您可能正在将内存写入需要特定字节排序的文件。
9999

100100
``` python
101101
>>> fixed_end_mem_arr = wrong_end_dtype_arr.byteswap()
102102
>>> fixed_end_mem_arr[0]
103103
1
104104
```
105105

106-
Now the array has changed in memory:
106+
现在阵列 ** 在内存中更改:
107107

108108
``` python
109109
>>> fixed_end_mem_arr.tobytes() == big_end_buffer
110110
False
111111
```
112112

113-
### Data and dtype endianness match, swap data and dtype
113+
### 数据和dtype字节序匹配,交换数据和dtype
114114

115-
You may have a correctly specified array dtype, but you need the array to have the opposite byte order in memory, and you want the dtype to match so the array values make sense. In this case you just do both of the previous operations:
115+
您可能有一个正确指定的数组dtype,但是您需要数组在内存中具有相反的字节顺序,并且您希望dtype匹配以便数组值有意义。在这种情况下,您只需执行上述两个操作:
116116

117117
``` python
118118
>>> swapped_end_arr = big_end_arr.byteswap().newbyteorder()
@@ -122,12 +122,12 @@ You may have a correctly specified array dtype, but you need the array to have t
122122
False
123123
```
124124

125-
An easier way of casting the data to a specific dtype and byte ordering can be achieved with the ndarray astype method:
125+
使用ndarray astype方法可以更简单地将数据转换为特定的dtype和字节顺序:
126126

127127
``` python
128128
>>> swapped_end_arr = big_end_arr.astype('<i2')
129129
>>> swapped_end_arr[0]
130130
1
131131
>>> swapped_end_arr.tobytes() == big_end_buffer
132132
False
133-
```
133+
```

0 commit comments

Comments
 (0)