官术网_书友最值得收藏!

Getting started with numpy.ndarray

In this section, we will go over some of the internals of numpy ndarray, including its structure and behavior. Let's start. Type in the following statements in the IPython prompt:

In [1]: import numpy as np 
 
In [2]: x = np.array([[1,2,3],[2,3,4]]) 
 
In [3]: print(x)

NumPy shares the names of its functions with functions in other modules, such as the math module in the Python standard library. Using imports like the following there is not recommended:

from numpy import * 

As it may overwrite many functions that are already in the global namespace, which is not recommended. This may lead to unexpected behavior from your code and may introduce very subtle bugs in it . This may also create conflicts in the code itself, (example numPy has any and will cause conflicts with the system any keyword) and may cause confusion when reviewing or debugging a piece of code. Therefore, it is important and recommended to always follow the import numPy with explicit name such as np convention used in the first line: , — import numpy as np, which is the standard convention used for the purpose of for importing, as it helps the a developer figure out where a function comes from. This can prevent a lot of confusion in large programs..

NumPy arrays can be created in a number of ways, as we shall see. One of the simplest ways of creating arrays is using the array function. Notice that we passed a list of lists to the function, and the constituent lists were equal in length. Each constituent list became a row in the array, and the elements of these lists populated the columns of the resulting array. The array function can be called on lists or even nested lists. Since the level of nesting in our input here was two, the resulting array is two-dimensional. This means that the array can be indexed with a set of two integers. The simplest way of calculating the dimensionality of an array is by checking the ndim attribute of the array:

In [4]: x.ndim 
 
Out [4]: 2 

This can also be accomplished in a different (and indirect) way-by checking the shape attribute of the array. The dimensionality of the array will be equal to how many numbers you see in the shape attribute. (Note that this, however, is not the purpose of the shape attribute.):

In [5]: x.shape 
 
Out [5]: (2, 3) 

This means that the array has two rows and three columns. It is important to note that, unlike MATLAB and R, the indexing of NumPy arrays is zero-based; that is, the first element of a NumPy array is indexed by a zero and the last element is indexed by the integer n-1, where n is the length of the array along the respective dimension. Thus, in the case of the array we just created, the element in the top-left corner of the array can be accessed using a pair of zeros, and the one in the bottom-right corner can be accessed with indices (1,2):

In [6]: x 
 
Out[6]: 
array([[1, 2, 3], 
       [2, 3, 4]]) 
 
In [7]: x[0,0] 
 
Out[7]: 1 
 
In [8]: x[1,2] 
 
Out[8]: 4 

The ndarray object has a lot of useful methods. To get a list of the methods that can be called on an ndarray object, type the array variable (in the preceding example, it's x) in the IPython prompt and press Tab. This should list all the methods available for the object. As an exercise, try playing around with a few of them.

主站蜘蛛池模板: 宜川县| 郓城县| 宝兴县| 栾城县| 昭苏县| 金沙县| 璧山县| 驻马店市| 南安市| 松原市| 永寿县| 夹江县| 寻乌县| 台中市| 荥阳市| 安乡县| 万荣县| 堆龙德庆县| 安宁市| 全椒县| 泽普县| 宜阳县| 斗六市| 历史| 个旧市| 富阳市| 手游| 玉环县| 罗城| 太仓市| 南华县| 万盛区| 三台县| 黄大仙区| 锡林郭勒盟| 沅江市| 东安县| 广水市| 承德县| 兴化市| 含山县|