第十一周作业:numpy作业

题目清单

这里写图片描述

生成矩阵

代码

import numpy as np
from scipy.linalg import toeplitz

A = np.random.normal(loc = 0, scale = 1, size=(200,500))
B = np.random.normal(loc = 0, scale = 0.8, size=(500,500))

row = list(range(1,501))
T = toeplitz(row)
print(A)
print(B)
print(T)

A:

[[-0.66934236 -0.73039498 0.35874842 … -0.09271093 -1.546918
-1.30470827]
[-0.79737672 -0.89180983 0.2265799 … 1.22830308 -0.9571836
0.01487323]
[ 2.57771489 -0.13494096 0.13641614 … -0.43055211 1.50141297
1.22070377]

[-1.6245896 -1.83144213 -0.52343475 … 0.14323491 -1.93611371
-0.98315452]
[ 1.42288758 1.1528398 0.11913474 … 0.25071119 0.1185877
0.55080314]
[ 0.30648748 -1.10378496 2.08430323 … 0.29915158 -0.03481484
0.27275209]]

B:

[[-0.42493955 -0.78028827 0.38021809 … -1.08491428 1.09668302
-0.16210884]
[-1.03611103 0.35224429 -1.59971981 … 0.18599272 -0.96187296
0.19140574]
[-0.42404332 0.95682417 1.89480551 … -0.92708997 1.21462123
-2.19569536]

[ 0.66606066 -1.39795849 1.61913321 … 1.14382328 0.32784417
0.39018398]
[-0.16651916 -0.19553692 1.46635431 … 0.4106421 -0.43958984
-1.15612039]
[ 0.261861 1.89518835 0.91785474 … -0.64521981 0.12385743
-1.56748443]]

T(Toeplitz Matrix):

[[ 1 2 3 … 498 499 500]
[ 2 1 2 … 497 498 499]
[ 3 2 1 … 496 497 498]

[498 497 496 … 1 2 3]
[499 498 497 … 2 1 2]
[500 499 498 … 3 2 1]]

Exercise 9.1: Matrix operations

import numpy as np
A = np.random.normal(loc = 0, scale = 1, size=(200,500))
B = np.random.normal(loc = 0, scale = 0.8, size=(500,500))
# A + A,
sum_of_AA = A + A
# AA^T
mul_A_AT = np.dot(A,A.T)
# A^TA
mul_AT_A = np.dot(A.T,A)
# AB
mul_A_B = np.dot(A,B)
# function computing A(B - λI) for any λ.
def MulOfMatrix(A,B,lamda):
    Bm = B-lamda*identity(len(B))
    return np.dot(A,Bm)

Exercise 9.2: Solving a linear system

import numpy as np
B = np.random.normal(loc = 0, scale = 0.8, size=(500,500))
b = np.random.random((500,1))
ans = np.linalg.solve(B, b)
print(ans)

Exercise 9.3: Norms

相关知识:

范数:https://www.zhihu.com/question/20473040

代码

import numpy as np

A = np.random.normal(loc = 0, scale = 1, size=(200,500))
B = np.random.normal(loc = 0, scale = 0.8, size=(500,500))
# A_fro_norm
A_fro_norm = np.linalg.norm(A,ord='fro')
# B_inf_norm
B_inf_norm = np.linalg.norm(B,ord=np.inf)
i = np.linalg.eigvals(np.dot(B,B))
max_val = np.max(i)
min_val = np.min(i)

Exercise 9.4: Power iteration

相关知识:

https://www.zhihu.com/question/21874816
https://blog.csdn.net/ljhandlwt/article/details/76576915
https://blog.csdn.net/pipisorry/article/details/38662887

代码

import numpy as np
# 注:随机生成的矩阵以及初始值的选取不当可能导致无法收敛

n = 500
Z = np.random.normal(loc = 0, scale = 5, size=(n,n))
# ~ n = 3
# ~ Z = [[1,-3,3],[3,-5,3],[6,-6,4]]
uk = np.zeros(n)
ukplus = np.ones(n)
mk = 0
vk = np.ones(n)
mktem = 1
t = 0
# 循环到║ Xk- Xkplus║∞ < ε
while np.linalg.norm(ukplus-uk,ord=np.inf)>0.1 and abs(mk-mktem)>0.001:
    uk = ukplus
    uk_abs = abs(uk)
    mktem = mk
    mk_index = np.argmax(uk) # 模最大值下标
    mk = uk[mk_index]
    vk = uk/mk # 归一化
    ukplus = np.dot(Z,vk)
    t = t+1
    print(t)
# iteration times
print("iteration times:" + str(t))
# eigenvalue
eigenvalue = np.max(uk)
print("eigenvalue:" + str(eigenvalue))
# eigenvector
print("eigenvector:")
eigenvector = vk
print(eigenvector)

示例1(简单矩阵):

矩阵如下:

n = 3
Z = [[1,-3,3],[3,-5,3],[6,-6,4]]

其结果为:

iteration times:6
eigenvalue:4.0
eigenvector:
[0.484375 0.484375 1. ]

示例2(随机矩阵)

iteration times:352
eigenvalue:114.61108989358615
eigenvector:
[-0.23370157 -0.0091695 -0.34815888 0.06290541 0.15649416 0.01280086
-0.11514434 -0.18001434 -0.17619587 0.72696853 0.29290727 0.12365829
0.02276859 0.27640786 0.44345776 0.45877351 -0.11433718 0.11992938
-0.03623894 0.64238248 -0.20578191 0.21782423 -0.5718889 -0.01126742
0.41166957 0.34027293 -0.28916848 -0.27978122 -0.26719497 0.31765091
0.27989961 0.10753823 0.0647977 -0.29414155 -0.09885035 0.44002102
-0.16204982 0.09777376 0.14763332 -0.23214146 -0.11303903 0.13375778
-0.0514124 0.0212906 -0.46510703 0.34232391 -0.20142009 0.60662286
0.21830439 -0.04093544 -0.14957262 0.22319758 0.36811962 -0.3448301
0.08568472 -0.19808286 -0.13443645 -0.15176813 0.57604482 0.22527705
-0.69716624 -0.07954922 0.16685391 0.23715948 0.52383437 -0.02129523
0.02639307 -0.12282269 0.10809525 0.08764046 -0.02674204 -0.31724434
-0.5710219 0.15653847 -0.00560809 0.34023673 -0.07292783 -0.31751488
0.15081934 0.04887169 -0.35693579 -0.03745066 -0.09029062 -0.13893416
0.16543723 -0.37156673 -0.2434307 -0.31228506 -0.41803606 0.02556508
0.21965814 0.32501634 0.2611086 -0.18470759 0.07763 0.10310693
0.14574414 0.14243011 0.00366438 -0.12138775 -0.60690647 0.02144514
-0.09376609 -0.06007508 0.06731213 0.063338 -0.65445524 -0.12090075
-0.19135565 -0.29236267 0.04647993 0.15030545 0.00837339 -0.37115545
0.04579759 -0.02952034 -0.22414419 -0.0489431 0.01477856 0.18430124
-0.37503568 0.05405271 0.37844423 -0.2158558 -0.25169175 0.10134116
0.79661185 -0.00305358 0.49728927 -0.19609634 -0.076772 0.07830637
0.10047445 0.08896671 -0.54177329 -0.17194404 -0.43683173 0.07362038
-0.06174099 0.32196223 -0.28023992 -0.13182915 -0.22307122 -0.10207128
0.14776261 -0.59338526 -0.17540063 -0.18216585 -0.40246449 0.25642909
0.21211616 -0.28443516 -0.08500938 -0.09864253 0.48943003 -0.37885346
-0.28246759 -0.46942764 0.27860304 -0.20501349 0.0875133 0.52379699
-0.41620276 0.95783276 0.27966652 -0.31081567 -0.63503095 -0.45379496
-0.36483133 0.14612323 -0.15228727 0.24693912 -0.14794807 0.59877566
0.10627217 0.11393787 -0.12070175 -0.55282151 0.16438967 0.11543508
-0.165726 0.38698009 -0.76999584 -0.31644798 0.81578881 0.25800906
-0.09192331 0.15837132 0.13931794 0.03584506 0.0448816 0.25662597
-0.32546108 -0.57106838 -0.03383931 0.09379923 0.5377159 0.54247036
0.19511318 -0.10380148 -0.12591964 -0.28031895 -0.45453788 -0.26322729
0.11106755 0.09436281 0.35090972 0.47147437 0.62284294 0.40284026
-0.29876131 -0.45049463 0.36506946 0.07472392 0.03825477 -0.1354568
0.01086347 -0.00980625 -0.37408041 0.15090827 -0.42897796 0.68642684
0.01700749 -0.27240354 0.95797911 -0.15034965 0.14736432 -0.18356249
-0.07877502 0.09790265 -0.14420692 -0.33028241 -0.59556952 0.51776515
0.34072693 -0.68215928 0.06613412 0.61485835 0.17106158 -0.09166663
-0.07610102 0.09443567 0.03022295 0.09318628 0.44060116 -0.38945694
-0.19014822 0.10853123 -0.37752721 -0.05719242 0.02040358 -0.14709037
-0.48646337 -0.17660948 0.59217911 0.65996116 -0.55428801 -0.41374211
0.0522862 0.26912398 -0.0548476 0.1725578 -0.20640702 0.1691625
0.26744807 0.1087185 0.01916744 -0.14458196 0.11389704 -0.06305376
0.25636596 0.24348817 -0.13917912 -0.26518354 -0.04699204 0.17555747
-0.92192233 0.43443074 0.11489367 0.29141727 -0.28745683 -0.3624005
-0.37242042 -0.09446235 0.13830429 0.30173962 0.24501044 0.17036171
0.24425922 -0.00209491 -0.18097225 -0.08198254 0.33700435 0.3015075
0.02572453 -0.09939294 -0.09989604 0.44177691 0.08850545 -0.10710619
0.31431618 0.56106771 -0.21916062 -0.2507381 0.15952378 -0.18914489
0.76800274 -0.21373727 0.44909436 -0.56456835 0.15010024 -0.03007064
0.50935248 0.16361181 0.57480132 -0.31230342 0.12556682 0.58415742
0.08651084 -0.02250626 -0.02050971 0.16025202 0.26929929 0.02940026
1. -0.21848079 -0.09087679 -0.39360667 0.51601385 -1.07127685
-0.42930966 0.26350619 -0.40223207 -0.04779845 0.21328163 -0.5897203
0.11562574 0.3819726 0.33120579 -0.22652767 0.1906501 -0.51592019
-0.12355216 0.52201383 -0.39736234 0.13063256 -0.50126355 -0.17894785
0.28816475 0.27353247 0.43706011 0.38874154 0.15006954 -0.24160992
0.01408373 -0.7580161 -0.14061292 -0.04290058 -0.09533804 0.15985248
-0.31773696 -0.4450466 0.29344501 -0.00581009 0.1003683 -0.03183654
-0.00942273 0.06465384 -0.1508579 0.21220896 0.46660265 -0.20287461
-0.00424517 0.19853462 0.1189299 -0.07478842 0.22098219 0.02118422
0.07736253 -0.13508909 -0.39038914 -0.07668161 0.45899417 0.1390143
0.46042581 -0.30983421 0.0423396 -0.14122975 -0.10120877 0.01166214
-0.53108379 -0.25996932 0.00520171 0.11371266 -0.36401323 -0.57657539
-0.12973489 -0.20098669 0.1743463 -0.16067218 0.57874271 -0.01798699
0.03328081 -0.24426011 0.33980925 0.59971217 0.13759961 -0.32226296
0.30917742 0.24772557 0.20884036 -0.22805985 -0.3766731 -0.0222822
-0.3806352 -0.42844241 0.04206499 0.01777046 0.27846245 -0.5761463
0.57918434 -0.19301308 -0.13911784 -0.31702457 0.02218382 0.05491303
0.60983726 0.10838378 -0.56012728 0.27439795 -0.67225813 0.41669569
-0.0095371 -0.20195333 -0.42409955 0.10437782 -0.05633455 -0.21054715
-0.86263556 -0.01055486 -0.44121736 -0.42546221 -0.26069979 -0.20363177
0.28118733 -0.05153626 -0.11905405 0.11685501 0.3371445 -0.31089769
-0.3213434 -0.56246755 -0.03388474 -0.27705204 0.37190234 0.0357365
-0.10825846 0.29969535 0.34896004 -0.13358665 -0.35275402 0.6512452
0.61000661 -0.46045328 -0.36599284 0.31221951 -0.22262937 0.29308136
0.30325183 0.43207643 -0.46307219 -0.51825622 0.23236729 -0.37911317
-0.07137845 -0.31548521 -0.13728603 0.0073547 -0.54215834 -0.21605899
0.31135359 0.02582818 -0.32648063 0.53988417 -0.22911116 -0.32932704
0.1261268 0.26191905 0.28667558 -0.03794487 0.07553128 0.3582989
0.51558958 0.10638951 -0.21428406 0.92574105 0.47796975 0.01972039
0.48406948 0.2400549 ]

