2.2.1: Introduction to NumPy Arrays NumPy 数组简介
NumPy is a Python module designed for scientific computation. NumPy是为科学计算而设计的Python模块。 NumPy has several very useful features. NumPy有几个非常有用的特性。 Here are some examples. 这里有一些例子。 NumPy arrays are n-dimensional array objects and they are a core component of scientific and numerical computation in Python. NumPy数组是n维数组对象,是Python中科学和数值计算的核心组件。 NumPy also provides tools for integrating your code with existing C,C , and Fortran code. NUMPY还提供了将代码与现有C、C 和FORTRAN代码集成的工具。 NumPy also provides many useful tools to help you perform linear algebra, generate random numbers, and much, much more. NumPy还提供了许多有用的工具来帮助您执行线性代数、生成随机数等等。 You can learn more about NumPy from the website numpy.org. 您可以从网站NumPy.org了解更多关于NumPy的信息。 NumPy arrays are an additional data type provided by NumPy,and they are used for representing vectors and matrices. NumPy数组是NumPy提供的附加数据类型,用于表示向量和矩阵。 Unlike dynamically growing Python lists, NumPy arrays have a size that is fixed when they are constructed. 与动态增长的Python列表不同,NumPy数组的大小在构造时是固定的。 Elements of NumPy arrays are also all of the same data type leading to more efficient and simpler code than using Python’s standard data types. NumPy数组的元素也都是相同的数据类型,这使得代码比使用Python的标准数据类型更高效、更简单。 By default, the elements are floating point numbers. 默认情况下,元素是浮点数。 Let’s start by constructing an empty vector and an empty matrix. 让我们先构造一个空向量和一个空矩阵。 By the way, don’t worry if you’re not that familiar with matrices. 顺便说一句,如果你对矩阵不太熟悉,别担心。 You can just think of them as two-dimensional tables. 你可以把它们想象成二维表格。 We will always use the following way to import NumPy into Python– import numpy as np. 我们将始终使用以下方法将NumPy导入Python——将NumPy作为np导入。 This is the import we will always use. 这是我们将始终使用的导入。 We’re first going to define our first zero vector using the numpy np.zeros function. 我们首先要用numpy np.zeros函数定义我们的第一个零向量。 In this case, if we would like to have five elements in the vector,we can just type np.zeros and place the number 5 inside the parentheses. 在这种情况下,如果我们想在向量中有五个元素,我们可以只键入np.zero并将数字5放在括号内。 We can define a two-dimensional array– let’s called that zero_matrix– in the following way: 我们可以用以下方式定义一个二维数组——我们称之为零矩阵: We, again, provide only one argument to NumPy. 我们再次向NumPy提供了一个参数。 In this case, it has to be a tuple. 在这种情况下,它必须是一个元组。 A tuple specifies two things. 元组指定两件事。 The first argument is the number of rows in the table and the second argument is the number of columns in our table. 第一个参数是表中的行数,第二个参数是表中的列数。 So if you would like to create a five by three table,we type np.zeros, two parentheses, 5 comma 3, and two closing parentheses. 如果你想创建一个五乘三的表,我们输入np.0,两个括号,5个逗号3和两个右括号。 Both the zero_vector and the zero_matrix will contain only zeroes as their elements. zero_向量和zero_矩阵将只包含零作为其元素。 We can see it as we type zero_vector, and similarly if we type zero_matrix. 当我们输入zero_向量时,我们可以看到它,同样,如果我们输入zero_矩阵。 You can also construct arrays of ones using the np.ones function,and its index is identical to the syntax of the zero’s function. 还可以使用np.ones函数构造一个数组,其索引与zero函数的语法相同。 To create an empty array, you can use the np.empty function, which allocates the requested space for the array, but does not initialize it,meaning that the content could be anything, whatever happens to be in the computer’s memory at the location where the array is set up. 要创建空数组,您可以使用np.empty函数,该函数为数组分配请求的空间,但不初始化它,这意味着内容可以是任何内容,不管在设置数组的位置计算机内存中发生了什么。 If you are dealing with a very large array and you know for sure that you will be updating each element of the array,this could save you some computation time because Python doesn’t need to initialize the array. 如果您处理的是一个非常大的数组,并且您肯定会更新数组的每个元素,那么这可以节省一些计算时间,因为Python不需要初始化数组。 However, you should use this function with care and if you’re new to NumPy, it’s probably best to avoid it at first. 但是,您应该小心地使用此函数,如果您是NumPy新手,可能最好首先避免使用它。 We can also construct NumPy arrays using specified values, in which case,we use the np.array function, and the input argument to the function is a sequence of numbers, typically a list of numbers. 我们还可以使用指定的值构造NumPy数组,在这种情况下,我们使用np.array函数,该函数的输入参数是一个数字序列,通常是一个数字列表。 In what follows, we assume that lower case variables are vectors or one-dimensional arrays and upper case variables are matrices, or two-dimensional arrays. 在下面的内容中,我们假设小写变量是向量或一维数组,而大写变量是矩阵或二维数组。 To practice creating NumPy arrays, let’s create two, short, one-dimensional arrays. 为了练习创建NumPy数组,让我们创建两个短的一维数组。 Our first array is going to be called x, and it consists of the numbers 1, 2, and 3. 我们的第一个数组将被称为x,它由数字1、2和3组成。 Our second NumPy array is going to be called y,and that’s going to consist of the numbers 2, 4, and 6. 我们的第二个NumPy数组将被称为y,它将由数字2、4和6组成。 When you construct a two-dimensional NumPy array,you specify the elements of each row as a list and you can then define the entire table as a list that contains at its elements each of the lists of the row elements you’ve defined. 构造二维NumPy数组时,将每行的元素指定为列表,然后可以将整个表定义为一个列表,该列表在其元素处包含已定义的行元素列表的每个元素。 Let’s work through a simple example. 让我们看一个简单的例子。 Let’s define the first row as consisting of numbers 1 and 3. 让我们将第一行定义为由数字1和3组成。 Then we can define the second row as consisting of the numbers 5 and 9. 然后我们可以将第二行定义为由数字5和9组成。 So here we have two lists that are separated by a comma. 这里我们有两个列表,用逗号分隔。 And we will embed these two lists inside yet another list,and now we have our nested list object. 我们将把这两个列表嵌入到另一个列表中,现在我们有了嵌套的列表对象。 To turn this into a NumPy array, we type np.array and the nested list object goes inside the parentheses. 要将其转换为NumPy数组,我们键入np.array,嵌套的list对象放在括号内。 Finally, sometimes you want to turn the table sideways. 最后,有时你想把桌子转向一边。 This is called taking the transpose of a matrix, which means that the first row becomes the first column,the second row becomes the second column, and so on. 这被称为对矩阵进行转置,这意味着第一行成为第一列,第二行成为第二列,依此类推。 Notice that another identical way to state this is to say that the first column becomes the first row. 请注意,另一种相同的方式是将第一列变为第一行。 The second column becomes the second row, and so on. 第二列成为第二行,依此类推。 We can transpose a two-dimensional array using the transpose method. 我们可以使用转置方法对二维数组进行转置。 If we go back to the array that we had here– let’s call this A. 如果我们回到这里的数组,我们称之为A。 We can use now the transpose method to flip the array around. 我们现在可以使用转置方法来翻转数组。