Difference between revisions of "Pyhton: NumPy Matrix dan Aljabar Linear"
Onnowpurbo (talk | contribs) (New page: Sumber: http://www.bogotobogo.com/python/python_numpy_matrix_tutorial.php difference between numpy dot() and inner() What's difference between numpy dot() and inner()? Let's look into ...) |
Onnowpurbo (talk | contribs) |
||
Line 8: | Line 8: | ||
Let's look into 2D array as an example: | Let's look into 2D array as an example: | ||
− | >>> a=np.array([[1,2],[3,4]]) | + | >>> a=np.array([[1,2],[3,4]]) |
− | >>> b=np.array([[11,12],[13,14]]) | + | >>> b=np.array([[11,12],[13,14]]) |
− | >>> np.dot(a,b) | + | >>> np.dot(a,b) |
− | array([[37, 40], | + | array([[37, 40], |
− | + | [85, 92]]) | |
− | >>> np.inner(a,b) | + | >>> np.inner(a,b) |
− | array([[35, 41], | + | array([[35, 41], |
− | + | [81, 95]]) | |
− | [1324] [11131214] | + | [1324] [11131214] |
With dot(): | With dot(): | ||
− | [1∗11+2∗133∗11+4∗131∗12+2∗143∗12+4∗14] = [37854092] | + | |
+ | [1∗11+2∗133∗11+4∗131∗12+2∗143∗12+4∗14] = [37854092] | ||
With inner(): | With inner(): | ||
− | [1∗11+2∗123∗11+4∗121∗13+2∗143∗13+4∗14] = [35814195] | + | [1∗11+2∗123∗11+4∗121∗13+2∗143∗13+4∗14] = [35814195] |
Line 32: | Line 33: | ||
The chapters on NumPy have been using arrays (NumPy Array Basics A and NumPy Array Basics B). However, for certain areas such as linear algebra, we may instead want to use matrix. | The chapters on NumPy have been using arrays (NumPy Array Basics A and NumPy Array Basics B). However, for certain areas such as linear algebra, we may instead want to use matrix. | ||
− | >>> import numpy as np | + | >>> import numpy as np |
− | >>> A = np.matrix([[1.,2], [3,4], [5,6]]) | + | >>> A = np.matrix([[1.,2], [3,4], [5,6]]) |
− | >>> A | + | >>> A |
− | matrix([[ 1., 2.], | + | matrix([[ 1., 2.], |
− | + | [ 3., 4.], | |
− | + | [ 5., 6.]]) | |
We may also take the Matlab style by giving a string rather than a list: | We may also take the Matlab style by giving a string rather than a list: | ||
− | >>> B = np.matrix("1.,2; 3,4; 5,6") | + | >>> B = np.matrix("1.,2; 3,4; 5,6") |
− | >>> B | + | >>> B |
− | matrix([[ 1., 2.], | + | matrix([[ 1., 2.], |
− | + | [ 3., 4.], | |
− | + | [ 5., 6.]]) | |
Line 58: | Line 59: | ||
Vectors are handled as matrices with one row or one column: | Vectors are handled as matrices with one row or one column: | ||
− | >>> x = np.matrix("10., 20.") | + | >>> x = np.matrix("10., 20.") |
− | >>> x | + | >>> x |
− | matrix([[ 10., 20.]]) | + | matrix([[ 10., 20.]]) |
− | >>> x.T | + | >>> x.T |
− | matrix([[ 10.], | + | matrix([[ 10.], |
− | + | [ 20.]]) | |
Here is an example for matrix and vector multiplication: | Here is an example for matrix and vector multiplication: | ||
− | >>> x = np.matrix("4.;5.") | + | >>> x = np.matrix("4.;5.") |
− | >>> x | + | >>> x |
− | matrix([[ 4.], | + | matrix([[ 4.], |
− | + | [ 5.]]) | |
− | + | ||
− | >>> A = np.matrix([[1.,2], [3,4], [5,6]]) | + | >>> A = np.matrix([[1.,2], [3,4], [5,6]]) |
− | >>> A | + | >>> A |
− | matrix([[ 1., 2.], | + | matrix([[ 1., 2.], |
− | + | [ 3., 4.], | |
− | + | [ 5., 6.]]) | |
− | + | ||
− | >>> A*x | + | >>> A*x |
− | matrix([[ 14.], | + | matrix([[ 14.], |
− | + | [ 32.], | |
− | + | [ 50.]]) | |
For vectors, indexing requires two indices: | For vectors, indexing requires two indices: | ||
− | >>> print x[0,0], x[1,0] | + | >>> print x[0,0], x[1,0] |
− | 4.0 5.0 | + | 4.0 5.0 |
Line 100: | Line 101: | ||
Rank | Rank | ||
− | >>> import numpy as np | + | >>> import numpy as np |
− | >>> A = np.ones((4,3)) | + | >>> A = np.ones((4,3)) |
− | >>> A | + | >>> A |
− | array([[ 1., 1., 1.], | + | array([[ 1., 1., 1.], |
− | + | [ 1., 1., 1.], | |
− | + | [ 1., 1., 1.], | |
− | + | [ 1., 1., 1.]]) | |
− | >>> np.rank(A) | + | >>> np.rank(A) |
− | 2 | + | 2 |
Note that the rank of the array is not the rank of the matrix in linear algebra (dimension of the column space) but the number of subscripts it takes! | Note that the rank of the array is not the rank of the matrix in linear algebra (dimension of the column space) but the number of subscripts it takes! | ||
Line 114: | Line 115: | ||
Scalars have rank 0: | Scalars have rank 0: | ||
− | >>> x = np.array(10) | + | >>> x = np.array(10) |
− | >>> x | + | >>> x |
− | array(10) | + | array(10) |
− | >>> np.rank(x) | + | >>> np.rank(x) |
− | 0 | + | 0 |
NumPy supports arrays of any dimension such as rank 3 (2x2x2): | NumPy supports arrays of any dimension such as rank 3 (2x2x2): | ||
− | >>> A = np.ones((2,2,2)) | + | >>> A = np.ones((2,2,2)) |
− | >>> A | + | >>> A |
− | array([[[ 1., 1.], | + | array([[[ 1., 1.], |
− | + | [ 1., 1.]], | |
− | + | ||
− | + | [[ 1., 1.], | |
− | + | [ 1., 1.]]]) | |
− | >>> A[1,0,1] | + | >>> A[1,0,1] |
− | 1.0 | + | 1.0 |
Line 137: | Line 138: | ||
dot product | dot product | ||
− | >>> A = np.array([[1,2],[3,4]]) | + | >>> A = np.array([[1,2],[3,4]]) |
− | >>> A | + | >>> A |
− | array([[1, 2], | + | array([[1, 2], |
− | + | [3, 4]]) | |
− | >>> b = np.array([10, 20]) | + | >>> b = np.array([10, 20]) |
− | >>> b | + | >>> b |
− | array([10, 20]) | + | array([10, 20]) |
− | >>> ans = np.dot(A,b) | + | >>> ans = np.dot(A,b) |
− | >>> ans | + | >>> ans |
− | array([ 50, 110]) | + | array([ 50, 110]) |
− | Ax = b : numpy.linalg | + | Ax = b : numpy.linalg |
Now we want to solve Ax = b: | Now we want to solve Ax = b: | ||
− | >>> import numpy as np | + | >>> import numpy as np |
− | >>> from numpy.linalg import solve | + | >>> from numpy.linalg import solve |
− | >>> A = np.array([[1,2],[3,4]]) | + | >>> A = np.array([[1,2],[3,4]]) |
− | >>> A | + | >>> A |
− | array([[1, 2], | + | array([[1, 2], |
− | + | [3, 4]]) | |
− | >>> b = np.array([10, 20]) | + | >>> b = np.array([10, 20]) |
− | >>> b | + | >>> b |
− | >>> x = solve(A,b) | + | >>> x = solve(A,b) |
− | >>> x | + | >>> x |
− | array([ 0., 5.]) | + | array([ 0., 5.]) |
Line 172: | Line 173: | ||
eigen values and vectors | eigen values and vectors | ||
− | >>> import numpy as np | + | >>> import numpy as np |
− | >>> from numpy.linalg import eig | + | >>> from numpy.linalg import eig |
− | >>> A = np.array([[1,2],[3,4]]) | + | >>> A = np.array([[1,2],[3,4]]) |
− | >>> eig(A) | + | >>> eig(A) |
− | (array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356], | + | (array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356], |
− | + | [ 0.56576746, -0.90937671]])) | |
The eig returns two tuples: the first one is the eigen values and the second one is a matrix whose columns are the two eigen vectors. | The eig returns two tuples: the first one is the eigen values and the second one is a matrix whose columns are the two eigen vectors. | ||
Line 183: | Line 184: | ||
We can unpack the tuples: | We can unpack the tuples: | ||
− | >>> eigen_val, eigen_vec = eig(A) | + | >>> eigen_val, eigen_vec = eig(A) |
− | >>> eigen_val | + | >>> eigen_val |
− | array([-0.37228132, 5.37228132]) | + | array([-0.37228132, 5.37228132]) |
− | >>> eigen_vec | + | >>> eigen_vec |
− | array([[-0.82456484, -0.41597356], | + | array([[-0.82456484, -0.41597356], |
− | + | [ 0.56576746, -0.90937671]]) | |
Line 197: | Line 198: | ||
We want to solve ∫30x4dx=2434: | We want to solve ∫30x4dx=2434: | ||
− | >>> from scipy.integrate import quad | + | >>> from scipy.integrate import quad |
− | >>> def f(x): | + | >>> def f(x): |
− | ... return x**4 | + | ... return x**4 |
− | ... | + | ... |
− | >>> quad(f, 0., 3.) | + | >>> quad(f, 0., 3.) |
− | (48.599999999999994, 5.39568389967826e-13) | + | (48.599999999999994, 5.39568389967826e-13) |
The returned tuple indicates (ans, error estimate). | The returned tuple indicates (ans, error estimate). | ||
Line 208: | Line 209: | ||
We can get the same answer if we use lambda instead: | We can get the same answer if we use lambda instead: | ||
− | >>> quad(lambda x: x**4, 0, 3) | + | >>> quad(lambda x: x**4, 0, 3) |
− | (48.599999999999994, 5.39568389967826e-13) | + | (48.599999999999994, 5.39568389967826e-13) |
Revision as of 08:54, 4 December 2015
Sumber: http://www.bogotobogo.com/python/python_numpy_matrix_tutorial.php
difference between numpy dot() and inner()
What's difference between numpy dot() and inner()?
Let's look into 2D array as an example:
>>> a=np.array([[1,2],[3,4]]) >>> b=np.array([[11,12],[13,14]]) >>> np.dot(a,b) array([[37, 40], [85, 92]]) >>> np.inner(a,b) array([[35, 41], [81, 95]])
[1324] [11131214]
With dot():
[1∗11+2∗133∗11+4∗131∗12+2∗143∗12+4∗14] = [37854092]
With inner():
[1∗11+2∗123∗11+4∗121∗13+2∗143∗13+4∗14] = [35814195]
NumPy Matrix
The chapters on NumPy have been using arrays (NumPy Array Basics A and NumPy Array Basics B). However, for certain areas such as linear algebra, we may instead want to use matrix.
>>> import numpy as np >>> A = np.matrix([[1.,2], [3,4], [5,6]]) >>> A matrix([[ 1., 2.], [ 3., 4.], [ 5., 6.]])
We may also take the Matlab style by giving a string rather than a list:
>>> B = np.matrix("1.,2; 3,4; 5,6") >>> B matrix([[ 1., 2.], [ 3., 4.], [ 5., 6.]])
A vector as a matrix
Vectors are handled as matrices with one row or one column:
>>> x = np.matrix("10., 20.") >>> x matrix(10., 20.) >>> x.T matrix([[ 10.], [ 20.]])
Here is an example for matrix and vector multiplication:
>>> x = np.matrix("4.;5.") >>> x matrix([[ 4.], [ 5.]]) >>> A = np.matrix([[1.,2], [3,4], [5,6]]) >>> A matrix([[ 1., 2.], [ 3., 4.], [ 5., 6.]]) >>> A*x matrix([[ 14.], [ 32.], [ 50.]])
For vectors, indexing requires two indices:
>>> print x[0,0], x[1,0] 4.0 5.0
Note
Though np.matrix takes a real matrix form and look pleasing, usually, for most of the cases, arrays are good enough.
Rank
>>> import numpy as np >>> A = np.ones((4,3)) >>> A array([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) >>> np.rank(A) 2
Note that the rank of the array is not the rank of the matrix in linear algebra (dimension of the column space) but the number of subscripts it takes!
Scalars have rank 0:
>>> x = np.array(10) >>> x array(10) >>> np.rank(x) 0
NumPy supports arrays of any dimension such as rank 3 (2x2x2):
>>> A = np.ones((2,2,2)) >>> A array([[[ 1., 1.], [ 1., 1.]], [[ 1., 1.], [ 1., 1.]]]) >>> A[1,0,1] 1.0
dot product
>>> A = np.array([[1,2],[3,4]]) >>> A array([[1, 2], [3, 4]]) >>> b = np.array([10, 20]) >>> b array([10, 20]) >>> ans = np.dot(A,b) >>> ans array([ 50, 110])
Ax = b : numpy.linalg
Now we want to solve Ax = b:
>>> import numpy as np >>> from numpy.linalg import solve >>> A = np.array([[1,2],[3,4]]) >>> A array([[1, 2], [3, 4]]) >>> b = np.array([10, 20]) >>> b >>> x = solve(A,b) >>> x array([ 0., 5.])
eigen values and vectors
>>> import numpy as np >>> from numpy.linalg import eig >>> A = np.array([[1,2],[3,4]]) >>> eig(A) (array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356], [ 0.56576746, -0.90937671]]))
The eig returns two tuples: the first one is the eigen values and the second one is a matrix whose columns are the two eigen vectors.
We can unpack the tuples:
>>> eigen_val, eigen_vec = eig(A) >>> eigen_val array([-0.37228132, 5.37228132]) >>> eigen_vec array([[-0.82456484, -0.41597356], [ 0.56576746, -0.90937671]])
Quadrature
We want to solve ∫30x4dx=2434:
>>> from scipy.integrate import quad >>> def f(x): ... return x**4 ... >>> quad(f, 0., 3.) (48.599999999999994, 5.39568389967826e-13)
The returned tuple indicates (ans, error estimate).
We can get the same answer if we use lambda instead:
>>> quad(lambda x: x**4, 0, 3) (48.599999999999994, 5.39568389967826e-13)