linear_algebra_for_machine_learning_101

Linear Algebra: Scalar, Vectors, Matrices, Tensors

  1. Scalar: a single number or value.
  2. Vector: an 1-Dimension array of numbers, either in a row or in a column, identified by an index.
  3. Matrix: a 2-Dimensions array of numbers, where each elements is identified by two indeces. 4. Tensors: more than 2-Dimensions; In general, an array of numbers with a variable number of Dimensions is known as a tensor.
  4. If a 2-Dimensions Matrix has shape (i,j), a 3-Dimensions Tensor would have shape (k,i,j); the number of Matrix (i,j) is k.
  5. If a 2-Dimensions Matrix has shape (i,j), a 4-Dimensions Tensor would have shape (l,m,i,j); the number of Matrix (i,j) is (l,m).
In [68]:
import sys
import numpy as np 

Defining a scalar

In [69]:
x = 6
x
Out[69]:
6

Defining a vector

In [95]:
x = np.array((1,2,3))
x
Out[95]:
array([1, 2, 3])
In [96]:
print ('Vector Dimensions: {}'.format(x.shape))
print ('Vector size: {}'.format(x.size))
Vector Dimensions: (3,)
Vector size: 3

Defining a matrix

In [97]:
x = np.array([[1,2,3],[4,5,6],[7,8,9]])
x
Out[97]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [98]:
print ('Matrix Dimensions: {}'.format(x.shape))
print ('Matrix size: {}'.format(x.size))
Matrix Dimensions: (3, 3)
Matrix size: 9

Defining a matrix of a given dimension

In [99]:
x = np.ones((3,3))
x
Out[99]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

Defining a tensor of a given dimension

2-Dimensions Matrix (3,5), the number of 2-Dimensions Matrix (3,5) is (2,3)

In [100]:
x = np.ones((2,3,3,5))
x
Out[100]:
array([[[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]],


       [[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]]])
In [101]:
print ('Tensor Dimensions: {}'.format(x.shape))
print ('Tensor size: {}'.format(x.size))
Tensor Dimensions: (2, 3, 3, 5)
Tensor size: 90

Indexing

In [102]:
A = np.ones((5,5), dtype = np.int)
A
Out[102]:
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])

Indexing starts at 0

In [103]:
A[0,1] = 2
A
Out[103]:
array([[1, 2, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
In [104]:
A[:,0] = 3
A
Out[104]:
array([[3, 2, 1, 1, 1],
       [3, 1, 1, 1, 1],
       [3, 1, 1, 1, 1],
       [3, 1, 1, 1, 1],
       [3, 1, 1, 1, 1]])
In [105]:
A[:,:] = 5
A
Out[105]:
array([[5, 5, 5, 5, 5],
       [5, 5, 5, 5, 5],
       [5, 5, 5, 5, 5],
       [5, 5, 5, 5, 5],
       [5, 5, 5, 5, 5]])

2-Dimensions Matrix (5,5), the number of 2-Dimensions Matrix (5,5) is 6

In [109]:
A = np.ones((6,5,5), dtype = np.int)
A
Out[109]:
array([[[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]]])

For higher dimensions, simply add an index; Assign first row a new value

In [108]:
A[:,0,0] = 3
A
Out[108]:
array([[[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[3, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]]])

Matrix operation

In [110]:
A = np.array([[1,2], [3,4]])

print(A)
print ('Matrix Dimensions: {}'.format(A.shape))
print ('Matrix size: {}'.format(A.size))
[[1 2]
 [3 4]]
Matrix Dimensions: (2, 2)
Matrix size: 4
In [111]:
B = np.ones((2,2), dtype = np.int)

print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
[[1 1]
 [1 1]]
Matrix Dimensions: (2, 2)
Matrix size: 4

Element wise sum

In [112]:
C = A + B

print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
[[2 3]
 [4 5]]
Matrix Dimensions: (2, 2)
Matrix size: 4

Element wise subtraction

In [85]:
C = A - B

print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
[[0 1]
 [2 3]]
Matrix Dimensions: (2, 2)
Matrix size: 4

Element wise multiplication

In [113]:
C = np.dot(A, B)

print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
[[3 3]
 [7 7]]
Matrix Dimensions: (2, 2)
Matrix size: 4

Matrix transpose

In [88]:
# matrix transpose
A = np.array(range(9))
A = A.reshape(3,3)

print(A)
print ('Matrix Dimensions: {}'.format(A.shape))
print ('Matrix size: {}'.format(A.size))
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Matrix Dimensions: (3, 3)
Matrix size: 9
In [89]:
B = A.T

print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
[[0 3 6]
 [1 4 7]
 [2 5 8]]
Matrix Dimensions: (3, 3)
Matrix size: 9
In [90]:
C = B.T

print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Matrix Dimensions: (3, 3)
Matrix size: 9
In [91]:
A = np.array(range(10))
A = A.reshape(2,5)

print(A)
print ('Matrix Dimensions: {}'.format(A.shape))
print ('Matrix size: {}'.format(A.size))
[[0 1 2 3 4]
 [5 6 7 8 9]]
Matrix Dimensions: (2, 5)
Matrix size: 10
In [92]:
B = A.T

print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
[[0 5]
 [1 6]
 [2 7]
 [3 8]
 [4 9]]
Matrix Dimensions: (5, 2)
Matrix size: 10
Tensor
In [94]:
# tensor
A = np.ones((3,3,3,3,3,3,3,3,3,3), dtype = np.int)

print ('Matrix Dimensions: {}'.format(A.shape))
print ('Matrix size: {}'.format(A.size))
Matrix Dimensions: (3, 3, 3, 3, 3, 3, 3, 3, 3, 3)
Matrix size: 59049
In [ ]:
 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据