> For the complete documentation index, see [llms.txt](https://windmising.gitbook.io/liu-yu-bo-play-with-machine-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://windmising.gitbook.io/liu-yu-bo-play-with-machine-learning/src/chapter4-7/4-7.md).

# 4-7

![](https://github.com/windmissing/liu_yu_bo_play_with_machine_learning/blob/master/src/Chapter4/http:/windmissing.github.io/images/2019/36.png)\
样本间的距离被发现时间所主导\
如果把发现时间改成以年为单位，样本间的距离又会被肿瘤大小所主导\
**如果不对样本进行预处理，样本间的距离可能会被部分特征主导**\
解决方案：将所有的数据映射到同一个尺度，即归一化

## 数据归一化 (feature scaling)

### 最值归一化 normalization

把所有数据映射到0-1之间

$$
x\_{scale} = \frac {x - x\_{min}}{x\_{max} - x\_{min}}
$$

适用于分布有明显边界的情况，但是受outlier也就是极值(极端数据)影响较大会不准确

### 均值方差归一化 standardization

把所有数据归一到均值为0方差为1的分布中

$$
x\_{scale} = \frac {x-x\_{mean}}{S}
$$

适用于数据分布没有明显的边界，有可能存在极端的数据值，其中S是标准差。由于均值方差归一化对于符合最值归一化的数据集有着同样好的归一化处理结果，所以一般推荐使用均值方差归一化方法。

## 代码实现归一化

### 最值归一化

#### 对向量所有元素归一化

生成随机数据

```python
x = np.random.randint(0, 100, size = 100)
```

![](https://github.com/windmissing/liu_yu_bo_play_with_machine_learning/blob/master/src/Chapter4/http:/windmissing.github.io/images/2019/39.png)

归一化后

```python
(x - np.min(x)) / (np.max(x) - np.min(x))
```

![](https://github.com/windmissing/liu_yu_bo_play_with_machine_learning/blob/master/src/Chapter4/http:/windmissing.github.io/images/2019/40.png)

#### 对矩阵元素归一化

```python
X = np.random.randint(0, 100, (50, 2))
X = np.array(X, dtype = float)
X[:, 0] = (X[:,0] - np.min(X[:,0])) / (np.max(X[:,0])-np.min(X[:,0]))
X[:, 1] = (X[:,1] - np.min(X[:,1])) / (np.max(X[:,1])-np.min(X[:,1]))
```

### 均值方差归一化 standardization

```python
X2 = np.random.randint(0, 100, (50, 2))
X2 = np.array(X2, dtype = float)

X2[:,0] = (X2[:,0] - np.mean(X2[:,0])) / np.std(X2[:,0])
X2[:,1] = (X2[:,1] - np.mean(X2[:,1])) / np.std(X2[:,1])
```

输入：np.mean(X2\[:,0])\
输出：7.771561172376095e-17

输入：np.std(X2\[:,0])\
输出：1.0
