Scipy库的一些应用

PIC_1
PIC_2

Exercise 10.1


对于最小二乘法, 因为Scipy中对应的实现是基于假定: Data=m*(n+1)来的, 也就是说我们需要附加一列全为 1 的列到原始数据中. 这样一来, 我们就可以省略截距. 具体证明过程可以看这里:
正规方程.

代码实现如下:

"""Exercise 10.1: Least squares
Generate matrix A 2 Rm×n with m > n. Also generate some vector b 2 Rm.
Now find x = arg minx kAx − bk2.
Print the norm of the residual."""


import numpy as np
import matplotlib.pyplot as plt

M, N = 10, 5
data = np.random.normal(0, 1, [M,N])

# 将原始数据堆成的矩阵添加一列全为 1 的列
X = np.c_[data, np.ones(M)]
Y=np.random.randn(M)

least_square = np.linalg.lstsq(X, Y,rcond=0)

print(least_square[0])
print(least_square[1])

输出如下, 第一列为线性方程组的解, 第二列为残差.

[ 0.33056951 -0.29096465 -0.11263886  0.26678383  0.14347812 -0.0283256 ]
[3.27726913]

Exercise 10.2


第二题中Scipy的optimize.fmin需要我们先提供一个估计值才能计算函数的最小值, 因此我们先做出函数的图像. 判断在0附近函数取得极值, 再把x0=0代入.

"""Exercise 10.2: Optimization
Find the maximum of the function
f(x) = sin2(x − 2)e−x2"""

import numpy as np
from scipy import optimize
from matplotlib import pyplot as plt


def f(x):
    return np.sin(x - 2) ** 2 * np.exp(-x * x)


X=np.linspace(-2,2,1000)
v_f=np.vectorize(f)
plt.plot(X,v_f(X))
plt.show()

fmax = optimize.fmin(lambda x:-f(x),0)
print(fmax)

图像如下:
FIg

因此我们选择0作为guess参数, 得到输出如下:

Optimization terminated successfully.
         Current function value: -0.911685
         Iterations: 20
         Function evaluations: 40
[0.21625]

Exercise 10.3


直接Call 即可.

"""Exercise 10.3: Pairwise distances
Let X be a matrix with n rows and m columns. How can you compute the pairwise distances between
every two rows?
As an example application, consider n cities, and we are given their coordinates in two columns. Now
we want a nice table that tells us for each two cities, how far they are apart.
Again, make sure you make use of Scipy’s functionality instead of writing your own routine"""


import numpy as np
import scipy.spatial.distance as dis

M,N=20,10
data=np.random.normal(1,2,[M,N])


Y = dis.pdist(data, 'euclidean')
print(Y)

# list(combinations(range(M),2))

输出如下:

[10.88813903  8.63106988  7.33021805  7.28439911  9.56441818  9.282705
  9.07966246  6.94016475  8.75912049  5.56562302 10.95172379 10.59190322
  8.39602213  7.88415367  8.45233639  5.55209208  9.09698523  8.92923875
  8.62920539  9.1406873  11.28588762 12.91651311  9.40100736 11.11684385
  9.42223911  9.60988321  9.21437015  9.55958996  6.63086316  6.68677911
  9.82604987 15.1098964   6.78239539 11.46355829  8.43616865  9.62090204
  8.32322967  9.97430281 10.48738252 11.41369299 11.28933137  9.92562744
  4.83432748  8.60470737 10.67855316  9.36831638  8.20297695  6.93218774
 13.00701738 10.05943165 11.61996126 10.06111312 11.00754556  8.79064398
  6.54010407  9.88340176  5.02142225  5.19913494  8.24853766  8.83285554
  5.79328667  9.15744418  9.81713446  8.50685217  8.43483134  7.4086033
  6.69000691  6.95142757  7.49329436  8.63882191 12.50333538  8.97309376
  8.68662017  9.501256   11.70734355  8.00118662 12.9271715  12.09969338
 11.70634432 11.36044008 10.94442526  8.66862737 11.48839508 10.45548531
 11.35437997 11.45938707  8.96664498  9.32600222  7.24608344  7.13436564
  8.83073137  9.49042799 10.49931923 11.22309248  9.32400016  9.59557166
 10.20163475  8.45919372  7.28506636  5.94244473  8.8625194   8.68464892
  8.17075176  8.63821569  8.89668679  9.02887254 10.88979116  6.70749846
  8.19059758  6.6980297   6.0172943  10.28306961  8.59811071  9.07312652
  6.18412919  7.86904444  8.64653044  7.71762691 10.40267301  6.74749074
  7.89420141  7.058271    6.96093991  9.12171516  6.14043729  8.92711084
  8.68441237  6.39537973  5.91776729 10.69150068  9.39017435  9.22207365
  9.05266309  7.93328136  8.25782565  9.43222844  5.70911842  6.58802168
  7.6885412  11.57518599  8.04802935 10.72488864  9.24914394  8.26905376
  7.14832304  9.88647363 10.33997206  9.29947407  7.91288042  6.69956007
  4.01618389  7.09616393  6.86507371  8.04377204  5.83750251  7.21153621
 12.88556376  6.34048834 11.77490983  7.59361109  9.35388695  8.24599836
  7.30698379 14.05157808  8.73157448 10.95056824  9.21339057  8.52293877
  8.10661107  9.74116609  8.60190066  9.591897    8.12360119  9.93160876
  9.26622227 11.40506458  8.26588109  9.94005442 10.9146074  13.00906237
  8.31022561  4.25987826  6.9540202   7.86577437  8.18593024  7.40482871
  8.96851466  6.38028504  9.35919644  9.52299056]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/wayne_mai/article/details/80548049