Scipy 简明教程

SciPy - Basic Functionality

默认情况下,所有 NumPy 函数都可通过 SciPy 名称空间获得。当导入 SciPy 时,无需显式导入 NumPy 函数。NumPy 的主要对象是同构多维数组。它是元素(通常是数字)的表格,所有元素都具有相同的类型,由正整数元组编制索引。在 NumPy 中,维度称为轴。 axes 的数量称为 rank

By default, all the NumPy functions have been available through the SciPy namespace. There is no need to import the NumPy functions explicitly, when SciPy is imported. The main object of NumPy is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy, dimensions are called as axes. The number of axes is called as rank.

现在,让我们回顾一下 NumPy 中向量和矩阵的基本功能。由于 SciPy 构建在 NumPy 数组之上,因此需要了解 NumPy 基础知识。由于线性代数的大部分内容只处理矩阵。

Now, let us revise the basic functionality of Vectors and Matrices in NumPy. As SciPy is built on top of NumPy arrays, understanding of NumPy basics is necessary. As most parts of linear algebra deals with matrices only.

NumPy Vector

可以通过多种方式创建向量。下面描述了其中一些。

A Vector can be created in multiple ways. Some of them are described below.

Converting Python array-like objects to NumPy

让我们考虑以下示例。

Let us consider the following example.

import numpy as np
list = [1,2,3,4]
arr = np.array(list)
print arr

以上程序的输出如下。

The output of the above program will be as follows.

[1 2 3 4]

Intrinsic NumPy Array Creation

NumPy 具有用于从头开始创建数组的内置函数。下面说明了其中一些函数。

NumPy has built-in functions for creating arrays from scratch. Some of these functions are explained below.

Using zeros()

zeros(shape) 函数将创建一个填充有 0 值且形状为指定形状的数组。默认数据类型是 float64。让我们考虑以下示例。

The zeros(shape) function will create an array filled with 0 values with the specified shape. The default dtype is float64. Let us consider the following example.

import numpy as np
print np.zeros((2, 3))

以上程序的输出如下。

The output of the above program will be as follows.

array([[ 0., 0., 0.],
[ 0., 0., 0.]])

Using ones()

ones(shape) 函数将创建一个填充有 1 值的数组。它在其他所有方面都与 zeros 相同。让我们考虑以下示例。

The ones(shape) function will create an array filled with 1 values. It is identical to zeros in all the other respects. Let us consider the following example.

import numpy as np
print np.ones((2, 3))

以上程序的输出如下。

The output of the above program will be as follows.

array([[ 1., 1., 1.],
[ 1., 1., 1.]])

Using arange()

arange() 函数将创建一个以规则递增值递增的数组。让我们考虑以下示例。

The arange() function will create arrays with regularly incrementing values. Let us consider the following example.

import numpy as np
print np.arange(7)

上述程序将生成以下输出。

The above program will generate the following output.

array([0, 1, 2, 3, 4, 5, 6])

Defining the data type of the values

让我们考虑以下示例。

Let us consider the following example.

import numpy as np
arr = np.arange(2, 10, dtype = np.float)
print arr
print "Array Data Type :",arr.dtype

上述程序将生成以下输出。

The above program will generate the following output.

[ 2. 3. 4. 5. 6. 7. 8. 9.]
Array Data Type : float64

Using linspace()

linspace() 函数将创建具有指定数量元素的数组,这些元素将在指定的开始值和结束值之间均等地分布。让我们考虑以下示例。

The linspace() function will create arrays with a specified number of elements, which will be spaced equally between the specified beginning and end values. Let us consider the following example.

import numpy as np
print np.linspace(1., 4., 6)

上述程序将生成以下输出。

The above program will generate the following output.

array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])

Matrix

矩阵是一个专门的二维数组,它通过操作保持其二维特性。它具有一些特殊运算符,例如 (矩阵乘法)和 *(矩阵幂)。让我们考虑以下示例。

A matrix is a specialized 2-D array that retains its 2-D nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power). Let us consider the following example.

import numpy as np
print np.matrix('1 2; 3 4')

上述程序将生成以下输出。

The above program will generate the following output.

matrix([[1, 2],
[3, 4]])

Conjugate Transpose of Matrix

此特性返回 self 的(复数)共轭转置。让我们考虑以下示例。

This feature returns the (complex) conjugate transpose of self. Let us consider the following example.

import numpy as np
mat = np.matrix('1 2; 3 4')
print mat.H

上述程序将生成以下输出。

The above program will generate the following output.

matrix([[1, 3],
        [2, 4]])

Transpose of Matrix

此特性返回自身的转置。让我们考虑以下示例。

This feature returns the transpose of self. Let us consider the following example.

import numpy as np
mat = np.matrix('1 2; 3 4')
mat.T

上述程序将生成以下输出。

The above program will generate the following output.

matrix([[1, 3],
        [2, 4]])

当我们转置一个矩阵时,我们会创建一个新矩阵,其行是原始矩阵的列。另一方面,共轭转置将每个矩阵元素的行和列索引互换。矩阵的逆是一个矩阵,如果与原始矩阵相乘,将得到单位矩阵。

When we transpose a matrix, we make a new matrix whose rows are the columns of the original. A conjugate transposition, on the other hand, interchanges the row and the column index for each matrix element. The inverse of a matrix is a matrix that, if multiplied with the original matrix, results in an identity matrix.