
Read Time:189 Minute, 46 Second
Linear Algebra: Scalar, Vectors, Matrices, Tensors¶
- Scalar: a single number or value.
- Vector: an 1-Dimension array of numbers, either in a row or in a column, identified by an index.
- 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.
- 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.
- 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]:
Defining a vector¶
In [95]:
x = np.array((1,2,3))
x
Out[95]:
In [96]:
print ('Vector Dimensions: {}'.format(x.shape))
print ('Vector size: {}'.format(x.size))
Defining a matrix¶
In [97]:
x = np.array([[1,2,3],[4,5,6],[7,8,9]])
x
Out[97]:
In [98]:
print ('Matrix Dimensions: {}'.format(x.shape))
print ('Matrix size: {}'.format(x.size))
Defining a matrix of a given dimension¶
In [99]:
x = np.ones((3,3))
x
Out[99]:
In [100]:
x = np.ones((2,3,3,5))
x
Out[100]:
In [101]:
print ('Tensor Dimensions: {}'.format(x.shape))
print ('Tensor size: {}'.format(x.size))
Indexing¶
In [102]:
A = np.ones((5,5), dtype = np.int)
A
Out[102]:
Indexing starts at 0¶
In [103]:
A[0,1] = 2
A
Out[103]:
In [104]:
A[:,0] = 3
A
Out[104]:
In [105]:
A[:,:] = 5
A
Out[105]:
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]:
For higher dimensions, simply add an index; Assign first row a new value¶
In [108]:
A[:,0,0] = 3
A
Out[108]:
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))
In [111]:
B = np.ones((2,2), dtype = np.int)
print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
Element wise sum¶
In [112]:
C = A + B
print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
Element wise subtraction¶
In [85]:
C = A - B
print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
Element wise multiplication¶
In [113]:
C = np.dot(A, B)
print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
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))
In [89]:
B = A.T
print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
In [90]:
C = B.T
print(C)
print ('Matrix Dimensions: {}'.format(C.shape))
print ('Matrix size: {}'.format(C.size))
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))
In [92]:
B = A.T
print(B)
print ('Matrix Dimensions: {}'.format(B.shape))
print ('Matrix size: {}'.format(B.size))
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))
In [ ]: