37

机器学习: 线性代数 101 - Cloud-Native 产品级敏捷

 4 years ago
source link: https://www.deva9.com/ml/2165/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Skip to content

搜索:

机器学习: 线性代数 101

allie-smith-1700312-unsplash.jpg

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 [ ]:

Deep Learning

发表评论 取消回复

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

评论

姓名 *

电子邮件 *

站点

通过邮件通知我后续评论

通过邮件通知我有新文章

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

方俊贤; Ken Fang; A9 Atlas 工作室

2019, 08. 一种深度学习的算法, 预测微服务持续部署、持续发布后对产品整体质量的影响, 获得国家知识财产局专利; 符合专利法实施细则第 44 条的规定。
专利号: 201910652769.4

主要专长: 运用深度学习的算法模型分析开发者的行为, 以提升软件开发的效率与质量、微服务化的架构设计、探索性测试、有价值的产品特性挖掘、使用者行为 (场景) 分析、领域驱动设计。

曾任职于: 腾讯科技 (深圳) 有限公司 专家项目经理; 雅各布森软件 (北京) 有限公司 首席谘询顾问; Rational; Telelogic; Borland; 联华电子; 京元电子。

有二十多年半导体、 电信产业、军事研究单位与互联网的产品研发与咨询服务等的经验。

于 Illinois Institute of Technology, Chicago, USA 获得电子计算机科学硕士

kenfangtitle.png

通过电子邮件订阅博客

输入您的电子邮件地址订阅此博客,并通过电子邮件接收博客更新通知。

电子邮件地址

Copyright All rights reserved Theme: Blog Expert by Themeinwp

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK