Python习题——2018-05-30作业

Scipy 练习题

Exercise 10.1: Least squares

  Generate matrix A R m × n with m > n . Also generate some vector b R m .

  Now find x = arg min x A x b 2 .

  Print the norm of the residual.

import numpy as np
from scipy.optimize import leastsq
from scipy.linalg import norm


def error(x, A, b):
    '''返回误差'''
    x.reshape((10, 1))
    result = (A @ x - b).flatten()
    return result


A = np.random.random((20, 10))
b = np.random.random((20, 1))
x = np.ones((1, 10))
x = leastsq(error, x, args=(A,b))[0].reshape((10, 1))
print('x = ')
print(x, end='\n\n')
b_ = A @ x
print('The norm of residual: ', end='')
print(norm(b - b_))

Output:

x =
[[0.11075377]
 [0.11326088]
 [0.09987185]
 [0.12536225]
 [0.11074673]
 [0.06318726]
 [0.23196592]
 [0.06064075]
 [0.04162506]
 [0.05964665]]

The norm of residual: 1.2490323998004111

Exercise 10.2: Optimization

  Find the maximum of the function

f ( x ) = sin 2 ( x 2 ) e x 2

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt
import math


func  = lambda x : pow(np.sin(x - 2), 2) * np.exp(-1 * (x * x))
negaf = lambda x : -1 * pow(np.sin(x - 2), 2) * np.exp(-1 * (x * x))
res = opt.minimize_scalar(negaf)
print('The maximum is ', end='')
print(-1 * res['fun'], end='.')

x = np.linspace(-10, 10, 1000)
plt.title('$f(x)=sin^2(x-2)e^{-x^2}$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.xlim(-10, 10)
plt.plot(x, func(x))
plt.show()

Output:

The maximum is 0.9116854118471548.

Figure_1.png

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
from scipy.spatial.distance import pdist
from scipy.spatial.distance import squareform


X = np.random.random((5, 6))
print('Matrix X:')
print(X, end='\n\n')
print('The pairwise distance is shown as a matrix below.')
print('The ith row and jth column element represents the')
print('distance from the ith row and the jth row in matrix X.')
print(squareform(pdist(X)))

Output:

Matrix X:
[[0.00149391 0.43881434 0.7238691  0.89246183 0.83287866 0.30924132]
 [0.08737163 0.36771012 0.04160294 0.48787342 0.70568696 0.08410919]
 [0.49087853 0.12922004 0.69709952 0.56344638 0.67665853 0.23902811]
 [0.50285364 0.42753739 0.2185012  0.96359673 0.9051469  0.44587572]
 [0.48980019 0.69261204 0.10100373 0.24449646 0.84719487 0.71502118]]

The pairwise distance is shown as a matrix below.
The ith row and jth column element represents the
distance from the ith row and the jth row in matrix X.
[[0.         0.84170772 0.68822114 0.73200908 1.12939759]
 [0.84170772 0.         0.82457559 0.77747641 0.86506949]
 [0.68822114 0.82457559 0.         0.75716985 1.01494975]
 [0.73200908 0.77747641 0.75716985 0.         0.82288731]
 [1.12939759 0.86506949 1.01494975 0.82288731 0.        ]]

猜你喜欢

转载自blog.csdn.net/Draymond_666/article/details/80516269