1. 最小二乘法
上图是数据点
x⃗ ,y⃗
大致满足线性关系
y=ax+b
,在生成过程中加入了随机噪声,具体生成代码如下。请使用最小二乘法拟合出参数
a,b
的值来。![]
a = 0.5; b = 2
n_samples = 50
x = np.linspace(0, 10, n_samples)
y = a * x + b + np.random.randn(n_samples) * 0.5
2. 二分类问题
上图中蓝色点和红色点集分别是以
[0,1]
和
[1,0]
为中心的满足标准正态分布的随机点,具体生成代码如下。请使用 SVM (支持向量机)模型对两类数据点进行分类。
n_samples = 200
## 蓝色点集
x1 = np.random.randn(n_samples, 2) * 0.5 + np.asarray([[0, 1]])
y1 = np.asarray([[1, 0] for ii in range(len(x1))])
## 红色点集
x2 = np.random.randn(n_samples, 2) * 0.5 + np.asarray([[1, 0]])
y2 = np.asarray([[0, 1] for ii in range(len(x2))])
3. 曲线拟合
使用 numpy 生成拟合数据集
D={(x1,y1),(x2,y2),...,(x100,y100)}
,如下图中红色的点所示,其中蓝色实线表示函数
y=sin(x)
,红色点的涨落是由于加入高斯随机变量造成的。
使用 python 编写一个多项式回归模型
y^=w0+w1x+w2x2+w3x3
对数据集
D
进行拟合,求出参数
n_samples = 100
x = np.linspace(0, 2*np.pi, n_samples).reshape(n_samples, 1)
y_1 = np.sin(x)
y = np.sin(x) + np.random.randn(x.shape[0], 1)*0.1
4.
实现快速排序算法
5.
以人民币的面额为例,给出对于任意数额(x元)的所有面额组合方式的数目 n。比如 x=7时,n=2 (第一种方式:7张1元; 第二种方式:1张5元+2张1元)
6.
解释Confusion Matrix中各个指标的意义,并举例说明,
解释 ROC(Receiver Operating Characteristic)曲线和AUC 曲线的概念。试着使用python画出两条曲线。
7.
使用 numpy 实现一个全连接神经网络算法的Layer类, 包括数据预处理(preprocessing), 前向传播(forward), 具体如下。
class Layer(object):
def __init__(self, input_dim, output_dim, activation='sigmoid')
pass
def weights_init(self, **kwargs):
"""
生成权重矩阵W (W.shape=[input_dim, output_dim]),
W 中的元素满足均值为0, 方差为 sigma 的高斯分布
"""
pass
def preprocessing(self, X, **kwargs):
"""
argments:
X: 2D matrix with shape [n_samples, input_dim]
return:
preprocessed 2D matrix with same shape as X
本函数的功能:
1.检查X矩阵中是否有'Nan', 'inf','-inf'类型的元素,
如果有将他们分别替换为对应列的均值,最大值和最小值
2.对输入矩阵的每一列做高斯变换(均值为0, 方差为1)
"""
pass
def forward(self, X, **kwargs):
"""
本函数功能:
计算 y = sigmoid(W*X), 或 y = tanh(W*X), 根据 activation 而定
argments:
X: 2D matrix with shape [n_samples, input_dim]
return:
y: 2D matrix with shape [n_samples, output_dim]
"""
pass
利用类 Layer 实现下图中的网络模型,其中 hidden_layer 和 output_layer 的 ‘activation’ 分别取 sigmoid 和 tanh。并自己生成实验数据,测试模型的结果