矩阵
1.矩阵和二维数组
多数情况下,一个二维数组就可以被看做是一个矩阵,二者之间没有本质性的差别,但作为矩阵,在个别属性和规则上有别于数组,因此Numpy为矩阵提供了专门的类matrix,该类是ndarray类的子类,通过重载基类方法,体现了这些差别。
二维数组的乘法:
X = [[a, b, c],
[d, e, f]]
Y = [[g, h, i],
[j, k, l]]
Z = XY =
[[ag, bh, ci],
[dj, ek, fl]]
同维数组之间相乘,对应位置的元素直接相乘。
矩阵乘法:
g h i
j k l
m n o
a b c A B C
d e f D E F
A = ag+bj+cm
B = ah+bk+cn
C = ai+bl+co
D = dg+ej+fm
E = dh+ek+fn
F = di+el+fo
2.创建矩阵实例
np.matrix(可被解释为矩阵的二维容器)
-> 矩阵对象,数据独立
np.matrix(可被解释为矩阵的二维容器, copy=False)
-> 矩阵对象,数据共享
np.mat(可被解释为矩阵的二维容器或字符串)
-> 矩阵对象,数据共享
mp.bmat(拼接描述字符串)
-> 拼接小矩阵为大矩阵
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = [[1, 2, 6],
[3, 5, 7],
[4, 8, 9]]
print(a, type(a))
b = np.array(a)
print(b, type(b))
c = np.mat(b)
print(c, type(c))
c *= 10
print(a, b, c, sep='\n')
#d = np.matrix(c)
d = np.matrix(c, copy=False)
print(d)
d += 1
print(d)
print(c)
e = np.mat('1 1; 1 1')
f = e + 1
g = f + 1
h = g + 1
print(e, f, g, h, sep='\n')
i = np.bmat('e f; g h')
print(i)
j = np.arange(1, 5).reshape(2, 2)
print(j)
k = np.arange(5, 9).reshape(2, 2)
print(k)
l = j * k
print(l)
m = np.mat(j)
print(m)
n = np.mat(k)
print(n)
o = m * n
print(o)
p = c.I # 矩阵的逆矩阵
print(p, p * c, sep='\n')
#q = b.I
通用函数
1.什么是通用函数
通用函数的本质就是一个ufunc类的实例化对象,在该对象的内部又封装了另一个函数,且该对象可被当做函数调用,而实际被调用的是其内部封装的函数。
np.frompyfunc(被封装函数, 参数个数, 返回值数)
->通用函数对象
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
def foo(a, b):
c = a + b
d = a - b
e = a * b
return c, d, e
# 基于通用函数的闭包
# ————从函数中返回封装了函数的通用函数
def hum(a):
def fun(b):
c = a + b
d = a - b
e = a * b
return c, d, e
return np.frompyfunc(fun, 1, 3)
print(type(foo))
A = np.array([1, 2, 3, 4, 5])
B = np.array([5, 4, 3, 2, 1])
bar = np.frompyfunc(foo, 2, 3)
print(type(bar))
C, D, E = bar(A, B)
print(C, D, E, sep='\n')
F, G, H = hum(100)(A)
print(F, G, H, sep='\n')
2.求和通用函数:add
通用函数是一个可被当做函数调用的对象,因此即可被视作函数,也可拥有自己的属性和方法。
np.add(数组, 数组) # 对应元素求和
np.add.reduce(数组) # 各元素求和
np.add.accumulate(数组) # 记录求和过程
np.add.reduceat(数组,索引序列) # 分段求和
np.add.outer(数组, 数组) # 计算外和
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.arange(1, 7)
print(a)
b = np.arange(4, 10)
print(b)
c = np.add(a, b)
print(c)
d = np.add.reduce(a)
print(d)
e = np.add.accumulate(a)
print(e)
f = np.add.reduceat(a, [0, 2, 4])
# 0 2 4
# 1 2 3 4 5 6
#
print(f)
g = np.add.outer([10, 20, 30], a)
print(g)
# + 1 2 3 4 5 6
# 10 11 12 13 14 15 16
# 20 ...
# 30 ...
h = np.outer([10, 20, 30], a)
print(h)
# x 1 2 3 4 5 6
# 10 10 20 30 40 50 60
# 20 ...
# 30 ...
3.除法通用函数:divide/true_divide/floor_divide
[5 5 -5 -5]<真除>[2 -2 2 -2]=[2.5 -2.5 -2.5 2.5]
np.divide
np.true_divide
/
[5 5 -5 -5]<地板除>[2 -2 2 -2]=[2 -3 -3 2]
np.floor_divide
//
[5 5 -5 -5]<天花板除>[2 -2 2 -2]=[3 -2 -2 3]
[5 5 -5 -5]<截断除>[2 -2 2 -2]=[2 -2 -2 2]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.array([5, 5, -5, -5])
b = np.array([2, -2, 2, -2])
print(a, b)
#c = np.divide(a, b)
#c = np.true_divide(a, b)
c = a / b
print(c)
#d = np.floor_divide(a, b)
d = a // b
print(d)
e = np.ceil(a / b).astype(int)
print(e)
f = (a / b).astype(int)
print(f)
4.模运算通用函数
被除数<除以>除数=商......余数
除数x商+余数=被除数
[5 5 -5 -5]<地板除>[2 -2 2 -2]=[2 -3 -3 2]...[1 -1 1 -1]
np.remainder()
np.mod()
%
[5 5 -5 -5]<截断除>[2 -2 2 -2]=[2 -2 -2 2]...[1 1 -1 -1]
np.fmod()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.array([5, 5, -5, -5])
b = np.array([2, -2, 2, -2])
print(a, b)
#c = np.remainder(a, b)
#c = np.mod(a, b)
c = a % b
print(c)
d = np.fmod(a, b)
print(d)
5.绝大部分的Python运算符都被Numpy以通用函数的形式进行了重载,以支持矢量运算
Python: 标量 ** 指数 -> 标量幂
Numpy: 矢量 ** 指数 -> 矢量幂
斐波那契数列:1 1 2 3 5 8 13 21 34 ...
f1=f2=1
fn=fn-1+fn-2, n=3,4,5...
11 11 11
F 10 10 10
11 21 32 53
10 11 21 32
F1 F2 F3 F4 ... F^n-1
12 3 4 5 n
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
n = 35
def fibo(n):
return 1 if n < 3 else fibo(n - 1) + fibo(n - 2)
print(fibo(n))
fn_1, fn_2 = 0, 1
for i in range(n):
fn = fn_1 + fn_2
fn_1, fn_2 = fn, fn_1
print(fn)
print(int((np.mat('1. 1.; 1. 0.') ** (n - 1))[0, 0]))
r5 = np.sqrt(5)
print(int((((1 + r5) / 2)**n - ((1 - r5) / 2)**n) / r5))
6.Numpy中的三角函数都是通用函数,都可以针对矢量计算其三角函数的值
震动合成:利萨茹曲线
x = Asin(at+fai)
y = Bsin(bt+sai)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
t = np.linspace(0, 2 * np.pi, 1000)
x = 10 * np.sin(9 * t + np.pi / 2)
y = 5 * np.sin(8 * t + np.pi / 3)
mp.figure('Lissajous', facecolor='lightgray')
mp.title('Lissajous', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.plot(x, y, c='orangered', label='Lissajous')
mp.legend()
mp.show()
波形合成
y = 4/1pi sin(1x) 1
y = 4/3pi sin(3x) 2
y = 4/5pi sin(5x) 3
y = 4/7pi sin(7x) 4
...
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
def wave(n):
k = np.arange(1, n + 1) * 2 - 1
def f(x):
return (4 / (k * np.pi) * np.sin(k * x)).sum()
return np.frompyfunc(f, 1, 1)
x = np.linspace(0, 2 * np.pi, 1000)
y1 = wave(1)(x)
y2 = wave(2)(x)
y3 = wave(3)(x)
y4 = wave(10)(x)
y5 = wave(100)(x)
y6 = wave(1000)(x)
mp.figure('Wave', facecolor='lightgray')
mp.title('Wave', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.plot(x, y1, label='n=1')
mp.plot(x, y2, label='n=2')
mp.plot(x, y3, label='n=3')
mp.plot(x, y4, label='n=10')
mp.plot(x, y5, label='n=100')
mp.plot(x, y6, label='n=1000')
mp.show()
7.位运算通用函数
1)异或:^算符/bitwise_xor函数/__xor__方法
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
if a^b<0 then a和b一定不同号
2)与:&算符/bitwise_and函数/__and__方法
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
a是2的幂
1 2^0 00000001 0 00000000
2 2^1 00000010 1 00000001
4 2^2 00000100 3 00000011
8 2^3 00001000 7 00000111
16 2^4 00010000 15 00001111
\______&_____/
|
v
0
if a & (a-1) == 0 then a是2的幂
3)或:|算符/bitwise_or函数/__or__方法
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
4)非:~算符/bitwise_not函数/__not__方法
~0 = 1
~1 = 0
5)移位
>>算符/right_shift函数/__rshift__方法,除2
<<算符/left_shift函数/__lshift__方法,乘2
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.arange(-5, 6)
print(a)
b = a.copy()
b[b < 0] *= -1
print(b)
#c = a ^ b
#c = np.bitwise_xor(a, b)
c = a.__xor__(b)
print(c)
print(np.where(c < 0)[0])
d = np.arange(1, 21)
print(d)
#e = d & (d - 1)
#e = np.bitwise_and(d, d - 1)
e = d.__and__(d - 1)
print(e)
print(d[e == 0])
f = np.array([11, 13, 17, 19, 21])
print(f)
#g = f >> 1
#g = np.right_shift(f, 1)
g = f.__rshift__(1)
print(g)
#h = f << 2
#h = np.left_shift(f, 2)
h = f.__lshift__(2)
print(h)
想要看更多的课程请微信关注SkrEric的编程课堂