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. ]]