Pyhton: NumPy Matrix dan Aljabar Linear: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
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 ...
 
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:




difference between numpy dot() and inner()
==Apa beda antara numpy dot() and inner()==


What's difference between numpy dot() and inner()?
Mari kita lihat ke 2D array:


Let's look into 2D array as an example:
import numpy as np
a=np.array([[1,2],[3,4]])
b=np.array([[11,12],[13,14]])


>>> a=np.array([[1,2],[3,4]])
[[File:Screenshot from 2023-04-08 06-21-51.png|center|400px|thumb]]
>>> 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]])


np.dot(a,b)


[1324] [11131214]
[[File:Screenshot from 2023-04-08 06-32-19.png|center|400px|thumb]]


With dot():
array([[37, 40],
[1∗11+2∗133∗11+4∗131∗12+2∗143∗12+4∗14] = [37854092]
        [85, 92]])


With inner():
[1∗11+2∗123∗11+4∗121∗13+2∗143∗13+4∗14] = [35814195]




np.inner(a,b)


NumPy Matrix
[[File:Screenshot from 2023-04-08 06-34-07.png|center|400px|thumb]]


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.
array([[35, 41],
        [81, 95]])


>>> import numpy as np
==NumPy Matrix==
>>> 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:
Bab-bab tentang NumPy telah menggunakan array (NumPy Array Basics A dan NumPy Array Basics B). Namun, untuk area tertentu seperti aljabar linier, kita mungkin ingin menggunakan matriks.


>>> B = np.matrix("1.,2; 3,4; 5,6")
import numpy as np
>>> B
A = np.matrix([[1.,2], [3,4], [5,6]])
matrix([[ 1., 2.],
A
        [ 3., 4.],
        [ 5., 6.]])


matrix([[1., 2.],
        [3., 4.],
        [5., 6.]])


Kami juga dapat menggunakan gaya Matlab dengan memberikan string bukan list:


import numpy as np
B = np.matrix("1.,2; 3,4; 5,6")
B


matrix([[1., 2.],
        [3., 4.],
        [5., 6.]])


==Vector sebagai matrix==


Vektor ditangani sebagai matriks dengan satu baris atau satu kolom:


x = np.matrix("10., 20.")
x


A vector as a matrix
matrix([[10., 20.]])


Vectors are handled as matrices with one row or one column:
x.T


>>> x = np.matrix("10., 20.")
  matrix([[10.],
>>> x
        [20.]])
matrix([[ 10., 20.]])
>>> x.T
matrix([[ 10.],
        [ 20.]])


Here is an example for matrix and vector multiplication:
Berikut adalah contoh perkalian matriks dan vektor:


>>> x = np.matrix("4.;5.")
x = np.matrix("4.;5.")
>>> x
x
matrix([[ 4.],
        [ 5.]])


>>> A = np.matrix([[1.,2], [3,4], [5,6]])
matrix([[4.],
>>> A
        [5.]])
matrix([[ 1., 2.],
        [ 3., 4.],
A = np.matrix([[1.,2], [3,4], [5,6]])
        [ 5., 6.]])
A


>>> A*x
matrix([[1., 2.],
matrix([[ 14.],
        [3., 4.],
        [ 32.],
        [5., 6.]])
        [ 50.]])
A*x


For vectors, indexing requires two indices:
matrix([[14.],
        [32.],
        [50.]])


>>> print x[0,0], x[1,0]
Untuk vektor, pengindeksan memerlukan dua indeks:
4.0 5.0


print( x[0,0], x[1,0] )


4.0 5.0


Catatan


Meskipun np.matrix mengambil bentuk matriks nyata dan terlihat bagus, biasanya, untuk sebagian besar kasus, array sudah cukup baik.


Note
==Rank==


Though np.matrix takes a real matrix form and look pleasing, usually, for most of the cases, arrays are good enough.
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)  # rank sudah sudah tidak ada NumPy
# 2


Rank
Perhatikan bahwa pangkat array bukanlah pangkat matriks dalam aljabar linier (dimensi ruang kolom) tetapi jumlah subskrip yang diperlukan!


>>> import numpy as np
Skalar memiliki rank 0:
>>> 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!
x = np.array(10)
x


Scalars have rank 0:
array(10)


>>> x = np.array(10)
# np.rank(x)   # rank sudah sudah tidak ada NumPy
>>> x
# 0
array(10)
>>> np.rank(x)
0


NumPy supports arrays of any dimension such as rank 3 (2x2x2):
Menghitung rank matrix


>>> A = np.ones((2,2,2))
from numpy.linalg import matrix_rank
>>> A
matrix_rank(np.eye(4)) # Full rank matrix
array([[[ 1.,  1.],
        [ 1.,  1.]],


      [[ 1., 1.],
  4
        [ 1.,  1.]]])
>>> A[1,0,1]
1.0




I=np.eye(4); I[-1,-1] = 0. # rank deficient matrix
matrix_rank(I)
3
matrix_rank(np.ones((4,))) # 1 dimension - rank 1 unless all 0
1
matrix_rank(np.zeros((4,)))
0
NumPy mendukung array dari dimensi apa pun seperti peringkat 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
dot product


>>> A = np.array([[1,2],[3,4]])
A = np.array([[1,2],[3,4]])
>>> A
A
array([[1, 2],
 
      [3, 4]])
array([[1, 2],
>>> b = np.array([10, 20])
        [3, 4]])
>>> b
 
array([10, 20])
b = np.array([10, 20])
>>> ans = np.dot(A,b)
b
>>> ans
 
array([ 50, 110])
array([10, 20])
 
 
ans = np.dot(A,b)
ans
 
array([ 50, 110])
 
 




  Ax = b : numpy.linalg


