import numpy as np import scipy.optimize as opt #suppose that m=20 n=3 A = np.random.randn(20,3) B = np.random.randn(20,1) print("A: ",A) print("B: ",B) """ #suppose that m=8 n=1 A = np.array([[5],[31],[13],[49],[20],[18],[16],[33]]) B = np.array([[33],[72],[70],[10],[21],[6],[21],[52]]) """ def f(x,target,var): return sum(target - var@x) target = B var = A ans=opt.leastsq(f,[[1],[1],[1]] ,args=(target,var)) print("Solution x for Ax=b:\n",ans[0])
运行结果:
import scipy.optimize as opt import numpy as np import math """ x = arrange(0,10,1000) f = lamda x: -np.power(np.sin((x-2) * np.power(np.e,-np.power(x,2)) ) ,2) #y = np.power(np.sin((x-2) * np.power(np.e,-np.power(x,2)) ) ,2) """ def f(x): return -math.pow(math.sin(x-2) ,2) * math.pow(math.e,-math.pow(x,2)) max_ = opt.minimize_scalar(f) print(-max_.fun)
截图:
import numpy as np from scipy.spatial.distance import cdist #suppose n = 10, m = 21 X = np.random.randint(0,100,(10,21)) print("X:\n",X) dist = [] for i in range(0,10): dist.append([]) for j in range(0,10): ele_dist=cdist([X[i]],[X[j]],metric='euclidean') dist[i].append(ele_dist[0][0]) for i in range(0,10): print("From row",i," to rows 0-9:") print(dist[i])
运行截图: