1、 What is Numpy
Very simple, Numpy is a scientific computing library of Python, which provides the function of matrix operations. It is generally used with Scipy and matplotlib. In fact, list already provides a representation similar to a matrix, but numpy provides us with more functions. If you have been exposed to matlab and scilab, then numpy is a good starting point. In the following code example, numpy is always imported first:
code show as below:
import numpy as np
print np.version.version
1.6.2
2、 Multidimensional Arrays
The type of multidimensional array is: numpy.ndarray.
Use numpy.array method
Generate a one-dimensional array with list or tuple variables as parameters:
code show as below:
print np.array([1,2,3,4])
[1 2 3 4]print np.array((1.2,2,3,4))
[ 1.2 2. 3. 4. ]print type(np.array((1.2,2,3,4)))
< type 'numpy.ndarray'>
Generate a two-dimensional array with list or tuple variables as elements:
code show as below:
print np.array([[1,2],[3,4]])
[[1 2]
[3 4]]
When generating an array, you can specify the data type, such as numpy.int32, numpy.int16, and numpy.float64, etc.:
code show as below:
print np.array((1.2,2,3,4), dtype=np.int32)
[1 2 3 4]
Use numpy.arange method
code show as below:
print np.arange(15)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]print type(np.arange(15))
< type 'numpy.ndarray'>print np.arange(15).reshape(3,5)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]print type(np.arange(15).reshape(3,5))
< type 'numpy.ndarray'>
Use numpy.linspace method
For example, to generate 9 numbers from 1 to 3:
code show as below:
print np.linspace(1,3,9)
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]
Use numpy.zeros, numpy.ones, numpy.eye and other methods to construct a specific matrix
E.g:
code show as below:
print np.zeros((3,4))
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]print np.ones((3,4))
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]print np.eye(3)
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
Create a three-dimensional array:
code show as below:
print np.zeros((2,2,2))
[[[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]]]
**Get the attributes of the array: **
code show as below:
a = np.zeros((2,2,2))
print a.ndim #Dimensions of the array
3print a.shape #The size of each dimension of the array
(2, 2, 2)print a.size #number of elements of the array
8print a.dtype #Element type
float64print a.itemsize #The number of bytes occupied by each element
8
Array index, slice, assignment
Example:
code show as below:
a = np.array( [[2,3,4],[5,6,7]] )
print a
[[2 3 4]
[5 6 7]]print a[1,2]
7print a[1,:]
[5 6 7]print a[1,1:2]
[6]a[1,:] = [8,9,10]
print a
[[ 2 3 4]
[ 8 9 10]]
Use for to manipulate elements
code show as below:
for x in np.linspace(1,3,3):
... print x
...
1.0
2.0
3.0
Basic array operations
First construct the arrays a and b:
code show as below:
a = np.ones((2,2))
b = np.eye(2)
print a
[[ 1. 1.]
[ 1. 1.]]print b
[[ 1. 0.]
[ 0. 1.]]
Addition, subtraction, multiplication and division of arrays:
code show as below:
print a > 2
[[ False False]
[ False False]]print a+b
[[ 2. 1.]
[ 1. 2.]]print a-b
[[ 0. 1.]
[ 1. 0.]]print b*2
[[ 2. 0.]
[ 0. 2.]]print (a2)(b*2)
[[ 4. 0.]
[ 0. 4.]]print b/(a*2)
[[ 0.5 0. ]
[ 0. 0.5]]print (a*2)**4
[[ 16. 16.]
[ 16. 16.]]
Use the method that comes with the array object:
code show as below:
a.sum()
4.0a.sum(axis=0) #Calculate the sum of each column (a column similar to a matrix in a two-dimensional array)
array([ 2., 2.])a.min()
1.0a.max()
1.0
Use the method under numpy:
code show as below:
np.sin(a)
array([[ 0.84147098, 0.84147098],
[ 0.84147098, 0.84147098]])np.max(a)
1.0np.floor(a)
array([[ 1., 1.],
[ 1., 1.]])np.exp(a)
array([[ 2.71828183, 2.71828183],
[ 2.71828183, 2.71828183]])np.dot(a,a) ##Matrix multiplication
array([[ 2., 2.],
[ 2., 2.]])
Combine arrays
Use the vstack and hstack functions under numpy:
code show as below:
a = np.ones((2,2))
b = np.eye(2)
print np.vstack((a,b))
[[ 1. 1.]
[ 1. 1.]
[ 1. 0.]
[ 0. 1.]]print np.hstack((a,b))
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]
See if these two functions involve shallow copy issues:
code show as below:
c = np.hstack((a,b))
print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]a[1,1] = 5
b[1,1] = 5
print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]
It can be seen that the change of elements in a and b does not affect c.
Deep copy array
Array objects come with shallow copy and deep copy methods, but generally use deep copy more:
code show as below:
a = np.ones((2,2))
b = a
b is a
Truec = a.copy() #deep copy
c is a
False
Basic matrix operations
Transpose:
code show as below:
a = np.array([[1,0],[2,3]])
print a
[[1 0]
[2 3]]print a.transpose()
[[1 2]
[0 3]]
trace:
code show as below:
print np.trace(a)
4
There are many methods for matrix operations in the numpy.linalg module:
code show as below:
import numpy.linalg as nplg
Eigenvalue, eigenvector:
code show as below:
print nplg.eig(a)
( array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))
3、 matrix
Numpy can also construct matrix objects, which is not discussed here.
Recommended Posts