Numpy 矩阵计算

列表元素

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

n = 10
l = [i for i in range(n)]
l  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l * 2 
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

a = []
for e in l:
    a.append(2 * e)
a # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]


效率比较

当 n 值比较大的时候,非常耗时;
使用 for 循环效率远低于 生成表达式:(基本快一倍的时间) ;numpy 的计算则更快。

n = 1000000
l = [i for i in range(n)]

%%time 
a = []
for e in l:
    a.append(2 * e)
# CPU times: user 147 ms, sys: 15.2 ms, total: 163 ms
# Wall time: 161 ms

%%time 
a = [2 * e for e in l]
# CPU times: user 72.2 ms, sys: 20.5 ms, total: 92.7 ms
# Wall time: 91 ms


l1 = np.arange(n)
%%time
a = np.array(2*e for e in l1)
# CPU times: user 12.5 ms, sys: 5.32 ms, total: 17.8 ms
# Wall time: 17.7 ms


numpy 中将每个数组看做向量或矩阵,这种方式也称作 Universal Functions,支持所有运算符运算。

%%time
a = 2 * l1
# CPU times: user 3.58 ms, sys: 4.12 ms, total: 7.7 ms
# Wall time: 5.4 ms

l1 = np.arange(5)
l1 # array([0, 1, 2, 3, 4])

a = 2 * l1
a # array([0, 2, 4, 6, 8])

Universal Functions

基本运算


x + 1

'''
    array([[ 2,  3,  4,  5,  6],
           [ 7,  8,  9, 10, 11],
           [12, 13, 14, 15, 16]]) 
'''
 
x * 2 
'''
    array([[ 2,  4,  6,  8, 10],
           [12, 14, 16, 18, 20],
           [22, 24, 26, 28, 30]])
'''

 
# 保留浮点数除法
x / 2 

'''
    array([[0.5, 1. , 1.5, 2. , 2.5],
           [3. , 3.5, 4. , 4.5, 5. ],
           [5.5, 6. , 6.5, 7. , 7.5]])
'''
 
# 整数除法
x // 2 
'''
    array([[0, 1, 1, 2, 2],
           [3, 3, 4, 4, 5],
           [5, 6, 6, 7, 7]])
'''

 
# 幂运算
x ** 2
'''
    array([[  1,   4,   9,  16,  25],
           [ 36,  49,  64,  81, 100],
           [121, 144, 169, 196, 225]])
'''

 
# 取余
x % 2
'''
    array([[1, 0, 1, 0, 1],
           [0, 1, 0, 1, 0],
           [1, 0, 1, 0, 1]])
'''


特殊运算



# 绝对值
np.abs(x)

'''
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10],
           [11, 12, 13, 14, 15]])
'''

 
np.sin(x)

'''
    array([[ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427],
           [-0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849, -0.54402111],
           [-0.99999021, -0.53657292,  0.42016704,  0.99060736,  0.65028784]])
'''

 
np.exp(x)
'''
    array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
            1.48413159e+02],
           [4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03,
            2.20264658e+04],
           [5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06,
            3.26901737e+06]])
'''

 
np.power(3, x) # 3 的 x 次方

'''
    array([[       3,        9,       27,       81,      243],
           [     729,     2187,     6561,    19683,    59049],
           [  177147,   531441,  1594323,  4782969, 14348907]])
'''
 
np.log(x) # 对矩阵的元素取 log 值
'''
    array([[0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791],
           [1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509],
           [2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ]])
'''
 
# 以2为底数
np.log2(x)

'''
    array([[0.        , 1.        , 1.5849625 , 2.        , 2.32192809],
           [2.5849625 , 2.80735492, 3.        , 3.169925  , 3.32192809],
           [3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])
'''
 
# 以10为底数
np.log10(x)
'''
    array([[0.        , 0.30103   , 0.47712125, 0.60205999, 0.69897   ],
           [0.77815125, 0.84509804, 0.90308999, 0.95424251, 1.        ],
           [1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]])
'''

矩阵之间的计算

对两个计算的矩阵的尺寸有要求

a = np.arange(4).reshape(2, 2)
a
'''
    array([[0, 1],
           [2, 3]])
'''
 
b = np.full((2, 2), 10)
b

'''
    array([[10, 10],
           [10, 10]])
'''
 
a + b
'''
    array([[10, 11],
           [12, 13]])
'''
 
a * b # 对应元素相乘;非矩阵乘法
'''
    array([[ 0, 10],
           [20, 30]])
'''
 
a / b
'''
    array([[0. , 0.1],
           [0.2, 0.3]])
'''
 
# 矩阵乘法
np.dot(a, b) 
'''
    array([[10, 10],
           [50, 50]])
'''


 
a.dot(b)
 
'''
    array([[10, 10],
           [50, 50]])
'''


 
c = np.arange(4).reshape(2, 2)
c 
'''
    array([[0, 1],
           [2, 3]])
'''
 
a.dot(c) 
'''
 array([[ 2,  3],
           [ 6, 11]])
'''
    
np.dot(a, c) 
'''
    array([[ 2,  3],
           [ 6, 11]])
'''

 
# 转置
a.T
'''
    array([[0, 2],
           [1, 3]])
'''

向量和矩阵的运算

v = np.array([1, 2])
 
v + a
'''
    array([[1, 3],
           [3, 5]])
'''
 
a
'''
    array([[0, 1],
           [2, 3]])
'''
 
v2 = np.array([1, 2, 3])
a + v2  # 报错
 
