您的位置:首页 > 编程语言 > Python开发

Numpy学习笔记——矩阵和通用函数

2017-09-06 19:56 288 查看

5.1 矩阵

在NumPy中,矩阵是ndarray的子类,可以由专用的字符串格式来创建,使用mat、matrix以及bmat函数来创建矩阵。

5.2 动手实践:创建矩阵

在创建矩阵的专用字符串中,矩阵的行与行之间用分号隔开,行内的元素之间用空格隔开:

A = np.mat('1 2 3; 4 5 6; 7 8 9')
print "Creation from string", A


使用NumPy数组进行创建:

print "Creation from array", np.mat(np.arange(9).reshape(3, 3))


用T属性获取转置矩阵:

print "transpose A", A.T


用I属性获取逆矩阵:

print "Inverse A", A.I


5.3 从已有矩阵创建新矩阵

用bmat函数来实现。这里的b表示“分块”,bmat即分块矩阵(block matrix)。

创建一个2×2的单位矩阵:

A = np.eye(2)
print "A", A

B = 2 * A
print "B", B


使用字符串创建复合矩阵,该字符串的格式与mat函数中一致:

print "Compound matrix\n", np.bmat("A B; A B")


5.5 通用函数

def ultimate_answer(a):
result = np.zeros_like(a)
result.flat = 42
return result


使用frompyfunc创建通用函数。指定输入参数的个数为1,随后的1为输出参数的个数:

ufunc = np.frompyfunc(ultimate_answer, 1, 1)

print "The answer", ufunc(np.arange(4))
print "The answer", ufunc(np.arange(4).reshape(2, 2))


5.7 通用函数的方法

通用函数有四个方法,不过这些方法只对输入两个参数、输出一个参数的ufunc对象有效,例如add函数。

 reduce

 accumulate

 reduceat

 outer

沿着指定的轴,在连续的数组元素之间递归调
c7d8
用通用函数,即可得到输入数组的规约(reduce)计算结果。对于add函数,其对数组的reduce计算结果等价于对数组元素求和:

a = np.arange(9)
print "Reduce", np.add.reduce(a)


accumulate方法同样可以递归作用于输入数组。但是与reduce方法不同的是,它将存储运算的中间结果并返回。因此在add函数上调用accumulate方法,等价于直接调用cumsum函数:

print "Accumulate", np.add.accumulate(a)
# Accumulate [ 0 1 3 6 10 15 21 28 36]


第一步用到索引值列表中的0和5,实际上就是对数组中索引值在0到5之间的元素进行reduce操作。

第二步用到索引值5和2。由于2比5小,所以直接返回索引值为5的元素。

第三步用到索引值2和7。这一步是对索引值在2到7之间的数组元素进行reduce操作。

第四步用到索引值7。这一步是对索引值从7开始直到数组末端的元素进行reduce操作。

print "Reduceat", np.add.reduceat(a, [0, 5, 2, 7])
# Reduceat [10 5 20 15]


outer方法返回一个数组,它的秩(rank)等于两个输入数组的秩的和。它会作用于两个输入数组之间存在的所有元素对。

print "Outer", np.add.outer(np.arange(3), a)
# Outer [[ 0 1 2 3 4 5 6 7 8]
#        [ 1 2 3 4 5 6 7 8 9]
#        [ 2 3 4 5 6 7 8 9 10]]


5.9 算术运算

在NumPy中,基本算术运算符+、-和*隐式关联着通用函数add、subtract和multiply.在数组的除法运算中涉及三个通用函数divide、true_divide和

floor_division,以及两个对应的运算符/和//。

divide函数在整数和浮点数除法中均只保留整数部分:

a = np.array([2, 6, 5])
b = np.array([1, 2, 3])
print "Divide", np.divide(a, b), np.divide(b, a)
# Divide [2 3 1] [0 0 0]


true_divide函数与数学中的除法定义更为接近,即返回除法的浮点数结果而不作截断:

print "True Divide", np.true_divide(a, b), np.true_divide(b, a)


floor_divide函数总是返回整数结果,相当于先调用divide函数再调用floor函数。

floor函数将对浮点数进行向下取整并返回整数:

