numpy作业

9.1,Matrix operations
都是一些基本操作
n = 200
m = 500
A = numpy.array([numpy.random.normal(0, 1, m) for _ in range(n)])
B = scipy.linalg.toeplitz([1., -.25, *numpy.zeros(m-2)])

# exercise 9.1
print('\033[1;35m A+A: \033[0m')
print(A+A)
print('\033[1;35m AA^T: \033[0m')
print(A.dot(A.T))
print('\033[1;35m A^TA: \033[0m')
print(A.T.dot(A))
print('\033[1;35m AB: \033[0m')
print(A.dot(B))
def compute(Lambda, A, B):
    return A.dot(B-Lambda*numpy.eye(m, m))
print('\033[1;35m A(B-λI): \033[0m')
print(compute(1, A, B))
numpy作业_第1张图片

numpy作业_第2张图片

numpy作业_第3张图片

numpy作业_第4张图片

numpy作业_第5张图片

9.2,Solving a linear system
使用函数numpy.linal.solve即可解决形如Ax = b的线性系统
# exercise 9.2
b = numpy.array([_ for _ in range(m)])
print('\033[1;35m Bx = b, x = : \033[0m')
print(numpy.linalg.solve(B, b))
图片仅显示部分数据

numpy作业_第6张图片

9.3,Norms
使用numpy.linalg.norm可解决范数问题,使用numpy.linalg.svd解决奇异值分解问题
# exercise 9.3
print('\033[1;35m ||A||1: \033[0m')
print(numpy.linalg.norm(A, ord = 1))
print('\033[1;35m ||B||inf: \033[0m')
print(numpy.linalg.norm(B, ord = numpy.inf))
print('\033[1;35mB\'s largest singular values: \033[0m')
print(max(numpy.linalg.svd(B)[1]))
print('\033[1;35mB\'s smallest singular values: \033[0m')
print(min(numpy.linalg.svd(B)[1]))
numpy作业_第7张图片
9.4,Power iteration
幂迭代求矩阵的特征根和特征向量,详见https://blog.csdn.net/luckisok/article/details/1602266
# exercise 9.4
def matrixx(A, x, v, N):
    for i in range(N):
        v[i]=0
        for j in range(N):
            v[i] += A[i][j] * x[j]

def slove(v, N):
    max1 = 0
    for i in range(N-1):
        max1 = max(v[i], v[i+1])
    return max1

Z = numpy.array([numpy.random.normal(0, 1, n) for _ in range(n)])
N = Z.shape[1]
x = [1 for _ in range(N)]
v = [0 for _ in range(N)]
u = [0 for _ in range(N)]
p = [0 for _ in range(N)]
e = 10**(-1)
delta = 1
iterations = 0
computation_time = time.clock()
while delta >= e:
    for q in range(N):
        p[q]=v[q]
    matrixx(Z, x, v, N)
    for i in range(N):
        u[i]=v[i] / (slove(v, N))
    delta=numpy.fabs(slove(v, N)-slove(p, N))
    iterations += 1
    for l in range(N):
        x[l]=u[l]

print('\033[1;35mlargest eigenvalue of Z:\n\033[0m', slove(v, N))
print('\033[1;35mand mcorresponding eigenvector of Z:\n\033[0m', u)
print('\033[1;35miterations are:\n\033[0m', iterations)
print('\033[1;35mcomputation time is(s):\n\033[0m', time.clock()-computation_time)
numpy作业_第8张图片
9.5,Singular values
使用numpy.random.choice生成概率p为0,概率1-p为1的矩阵,使用scipy.linalg.svd求解奇异值问题,共进行了100次测试,p从0每次递增0.01至1
# exercise 9.5
for i in range(100):
    C = numpy.random.choice(2, (n, n), p = [i/100, 1-i/100])
    print('\033[1;35mp = \033[0m', i/100)
    print('\033[1;35mlargest singular value: \033[0m')
    print(max(scipy.linalg.svd(C)[1]))

图片仅提供部分数据
numpy作业_第9张图片numpy作业_第10张图片

从图中可以看出,随着p增大,最大奇异值逐渐减小。(注意p是元素为0时的概率)

9.6,Nearest neighbor
要求出数z在矩阵中最接近的数,可先将矩阵中每个元素减去z,再对每个元素取绝对值,然后求所有元素中最小值所在位置的坐标。该座标在原矩阵中对应的数即为所求。
# exersicse 9.6
z = 1.000
def closest(A, z):
    position = numpy.argmin(numpy.abs(A - z))
    x = position // A.shape[1]
    y = position % A.shape[1]
    return A[x][y]
print('\033[1;35mthe closest value of z(' + str(z) +  ') in A is: \033[0m')
print(closest(A, z))


你可能感兴趣的:(numpy作业)