Modules - adibaba/rescal.py GitHub Wiki

Used Python modules

  • NumPy (Website) is the fundamental package for scientific computing with Python.
    • numpy.array (ref) Create an array.
    • numpy.dot (ref) Dot product of two arrays. Specifically,
    • numpy.eye (ref) Return a 2-D array with ones on the diagonal and zeros elsewhere.
    • numpy.kron (ref) Kronecker product of two arrays.
    • numpy.prod (ref) Return the product of array elements over a given axis.
    • numpy.zeros (ref) Return a new array of given shape and type, filled with zeros.
    • numpy.linalg.inv (ref) Compute the (multiplicative) inverse of a matrix.
    • numpy.linalg.norm (ref) Matrix or vector norm.
    • numpy.linalg.solve (ref) Solve a linear matrix equation, or system of linear scalar equations.
    • numpy.linalg.svd (ref) Singular Value Decomposition.
    • numpy.random.rand (ref) Random values in a given shape.
  • SciPy is open-source software for mathematics, science, and engineering.
    • scipy.sparse.csr_matrix (ref) Compressed Sparse Row matrix
    • scipy.sparse.csr_matrix.shape (ref) Get shape of a matrix.
    • scipy.sparse.issparse (ref) Is x of a sparse matrix type?
    • scipy.sparse.linalg.eigsh (ref) Find k eigenvalues and eigenvectors of the real symmetric square matrix or complex hermitian matrix A.
  • Python functions
    • len (ref) Return the length (the number of items) of an object.
  • Python
    • kwargs (faq) How can I pass optional or keyword parameters from one function to another?

Example: numpy.linalg.svd

    A = np.array([2.0,-2,1],[5,1,4](/adibaba/rescal.py/wiki/2.0,-2,1],[5,1,4))
    U, S, Vt = svd(A, full_matrices=False)
    print('type(A): ', type(A))
    print('A.shape: ', A.shape)
    print('A.dtype: ', A.dtype)
    print('A: ', A)
    print('U: ', U)
    print('S: ', S)
    print('Vt: ', Vt)

type(A):  <class 'numpy.ndarray'>
A.shape:  (2, 3)
A.dtype:  float64
A:  [[ 2. -2.  1.]
 [ 5.  1.  4.]]
U:  [[-0.30924417 -0.95098267]
 [-0.95098267  0.30924417]]
S:  [ 6.77511666  2.25782954]
Vt:  [[-0.79310837 -0.04907581 -0.60710023]
 [-0.15756038  0.97935184  0.12666767]]

math source

Example: numpy.kron

    S = np.array([3.0,5,7])
    print('type(S): ', type(S))
    print('S.shape: ', S.shape)
    print('S.dtype: ', S.dtype)
    print('S: ', S)
    Shat = kron(S, S)
    print('Shat: ', Shat)
    quit()

type(S):  <class 'numpy.ndarray'>
S.shape:  (3,)
S.dtype:  float64
S:  [ 3.  5.  7.]
Shat:  [  9.  15.  21.  15.  25.  35.  21.  35.  49.]

Example: numpy.ndarray.reshape

    tmpA = (Shat / (Shat ** 2 + lmbdaR))
    print('type(tmpA): ', type(tmpA))
    print('tmpA.shape: ', tmpA.shape)
    print('tmpA.dtype: ', tmpA.dtype)
    print('tmpA: ', tmpA)
    print('rank: ', rank)
    tmpB = tmpA.reshape(rank, rank)
    print('type(tmpB): ', type(tmpB))
    print('tmpB.shape: ', tmpB.shape)
    print('tmpB.dtype: ', tmpB.dtype)
    print('tmpB: ', tmpB)
    quit()

type(tmpA):  <class 'numpy.ndarray'>
tmpA.shape:  (10000,)
tmpA.dtype:  float64
tmpA:  [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
  0.09090909]
rank:  100
type(tmpB):  <class 'numpy.ndarray'>
tmpB.shape:  (100, 100)
tmpB.dtype:  float64
tmpB:  [[ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]
 [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]
 [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]
 ..., 
 [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]
 [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]
 [ 0.09090909  0.09090909  0.09090909 ...,  0.09090909  0.09090909
   0.09090909]]