《Python导论》练习(Introduction to Python)Exercise of Scipy

Exercise 10.1: Least squares

Generate matrix A ∈ Rm×n with m > n. Also generate some vector b ∈ Rm. Now find x ^ = arg min x     | | A x b | | 2 . Print the norm of the residual.

from scipy.linalg import *
import numpy as np 
import matplotlib.pyplot as plt

m = 10
n = 8
A = np.random.random((m, n))
b = np.random.random((m,))

c, resid, rank, sigma = lstsq(A, b)
b_hat = A.dot(c)
delta = b - b_hat
print(norm(delta))
# plt.ylim((-0.5,0.5))
# plt.ylabel('Residual')
# plt.plot(delta)
# plt.show()

Exercise 10.2: Optimization

Find the maximum of the function
f ( x ) = s i n 2 ( x 2 ) e x 2

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


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

opt = fmin(f, 0)
# X = np.linspace(-2, 2, 1000)
# plt.plot(X, f(X))
print('maxmium:',-f(opt)[0])
# plt.show()

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 scipy
from scipy.optimize import fmin
from scipy.spatial.distance import *
import numpy as np
import matplotlib.pyplot as plt
n = 20
m = 10
X = np.random.random((n, m))
print(pdist(X))

data = np.random.random((n, 2))
dist = pdist(data)
print(squareform(dist))

猜你喜欢

转载自blog.csdn.net/qq_36183810/article/details/80571693