Kita akan solve Ax = b:


Ax = b : numpy.linalg
import numpy as np
from numpy.linalg import solve
A = np.array([[1,2],[3,4]])
A


Now we want to solve Ax = b:
array([[1, 2],
        [3, 4]])


>>> import numpy as np
b = np.array([10, 20])
>>> from numpy.linalg import solve
b
>>> 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.])


array([10, 20])


x = solve(A,b)
x


array([0., 5.])


eigen values and vectors
==Eigen Value dan Vector==


>>> 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],
      [ 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:
(array([-0.37228132,  5.37228132]),
  array([[-0.82456484, -0.41597356],
        [ 0.56576746, -0.90937671]]))


>>> eigen_val, eigen_vec = eig(A)
Eig mengembalikan dua tupel: yang pertama adalah nilai eigen dan yang kedua adalah matriks yang kolomnya berupa dua vektor eigen.
>>> eigen_val
array([-0.37228132,  5.37228132])
>>> eigen_vec
array([[-0.82456484, -0.41597356],
      [ 0.56576746, -0.90937671]])




Kami dapat membongkar tupel:


eigen_val, eigen_vec = eig(A)
eigen_val


Quadrature
array([-0.37228132,  5.37228132])


We want to solve ∫30x4dx=2434:
eigen_vec


>>> from scipy.integrate import quad
array([[-0.82456484, -0.41597356],
>>> def f(x):
        [ 0.56576746, -0.90937671]])
...    return x**4
...  
>>> quad(f, 0., 3.)
(48.599999999999994, 5.39568389967826e-13)


The returned tuple indicates (ans, error estimate).
==Quadrature==


We can get the same answer if we use lambda instead:
Kita ingin menghitung ∫03 x^4 dx=2434:


>>> quad(lambda x: x**4, 0, 3)
from scipy.integrate import quad
(48.599999999999994, 5.39568389967826e-13)
def f(x): return x**4
quad(f, 0., 3.)


(48.599999999999994, 5.39568389967826e-13)


Tuple yang dikembalikan menunjukkan (ans, error estimate).


Kita bisa mendapatkan jawaban yang sama jika kita menggunakan lambda sebagai gantinya:


quad(lambda x: x**4, 0, 3)


(48.599999999999994, 5.39568389967826e-13)


==Referensi==
==Referensi==


* http://www.bogotobogo.com/python/python_numpy_matrix_tutorial.php
* http://www.bogotobogo.com/python/python_numpy_matrix_tutorial.php

Latest revision as of 00:27, 8 April 2023

Sumber: http://www.bogotobogo.com/python/python_numpy_matrix_tutorial.php


Apa beda antara numpy dot() and inner()

Mari kita lihat ke 2D array:

import numpy as np
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]])

NumPy Matrix

Bab-bab tentang NumPy telah menggunakan array (NumPy Array Basics A dan NumPy Array Basics B). Namun, untuk area tertentu seperti aljabar linier, kita mungkin ingin menggunakan matriks.

import numpy as np
A = np.matrix([[1.,2], [3,4], [5,6]])
A
matrix([[1., 2.],
        [3., 4.],
        [5., 6.]])

Kami juga dapat menggunakan gaya Matlab dengan memberikan string bukan list:

import numpy as np
B = np.matrix("1.,2; 3,4; 5,6")
B
matrix([[1., 2.],
        [3., 4.],
        [5., 6.]])

Vector sebagai matrix

Vektor ditangani sebagai matriks dengan satu baris atau satu kolom:

x = np.matrix("10., 20.")
x
matrix(10., 20.)
x.T
matrix([[10.],
        [20.]])

Berikut adalah contoh perkalian matriks dan vektor:

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.]])

Untuk vektor, pengindeksan memerlukan dua indeks:

print( x[0,0], x[1,0] )
4.0 5.0

Catatan

Meskipun np.matrix mengambil bentuk matriks nyata dan terlihat bagus, biasanya, untuk sebagian besar kasus, array sudah cukup baik.

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)   # rank sudah sudah tidak ada NumPy
# 2

Perhatikan bahwa pangkat array bukanlah pangkat matriks dalam aljabar linier (dimensi ruang kolom) tetapi jumlah subskrip yang diperlukan!

Skalar memiliki rank 0:

x = np.array(10)
x
array(10)
# np.rank(x)   # rank sudah sudah tidak ada NumPy
# 0

Menghitung rank matrix

from numpy.linalg import matrix_rank
matrix_rank(np.eye(4)) # Full rank matrix
4


I=np.eye(4); I[-1,-1] = 0. # rank deficient matrix
matrix_rank(I)
3


matrix_rank(np.ones((4,))) # 1 dimension - rank 1 unless all 0
1


matrix_rank(np.zeros((4,)))
0


NumPy mendukung array dari dimensi apa pun seperti peringkat 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

Kita akan 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
array([10, 20])


x = solve(A,b)
x
array([0., 5.])

Eigen Value dan Vector

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]]))

Eig mengembalikan dua tupel: yang pertama adalah nilai eigen dan yang kedua adalah matriks yang kolomnya berupa dua vektor eigen.


Kami dapat membongkar tupel:

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

Kita ingin menghitung ∫03 x^4 dx=2434:

from scipy.integrate import quad
def f(x): return x**4
quad(f, 0., 3.)
(48.599999999999994, 5.39568389967826e-13)

Tuple yang dikembalikan menunjukkan (ans, error estimate).

Kita bisa mendapatkan jawaban yang sama jika kita menggunakan lambda sebagai gantinya:

quad(lambda x: x**4, 0, 3)
(48.599999999999994, 5.39568389967826e-13)

Referensi