'''
    ---------------------------------------------------------------------------
    
    ValueError                                Traceback (most recent call last)
    
    <ipython-input-48-a196cec3120e> in <module>
          1 v2 = np.array([1, 2, 3])
    ----> 2 a + v2
 
    ValueError: operands could not be broadcast together with shapes (2,2) (3,) 
'''

 
np.vstack([v] * a.shape[0])  # 实际是将这个矩阵 和 a 做处理

'''
    array([[1, 2],
           [1, 2]])
'''

 
a.shape # (2, 2)
 
a.size # 4

 
a.shape[0]  # 2
 
np.vstack([v] * a.shape[0])  + a
'''
    array([[1, 3],
           [3, 5]])
'''
 
## tile 函数

np.tile(v, (3, 2))
'''
    array([[1, 2, 1, 2],
           [1, 2, 1, 2],
           [1, 2, 1, 2]])
'''
 
v # array([1, 2])

 
a 
'''
    array([[0, 1],
           [2, 3]])
'''
 
v * a 
'''
    array([[0, 2],
           [2, 6]])
'''
 
a * v 
'''
    array([[0, 2],
           [2, 6]])
'''
 
v.dot(a)
'''
    array([4, 7])
'''
 
a.dot(v)
'''
    array([2, 8])
'''


矩阵的逆

方阵才可能存在逆矩阵;非方阵可以求伪逆矩阵。

a

'''
    array([[0, 1],
           [2, 3]])
'''
 
invA = np.linalg.inv(a)
invA 
'''
    array([[-1.5,  0.5],
           [ 1. ,  0. ]])
'''
 
a.dot(invA) 
'''
    array([[1., 0.],
           [0., 1.]])
'''
 
invA.dot(a) 
'''
    array([[1., 0.],
           [0., 1.]])
''' 

X = np.arange(16).reshape((2,8))
X
'''
    array([[ 0,  1,  2,  3,  4,  5,  6,  7],
           [ 8,  9, 10, 11, 12, 13, 14, 15]])
'''
 
np.linalg.inv(X)
 

'''
    ---------------------------------------------------------------------------
    
    LinAlgError                               Traceback (most recent call last)
    
    <ipython-input-71-47889a8f1529> in <module>
    ----> 1 np.linalg.inv(X)


    <__array_function__ internals> in inv(*args, **kwargs)


    ~/opt/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in inv(a)
        539     a, wrap = _makearray(a)
        540     _assert_stacked_2d(a)
    --> 541     _assert_stacked_square(a)
        542     t, result_t = _commonType(a)
        543 


    ~/opt/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in _assert_stacked_square(*arrays)
        202         m, n = a.shape[-2:]
        203         if m != n:
    --> 204             raise LinAlgError('Last 2 dimensions of the array must be square')
        205 
        206 def _assert_finite(*arrays):


    LinAlgError: Last 2 dimensions of the array must be square

'''

## 伪逆矩阵
pinvX = np.linalg.pinv(X)
pinvX
 
'''
    array([[-1.35416667e-01,  5.20833333e-02],
           [-1.01190476e-01,  4.16666667e-02],
           [-6.69642857e-02,  3.12500000e-02],
           [-3.27380952e-02,  2.08333333e-02],
           [ 1.48809524e-03,  1.04166667e-02],
           [ 3.57142857e-02, -7.30583920e-18],
           [ 6.99404762e-02, -1.04166667e-02],
           [ 1.04166667e-01, -2.08333333e-02]])
'''

 
pinvX.shape # (8, 2)
 
pinvX.dot(X)
'''
    array([[ 4.16666667e-01,  3.33333333e-01,  2.50000000e-01,
             1.66666667e-01,  8.33333333e-02,  4.78783679e-16,
            -8.33333333e-02, -1.66666667e-01],
           [ 3.33333333e-01,  2.73809524e-01,  2.14285714e-01,
             1.54761905e-01,  9.52380952e-02,  3.57142857e-02,
            -2.38095238e-02, -8.33333333e-02],
           [ 2.50000000e-01,  2.14285714e-01,  1.78571429e-01,
             1.42857143e-01,  1.07142857e-01,  7.14285714e-02,
             3.57142857e-02,  2.84494650e-16],
           [ 1.66666667e-01,  1.54761905e-01,  1.42857143e-01,
             1.30952381e-01,  1.19047619e-01,  1.07142857e-01,
             9.52380952e-02,  8.33333333e-02],
           [ 8.33333333e-02,  9.52380952e-02,  1.07142857e-01,
             1.19047619e-01,  1.30952381e-01,  1.42857143e-01,
             1.54761905e-01,  1.66666667e-01],
           [-5.84467136e-17,  3.57142857e-02,  7.14285714e-02,
             1.07142857e-01,  1.42857143e-01,  1.78571429e-01,
             2.14285714e-01,  2.50000000e-01],
           [-8.33333333e-02, -2.38095238e-02,  3.57142857e-02,
             9.52380952e-02,  1.54761905e-01,  2.14285714e-01,
             2.73809524e-01,  3.33333333e-01],
           [-1.66666667e-01, -8.33333333e-02, -2.22044605e-16,
             8.33333333e-02,  1.66666667e-01,  2.50000000e-01,
             3.33333333e-01,  4.16666667e-01]])
'''

 
X.dot(pinvX)  # 计算结果不完全是0,是计算机的浮点误差造成的 

'''
    array([[ 1.00000000e+00, -2.77555756e-16],
           [ 1.69309011e-15,  1.00000000e+00]])
'''
 
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,326评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,228评论 1 304
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,979评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,489评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,894评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,900评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,075评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,803评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,565评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,778评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,255评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,582评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,254评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,151评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,952评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,035评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,839评论 2 277

推荐阅读更多精彩内容