print "Floor Divide", np.floor_divide(a, b), np.floor_divide(b, a) c = 3.14 * b
print "Floor Divide 2", np.floor_divide(c, b), np.floor_divide(b, c)


默认情况下,使用/运算符相当于调用divide函数:

from__future__import division

但如果在Python程序的开头有上面那句代码,则改为调用true_divide函数

print "/ operator", a/b, b/a


运算符//对应于floor_divide函数

print "// operator", a//b, b//a
print "// operator 2", c//b, b//c


5.11 模运算

计算模数或者余数,可以使用NumPy中的mod、remainder和fmod函数。当然,也可以使用%运算符。这些函数的主要差异在于处理负数的方式。fmod函数在这方面异于其他函数。

remainder函数逐个返回两个数组中元素相除后的余数。如果第二个数字为0,则直接返回0:

a = np.arange(-4, 4)
print "Remainder", np.remainder(a, 2)


mod函数与remainder函数的功能完全一致:

print "Mod", np.mod(a, 2)


%操作符仅仅是remainder函数的简写:

print "% operator", a % 2


fmod函数处理负数的方式与remainder、mod和%不同。所得余数的正负由被除数决定,与除数的正负无关:

print "Fmod", np.fmod(a, 2)


5.13 斐波那契数列

使用matrix函数创建矩阵,rint函数对浮点数取整,但结果仍为浮点数类型。

F = np.matrix([[1, 1], [1, 0]])
print "8th Fibonacci", (F ** 7)[0, 0]


利用黄金分割公式或通常所说的比奈公式(Binet’ s Formula),加上取整函数,就可以直接计算斐波那契数:

n = np.arange(1, 9)
sqrt5 = np.sqrt(5)
phi = (1 + sqrt5)/2
fibonacci = np.rint((phi**n - (-1/phi)**n)/sqrt5)


5.15 利萨茹曲线

利萨茹曲线由以下参数方程定义:

x = A sin(at + n/2)

y = B sin(bt)

a = float(sys.argv[1])
b = float(sys.argv[2])
t = np.linspace(-np.pi, np.pi, 201)

x = np.sin(a * t + np.pi/2)
y = np.sin(b * t)

plot(x, y)
show()


5.17 方波

方波可以近似表示为多个正弦波的叠加。事实上,任意一个方波信号都可以用无穷傅里叶级数来表示。

t = np.linspace(-np.pi, np.pi, 201)
k = np.arange(1, float(sys.argv[1]))
k = 2 * k - 1
f = np.zeros_like(t)
#对于一个数组中每个元素都进行的计算,可以直接像使用变量一样,直接对数组做计算就可以了
for i in range(len(t)):
f[i] = np.sum(np.sin(k * t[i])/k)
f = (4 / np.pi) * f
plot(t, f)
show()


5.19 锯齿波和三角波

t = np.linspace(-np.pi, np.pi, 201)
k = np.arange(1, float(sys.argv[1]))
f = np.zeros_like(t)
# 可以改进为不使用循环语句
for i in range(len(t)):
f[i] = np.sum(np.sin(2 * np.pi * k * t[i])/k)
f = (-2 / np.pi) * f
plot(t, f, lw=1.0)
plot(t, np.abs(f), lw=2.0)
show()


5.21 位操作函数和比较函数

第一个小技巧需要用XOR或者^操作符。XOR操作符又被称为不等运算符,因此当两个操作数的符号不一致时,XOR操作的结果为负数。在NumPy中,

^ 操作符对应于bitwise_xor函数,

< 操作符对应于less函数。

& 操作符对应于bitwise_and函数,

== 操作符对应于equal函数。

<< 操作符对应于left_shift函数。

x = np.arange(-9, 9)
y = -x
print "Sign different?", (x ^ y) < 0
print "Sign different?", np.less(np.bitwise_xor(x, y), 0)

print "Power of 2?\n", x, "\n", (x & (x - 1)) == 0
print "Power of 2?\n", x, "\n", np.equal(np.bitwise_and(x, (x - 1)), 0)

print "Modulus 4\n", x, "\n", x & ((1 << 2) - 1)
print "Modulus 4\n", x, "\n", np.bitwise_and(x, np.left_shift(1, 2) - 1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: