《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^=argminx  ||Axb||2 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)=sin2(x2)ex2 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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值