Python学习之路_numpy
这周老师给我们布置了一些关于numpy使用的题目,下面我来展示一下
0.
思路:
使用numpy中的randn来生成random Gaussian entries,使用自己写的Toeplitz函数生成Toeplitz矩阵
代码:
import numpy as np
def toeplitz(row_vector, column_vector):
res = row_vector
for i in range(1, column_vector.size):
row_vector = np.roll(row_vector, 1)
row_vector[0,0] = column_vector[i, 0]
res = np.vstack((res, row_vector))
return res
def myfunction(hh):
tmp = np.ones((500,500))
tmp = tmp*hh
return np.dot(A, B-tmp)
A = np.random.randn(200,500)
first_row = range(1,501)
first_col = range(1,501)
B = toeplitz(np.random.randn(1, 500), np.random.randn(500, 1))
结果:
1.
思路:
加减可以直接用,但是点乘不行,要使用np.dot()函数
代码:
def myfunction(hh):
tmp = np.ones((500,500))
tmp = tmp*hh
return np.dot(A, B-tmp)
print('A+A=',A+A)
print('A*A T =',np.dot(A, A.T))
print('A T *A=',np.dot(A.T, A))
print('A*B=',np.dot(A, B))
print('A*(B- 20*I)=',myfunction(20))
结果:
2.
思路:
使用numpy.linalg.solve()函数
代码:
import numpy as np
def toeplitz(row_vector, column_vector):
res = row_vector
for i in range(1, column_vector.size):
row_vector = np.roll(row_vector, 1)
row_vector[0,0] = column_vector[i, 0]
res = np.vstack((res, row_vector))
return res
A = np.random.randn(200,500)
first_row = range(1,501)
first_col = range(1,501)
B = toeplitz(np.random.randn(1, 500), np.random.randn(500, 1))
b = np.random.randn(500,1)
print(np.linalg.solve(B, b))
结果:
3.
思路:
使用 numpy.linalg.norm 函数得到范数。给参数 val 传入不同的值能够得到不同的范数,如传入 numpy.inf 得到无穷范数
代码:
print(np.linalg.norm(A, 'fro'))
print(np.linalg.norm(B, np.inf))
print(np.linalg.norm(B, 2))
print(np.linalg.norm(B, -2))
结果:
4.
思路:
使用time函数计时,使用count记录迭代次数,使用一个足够小的误差来判断循环是否终止
代码:
import numpy as np
import time
def find_max(y):
largest = abs(y[0, 0])
ans = y[0, 0]
for i in range(1, y.size):
if abs(y[i, 0]) > largest:
largest = abs(y[i, 0])
ans = y[i, 0]
return ans
def power_iteration(Z, eps):
time_stamp = time.clock()
# x = random.rand(n, 1)
x = np.ones((200, 1))
#迭代次数
count = 1
y = np.dot(Z, x)
c1 = find_max(y)
x = y / c1
#误差
c0 = c1 + eps + 1e7
while abs(c1 - c0) >= eps:
count += 1
y = np.dot(Z, x)
c0 = c1
c1 = find_max(y)
x = y / c1
'''''过程花费时间'''
duration = time.clock() - time_stamp
return x, c1, duration, count
# def toeplitz(row_vector, column_vector):
# res = row_vector
# for i in range(1, column_vector.size):
# row_vector = np.roll(row_vector, 1)
# row_vector[0,0] = column_vector[i, 0]
# res = np.vstack((res, row_vector))
# return res
Z = np.random.randn(200, 200)
eigen_vector, eigen_value, duration, count = power_iteration(Z, 1e-3)
print(eigen_vector)
print(eigen_value)
print('time cost: ', duration)
print('iteration count: ', count)
结果:
5.
思路:
其实我发现numpy.linalg 库也提供了计算奇异值的函数 svd,所以使用它来做测试,同时自己也写一个产生C矩阵的函数
代码:
import numpy as np
def generate_matrix(row,col,p):
re = np.ones((row,col))
for i in range(row):
for j in range(col):
if np.random.rand() < p :
re[i][j] =1
else:
re[i][j] = 0
return re
for n in [100,200,300,400,500]:
for p in [0.1,0.2,0.5,0.8]:
C = generate_matrix(n, n, p)
u, s, vh = np.linalg.svd(C)
max_singular_value = np.max(s)
print('n:', n, ' p:', p, ' s:', max_singular_value, '\n')
结果:
最大奇异值 ≈ n × p
6.
思路:
使用题目中提到的argmin函数来找出答案
代码:
import numpy as np
n=200
def find_closest(A, z):
C = np.abs(A - z)
idx = np.argmin(C)
return A[idx // A.shape[0]][idx % A.shape[0]]
A = np.random.rand(n, n)
print(A)
print(find_closest(A, 1))