|
1 | | -# Array creation |
| 1 | +# 创建数组 |
2 | 2 |
|
3 | | -::: tip See also |
| 3 | +::: tip 另见 |
4 | 4 |
|
5 | | -[Array creation routines](https://numpy.org/devdocs/reference/routines.array-creation.html#routines-array-creation) |
| 5 | +[数组创建相关API](/reference/routines.array-creation.html#routines-array-creation) |
6 | 6 |
|
7 | 7 | ::: |
8 | 8 |
|
9 | | -## Introduction |
| 9 | +## 简介 |
10 | 10 |
|
11 | | -There are 5 general mechanisms for creating arrays: |
| 11 | +创建数组有5种常规机制: |
12 | 12 |
|
13 | | -1. Conversion from other Python structures (e.g., lists, tuples) |
14 | | -1. Intrinsic numpy array creation objects (e.g., arange, ones, zeros, |
15 | | -etc.) |
16 | | -1. Reading arrays from disk, either from standard or custom formats |
17 | | -1. Creating arrays from raw bytes through the use of strings or buffers |
18 | | -1. Use of special library functions (e.g., random) |
| 13 | +1. 从其他Python结构(例如,列表,元组)转换 |
| 14 | +1. numpy原生数组的创建(例如,arange、ones、zeros等) |
| 15 | +1. 从磁盘读取数组,无论是标准格式还是自定义格式 |
| 16 | +1. 通过使用字符串或缓冲区从原始字节创建数组 |
| 17 | +1. 使用特殊库函数(例如,random) |
19 | 18 |
|
20 | | -This section will not cover means of replicating, joining, or otherwise |
21 | | -expanding or mutating existing arrays. Nor will it cover creating object |
22 | | -arrays or structured arrays. Both of those are covered in their own sections. |
| 19 | +本节不包括复制,连接或以其他方式扩展或改变现有数组的方法。它也不会涵盖创建对象数组或结构化数组。这些都包含在他们自己的章节中。 |
23 | 20 |
|
24 | | -## Converting Python array_like Objects to NumPy Arrays |
| 21 | +## 将Python array_like对象转换为Numpy数组 |
25 | 22 |
|
26 | | -In general, numerical data arranged in an array-like structure in Python can |
27 | | -be converted to arrays through the use of the array() function. The most |
28 | | -obvious examples are lists and tuples. See the documentation for array() for |
29 | | -details for its use. Some objects may support the array-protocol and allow |
30 | | -conversion to arrays this way. A simple way to find out if the object can be |
31 | | -converted to a numpy array using array() is simply to try it interactively and |
32 | | -see if it works! (The Python Way). |
| 23 | +通常,在Python中排列成array-like结构的数值数据可以通过使用array()函数转换为数组。最明显的例子是列表和元组。有关其使用的详细信息,请参阅array()的文档。一些对象可能支持数组协议并允许以这种方式转换为数组。找出对象是否可以使用array()转换为一个数组numpy 数组的简单方法很简单,只要交互式试一下,看看它是否工作!(Python方式)。 |
33 | 24 |
|
34 | | -Examples: |
| 25 | +例子: |
35 | 26 |
|
36 | | -``` python |
| 27 | +```python |
37 | 28 | >>> x = np.array([2,3,1,0]) |
38 | 29 | >>> x = np.array([2, 3, 1, 0]) |
39 | 30 | >>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists, |
40 | 31 | and types |
41 | 32 | >>> x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]]) |
42 | 33 | ``` |
43 | 34 |
|
44 | | -## Intrinsic NumPy Array Creation |
| 35 | +## Numpy原生数组的创建 |
45 | 36 |
|
46 | | -NumPy has built-in functions for creating arrays from scratch: |
| 37 | +Numpy内置了从头开始创建数组的函数: |
47 | 38 |
|
48 | | -zeros(shape) will create an array filled with 0 values with the specified |
49 | | -shape. The default dtype is float64. |
| 39 | +zeros(shape)将创建一个用指定形状用0填充的数组。默认的dtype是float64。 |
50 | 40 |
|
51 | | -``` python |
52 | | ->>> np.zeros((2, 3)) |
53 | | -array([[ 0., 0., 0.], [ 0., 0., 0.]]) |
| 41 | +```python |
| 42 | +>>> np.zeros((2, 3)) array([[ 0., 0., 0.], [ 0., 0., 0.]]) |
54 | 43 | ``` |
55 | 44 |
|
56 | | -ones(shape) will create an array filled with 1 values. It is identical to |
57 | | -zeros in all other respects. |
| 45 | +ones(shape)将创建一个用1个值填充的数组。它在所有其他方面与zeros相同。 |
58 | 46 |
|
59 | | -arange() will create arrays with regularly incrementing values. Check the |
60 | | -docstring for complete information on the various ways it can be used. A few |
61 | | -examples will be given here: |
| 47 | +arange()将创建具有有规律递增值的数组。检查文档字符串以获取有关可以使用的各种方式的完整信息。这里给出几个例子: |
62 | 48 |
|
63 | | -``` python |
| 49 | +```python |
64 | 50 | >>> np.arange(10) |
65 | 51 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
66 | | ->>> np.arange(2, 10, dtype=float) |
| 52 | +>>> np.arange(2, 10, dtype=np.float) |
67 | 53 | array([ 2., 3., 4., 5., 6., 7., 8., 9.]) |
68 | 54 | >>> np.arange(2, 3, 0.1) |
69 | 55 | array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]) |
70 | 56 | ``` |
71 | 57 |
|
72 | | -Note that there are some subtleties regarding the last usage that the user |
73 | | -should be aware of that are described in the arange docstring. |
| 58 | +请注意,关于用户应该注意的最后用法在arange文档字符串中有一些细微的描述。 |
74 | 59 |
|
75 | | -linspace() will create arrays with a specified number of elements, and |
76 | | -spaced equally between the specified beginning and end values. For |
77 | | -example: |
| 60 | +linspace() 将创建具有指定数量元素的数组,并在指定的开始值和结束值之间平均间隔。例如: |
78 | 61 |
|
79 | | -``` python |
| 62 | +```python |
80 | 63 | >>> np.linspace(1., 4., 6) |
81 | 64 | array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ]) |
82 | 65 | ``` |
83 | 66 |
|
84 | | -The advantage of this creation function is that one can guarantee the |
85 | | -number of elements and the starting and end point, which arange() |
86 | | -generally will not do for arbitrary start, stop, and step values. |
| 67 | +这个创建函数的优点是可以保证元素的数量以及开始和结束点,对于任意的开始,停止和步骤值,arange()通常不会这样做。 |
87 | 68 |
|
88 | | -indices() will create a set of arrays (stacked as a one-higher dimensioned |
89 | | -array), one per dimension with each representing variation in that dimension. |
90 | | -An example illustrates much better than a verbal description: |
| 69 | +indices() 将创建一组数组(堆积为一个更高维的数组),每个维度一个,每个维度表示该维度中的变化。一个例子说明比口头描述要好得多: |
91 | 70 |
|
92 | | -``` python |
| 71 | +```python |
93 | 72 | >>> np.indices((3,3)) |
94 | 73 | array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]]) |
95 | 74 | ``` |
96 | 75 |
|
97 | | -This is particularly useful for evaluating functions of multiple dimensions on |
98 | | -a regular grid. |
| 76 | +这对于评估常规网格上多个维度的功能特别有用。 |
99 | 77 |
|
100 | | -## Reading Arrays From Disk |
| 78 | +## 从磁盘读取数组 |
101 | 79 |
|
102 | | -This is presumably the most common case of large array creation. The details, |
103 | | -of course, depend greatly on the format of data on disk and so this section |
104 | | -can only give general pointers on how to handle various formats. |
| 80 | +这大概是大阵列创建的最常见情况。当然,细节很大程度上取决于磁盘上的数据格式,所以本节只能给出如何处理各种格式的一般指示。 |
105 | 81 |
|
106 | | -### Standard Binary Formats |
| 82 | +### 标准二进制格式 |
107 | 83 |
|
108 | | -Various fields have standard formats for array data. The following lists the |
109 | | -ones with known python libraries to read them and return numpy arrays (there |
110 | | -may be others for which it is possible to read and convert to numpy arrays so |
111 | | -check the last section as well) |
| 84 | +各种字段都有阵列数据的标准格式。下面列出了那些已知的Python库来读取它们并返回numpy数组(可能有其他可能读取并转换为numpy数组的其他数据,因此请检查最后一节) |
112 | 85 |
|
113 | | -``` python |
| 86 | +``` |
114 | 87 | HDF5: h5py |
115 | 88 | FITS: Astropy |
116 | 89 | ``` |
117 | 90 |
|
118 | | -Examples of formats that cannot be read directly but for which it is not hard to |
119 | | -convert are those formats supported by libraries like PIL (able to read and |
120 | | -write many image formats such as jpg, png, etc). |
| 91 | +无法直接读取但不易转换的格式示例是像PIL这样的库支持的格式(能够读取和写入许多图像格式,如jpg,png等)。 |
121 | 92 |
|
122 | | -### Common ASCII Formats |
| 93 | +### 常见ASCII格式 |
123 | 94 |
|
124 | | -Comma Separated Value files (CSV) are widely used (and an export and import |
125 | | -option for programs like Excel). There are a number of ways of reading these |
126 | | -files in Python. There are CSV functions in Python and functions in pylab |
127 | | -(part of matplotlib). |
| 95 | +逗号分隔值文件(CSV)被广泛使用(以及Excel等程序的导出和导入选项)。有很多方法可以在Python中阅读这些文件。python中有CSV函数和pylab函数(matplotlib的一部分)。 |
128 | 96 |
|
129 | | -More generic ascii files can be read using the io package in scipy. |
| 97 | +更多通用的ascii文件可以在scipy中使用io软件包读取。 |
130 | 98 |
|
131 | | -### Custom Binary Formats |
| 99 | +### 自定义二进制格式 |
132 | 100 |
|
133 | | -There are a variety of approaches one can use. If the file has a relatively |
134 | | -simple format then one can write a simple I/O library and use the numpy |
135 | | -fromfile() function and .tofile() method to read and write numpy arrays |
136 | | -directly (mind your byteorder though!) If a good C or C++ library exists that |
137 | | -read the data, one can wrap that library with a variety of techniques though |
138 | | -that certainly is much more work and requires significantly more advanced |
139 | | -knowledge to interface with C or C++. |
| 101 | +有各种各样的方法可以使用。如果文件具有相对简单的格式,那么可以编写一个简单的 I/O 库,并使用 numpy fromfile() 函数和 .tofile() 方法直接读取和写入numpy数组(尽管介意你的字节序)!如果存在一个读取数据的良好 C 或 C++ 库,可以使用各种技术来封装该库,但这肯定要做得更多,并且需要更多的高级知识才能与C或C++ 接口。 |
140 | 102 |
|
141 | | -### Use of Special Libraries |
| 103 | +### 使用特殊库 |
142 | 104 |
|
143 | | -There are libraries that can be used to generate arrays for special purposes |
144 | | -and it isn’t possible to enumerate all of them. The most common uses are use |
145 | | -of the many array generation functions in random that can generate arrays of |
146 | | -random values, and some utility functions to generate special matrices (e.g. |
147 | | -diagonal). |
| 105 | +有些库可用于生成特殊用途的数组,且无法列出所有的这些库。最常见的用途是随机使用许多数组生成函数,这些函数可以生成随机值数组,以及一些实用函数来生成特殊矩阵(例如对角线)。 |
0 commit comments