Definition: A multidimensional array $A$ of dimension $d$ is a collection of individual data points indexed by $n$ numbers: i.e., an individual data point is denoted by
$$A[i_1, i_2, \dots, i_d]$$where $i_l = 1,\dots, n_l$ is the index corresponding to the $l$ axis, and where $n_l$ is the number of data points stored along the $l$ axis.
The shape of an array is the tuple
$$(n_1,\dots,n_d).$$Remark 1: The dimension above is different from the notion of dimension in linear algebra, which is the number of entries in the array (i.e. $n_1n_2\dots n_d$).
Remark 2: The dimension in the definition above emphasis the fact that a multidimensional array of dimension $d$ can be geometrically regarded as a $d$ dimensinal cube of numbers sitting in $\mathbb R^d$.
Construct a list by enumerating its elements
In [5]:
a = [1, 3, 4, 5, 6]
Construct a list by comprehension
In [6]:
b = [x**2 for x in range(100)]
In [7]:
A = [ [1, 2, 3], [4, 5, 6] ]
A
Out[7]:
In [8]:
A[0]
Out[8]:
In [9]:
A[0][1]
Out[9]:
Nest one more level of list
Shortcomings:
In [9]:
In [10]:
from numpy import array
To instanciate an numpy array, you may pass to its constructor a representation of your multidimensional array in terms of nested lists.
In [11]:
Arr = array(A)
print Arr
print '\n'
print A
Numpy construct 2D arrays from nested lists $[a_1,\dots, a_m]$, where $a_i=[a_{i1},\dots, a_{in}]$ by interpreting the list elements $a_i$ as the rows of the resulting 2D array: this is the row-major convention. (R will follow the column-major convention).
The array constructor has several optional argument customizing the behaviour of the array object that it creates. The most important for us is the dtype
argument, which can take among many possible values the two following ones:
float64
for real numbersint64
for integers
In [23]:
a = [[1,2,3], [4,5,6]]
A = array(a, dtype = "float64")
A
Out[23]:
Remarks: If one tries to assign (using the bracket opearator) a new value to an array component
A[i,j] = val
python will try to convert val
to an object of the dtype
class (whose name isstored as an attribute of the array object:
A.dtype
In [28]:
b = [1.9, 2, 4, 9, 0, 4, 2, 9]
B = array(b)
B.shape
Out[28]:
Caution: The reshape
methode returns an new array with desired shape, but does not modify the original array!
In [35]:
C = B.reshape((2, 4))
In [28]:
Barr = Arr.reshape((1,6))
Barr
Out[28]:
In [25]:
Arr.reshape((3,2))
Out[25]:
In [34]:
C = array([x**2 for x in range(25)]).reshape((5,5))
In [33]:
C
Out[33]:
In [46]:
from numpy import linspace
In [51]:
A = linspace(0, 100, 100)
A.size
A.reshape((10,10))
Out[51]:
In [45]:
A = array(range(25)).reshape((5, 5))
A
Out[45]:
In [48]:
Ind = A < 10
In [49]:
A[Ind]
Out[49]:
In [50]:
A[2,3]
Out[50]:
In [51]:
A
Out[51]:
In [52]:
A[0:2, 1:4]
Out[52]:
In [53]:
A[0:5:2, 0:5:2]
Out[53]:
In [54]:
A = array(range(25)).reshape((5,5))
B = array([x**2 for x in range(25)]).reshape((5,5))
In [55]:
A + B
Out[55]:
In [56]:
A * B
Out[56]:
In [58]:
from numpy import cos, exp
In [62]:
cos(A) * exp(B) - A / 3.0
Out[62]: