Numpy 简明教程
NumPy - Introduction
NumPy 是一个 Python 包,代表“Numerical Python”。它是一个包含多维数组对象和一组阵列处理例程的库。
NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.
Numeric 的祖先 NumPy 是由 Jim Hugunin 开发的。另一个软件包 Numarray 也得到了开发,具有其他一些功能。2005 年,Travis Oliphant 创建了 NumPy 包,将 Numarray 的功能合并到 Numeric 包中。许多贡献者参与了这个开源项目。
Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open source project.
Operations using NumPy
使用 NumPy,开发人员可以执行以下操作 −
Using NumPy, a developer can perform the following operations −
-
Mathematical and logical operations on arrays.
-
Fourier transforms and routines for shape manipulation.
-
Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.
NumPy – A Replacement for MatLab
NumPy 通常与 SciPy (科学 Python)和 Mat−plotlib (绘图库)等软件包一起使用。这种组合广泛用作 MatLab 的替代品,后者是技术计算的流行平台。但是,Python 替代版 MatLab 现在被视为一种更现代、更完整的编程语言。
NumPy is often used along with packages like SciPy (Scientific Python) and Mat−plotlib (plotting library). This combination is widely used as a replacement for MatLab, a popular platform for technical computing. However, Python alternative to MatLab is now seen as a more modern and complete programming language.
它是开源的,这是 NumPy 的一个额外优势。
It is open source, which is an added advantage of NumPy.
NumPy - Environment
标准 Python 发行版没有捆绑 NumPy 模块。一个轻量级的替代方法是使用流行的 Python 包安装程序 pip 安装 NumPy。
Standard Python distribution doesn’t come bundled with NumPy module. A lightweight alternative is to install NumPy using popular Python package installer, pip.
pip install numpy
启用 NumPy 的最佳途径是使用专门针对您的操作系统的可安装二进制包。这些二进制文件包含完整的 SciPy 堆栈(包括 NumPy、SciPy、matplotlib、IPython、SymPy 和 nose 包以及核心 Python)。
The best way to enable NumPy is to use an installable binary package specific to your operating system. These binaries contain full SciPy stack (inclusive of NumPy, SciPy, matplotlib, IPython, SymPy and nose packages along with core Python).
Windows
Anaconda(来自 https://www.continuum.io )是 SciPy 堆栈的免费 Python 分发版。它还可用于 Linux 和 Mac。
Anaconda (from https://www.continuum.io) is a free Python distribution for SciPy stack. It is also available for Linux and Mac.
Canopy ( https://www.enthought.com/products/canopy/ )可作为针对 Windows、Linux 和 Mac 的 SciPy 堆栈的免费以及商业版分发。
Canopy (https://www.enthought.com/products/canopy/) is available as free as well as commercial distribution with full SciPy stack for Windows, Linux and Mac.
Python (x,y):它是一个针对 Windows 操作系统的、拥有 SciPy 堆栈和 Spyder IDE 的免费 Python 分发版。(可从 https://www.python-xy.github.io/ 下载)
Python (x,y): It is a free Python distribution with SciPy stack and Spyder IDE for Windows OS. (Downloadable from https://www.python-xy.github.io/)
Linux
各个 Linux 发行版的包管理器用于安装 SciPy 堆栈中的一个或多个包。
Package managers of respective Linux distributions are used to install one or more packages in SciPy stack.
For Ubuntu
sudo apt-get install python-numpy
python-scipy python-matplotlibipythonipythonnotebook python-pandas
python-sympy python-nose
For Fedora
sudo yum install numpyscipy python-matplotlibipython
python-pandas sympy python-nose atlas-devel
Building from Source
必须安装带有 distutils 的核心 Python(2.6.x、2.7.x 和 3.2.x 及更高版本),并且应启用 zlib 模块。
Core Python (2.6.x, 2.7.x and 3.2.x onwards) must be installed with distutils and zlib module should be enabled.
必须有 GNU gcc(4.2 及更高版本)C 编译器。
GNU gcc (4.2 and above) C compiler must be available.
要安装 NumPy,请运行以下命令。
To install NumPy, run the following command.
Python setup.py install
要测试 NumPy 模块是否已正确安装,请尝试从 Python 提示符对其进行导入。
To test whether NumPy module is properly installed, try to import it from Python prompt.
import numpy
如果未安装,将显示以下错误消息。
If it is not installed, the following error message will be displayed.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named 'numpy'
或者,可以使用以下语法导入 NumPy 包
Alternatively, NumPy package is imported using the following syntax −
import numpy as np
NumPy - Ndarray Object
NumPy 中定义的最重要的对象是一个 N 维数组类型,称为 ndarray 。它描述同类型的项的集合。可以使用基于零的索引访问集合中的项。
The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index.
ndarray 中的每一项在内存中占用相同大小的块。ndarray 中的每个元素都是数据类型对象(称为 dtype )。
Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is an object of data-type object (called dtype).
从 ndarray 对象(通过切片)提取的任何项都通过一种数组标量类型表示为一个 Python 对象。下图展示了 ndarray、数据类型对象 (dtype) 与数组标量类型之间的关系:
Any item extracted from ndarray object (by slicing) is represented by a Python object of one of array scalar types. The following diagram shows a relationship between ndarray, data type object (dtype) and array scalar type −

在之后教程中所述的不同数组创建例程中,可以构造 ndarray 类的实例。使用 NumPy 中的数组函数按如下方式创建基本 ndarray:
An instance of ndarray class can be constructed by different array creation routines described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows −
numpy.array
它通过公开数组接口的任意对象或通过返回数组的任意方法创建 ndarray。
It creates an ndarray from any object exposing array interface, or from any method that returns an array.
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
上述构造函数接受以下参数:
The above constructor takes the following parameters −
Sr.No. |
Parameter & Description |
1 |
object Any object exposing the array interface method returns an array, or any (nested) sequence. |
2 |
dtype Desired data type of array, optional |
3 |
copy Optional. By default (true), the object is copied |
4 |
order C (row major) or F (column major) or A (any) (default) |
5 |
subok By default, returned array forced to be a base class array. If true, sub-classes passed through |
6 |
ndmin Specifies minimum dimensions of resultant array |
仔细查看以下示例,以便更好地理解。
Take a look at the following examples to understand better.
Example 1
import numpy as np
a = np.array([1,2,3])
print a
输出如下 −
The output is as follows −
[1, 2, 3]
Example 2
# more than one dimensions
import numpy as np
a = np.array([[1, 2], [3, 4]])
print a
输出如下 −
The output is as follows −
[[1, 2]
[3, 4]]
Example 3
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print a
输出如下 −
The output is as follows −
[[1, 2, 3, 4, 5]]
Example 4
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print a
输出如下 −
The output is as follows −
[ 1.+0.j, 2.+0.j, 3.+0.j]
ndarray 对象由连续的一维计算机内存段组成,该段与一个索引方案结合在一起,将每个项目映射到内存块中的一个位置。内存块按行优先顺序 (C 样式) 或列优先顺序 (FORTRAN 或 MatLab 样式) 保存元素。
The ndarray object consists of contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in a row-major order (C style) or a column-major order (FORTRAN or MatLab style).
NumPy - Data Types
NumPy 支持的数值类型比 Python 所支持的类型多得多。下表显示了 NumPy 中定义的不同标量数据类型。
NumPy supports a much greater variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.
Sr.No. |
Data Types & Description |
1 |
bool_ Boolean (True or False) stored as a byte |
2 |
int_ Default integer type (same as C long; normally either int64 or int32) |
3 |
intc Identical to C int (normally int32 or int64) |
4 |
intp Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
5 |
int8 Byte (-128 to 127) |
6 |
int16 Integer (-32768 to 32767) |
7 |
int32 Integer (-2147483648 to 2147483647) |
8 |
int64 Integer (-9223372036854775808 to 9223372036854775807) |
9 |
uint8 Unsigned integer (0 to 255) |
10 |
uint16 Unsigned integer (0 to 65535) |
11 |
uint32 Unsigned integer (0 to 4294967295) |
12 |
uint64 Unsigned integer (0 to 18446744073709551615) |
13 |
float_ Shorthand for float64 |
14 |
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
15 |
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa |
16 |
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa |
17 |
complex_ Shorthand for complex128 |
18 |
complex64 Complex number, represented by two 32-bit floats (real and imaginary components) |
19 |
complex128 Complex number, represented by two 64-bit floats (real and imaginary components) |
NumPy 数值类型是 dtype(数据类型)对象的实例,每个实例具有独特的特征。dtype 可用作 np.bool_、np.float32 等。
NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.
Data Type Objects (dtype)
数据类型对象描述了与数组相对应的固定内存块的解读,具体取决于以下几个方面:
A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects −
-
Type of data (integer, float or Python object)
-
Size of data
-
Byte order (little-endian or big-endian)
-
In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.
-
If data type is a subarray, its shape and data type
通过在数据类型前面添加'<'或'>'来确定字节顺序。'<'表示编码是小端序(最小重要位存储在最小地址)。'>'表示编码是大端序(最大重要位存储在最小地址)。
The byte order is decided by prefixing '<' or '>' to data type. '<' means that encoding is little-endian (least significant is stored in smallest address). '>' means that encoding is big-endian (most significant byte is stored in smallest address).
dtype对象使用以下语法构造 -
A dtype object is constructed using the following syntax −
numpy.dtype(object, align, copy)
参数是 -
The parameters are −
-
Object − To be converted to data type object
-
Align − If true, adds padding to the field to make it similar to C-struct
-
Copy − Makes a new copy of dtype object. If false, the result is reference to builtin data type object
Example 1
# using array-scalar type
import numpy as np
dt = np.dtype(np.int32)
print dt
输出如下 −
The output is as follows −
int32
Example 2
#int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc.
import numpy as np
dt = np.dtype('i4')
print dt
输出如下 −
The output is as follows −
int32
Example 3
# using endian notation
import numpy as np
dt = np.dtype('>i4')
print dt
输出如下 −
The output is as follows −
>i4
以下示例显示结构化数据类型的使用。此处,应声明字段名称和相应标量数据类型。
The following examples show the use of structured data type. Here, the field name and the corresponding scalar data type is to be declared.
Example 4
# first create structured data type
import numpy as np
dt = np.dtype([('age',np.int8)])
print dt
输出如下 −
The output is as follows −
[('age', 'i1')]
Example 5
# now apply it to ndarray object
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a
输出如下 −
The output is as follows −
[(10,) (20,) (30,)]
Example 6
# file name can be used to access content of age column
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a['age']
输出如下 −
The output is as follows −
[10 20 30]
Example 7
以下示例定义一个名为 student 的结构化数据类型,该数据类型具有字符串字段’name',一个 integer field 'age’和一个 float field 'marks'。此dtype应用于ndarray对象。
The following examples define a structured data type called student with a string field 'name', an integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object.
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print student
输出如下 −
The output is as follows −
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
Example 8
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print a
输出如下 −
The output is as follows −
[('abc', 21, 50.0), ('xyz', 18, 75.0)]
每个内置数据类型都具有唯一标识它的字符代码。
Each built-in data type has a character code that uniquely identifies it.
-
'b' − boolean
-
'i' − (signed) integer
-
'u' − unsigned integer
-
'f' − floating-point
-
'c' − complex-floating point
-
'm' − timedelta
-
'M' − datetime
-
'O' − (Python) objects
-
'S', 'a' − (byte-)string
-
'U' − Unicode
-
'V' − raw data (void)
NumPy - Array Attributes
在本章中,我们将讨论NumPy的各种数组属性。
In this chapter, we will discuss the various array attributes of NumPy.
ndarray.shape
此数组属性返回一个包含数组维度的元组。它还可用于调整数组大小。
This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.
Example 1
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print a.shape
输出如下 −
The output is as follows −
(2, 3)
ndarray.ndim
此数组属性返回数组的维度数。
This array attribute returns the number of array dimensions.
Example 1
# an array of evenly spaced numbers
import numpy as np
a = np.arange(24)
print a
输出如下 −
The output is as follows −
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
Example 2
# this is one dimensional array
import numpy as np
a = np.arange(24)
a.ndim
# now reshape it
b = a.reshape(2,4,3)
print b
# b is having three dimensions
输出如下 −
The output is as follows −
[[[ 0, 1, 2]
[ 3, 4, 5]
[ 6, 7, 8]
[ 9, 10, 11]]
[[12, 13, 14]
[15, 16, 17]
[18, 19, 20]
[21, 22, 23]]]
numpy.itemsize
此 array 属性以字节返回数组中每个元素的长度。
This array attribute returns the length of each element of array in bytes.
numpy.flags
ndarray 对象具有以下属性。其当前值由该函数返回。
The ndarray object has the following attributes. Its current values are returned by this function.
Sr.No. |
Attribute & Description |
1 |
C_CONTIGUOUS © The data is in a single, C-style contiguous segment |
2 |
F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous segment |
3 |
OWNDATA (O) The array owns the memory it uses or borrows it from another object |
4 |
WRITEABLE (W) The data area can be written to. Setting this to False locks the data, making it read-only |
5 |
ALIGNED (A) The data and all elements are aligned appropriately for the hardware |
6 |
UPDATEIFCOPY (U) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array |
NumPy - Array Creation Routines
可以通过以下任何数组创建例程或使用低级 ndarray 构造函数构建一个新的 ndarray 对象。
A new ndarray object can be constructed by any of the following array creation routines or using a low-level ndarray constructor.
numpy.empty
它创建一个指定形状和数据类型的未初始化数组。它使用以下构造函数 −
It creates an uninitialized array of specified shape and dtype. It uses the following constructor −
numpy.empty(shape, dtype = float, order = 'C')
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
Shape Shape of an empty array in int or tuple of int |
2 |
Dtype Desired output data type. Optional |
3 |
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array |
Example
以下代码显示了一个空数组的示例。
The following code shows an example of an empty array.
import numpy as np
x = np.empty([3,2], dtype = int)
print x
输出如下 −
The output is as follows −
[[22649312 1701344351]
[1818321759 1885959276]
[16779776 156368896]]
Note − 由于数组中的元素未初始化,所以它们显示随机值。
Note − The elements in an array show random values as they are not initialized.
numpy.zeros
返回一个指定的尺寸的新数组,并用零填充。
Returns a new array of specified size, filled with zeros.
numpy.zeros(shape, dtype = float, order = 'C')
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
Shape Shape of an empty array in int or sequence of int |
2 |
Dtype Desired output data type. Optional |
3 |
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array |
Example 1
# array of five zeros. Default dtype is float
import numpy as np
x = np.zeros(5)
print x
输出如下 −
The output is as follows −
[ 0. 0. 0. 0. 0.]
numpy.ones
返回指定尺寸和类型的新数组,并用 1 填充。
Returns a new array of specified size and type, filled with ones.
numpy.ones(shape, dtype = None, order = 'C')
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
Shape Shape of an empty array in int or tuple of int |
2 |
Dtype Desired output data type. Optional |
3 |
Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array |
NumPy - Array From Existing Data
在本章中,我们将讨论如何从现有数据创建数组。
In this chapter, we will discuss how to create an array from existing data.
numpy.asarray
此函数与 numpy.array 类似,但参数更少。此例程可用于将 Python 序列转换为 ndarray。
This function is similar to numpy.array except for the fact that it has fewer parameters. This routine is useful for converting Python sequence into ndarray.
numpy.asarray(a, dtype = None, order = None)
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
a Input data in any form such as list, list of tuples, tuples, tuple of tuples or tuple of lists |
2 |
dtype By default, the data type of input data is applied to the resultant ndarray |
3 |
order C (row major) or F (column major). C is default |
以下示例显示了如何使用 asarray 函数。
The following examples show how you can use the asarray function.
Example 1
# convert list to ndarray
import numpy as np
x = [1,2,3]
a = np.asarray(x)
print a
其输出如下所示 −
Its output would be as follows −
[1 2 3]
Example 2
# dtype is set
import numpy as np
x = [1,2,3]
a = np.asarray(x, dtype = float)
print a
现在,输出如下 −
Now, the output would be as follows −
[ 1. 2. 3.]
numpy.frombuffer
此函数将缓冲区解释为一维数组。公开缓冲区接口的任何对象都用作返回 ndarray 的参数。
This function interprets a buffer as one-dimensional array. Any object that exposes the buffer interface is used as parameter to return an ndarray.
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
buffer Any object that exposes buffer interface |
2 |
dtype Data type of returned ndarray. Defaults to float |
3 |
count The number of items to read, default -1 means all data |
4 |
offset The starting position to read from. Default is 0 |
numpy.fromiter
此函数从任何可迭代对象构建 ndarray 对象。此函数返回一个新的单维数组。
This function builds an ndarray object from any iterable object. A new one-dimensional array is returned by this function.
numpy.fromiter(iterable, dtype, count = -1)
此处,构造函数采用以下参数。
Here, the constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
iterable Any iterable object |
2 |
dtype Data type of resultant array |
3 |
count The number of items to be read from iterator. Default is -1 which means all data to be read |
以下示例显示如何使用内置 range() 函数返回列表对象。此列表的迭代器用于形成 ndarray 对象。
The following examples show how to use the built-in range() function to return a list object. An iterator of this list is used to form an ndarray object.
NumPy - Array From Numerical Ranges
在本章节中,我们将会介绍如何从数值范围创建数组。
In this chapter, we will see how to create an array from numerical ranges.
numpy.arange
此函数返回一个 ndarray 对象,其中包含给定范围内间隔均匀的值。该函数的格式如下所示 −
This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows −
numpy.arange(start, stop, step, dtype)
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
start The start of an interval. If omitted, defaults to 0 |
2 |
stop The end of an interval (not including this number) |
3 |
step Spacing between values, default is 1 |
4 |
dtype Data type of resulting ndarray. If not given, data type of input is used |
以下示例显示了如何使用此函数。
The following examples show how you can use this function.
Example 1
import numpy as np
x = np.arange(5)
print x
其输出如下所示 −
Its output would be as follows −
[0 1 2 3 4]
numpy.linspace
此函数类似于 arange() 函数。在此函数中,而不是步长,指定了区间内间隔均匀的值的数量。此函数的使用方式如下:
This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. The usage of this function is as follows −
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
构造函数需要以下参数。
The constructor takes the following parameters.
Sr.No. |
Parameter & Description |
1 |
start The starting value of the sequence |
2 |
stop The end value of the sequence, included in the sequence if endpoint set to true |
3 |
num The number of evenly spaced samples to be generated. Default is 50 |
4 |
endpoint True by default, hence the stop value is included in the sequence. If false, it is not included |
5 |
retstep If true, returns samples and step between the consecutive numbers |
6 |
dtype Data type of output ndarray |
以下示例演示了 linspace 函数的使用。
The following examples demonstrate the use linspace function.
Example 1
import numpy as np
x = np.linspace(10,20,5)
print x
其输出将为 −
Its output would be −
[10. 12.5 15. 17.5 20.]
numpy.logspace
此函数返回包含以对数刻度固定间隔排列的数字的 ndarray 对象。刻度的起始和结束端点是基数的指数,通常为 10。
This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10.
numpy.logspace(start, stop, num, endpoint, base, dtype)
下列参数决定 logspace 函数的输出。
Following parameters determine the output of logspace function.
Sr.No. |
Parameter & Description |
1 |
start The starting point of the sequence is basestart |
2 |
stop The final value of sequence is basestop |
3 |
num The number of values between the range. Default is 50 |
4 |
endpoint If true, stop is the last value in the range |
5 |
base Base of log space, default is 10 |
6 |
dtype Data type of output array. If not given, it depends upon other input arguments |
以下示例将帮助您理解 logspace 函数。
The following examples will help you understand the logspace function.
NumPy - Indexing & Slicing
ndarray 对象的内容可以通过索引或切片来访问和修改,就像 Python 的内置容器对象一样。
Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python’s in-built container objects.
如前所述,ndarray 对象中的项目遵循从 0 开始的索引。有三种类型的索引方法,即: field access, basic slicing 和 advanced indexing 。
As mentioned earlier, items in ndarray object follows zero-based index. Three types of indexing methods are available − field access, basic slicing and advanced indexing.
基本切片是 Python 在 n 维中对基本切片概念的扩展。通过将 start, stop 和 step 参数传递给内置 slice 函数来构建 Python 切片对象。将此切片对象传给数组以提取数组的一部分。
Basic slicing is an extension of Python’s basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.
Example 1
import numpy as np
a = np.arange(10)
s = slice(2,7,2)
print a[s]
它的输出如下:
Its output is as follows −
[2 4 6]
在上面的示例中,一个 ndarray 通过函数 arange() 准备对象。然后定义了一个切片对象,其中 start、stop 和 step 值分别为 2、7 和 2。当此切片对象被传递给 ndarray 时,它的一部分从索引 2 开始,到 7 结束,步长为 2,被切片。
In the above example, an ndarray object is prepared by arange() function. Then a slice object is defined with start, stop, and step values 2, 7, and 2 respectively. When this slice object is passed to the ndarray, a part of it starting with index 2 up to 7 with a step of 2 is sliced.
还可以通过将用冒号分隔的切片参数 (:start:stop:step) 直接传递给 ndarray 对象来获得相同的结果。
The same result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step) directly to the ndarray object.
Example 2
import numpy as np
a = np.arange(10)
b = a[2:7:2]
print b
在这里,我们将获得相同的输出 -
Here, we will get the same output −
[2 4 6]
如果只放一个参数,则将返回与索引对应的单个项目。如果在它的前面插入一个 :,将提取从该索引开始的所有项目。如果使用两个参数(它们之间带有 :),则切片两个索引之间的项目(不包括 stop 索引),默认步长为一。
If only one parameter is put, a single item corresponding to the index will be returned. If a : is inserted in front of it, all items from that index onwards will be extracted. If two parameters (with : between them) is used, items between the two indexes (not including the stop index) with default step one are sliced.
Example 3
# slice single item
import numpy as np
a = np.arange(10)
b = a[5]
print b
它的输出如下:
Its output is as follows −
5
Example 4
# slice items starting from index
import numpy as np
a = np.arange(10)
print a[2:]
现在,输出应为 −
Now, the output would be −
[2 3 4 5 6 7 8 9]
Example 5
# slice items between indexes
import numpy as np
a = np.arange(10)
print a[2:5]
在此处,输出将为 −
Here, the output would be −
[2 3 4]
上述描述也适用于多维数组 ndarray 。
The above description applies to multi-dimensional ndarray too.
Example 6
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print a
# slice items starting from index
print 'Now we will slice the array from the index a[1:]'
print a[1:]
输出如下 −
The output is as follows −
[[1 2 3]
[3 4 5]
[4 5 6]]
Now we will slice the array from the index a[1:]
[[3 4 5]
[4 5 6]]
切片还可以包含省略号 (…),以生成与数组维度长度相同的选取元组。如果省略号用于行位置,它将返回一个包含行中项目的 ndarray。
Slicing can also include ellipsis (…) to make a selection tuple of the same length as the dimension of an array. If ellipsis is used at the row position, it will return an ndarray comprising of items in rows.
Example 7
# array to begin with
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print 'Our array is:'
print a
print '\n'
# this returns array of items in the second column
print 'The items in the second column are:'
print a[...,1]
print '\n'
# Now we will slice all items from the second row
print 'The items in the second row are:'
print a[1,...]
print '\n'
# Now we will slice all items from column 1 onwards
print 'The items column 1 onwards are:'
print a[...,1:]
此程序的输出如下 -
The output of this program is as follows −
Our array is:
[[1 2 3]
[3 4 5]
[4 5 6]]
The items in the second column are:
[2 4 5]
The items in the second row are:
[3 4 5]
The items column 1 onwards are:
[[2 3]
[4 5]
[5 6]]
NumPy - Advanced Indexing
可以从 ndarray 中进行选择,ndarray 是非元组序列、整数或布尔数据类型的 ndarray 对象,或元组,其中至少有一项是序列对象。高级索引始终返回数据的副本。与此相反,切片只呈现视图。
It is possible to make a selection from ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. Advanced indexing always returns a copy of the data. As against this, the slicing only presents a view.
有两种高级索引类型: Integer 和 Boolean 。
There are two types of advanced indexing − Integer and Boolean.
Integer Indexing
此机制有助于根据其N维索引选择数组中的任意项。每个整数数组都表示该维度的索引数。当索引包含与目标ndarray维度一样多的整数数组时,就变得很直接。
This mechanism helps in selecting any arbitrary item in an array based on its N-dimensional index. Each integer array represents the number of indexes into that dimension. When the index consists of as many integer arrays as the dimensions of the target ndarray, it becomes straightforward.
在以下示例中,从 ndarray 对象的每行中选择指定列的一个元素。因此,行索引包含所有行号,并且列索引指定要选择的元素。
In the following example, one element of specified column from each row of ndarray object is selected. Hence, the row index contains all row numbers, and the column index specifies the element to be selected.
Example 1
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0,1,2], [0,1,0]]
print y
其输出如下所示 −
Its output would be as follows −
[1 4 5]
从第一个数组中选择了 (0,0)、(1,1) 和 (2,0) 处的元素。
The selection includes elements at (0,0), (1,1) and (2,0) from the first array.
在以下示例中,选择了放置在 4X3 数组边角处的元素。选择的行索引是 [0, 0] 和 [3,3],而列索引是 [0,2] 和 [0,2]。
In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].
Example 2
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
rows = np.array([[0,0],[3,3]])
cols = np.array([[0,2],[0,2]])
y = x[rows,cols]
print 'The corner elements of this array are:'
print y
此程序的输出如下 -
The output of this program is as follows −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The corner elements of this array are:
[[ 0 2]
[ 9 11]]
结果选择是包含边角元素的 ndarray 对象。
The resultant selection is an ndarray object containing corner elements.
可以通过将一片 (:) 或省略号 (…) 与索引数组一起使用,将高级和基本索引结合起来。以下示例对行使用切片,对列使用高级索引。当对两者使用切片时,结果相同。但是,高级索引会导致副本,并且可能有不同的内存布局。
Advanced and basic indexing can be combined by using one slice (:) or ellipsis (…) with an index array. The following example uses slice for row and advanced index for column. The result is the same when slice is used for both. But advanced index results in copy and may have different memory layout.
Example 3
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
# slicing
z = x[1:4,1:3]
print 'After slicing, our array becomes:'
print z
print '\n'
# using advanced index for column
y = x[1:4,[1,2]]
print 'Slicing using advanced index for column:'
print y
此程序的输出如下:
The output of this program would be as follows −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
After slicing, our array becomes:
[[ 4 5]
[ 7 8]
[10 11]]
Slicing using advanced index for column:
[[ 4 5]
[ 7 8]
[10 11]]
Boolean Array Indexing
此类高级索引在结果对象应该是布尔运算结果(例如比较运算符)时使用。
This type of advanced indexing is used when the resultant object is meant to be the result of Boolean operations, such as comparison operators.
Example 1
在此示例中,大于 5 的项目作为布尔索引的结果返回。
In this example, items greater than 5 are returned as a result of Boolean indexing.
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
# Now we will print the items greater than 5
print 'The items greater than 5 are:'
print x[x > 5]
此程序的输出如下:
The output of this program would be −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The items greater than 5 are:
[ 6 7 8 9 10 11]
NumPy - Broadcasting
术语 broadcasting 指的是 NumPy 在算术运算期间处理不同形状的数组的能力。数组上的算术运算通常在对应元素上执行。如果两个数组的形状完全相同,那么这些运算就会顺畅执行。
The term broadcasting refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.
Example 1
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a * b
print c
它的输出如下:
Its output is as follows −
[10 40 90 160]
如果两个数组的维度不同,则逐元素运算是不可能的。然而,由于广播功能,NumPy 中仍然可以对形状不相同的数组进行运算。较小的数组会被 broadcast 为较大数组的大小,以便它们具有兼容的形状。
If the dimensions of two arrays are dissimilar, element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.
如果满足以下规则,广播是可能的 −
Broadcasting is possible if the following rules are satisfied −
-
Array with smaller ndim than the other is prepended with '1' in its shape.
-
Size in each dimension of the output shape is maximum of the input sizes in that dimension.
-
An input can be used in calculation, if its size in a particular dimension matches the output size or its value is exactly 1.
-
If an input has a dimension size of 1, the first data entry in that dimension is used for all calculations along that dimension.
如果上述规则产生了有效的结果,并且满足以下条件之一,则一组数组被称作 broadcastable −
A set of arrays is said to be broadcastable if the above rules produce a valid result and one of the following is true −
-
Arrays have exactly the same shape.
-
Arrays have the same number of dimensions and the length of each dimension is either a common length or 1.
-
Array having too few dimensions can have its shape prepended with a dimension of length 1, so that the above stated property is true.
以下程序展示了一个广播示例。
The following program shows an example of broadcasting.
Example 2
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print 'First array:'
print a
print '\n'
print 'Second array:'
print b
print '\n'
print 'First Array + Second Array'
print a + b
此程序的输出如下:
The output of this program would be as follows −
First array:
[[ 0. 0. 0.]
[ 10. 10. 10.]
[ 20. 20. 20.]
[ 30. 30. 30.]]
Second array:
[ 1. 2. 3.]
First Array + Second Array
[[ 1. 2. 3.]
[ 11. 12. 13.]
[ 21. 22. 23.]
[ 31. 32. 33.]]
下图演示了如何将数组 b 广播为与 a 兼容。
The following figure demonstrates how array b is broadcast to become compatible with a.

NumPy - Iterating Over Array
NumPy 包含一个迭代器对象 numpy.nditer 。这是一个高效的多维迭代器对象,可以使用它来遍历数组。使用 Python 的标准迭代器接口访问数组中的每个元素。
NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.
让我们使用 arange() 函数创建一个 3X4 数组,并使用 nditer 遍历它。
Let us create a 3X4 array using arange() function and iterate over it using nditer.
Example 1
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Modified array is:'
for x in np.nditer(a):
print x,
此程序的输出如下 -
The output of this program is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
Example 2
迭代的顺序与数组的内存布局相匹配,无需考虑特定的排序。可以通过对上述数组的转置进行迭代来查看这一点。
The order of iteration is chosen to match the memory layout of an array, without considering a particular ordering. This can be seen by iterating over the transpose of the above array.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Transpose of the original array is:'
b = a.T
print b
print '\n'
print 'Modified array is:'
for x in np.nditer(b):
print x,
以上程序的输出如下:
The output of the above program is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
Iteration Order
如果使用 F 样式顺序存储相同的元素,则迭代器将选择更有效的方式在数组上进行迭代。
If the same elements are stored using F-style order, the iterator chooses the more efficient way of iterating over an array.
Example 1
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Transpose of the original array is:'
b = a.T
print b
print '\n'
print 'Sorted in C-style order:'
c = b.copy(order='C')
print c
for x in np.nditer(c):
print x,
print '\n'
print 'Sorted in F-style order:'
c = b.copy(order='F')
print c
for x in np.nditer(c):
print x,
其输出如下所示 −
Its output would be as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Sorted in C-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 20 40 5 25 45 10 30 50 15 35 55
Sorted in F-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 5 10 15 20 25 30 35 40 45 50 55
Example 2
可以通过明确地提及 nditer 对象来强制其使用特定的顺序。
It is possible to force nditer object to use a specific order by explicitly mentioning it.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Sorted in C-style order:'
for x in np.nditer(a, order = 'C'):
print x,
print '\n'
print 'Sorted in F-style order:'
for x in np.nditer(a, order = 'F'):
print x,
其输出将为 −
Its output would be −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Sorted in C-style order:
0 5 10 15 20 25 30 35 40 45 50 55
Sorted in F-style order:
0 20 40 5 25 45 10 30 50 15 35 55
Modifying Array Values
nditer 对象有另一个称为 op_flags 的可选参数。其默认值是只读的,但可以设置为读写或只写模式。这将允许使用此迭代器修改数组元素。
The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.
Example
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
for x in np.nditer(a, op_flags = ['readwrite']):
x[...] = 2*x
print 'Modified array is:'
print a
它的输出如下:
Its output is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
[[ 0 10 20 30]
[ 40 50 60 70]
[ 80 90 100 110]]
External Loop
nditer 类构造函数具有 ‘flags’ 参数,它可以采用以下值:
The nditer class constructor has a ‘flags’ parameter, which can take the following values −
Sr.No. |
Parameter & Description |
1 |
c_index C_order index can be tracked |
2 |
f_index Fortran_order index is tracked |
3 |
multi-index Type of indexes with one per iteration can be tracked |
4 |
external_loop Causes values given to be one-dimensional arrays with multiple values instead of zero-dimensional array |
Example
在下面的示例中,一维数组对应于每列由迭代器遍历。
In the following example, one-dimensional arrays corresponding to each column is traversed by the iterator.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Modified array is:'
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
print x,
输出如下 −
The output is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
[ 0 20 40] [ 5 25 45] [10 30 50] [15 35 55]
Broadcasting Iteration
如果两个数组是 broadcastable ,则一个合并的 nditer 对象能够同时在它们上进行迭代。假设数组 a 有 3X4 维,并且有另一个数组 b 有 1X4 维,则使用以下类型的迭代器(数组 b 被广播到 a 的大小)。
If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).
Example
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'First array is:'
print a
print '\n'
print 'Second array is:'
b = np.array([1, 2, 3, 4], dtype = int)
print b
print '\n'
print 'Modified array is:'
for x,y in np.nditer([a,b]):
print "%d:%d" % (x,y),
其输出如下所示 −
Its output would be as follows −
First array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Second array is:
[1 2 3 4]
Modified array is:
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4
NumPy - Array Manipulation
NumPy 包中提供了多个例程,用于处理 ndarray
对象中的元素。它们可以分类为以下类型:
Several routines are available in NumPy package for manipulation of elements in ndarray object. They can be classified into the following types −
Changing Shape
Sr.No. |
Shape & Description |
1 |
reshapeGives a new shape to an array without changing its data |
2 |
flatA 1-D iterator over the array |
3 |
flattenReturns a copy of the array collapsed into one dimension |
4 |
ravelReturns a contiguous flattened array |
Transpose Operations
Sr.No. |
Operation & Description |
1 |
transposePermutes the dimensions of an array |
2 |
ndarray.TSame as self.transpose() |
3 |
rollaxisRolls the specified axis backwards |
4 |
swapaxesInterchanges the two axes of an array |
Changing Dimensions
Sr.No. |
Dimension & Description |
1 |
broadcastProduces an object that mimics broadcasting |
2 |
broadcast_toBroadcasts an array to a new shape |
3 |
expand_dimsExpands the shape of an array |
4 |
squeezeRemoves single-dimensional entries from the shape of an array |
Joining Arrays
Sr.No. |
Array & Description |
1 |
concatenateJoins a sequence of arrays along an existing axis |
2 |
stackJoins a sequence of arrays along a new axis |
3 |
hstackStacks arrays in sequence horizontally (column wise) |
4 |
vstackStacks arrays in sequence vertically (row wise) |
Splitting Arrays
Sr.No. |
Array & Description |
1 |
splitSplits an array into multiple sub-arrays |
2 |
hsplitSplits an array into multiple sub-arrays horizontally (column-wise) |
3 |
vsplitSplits an array into multiple sub-arrays vertically (row-wise) |
Adding / Removing Elements
Sr.No. |
Element & Description |
1 |
resizeReturns a new array with the specified shape |
2 |
appendAppends the values to the end of an array |
3 |
insertInserts the values along the given axis before the given indices |
4 |
deleteReturns a new array with sub-arrays along an axis deleted |
5 |
uniqueFinds the unique elements of an array |
NumPy - Binary Operators
以下是 NumPy 包中可用的按位运算函数:
Following are the functions for bitwise operations available in NumPy package.
Sr.No. |
Operation & Description |
1 |
bitwise_andComputes bitwise AND operation of array elements |
2 |
bitwise_orComputes bitwise OR operation of array elements |
3 |
invertComputes bitwise NOT |
4 |
left_shiftShifts bits of a binary representation to the left |
5 |
right_shiftShifts bits of binary representation to the right |
NumPy - String Functions
以下函数用于对数据类型为 numpy.string_ 或 numpy.unicode_ 的数组执行矢量化字符串运算。它们基于 Python 内置库中的标准字符串函数。
The following functions are used to perform vectorized string operations for arrays of dtype numpy.string_ or numpy.unicode_. They are based on the standard string functions in Python’s built-in library.
Sr.No. |
Function & Description |
1 |
add()Returns element-wise string concatenation for two arrays of str or Unicode |
2 |
multiply()Returns the string with multiple concatenation, element-wise |
3 |
center()Returns a copy of the given string with elements centered in a string of specified length |
4 |
capitalize()Returns a copy of the string with only the first character capitalized |
5 |
title()Returns the element-wise title cased version of the string or unicode |
6 |
lower()Returns an array with the elements converted to lowercase |
7 |
upper()Returns an array with the elements converted to uppercase |
8 |
split()Returns a list of the words in the string, using separatordelimiter |
9 |
splitlines()Returns a list of the lines in the element, breaking at the line boundaries |
10 |
strip()Returns a copy with the leading and trailing characters removed |
11 |
join()Returns a string which is the concatenation of the strings in the sequence |
12 |
replace()Returns a copy of the string with all occurrences of substring replaced by the new string |
13 |
decode()Calls str.decode element-wise |
14 |
encode()Calls str.encode element-wise |
这些函数在字符数组类 (numpy.char) 中被定义。较早的 Numarray 包含 chararray 类。numpy.char 类中的上述函数可用于执行矢量化字符串操作。
These functions are defined in character array class (numpy.char). The older Numarray package contained chararray class. The above functions in numpy.char class are useful in performing vectorized string operations.
NumPy - Mathematical Functions
显然,NumPy 包含大量的各种数学运算。NumPy 提供标准三角函数、算术运算函数、处理复数等。
Quite understandably, NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.
Trigonometric Functions
NumPy 具有标准三角函数,可为给定的弧度角返回三角比率。
NumPy has standard trigonometric functions which return trigonometric ratios for a given angle in radians.
Example
import numpy as np
a = np.array([0,30,45,60,90])
print 'Sine of different angles:'
# Convert to radians by multiplying with pi/180
print np.sin(a*np.pi/180)
print '\n'
print 'Cosine values for angles in array:'
print np.cos(a*np.pi/180)
print '\n'
print 'Tangent values for given angles:'
print np.tan(a*np.pi/180)
以下是它的输出:
Here is its output −
Sine of different angles:
[ 0. 0.5 0.70710678 0.8660254 1. ]
Cosine values for angles in array:
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
Tangent values for given angles:
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
arcsin, arcos, 和 arctan 函数返回给定角度的正弦、余弦和正切的反三角函数。这些函数的结果可以通过将弧度转换为度数由 numpy.degrees() function 验证。
arcsin, arcos, and arctan functions return the trigonometric inverse of sin, cos, and tan of the given angle. The result of these functions can be verified by numpy.degrees() function by converting radians to degrees.
Example
import numpy as np
a = np.array([0,30,45,60,90])
print 'Array containing sine values:'
sin = np.sin(a*np.pi/180)
print sin
print '\n'
print 'Compute sine inverse of angles. Returned values are in radians.'
inv = np.arcsin(sin)
print inv
print '\n'
print 'Check result by converting to degrees:'
print np.degrees(inv)
print '\n'
print 'arccos and arctan functions behave similarly:'
cos = np.cos(a*np.pi/180)
print cos
print '\n'
print 'Inverse of cos:'
inv = np.arccos(cos)
print inv
print '\n'
print 'In degrees:'
print np.degrees(inv)
print '\n'
print 'Tan function:'
tan = np.tan(a*np.pi/180)
print tan
print '\n'
print 'Inverse of tan:'
inv = np.arctan(tan)
print inv
print '\n'
print 'In degrees:'
print np.degrees(inv)
它的输出如下:
Its output is as follows −
Array containing sine values:
[ 0. 0.5 0.70710678 0.8660254 1. ]
Compute sine inverse of angles. Returned values are in radians.
[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
Check result by converting to degrees:
[ 0. 30. 45. 60. 90.]
arccos and arctan functions behave similarly:
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
Inverse of cos:
[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
In degrees:
[ 0. 30. 45. 60. 90.]
Tan function:
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
Inverse of tan:
[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
In degrees:
[ 0. 30. 45. 60. 90.]
Functions for Rounding
numpy.around()
这是一个将值四舍五入到所需精度的函数。该函数采用以下参数。
This is a function that returns the value rounded to the desired precision. The function takes the following parameters.
numpy.around(a,decimals)
其中,
Where,
Sr.No. |
Parameter & Description |
1 |
a Input data |
2 |
decimals The number of decimals to round to. Default is 0. If negative, the integer is rounded to position to the left of the decimal point |
Example
import numpy as np
a = np.array([1.0,5.55, 123, 0.567, 25.532])
print 'Original array:'
print a
print '\n'
print 'After rounding:'
print np.around(a)
print np.around(a, decimals = 1)
print np.around(a, decimals = -1)
产出如下所示 −
It produces the following output −
Original array:
[ 1. 5.55 123. 0.567 25.532]
After rounding:
[ 1. 6. 123. 1. 26. ]
[ 1. 5.6 123. 0.6 25.5]
[ 0. 10. 120. 0. 30. ]
numpy.floor()
此函数返回不大于输入参数的最大整数。 scalar x 的底数是最大的 integer i ,即 i ⇐ x 。请注意,在 Python 中,舍入始终远离 0。
This function returns the largest integer not greater than the input parameter. The floor of the scalar x is the largest integer i, such that i ⇐ x. Note that in Python, flooring always is rounded away from 0.
Example
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print 'The given array:'
print a
print '\n'
print 'The modified array:'
print np.floor(a)
产出如下所示 −
It produces the following output −
The given array:
[ -1.7 1.5 -0.2 0.6 10. ]
The modified array:
[ -2. 1. -1. 0. 10.]
numpy.ceil()
ceil() 函数返回输入值的 ceiling,即 scalar x 的 ceiling 是最小的 integer i ,即 i >= x.
The ceil() function returns the ceiling of an input value, i.e. the ceil of the scalar x is the smallest integer i, such that i >= x.
Example
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print 'The given array:'
print a
print '\n'
print 'The modified array:'
print np.ceil(a)
它将生成如下输出:
It will produce the following output −
The given array:
[ -1.7 1.5 -0.2 0.6 10. ]
The modified array:
[ -1. 2. -0. 1. 10.]
NumPy - Arithmetic Operations
用于执行加法、减法、乘法和除法等算术运算的输入数组必须具有相同的形状,或者应遵守数组广播规则。
Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules.
Example
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print 'First array:'
print a
print '\n'
print 'Second array:'
b = np.array([10,10,10])
print b
print '\n'
print 'Add the two arrays:'
print np.add(a,b)
print '\n'
print 'Subtract the two arrays:'
print np.subtract(a,b)
print '\n'
print 'Multiply the two arrays:'
print np.multiply(a,b)
print '\n'
print 'Divide the two arrays:'
print np.divide(a,b)
它将生成如下输出:
It will produce the following output −
First array:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Second array:
[10 10 10]
Add the two arrays:
[[ 10. 11. 12.]
[ 13. 14. 15.]
[ 16. 17. 18.]]
Subtract the two arrays:
[[-10. -9. -8.]
[ -7. -6. -5.]
[ -4. -3. -2.]]
Multiply the two arrays:
[[ 0. 10. 20.]
[ 30. 40. 50.]
[ 60. 70. 80.]]
Divide the two arrays:
[[ 0. 0.1 0.2]
[ 0.3 0.4 0.5]
[ 0.6 0.7 0.8]]
下面我们将讨论 NumPy 中提供的一些其他重要算术函数。
Let us now discuss some of the other important arithmetic functions available in NumPy.
numpy.reciprocal()
该函数逐个元素返回参数的倒数。对于绝对值大于 1 的元素,由于 Python 处理整数除法的方式,结果始终为 0。对于整数 0,则会发出溢出警告。
This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 because of the way in which Python handles integer division. For integer 0, an overflow warning is issued.
Example
import numpy as np
a = np.array([0.25, 1.33, 1, 0, 100])
print 'Our array is:'
print a
print '\n'
print 'After applying reciprocal function:'
print np.reciprocal(a)
print '\n'
b = np.array([100], dtype = int)
print 'The second array is:'
print b
print '\n'
print 'After applying reciprocal function:'
print np.reciprocal(b)
它将生成如下输出:
It will produce the following output −
Our array is:
[ 0.25 1.33 1. 0. 100. ]
After applying reciprocal function:
main.py:9: RuntimeWarning: divide by zero encountered in reciprocal
print np.reciprocal(a)
[ 4. 0.7518797 1. inf 0.01 ]
The second array is:
[100]
After applying reciprocal function:
[0]
numpy.power()
此函数将第一个输入数组中的元素视为基,并返回它提高到第二个输入数组中相应元素的幂次。
This function treats elements in the first input array as base and returns it raised to the power of the corresponding element in the second input array.
import numpy as np
a = np.array([10,100,1000])
print 'Our array is:'
print a
print '\n'
print 'Applying power function:'
print np.power(a,2)
print '\n'
print 'Second array:'
b = np.array([1,2,3])
print b
print '\n'
print 'Applying power function again:'
print np.power(a,b)
它将生成如下输出:
It will produce the following output −
Our array is:
[ 10 100 1000]
Applying power function:
[ 100 10000 1000000]
Second array:
[1 2 3]
Applying power function again:
[ 10 10000 1000000000]
numpy.mod()
该函数返回输入数组中对应元素除法的余数。 numpy.remainder() 函数也会产生相同的结果。
This function returns the remainder of division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result.
import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])
print 'First array:'
print a
print '\n'
print 'Second array:'
print b
print '\n'
print 'Applying mod() function:'
print np.mod(a,b)
print '\n'
print 'Applying remainder() function:'
print np.remainder(a,b)
它将生成如下输出:
It will produce the following output −
First array:
[10 20 30]
Second array:
[3 5 7]
Applying mod() function:
[1 0 2]
Applying remainder() function:
[1 0 2]
以下函数用于对具有复数的数组执行运算。
The following functions are used to perform operations on array with complex numbers.
-
numpy.real() − returns the real part of the complex data type argument.
-
numpy.imag() − returns the imaginary part of the complex data type argument.
-
numpy.conj() − returns the complex conjugate, which is obtained by changing the sign of the imaginary part.
-
numpy.angle() − returns the angle of the complex argument. The function has degree parameter. If true, the angle in the degree is returned, otherwise the angle is in radians.
import numpy as np
a = np.array([-5.6j, 0.2j, 11. , 1+1j])
print 'Our array is:'
print a
print '\n'
print 'Applying real() function:'
print np.real(a)
print '\n'
print 'Applying imag() function:'
print np.imag(a)
print '\n'
print 'Applying conj() function:'
print np.conj(a)
print '\n'
print 'Applying angle() function:'
print np.angle(a)
print '\n'
print 'Applying angle() function again (result in degrees)'
print np.angle(a, deg = True)
它将生成如下输出:
It will produce the following output −
Our array is:
[ 0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ]
Applying real() function:
[ 0. 0. 11. 1.]
Applying imag() function:
[-5.6 0.2 0. 1. ]
Applying conj() function:
[ 0.+5.6j 0.-0.2j 11.-0.j 1.-1.j ]
Applying angle() function:
[-1.57079633 1.57079633 0. 0.78539816]
Applying angle() function again (result in degrees)
[-90. 90. 0. 45.]
NumPy - Statistical Functions
NumPy 有相当一些有用的统计函数,用于查找数组中给定元素的最小值、最大值、百分位数标准差和方差等。函数的说明如下 −
NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc. from the given elements in the array. The functions are explained as follows −
numpy.amin() and numpy.amax()
这些函数从给定数组中的元素沿指定轴返回最小值和最大值。
These functions return the minimum and the maximum from the elements in the given array along the specified axis.
Example
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print 'Our array is:'
print a
print '\n'
print 'Applying amin() function:'
print np.amin(a,1)
print '\n'
print 'Applying amin() function again:'
print np.amin(a,0)
print '\n'
print 'Applying amax() function:'
print np.amax(a)
print '\n'
print 'Applying amax() function again:'
print np.amax(a, axis = 0)
它将生成如下输出:
It will produce the following output −
Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
Applying amin() function:
[3 3 2]
Applying amin() function again:
[2 4 3]
Applying amax() function:
9
Applying amax() function again:
[8 7 9]
numpy.ptp()
numpy.ptp() 函数返回沿轴的范围(最大值 - 最小值)。
The numpy.ptp() function returns the range (maximum-minimum) of values along an axis.
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print 'Our array is:'
print a
print '\n'
print 'Applying ptp() function:'
print np.ptp(a)
print '\n'
print 'Applying ptp() function along axis 1:'
print np.ptp(a, axis = 1)
print '\n'
print 'Applying ptp() function along axis 0:'
print np.ptp(a, axis = 0)
它将生成如下输出:
It will produce the following output −
Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
Applying ptp() function:
7
Applying ptp() function along axis 1:
[4 5 7]
Applying ptp() function along axis 0:
[6 3 6]
numpy.percentile()
百分位数(或百分比)是统计中用于指示一组观察值中低于该值的观察值所占百分比的测量值。函数 numpy.percentile() 采用以下参数。
Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall. The function numpy.percentile() takes the following arguments.
numpy.percentile(a, q, axis)
其中,
Where,
Sr.No. |
Argument & Description |
1 |
a Input array |
2 |
q The percentile to compute must be between 0-100 |
3 |
axis The axis along which the percentile is to be calculated |
Example
import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying percentile() function:'
print np.percentile(a,50)
print '\n'
print 'Applying percentile() function along axis 1:'
print np.percentile(a,50, axis = 1)
print '\n'
print 'Applying percentile() function along axis 0:'
print np.percentile(a,50, axis = 0)
它将生成如下输出:
It will produce the following output −
Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
Applying percentile() function:
50.0
Applying percentile() function along axis 1:
[ 40. 20. 60.]
Applying percentile() function along axis 0:
[ 50. 40. 60.]
numpy.median()
Median 被定义为将数据样本的上半部分与下半部分分开的 value。 numpy.median() 函数的使用如下所示。
Median is defined as the value separating the higher half of a data sample from the lower half. The numpy.median() function is used as shown in the following program.
Example
import numpy as np
a = np.array([[30,65,70],[80,95,10],[50,90,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying median() function:'
print np.median(a)
print '\n'
print 'Applying median() function along axis 0:'
print np.median(a, axis = 0)
print '\n'
print 'Applying median() function along axis 1:'
print np.median(a, axis = 1)
它将生成如下输出:
It will produce the following output −
Our array is:
[[30 65 70]
[80 95 10]
[50 90 60]]
Applying median() function:
65.0
Applying median() function along axis 0:
[ 50. 90. 60.]
Applying median() function along axis 1:
[ 65. 80. 60.]
numpy.mean()
算术平均数是沿轴上的元素总和除以元素数量。 numpy.mean() 函数返回数组中元素的算术平均数。如果提到了轴,则沿着该轴计算。
Arithmetic mean is the sum of elements along an axis divided by the number of elements. The numpy.mean() function returns the arithmetic mean of elements in the array. If the axis is mentioned, it is calculated along it.
Example
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print 'Our array is:'
print a
print '\n'
print 'Applying mean() function:'
print np.mean(a)
print '\n'
print 'Applying mean() function along axis 0:'
print np.mean(a, axis = 0)
print '\n'
print 'Applying mean() function along axis 1:'
print np.mean(a, axis = 1)
它将生成如下输出:
It will produce the following output −
Our array is:
[[1 2 3]
[3 4 5]
[4 5 6]]
Applying mean() function:
3.66666666667
Applying mean() function along axis 0:
[ 2.66666667 3.66666667 4.66666667]
Applying mean() function along axis 1:
[ 2. 4. 5.]
numpy.average()
加权平均数是由每个分量的乘积加上表示其重要性的因数后形成的平均数。 numpy.average() 函数根据另一个数组中给出的各个权重计算数组中元素的加权平均数。此函数可以有 axis 参数。如果未指定轴,则将数组展平。
Weighted average is an average resulting from the multiplication of each component by a factor reflecting its importance. The numpy.average() function computes the weighted average of elements in an array according to their respective weight given in another array. The function can have an axis parameter. If the axis is not specified, the array is flattened.
考虑数组 [1,2,3,4] 和相应的权重 [4,3,2,1],加权平均数是通过将相应元素的乘积相加并将其和除以权重之和来计算的。
Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by adding the product of the corresponding elements and dividing the sum by the sum of weights.
加权平均数 = (1*4+2*3+3*2+4*1)/(4+3+2+1)
Weighted average = (1*4+2*3+3*2+4*1)/(4+3+2+1)
Example
import numpy as np
a = np.array([1,2,3,4])
print 'Our array is:'
print a
print '\n'
print 'Applying average() function:'
print np.average(a)
print '\n'
# this is same as mean when weight is not specified
wts = np.array([4,3,2,1])
print 'Applying average() function again:'
print np.average(a,weights = wts)
print '\n'
# Returns the sum of weights, if the returned parameter is set to True.
print 'Sum of weights'
print np.average([1,2,3, 4],weights = [4,3,2,1], returned = True)
它将生成如下输出:
It will produce the following output −
Our array is:
[1 2 3 4]
Applying average() function:
2.5
Applying average() function again:
2.0
Sum of weights
(2.0, 10.0)
在多维数组中,可以指定计算轴。
In a multi-dimensional array, the axis for computation can be specified.
Example
import numpy as np
a = np.arange(6).reshape(3,2)
print 'Our array is:'
print a
print '\n'
print 'Modified array:'
wt = np.array([3,5])
print np.average(a, axis = 1, weights = wt)
print '\n'
print 'Modified array:'
print np.average(a, axis = 1, weights = wt, returned = True)
它将生成如下输出:
It will produce the following output −
Our array is:
[[0 1]
[2 3]
[4 5]]
Modified array:
[ 0.625 2.625 4.625]
Modified array:
(array([ 0.625, 2.625, 4.625]), array([ 8., 8., 8.]))
Standard Deviation
标准差是平均平方离差的平方根。标准差的公式如下 −
Standard deviation is the square root of the average of squared deviations from mean. The formula for standard deviation is as follows −
std = sqrt(mean(abs(x - x.mean())**2))
如果数组为 [1, 2, 3, 4],则其平均值为 2.5。因此,平方偏差为 [2.25, 0.25, 0.25, 2.25],其均值的平方根除以 4,即 sqrt (5/4) 等于 1.1180339887498949。
If the array is [1, 2, 3, 4], then its mean is 2.5. Hence the squared deviations are [2.25, 0.25, 0.25, 2.25] and the square root of its mean divided by 4, i.e., sqrt (5/4) is 1.1180339887498949.
NumPy - Sort, Search & Counting Functions
NumPy 中提供了各种与排序相关的函数。这些排序函数实现了不同的排序算法,它们各自以执行速度、最坏情况性能、所需的作业空间和算法的稳定性为特征。下表显示了三种排序算法的比较。
A variety of sorting related functions are available in NumPy. These sorting functions implement different sorting algorithms, each of them characterized by the speed of execution, worst case performance, the workspace required and the stability of algorithms. Following table shows the comparison of three sorting algorithms.
kind |
speed |
worst case |
work space |
stable |
‘quicksort’ |
1 |
O(n^2) |
0 |
no |
‘mergesort’ |
2 |
O(n*log(n)) |
~n/2 |
yes |
‘heapsort’ |
3 |
O(n*log(n)) |
0 |
no |
numpy.sort()
sort() 函数返回输入数组的已排序副本。它有以下参数 −
The sort() function returns a sorted copy of the input array. It has the following parameters −
numpy.sort(a, axis, kind, order)
其中,
Where,
Sr.No. |
Parameter & Description |
1 |
a Array to be sorted |
2 |
axis The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis |
3 |
kind Default is quicksort |
4 |
order If the array contains fields, the order of fields to be sorted |
Example
import numpy as np
a = np.array([[3,7],[9,1]])
print 'Our array is:'
print a
print '\n'
print 'Applying sort() function:'
print np.sort(a)
print '\n'
print 'Sort along axis 0:'
print np.sort(a, axis = 0)
print '\n'
# Order parameter in sort function
dt = np.dtype([('name', 'S10'),('age', int)])
a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)
print 'Our array is:'
print a
print '\n'
print 'Order by name:'
print np.sort(a, order = 'name')
它将生成如下输出:
It will produce the following output −
Our array is:
[[3 7]
[9 1]]
Applying sort() function:
[[3 7]
[1 9]]
Sort along axis 0:
[[3 1]
[9 7]]
Our array is:
[('raju', 21) ('anil', 25) ('ravi', 17) ('amar', 27)]
Order by name:
[('amar', 27) ('anil', 25) ('raju', 21) ('ravi', 17)]
numpy.argsort()
numpy.argsort() 函数沿给定轴对输入数组执行间接排序,并使用指定类型的排序来返回数据索引数组。此索引数组用于构造已排序的数组。
The numpy.argsort() function performs an indirect sort on input array, along the given axis and using a specified kind of sort to return the array of indices of data. This indices array is used to construct the sorted array.
Example
import numpy as np
x = np.array([3, 1, 2])
print 'Our array is:'
print x
print '\n'
print 'Applying argsort() to x:'
y = np.argsort(x)
print y
print '\n'
print 'Reconstruct original array in sorted order:'
print x[y]
print '\n'
print 'Reconstruct the original array using loop:'
for i in y:
print x[i],
它将生成如下输出:
It will produce the following output −
Our array is:
[3 1 2]
Applying argsort() to x:
[1 2 0]
Reconstruct original array in sorted order:
[1 2 3]
Reconstruct the original array using loop:
1 2 3
numpy.lexsort()
函数使用一系列键执行间接排序。键可以看作是电子表格中的一列。该函数返回一个索引数组,可以使用该索引获取已排序的数据。请注意,最后一个键恰好是排序的主键。
function performs an indirect sort using a sequence of keys. The keys can be seen as a column in a spreadsheet. The function returns an array of indices, using which the sorted data can be obtained. Note, that the last key happens to be the primary key of sort.
Example
import numpy as np
nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))
print 'Applying lexsort() function:'
print ind
print '\n'
print 'Use this index to get sorted data:'
print [nm[i] + ", " + dv[i] for i in ind]
它将生成如下输出:
It will produce the following output −
Applying lexsort() function:
[3 1 0 2]
Use this index to get sorted data:
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']
NumPy 模块具有用于在数组内搜索的大量函数。提供了用于查找最大值、最小值以及满足给定条件的元素的函数。
NumPy module has a number of functions for searching inside an array. Functions for finding the maximum, the minimum as well as the elements satisfying a given condition are available.
numpy.argmax() and numpy.argmin()
这两个函数分别返回沿给定轴的最大元素和最小元素的索引。
These two functions return the indices of maximum and minimum elements respectively along the given axis.
Example
import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying argmax() function:'
print np.argmax(a)
print '\n'
print 'Index of maximum number in flattened array'
print a.flatten()
print '\n'
print 'Array containing indices of maximum along axis 0:'
maxindex = np.argmax(a, axis = 0)
print maxindex
print '\n'
print 'Array containing indices of maximum along axis 1:'
maxindex = np.argmax(a, axis = 1)
print maxindex
print '\n'
print 'Applying argmin() function:'
minindex = np.argmin(a)
print minindex
print '\n'
print 'Flattened array:'
print a.flatten()[minindex]
print '\n'
print 'Flattened array along axis 0:'
minindex = np.argmin(a, axis = 0)
print minindex
print '\n'
print 'Flattened array along axis 1:'
minindex = np.argmin(a, axis = 1)
print minindex
它将生成如下输出:
It will produce the following output −
Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
Applying argmax() function:
7
Index of maximum number in flattened array
[30 40 70 80 20 10 50 90 60]
Array containing indices of maximum along axis 0:
[1 2 0]
Array containing indices of maximum along axis 1:
[2 0 1]
Applying argmin() function:
5
Flattened array:
10
Flattened array along axis 0:
[0 1 1]
Flattened array along axis 1:
[0 2 0]
numpy.nonzero()
` numpy.nonzero() ` 函数返回输入数组中非零元素的索引。
The numpy.nonzero() function returns the indices of non-zero elements in the input array.
Example
import numpy as np
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying nonzero() function:'
print np.nonzero (a)
它将生成如下输出:
It will produce the following output −
Our array is:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
Applying nonzero() function:
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
numpy.where()
where()
函数返回输入数组中满足给定条件的元素的索引。
The where() function returns the indices of elements in an input array where the given condition is satisfied.
Example
import numpy as np
x = np.arange(9.).reshape(3, 3)
print 'Our array is:'
print x
print 'Indices of elements > 3'
y = np.where(x > 3)
print y
print 'Use these indices to get elements satisfying the condition'
print x[y]
它将生成如下输出:
It will produce the following output −
Our array is:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Indices of elements > 3
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
Use these indices to get elements satisfying the condition
[ 4. 5. 6. 7. 8.]
numpy.extract()
` extract() ` 函数返回满足任何条件的元素。
The extract() function returns the elements satisfying any condition.
import numpy as np
x = np.arange(9.).reshape(3, 3)
print 'Our array is:'
print x
# define a condition
condition = np.mod(x,2) == 0
print 'Element-wise value of condition'
print condition
print 'Extract elements using condition'
print np.extract(condition, x)
它将生成如下输出:
It will produce the following output −
Our array is:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Element-wise value of condition
[[ True False True]
[False True False]
[ True False True]]
Extract elements using condition
[ 0. 2. 4. 6. 8.]
NumPy - Byte Swapping
我们已经看到,计算机内存中存储的数据取决于CPU使用的哪种架构。它可能是小端(最低有效位存储在最小地址中)或大端(最高有效字节在最小地址中)。
We have seen that the data stored in the memory of a computer depends on which architecture the CPU uses. It may be little-endian (least significant is stored in the smallest address) or big-endian (most significant byte in the smallest address).
numpy.ndarray.byteswap()
numpy.ndarray.byteswap() 函数在两种表示形式之间切换:大端和小端。
The numpy.ndarray.byteswap() function toggles between the two representations: bigendian and little-endian.
import numpy as np
a = np.array([1, 256, 8755], dtype = np.int16)
print 'Our array is:'
print a
print 'Representation of data in memory in hexadecimal form:'
print map(hex,a)
# byteswap() function swaps in place by passing True parameter
print 'Applying byteswap() function:'
print a.byteswap(True)
print 'In hexadecimal form:'
print map(hex,a)
# We can see the bytes being swapped
它将生成如下输出:
It will produce the following output −
Our array is:
[1 256 8755]
Representation of data in memory in hexadecimal form:
['0x1', '0x100', '0x2233']
Applying byteswap() function:
[256 1 13090]
In hexadecimal form:
['0x100', '0x1', '0x3322']
NumPy - Copies & Views
在执行函数时,其中一些返回输入数组的副本,而另一些返回视图。当内容物理存储在另一个位置时,称为 Copy 。另一方面,如果提供了相同内存内容的不同视图,则我们将其称为 View 。
While executing the functions, some of them return a copy of the input array, while some return the view. When the contents are physically stored in another location, it is called Copy. If on the other hand, a different view of the same memory content is provided, we call it as View.
No Copy
简单赋值不会生成数组对象的副本。相反,它使用原始数组的相同id()来访问它。 id() 返回Python对象的通用标识符,类似于C语言中的指针。
Simple assignments do not make the copy of array object. Instead, it uses the same id() of the original array to access it. The id() returns a universal identifier of Python object, similar to the pointer in C.
此外,一个中的任何更改都会反映在另一个中。例如,改变一个的形状也将改变另一个的形状。
Furthermore, any changes in either gets reflected in the other. For example, the changing shape of one will change the shape of the other too.
Example
import numpy as np
a = np.arange(6)
print 'Our array is:'
print a
print 'Applying id() function:'
print id(a)
print 'a is assigned to b:'
b = a
print b
print 'b has same id():'
print id(b)
print 'Change shape of b:'
b.shape = 3,2
print b
print 'Shape of a also gets changed:'
print a
它将生成如下输出:
It will produce the following output −
Our array is:
[0 1 2 3 4 5]
Applying id() function:
139747815479536
a is assigned to b:
[0 1 2 3 4 5]
b has same id():
139747815479536
Change shape of b:
[[0 1]
[2 3]
[4 5]]
Shape of a also gets changed:
[[0 1]
[2 3]
[4 5]]
View or Shallow Copy
NumPy具有 ndarray.view() 方法,该方法是一个新的数组对象,它查看原始数组的相同数据。与之前的案例不同,新数组尺寸的变化不会改变原始数组的尺寸。
NumPy has ndarray.view() method which is a new array object that looks at the same data of the original array. Unlike the earlier case, change in dimensions of the new array doesn’t change dimensions of the original.
Example
import numpy as np
# To begin with, a is 3X2 array
a = np.arange(6).reshape(3,2)
print 'Array a:'
print a
print 'Create view of a:'
b = a.view()
print b
print 'id() for both the arrays are different:'
print 'id() of a:'
print id(a)
print 'id() of b:'
print id(b)
# Change the shape of b. It does not change the shape of a
b.shape = 2,3
print 'Shape of b:'
print b
print 'Shape of a:'
print a
它将生成如下输出:
It will produce the following output −
Array a:
[[0 1]
[2 3]
[4 5]]
Create view of a:
[[0 1]
[2 3]
[4 5]]
id() for both the arrays are different:
id() of a:
140424307227264
id() of b:
140424151696288
Shape of b:
[[0 1 2]
[3 4 5]]
Shape of a:
[[0 1]
[2 3]
[4 5]]
数组的分片创建视图。
Slice of an array creates a view.
Deep Copy
ndarray.copy() 函数创建深度副本。它是数组及其数据的完整副本,并且不与原始数组共享。
The ndarray.copy() function creates a deep copy. It is a complete copy of the array and its data, and doesn’t share with the original array.
Example
import numpy as np
a = np.array([[10,10], [2,3], [4,5]])
print 'Array a is:'
print a
print 'Create a deep copy of a:'
b = a.copy()
print 'Array b is:'
print b
#b does not share any memory of a
print 'Can we write b is a'
print b is a
print 'Change the contents of b:'
b[0,0] = 100
print 'Modified array b:'
print b
print 'a remains unchanged:'
print a
它将生成如下输出:
It will produce the following output −
Array a is:
[[10 10]
[ 2 3]
[ 4 5]]
Create a deep copy of a:
Array b is:
[[10 10]
[ 2 3]
[ 4 5]]
Can we write b is a
False
Change the contents of b:
Modified array b:
[[100 10]
[ 2 3]
[ 4 5]]
a remains unchanged:
[[10 10]
[ 2 3]
[ 4 5]]
NumPy - Matrix Library
NumPy 包含一个矩阵库 numpy.matlib 。此模块具有返回矩阵(而非 ndarray 对象)的函数。
NumPy package contains a Matrix library numpy.matlib. This module has functions that return matrices instead of ndarray objects.
matlib.empty()
matlib.empty() 函数返回一个新矩阵,而不初始化条目。此函数使用以下参数。
The matlib.empty() function returns a new matrix without initializing the entries. The function takes the following parameters.
numpy.matlib.empty(shape, dtype, order)
其中,
Where,
Sr.No. |
Parameter & Description |
1 |
shape int or tuple of int defining the shape of the new matrix |
2 |
Dtype Optional. Data type of the output |
3 |
order C or F |
numpy.matlib.zeros()
此函数返回填充有零的矩阵。
This function returns the matrix filled with zeros.
import numpy.matlib
import numpy as np
print np.matlib.zeros((2,2))
它将生成如下输出:
It will produce the following output −
[[ 0. 0.]
[ 0. 0.]]
numpy.matlib.ones()
此函数返回填充有 1 的矩阵。
This function returns the matrix filled with 1s.
import numpy.matlib
import numpy as np
print np.matlib.ones((2,2))
它将生成如下输出:
It will produce the following output −
[[ 1. 1.]
[ 1. 1.]]
numpy.matlib.eye()
此函数返回对角元素为 1、其他元素为 0 的矩阵。此函数使用以下参数。
This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters.
numpy.matlib.eye(n, M,k, dtype)
其中,
Where,
Sr.No. |
Parameter & Description |
1 |
n The number of rows in the resulting matrix |
2 |
M The number of columns, defaults to n |
3 |
k Index of diagonal |
4 |
dtype Data type of the output |
numpy.matlib.identity()
numpy.matlib.identity() 函数返回给定大小的单位矩阵。单位矩阵是一个所有对角元素均为 1 的方阵。
The numpy.matlib.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1.
import numpy.matlib
import numpy as np
print np.matlib.identity(5, dtype = float)
它将生成如下输出:
It will produce the following output −
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
numpy.matlib.rand()
numpy.matlib.rand() 函数返回一个填充了随机值的给定大小的矩阵。
The numpy.matlib.rand() function returns a matrix of the given size filled with random values.
Example
import numpy.matlib
import numpy as np
print np.matlib.rand(3,3)
它将生成如下输出:
It will produce the following output −
[[ 0.82674464 0.57206837 0.15497519]
[ 0.33857374 0.35742401 0.90895076]
[ 0.03968467 0.13962089 0.39665201]]
Note 矩阵始终是二维的,而 ndarray 是一个 n 维数组。这两个对象是可相互转换的。
Note that a matrix is always two-dimensional, whereas ndarray is an n-dimensional array. Both the objects are inter-convertible.
Example
import numpy.matlib
import numpy as np
i = np.matrix('1,2;3,4')
print i
它将生成如下输出:
It will produce the following output −
[[1 2]
[3 4]]
NumPy - Linear Algebra
NumPy包包含 numpy.linalg 模块,该模块提供了线性代数所需的所有功能。此模块中的部分重要函数在以下表格中有所描述。
NumPy package contains numpy.linalg module that provides all the functionality required for linear algebra. Some of the important functions in this module are described in the following table.
Sr.No. |
Function & Description |
1 |
dotDot product of the two arrays |
2 |
vdotDot product of the two vectors |
3 |
innerInner product of the two arrays |
4 |
matmulMatrix product of the two arrays |
5 |
determinantComputes the determinant of the array |
6 |
solveSolves the linear matrix equation |
7 |
invFinds the multiplicative inverse of the matrix |
NumPy - Matplotlib
Matplotlib 是 Python 的绘图库。它与 NumPy 一起使用,以提供一个作为 MatLab 的有效开源替代方案的环境。它还可与诸如 PyQt 和 wxPython 的图形工具包一起使用。
Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython.
Matplotlib 模块最早是由 John D. Hunter 编写的。自 2012 年以来,Michael Droettboom 一直是其主要开发者。当前,Matplotlib ver. 1.5.1 是可用的稳定版本。该软件包既以二进制发行版形式提供,又以源代码形式在 www.matplotlib.org 上提供。
Matplotlib module was first written by John D. Hunter. Since 2012, Michael Droettboom is the principal developer. Currently, Matplotlib ver. 1.5.1 is the stable version available. The package is available in binary distribution as well as in the source code form on www.matplotlib.org.
传统上,通过添加以下语句将包导入 Python 脚本:
Conventionally, the package is imported into the Python script by adding the following statement −
from matplotlib import pyplot as plt
pyplot() 是 matplotlib 库中最重要函数,用于绘制 2D 数据。以下脚本绘制方程式 y = 2x + 5
Here pyplot() is the most important function in matplotlib library, which is used to plot 2D data. The following script plots the equation y = 2x + 5
Example
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
将 ndarray 对象 x 从 np.arange() function 创建为 x axis 上的值。 y axis 上的相应值存储在另一个 ndarray object y 中。使用 matplotlib 包的 pyplot 子模块的 plot() 函数绘制这些值。
An ndarray object x is created from np.arange() function as the values on the x axis. The corresponding values on the y axis are stored in another ndarray object y. These values are plotted using plot() function of pyplot submodule of matplotlib package.
通过 show() 函数显示图形表示。
The graphical representation is displayed by show() function.
以上代码应会产生以下输出-
The above code should produce the following output −

可以使用格式字符串将离散值添加到 plot() 函数中,来代替线性图。可以使用以下格式化字符。
Instead of the linear graph, the values can be displayed discretely by adding a format string to the plot() function. Following formatting characters can be used.
Sr.No. |
Character & Description |
1 |
'-' Solid line style |
2 |
'--' Dashed line style |
3 |
'-.' Dash-dot line style |
4 |
':' Dotted line style |
5 |
'.' Point marker |
6 |
',' Pixel marker |
7 |
'o' Circle marker |
8 |
'v' Triangle_down marker |
9 |
'^' Triangle_up marker |
10 |
'<' Triangle_left marker |
11 |
'>' Triangle_right marker |
12 |
'1' Tri_down marker |
13 |
'2' Tri_up marker |
14 |
'3' Tri_left marker |
15 |
'4' Tri_right marker |
16 |
's' Square marker |
17 |
'p' Pentagon marker |
18 |
''* Star marker |
19 |
'h' Hexagon1 marker |
20 |
'H' Hexagon2 marker |
21 |
'+' Plus marker |
22 |
'x' X marker |
23 |
'D' Diamond marker |
24 |
'd' Thin_diamond marker |
25 |
*' |
'* Vline marker |
26 |
还定义了以下颜色缩写。
The following color abbreviations are also defined.
Character |
Color |
'b' |
Blue |
'g' |
Green |
'r' |
Red |
'c' |
Cyan |
'm' |
Magenta |
'y' |
Yellow |
'k' |
Black |
'w' |
White |
要显示代表点的圆形(而不是上面示例中的线条),请使用 “ob” 作为 plot() 函数中的格式字符串。
To display the circles representing points, instead of the line in the above example, use “ob” as the format string in plot() function.
Example
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.show()
以上代码应会产生以下输出-
The above code should produce the following output −

Sine Wave Plot
以下脚本使用 matplotlib 生成 sine wave plot 。
The following script produces the sine wave plot using matplotlib.
subplot()
subplot() 函数允许您在同一张图中绘制不同的内容。在以下脚本中,绘制的是 sine 和 cosine values 。
The subplot() function allows you to plot different things in the same figure. In the following script, sine and cosine values are plotted.
Example
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
以上代码应会产生以下输出-
The above code should produce the following output −

bar()
pyplot submodule 提供 bar() 函数来生成条形图。以下示例生成两个组的 x 和 y 数组的条形图。
The pyplot submodule provides bar() function to generate bar graphs. The following example produces the bar graph of two sets of x and y arrays.
Example
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = 'center')
plt.bar(x2, y2, color = 'g', align = 'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
此代码应产生以下输出-
This code should produce the following output −

NumPy - Histogram Using Matplotlib
NumPy 有一个 numpy.histogram() 函数,它是数据频率分布的图形表示。称为 bin 的类区间对应的水平大小相等的矩形和对应频率的 variable height 。
NumPy has a numpy.histogram() function that is a graphical representation of the frequency distribution of data. Rectangles of equal horizontal size corresponding to class interval called bin and variable height corresponding to frequency.
numpy.histogram()
numpy.histogram() 函数以输入数组和箱作为两个参数。箱数组中的连续元素作为每个箱的边界。
The numpy.histogram() function takes the input array and bins as two parameters. The successive elements in bin array act as the boundary of each bin.
import numpy as np
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(a,bins = [0,20,40,60,80,100])
hist,bins = np.histogram(a,bins = [0,20,40,60,80,100])
print hist
print bins
它将生成如下输出:
It will produce the following output −
[3 4 5 2 1]
[0 20 40 60 80 100]
plt()
Matplotlib 可以将这些数值表示的直方图转换为图形。pyplot 子模块的 plt() function 以包含数据和箱数组的数组作为参数,并将其转换为直方图。
Matplotlib can convert this numeric representation of histogram into a graph. The plt() function of pyplot submodule takes the array containing the data and bin array as parameters and converts into a histogram.
from matplotlib import pyplot as plt
import numpy as np
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title("histogram")
plt.show()
它应该产生以下输出 −
It should produce the following output −

I/O with NumPy
ndarray 对象可以保存到磁盘文件并从磁盘文件中加载。可用的 IO 函数有 −
The ndarray objects can be saved to and loaded from the disk files. The IO functions available are −
-
load() and save() functions handle /numPy binary files (with npy extension)
-
loadtxt() and savetxt() functions handle normal text files
NumPy 为 ndarray 对象引入了简单的文件格式。此 .npy 文件存储数据、形状、dtype 和在磁盘文件中重建 ndarray 所需的其他信息,以便即使文件在具有不同体系结构的另一台计算机上,也可以正确地检索数组。
NumPy introduces a simple file format for ndarray objects. This .npy file stores data, shape, dtype and other information required to reconstruct the ndarray in a disk file such that the array is correctly retrieved even if the file is on another machine with different architecture.
numpy.save()
numpy.save() 文件将输入数组存储在具有 npy 扩展名的磁盘文件中。
The numpy.save() file stores the input array in a disk file with npy extension.
import numpy as np
a = np.array([1,2,3,4,5])
np.save('outfile',a)
要从 outfile.npy 重建数组,请使用 load() 函数。
To reconstruct array from outfile.npy, use load() function.
import numpy as np
b = np.load('outfile.npy')
print b
它将生成如下输出:
It will produce the following output −
array([1, 2, 3, 4, 5])
save() 和 load() 函数接受一个额外的布尔参数 allow_pickles 。Python 中的 pickle 用于在保存到磁盘文件或从磁盘文件中读取之前对对象进行序列化和反序列化。
The save() and load() functions accept an additional Boolean parameter allow_pickles. A pickle in Python is used to serialize and de-serialize objects before saving to or reading from a disk file.
savetxt()
使用 savetxt() 和 loadtxt() 函数以简单的文本文件格式存储和检索数组数据。
The storage and retrieval of array data in simple text file format is done with savetxt() and loadtxt() functions.
Example
import numpy as np
a = np.array([1,2,3,4,5])
np.savetxt('out.txt',a)
b = np.loadtxt('out.txt')
print b
它将生成如下输出:
It will produce the following output −
[ 1. 2. 3. 4. 5.]
savetxt() 和 loadtxt() 函数接受其他可选参数,例如标题、页脚和分隔符。
The savetxt() and loadtxt() functions accept additional optional parameters such as header, footer, and delimiter.