Scipy练习

import numpy as np

m = 6
n = 4
A = np.random.normal(0,1,(m,n))
x = np.random.normal(0,1,(n,1))
b = A.dot(x)
tmp1 = np.dot(A.T,A)
tmp2 = np.linalg.inv(tmp1)
tmp3 = np.dot(tmp2,A.T)
xx = np.dot(tmp3,b)
print("A = ",A)
print("Real x = ",x)
print("b = ",b)
print("Estimated x = ",xx)
print("Norm of the residual = ",np.linalg.norm(np.dot(A,xx) - b,2 ) )

output:

A =  [[-0.23746829 -0.49619018  2.51938228  0.62917812]
 [ 1.09449491  0.47617682  0.55178212  0.15337221]
 [ 0.09449504  1.20639197  0.28287066  1.14217189]
 [-0.46268577  0.35975885  0.45245352  0.90551623]
 [ 1.05820922  0.8187108   0.0982688   1.11322871]
 [-0.87005886 -0.24117022  0.72189791  0.65326008]]
Real x =  [[ 0.16097507]
 [-1.0443316 ]
 [ 0.60456353]
 [ 0.42047366]]
b =  [[ 2.26764008]
 [ 0.07697622]
 [-0.59339545]
 [ 0.20409421]
 [-0.15716718]
 [ 0.8229157 ]]
Estimated x =  [[ 0.16097507]
 [-1.0443316 ]
 [ 0.60456353]
 [ 0.42047366]]
Norm of the residual =  3.741370260853751e-15

import numpy as np
from scipy.optimize import fmin
from math import sin,exp
import scipy

def func(x):
    F = (-1) * (sin(x-2)**2) * exp((-1)*(x ** 2))
    return F

my_opt = fmin(func,0)
print("maximum of the function : ",-func(my_opt))

output:

Optimization terminated successfully.
         Current function value: -0.911685
         Iterations: 20
         Function evaluations: 40
maximum of the function :  0.9116854117069156


import numpy as np
from scipy.spatial.distance import pdist,squareform

X = np.random.normal(0,1,(3,2))
print("Pairwise Distances:",pdist(X))

t = np.random.normal(0,1,(3,2))
t1 = pdist(t)
print("Far : ")
print(squareform(t1))

output:

Pairwise Distances: [1.82354005 1.505207   0.32496712]
Far : 
[[0.         1.52186076 1.75217918]
 [1.52186076 0.         0.29425653]
 [1.75217918 0.29425653 0.        ]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值