
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.