Exercise 9.5: Singular values

代码

import numpy as np
from scipy.linalg import svdvals

def exa(n,p):
    C = np.zeros(n*n)
    for i in range(0,n*n):
        if np.random.random() > p:
            C[i] = 1
        else:
            C[i] = 0
    C = C.reshape((n,n))
    sv_vec = svdvals(C)
    sv_max = np.max(sv_vec)
    return sv_max

print("%20s%20s%20s%20s%20s"%('5','6','7','8','9'))
for p in range(1,9):
    sv_list = []
    for n in range(5,10):
        sv_list.append(exa(n,(float)(p/10)))
    sv_str = ""
    for sv_val in sv_list:
        sv_str += "%-20s"%str(sv_val)
        sv_str += "    "
    print(str(p/10) + " " + sv_str)

统计结果

统计结果
通过上述数据可以发现,当n增大时,奇异值也在增大,而当p增大时,奇异值减小。

Exercise 9.6: Nearest neighbor

参考

http://www.jb51.net/article/111247.htm

代码

import numpy as np

def NearestNeighbor(val_z,arr_A):
    arr_B = arr_A - val_z
    arr_B_fabs = abs(arr_B)
    index = np.argmin(arr_B_fabs)
    return arr_A[index]

z = 1
A = np.random.rand(10)
neigh = NearestNeighbor(z,A)
print("The nearest neighbor of " + str(z) + " in A is " +
      str(neigh))

运行结果

The nearest neighbor of 1 in A is 0.9583850050081523

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值