4.1 Arrays
this chapter is about homogeneous arrays containing numeric data( an array where all elements have the same numeric type)
1-dimensional arrays:
>>> x = [0.0, 1, 2, 3, 4]
>>> y = array(x)
>>> y
array([ 0., 1., 2., 3., 4.])
>>> type(y)
numpy.ndarray
2-dimensional arrays are initialized by nested lists:
>>> y = array([[0.0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
>>> y
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.]])
>>> shape(y)
(2L, 5L)
>>> y = array([[[1,2],[3,4]],[[5,6],[7,8]]])
>>> y
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
>>> shape(y)
(2L, 2L, 2L)
4.1.4 Array dtypes
homogeneous arrays can contain a varity of numeric data types. all int → 'int32', int and floats →'float64'
NumPy attampts to find the smallest data type which can represent the data when constructing an array. You can use the keyword arguemnt dtype=datetype
to select a particular dtype
>>> x = [0, 1, 2, 3, 4]
>>> y = array(x, dtype = 'float64')
>>>y.dtype
dtype('float64')
4.2 Matrix
Matrices are a subset of arrays but always have 2 dimensions and follow the rules of linear algebra for *.
function mat
or asmatrix
can generate a matrix:
>>> x = [0.0, 1, 2, 3, 4]
>>> z = asmatrix(x)
>>> type(z)
numpy.matrixlib.defmatrix.matrix
4.3 1-dimensional Arrays
the difference
>>> w = array([[1.0, 2.0, 3.0, 4.0]])
>>> shape(w)
(1, 4)
>>> q = array([1.0, 2.0, 3.0, 4.0])
>>> shape(q)
(4,)
w is a 2x2 matrix while q is only a vector. Or q is a 1-dimensional array.
4.4 2-dimensional Arrays
two methods:
4.4.1 array()
>>> x=array([[1.0,2.0,3.0,4.0,5.0]])
array([[ 1., 2., 3., 4., 5.]])
>>> ndim(x) 2
4.4.2 matrix()
matrix()
need only one[]
, while array()
need two to make a nested list.
>>>x = matrix([1.0, 2.0, 3.0, 4.0, 5.0])
>>>x
matrix([[ 1., 2., 3., 4., 5.]])
>>> ndim(x) 2
4.4.3 general 2-dimensional Arrays
>>> x = array([[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]])
>>> x
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
to generate a 3x3 matrix
>>> x=matrix([[1.0],[2.0],[3.0],[4.0],[5.0]])
>>> x
matrix([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])
>>> x = array([[1.0],[2.0],[3.0],[4.0],[5.0]])
>>> x
array([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])
to generate a 5x1 matrix.
4.5 Multidimensional Arrays
Multidimensional arrays are available for N up to about 30, depending on the size of each matrix dimension. Function such as zeros((2,2,2))
and empty((2, 2, 2))
can be used to avoid tedious work and error.
4.6 Concatenation
concatenation is the process by which one vector or matrix is appended to another.
function1:concatenate
, and the keyword argument axis = 0 if arrays are to be vertically or axis = 1 if horizontally.
>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> y = array([[5.0,6.0],[7.0,8.0]])
>>> z = concatenate((x,y),axis = 0)
>>> z
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.],
[ 7., 8.]])
>>> z = concatenate((x,y),axis = 1)
>>> z
array([[ 1., 2., 5., 6.],
[ 3., 4., 7., 8.]])
functions2: vstack
and hstack
>>> z = vstack((x,y)) # Same as z = concatenate((x,y),axis = 0)
>>> z = hstack((x,y)) # Same as z = concatenate((x,y),axis = 1)
4.7 Accessing Elements of an Array
four ways: Scalar selection, slicing, numerical indexing and logical( or Boolean) indexing.
4.7.1 Scalar Selection
x[i] for 1-dimensional arrays
[i, j] for 2-dimensional arrays
and [i1, i2, i3…in] for general n-dimensional arrays
>>> x = array([1.0,2.0,3.0,4.0,5.0])
>>> x[0]
1.0
>>> x = array([[1.0,2,3],[4,5,6]])
>>> x[1,2]
6.0
>>> type(x[1,2])
numpy.float64
note: index begin from 0
Scalar selection can also be used to assign values in an array.
>>> x = array([1.0,2.0,3.0,4.0,5.0])
>>> x[0] = -5
>>> x
array([-5., 2., 3., 4., 5.])
4.7.2 Array slicing
[:,:,…,:]
to slice
standard: `[a:b:s] 1-dimension select every sTH elements where the indices i satisfy a<=i <b so that starting value a is always inclued in the list and the ending value b is always excluded.
for short:
- : and :: are the same as 0:n:1
- a: and a:n are the same as a:n:1 where n is the length of the array
- :b is the same as 0:b:1
-::s is the same as 0:n:s where n is the length of the array.
to dimension:
y[a:b, c:d]
is the same as y[a:b,:]
or y[a:b][:,c:d]
4.7.3 Mixed Selection using Scalar and Slice Selectors
>>> x = array([[0.0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
>>> x[:1,:] # Row 0, all columns, 2-dimensional
array([[ 0., 1., 2., 3., 4.]])
>>> ndim(x[:1,:])
2
>>> x[0,:] # Row 0, all column, dim reduction to 1-d array
array([ 0., 1., 2., 3., 4.])
>>> ndim(x[0,:])
1
>>> x[0,0] # Top left element, dim reduction to scalar (0-d array)
0.0
>>> ndim(x[0,0])
0
>>> x[:,0] # All rows, 1 column, dim reduction to 1-d array
array([ 0., 5.])
y[:1,:]
return a 2-dimension array, while y[0,:]
return 1-dimension array and y[0, 0]
return a scalar( or 0-dimensional array)
4.7.4 Assignment using Slicing
noteto place a float into the array results in the float being truncated and stored as an integer.
arrays should be initialized to contain floats unless a considered decision is taken to use a different data type.
4.7.5 Linear Slicing using flat
In 2-dimensions, linear slicing works by first counting across rows, and then down columns. To use linear slicing, the method or function flat must first be used.
>>> y = reshape(arange(25.0),(5,5))
>>> y
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24.]])
>>> y.flat[0] # Scalar slice, flat is 1-dimensional
0
>>> y[6] # Error
IndexError: index out of bounds
>>> y.flat[6] # Element 6
6.0
>>> y.flat[12:15]
array([ 12., 13., 14.])
>>> y.flat[:] # All element slice
array([[ 0., 1., 2., 3., 4.,
5., 6., 7.,
11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.,
22., 23., 24.]])
4.8 Slicing and Memory Management
slice of arrays do not copy the underlying data.changes to silce will change underlying array.
to avoid change of slice to underlying array, three methods can be applied.
>>> x = reshape(arange(4.0),(2,2))
>>> s1 = copy(x[0,:]) # Function copy
>>> s2 = x[:,0].copy() # Method copy
>>> s3 = array(x[0,:]) # Create a new array
while using pure scalar selection value will return a copy.
>>> x = arange(5.0)
>>> y = x[0] # Pure scalar selection
>>> z = x[:1] # A pure slice
>>> y = -3.14
>>> y # y Changes
-3.14
>>> x # No propagation
array([ 0., 1., 2., 3., 4.])
>>> z # No changes to z either
array([ 0.])
>>> z[0] = -2.79
>>> y # No propagation since y used pure scalar selection
-3.14
>>> x # z is a view of x, so changes propagate
array([-2.79, 1. , 2. , 3. , 4. ])
assignments from functions which change values will automatically creat a copy of the underlying array.
>>> x = array([[0.0, 1.0],[2.0,3.0]])
>>> y = x
>>> print(id(x),id(y)) # Same
129186368 129186368
>>> y = x + 1.0
>>> y
array([[ 1., 2.],
[ 3., 4.]])
>>> print(id(x),id(y)) # Different
129186368 129183104
>>> x # Unchanged
array([[ 0., 1.],
[ 2., 3.]])
>>> y = exp(x)
>>> print(id(x),id(y)) # Also Different
129186368 129185120
y = x + 0.0
can be used to copy x and change to y should not propagate to x.
4.9 import and Modules
from pylab import *
dangerous bacause function confllict.
import pylab
call function:pylab.log2
for short:
import pylab as pl
4.10 Calling Functions
function can take more than one input and return more than one output. `out1, out2, out3,… = functionname( in1, in2, in3,…)
- if multiple output are returned, but only one output variable is provided, the output will be a tuple.
- if more than one output variable is given in a function call, the number of output must match the number of output provided by the function.
- both input and outputs must be separated by commas( , )
- inputs can be the result of other functions.
Required Arguments
array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
object: required input
keyword arguments: dtype, copy, order, subok, ndmin
Keyword Arguments
- do NOT have to appear in order.(BUT you'd better list in order)
- do not to use when not needed.
Multiple Outputs
when a function can generate mutiple outputs, functions can be used in a single output mode or in multiple mode.
>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> s = shape(x)
>>> s
(2L, 2L)
>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> M,N = shape(x)
>>> M
2L
>>> N 2L
>>> M,N,P = shape(x) # Error
ValueError: need more than 2 values to unpack
>>> x = randn(10,10,10)
>>> shape(x)
(10L, 10L, 10L)
>>> M,N = shape(x) # Error
ValueError: too many values to unpack