You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 4.5 检测红酒品质
## 数据预处理
在使用k近邻算法进行检测之前我们可以先查看一下各个特征的均值和标准差。
```python
[1.30006180e+01 2.33634831e+00 2.36651685e+00 1.94949438e+01 9.97415730e+01 2.29511236e+00 2.02926966e+00 3.61853933e-01 1.59089888e+00 5.05808988e+00 9.57449438e-01 2.61168539e+00 7.46893258e+02]
[8.09542915e-01 1.11400363e+00 2.73572294e-01 3.33016976e+00 1.42423077e+01 6.24090564e-01 9.96048950e-01 1.24103260e-01 5.70748849e-01 2.31176466e+00 2.27928607e-01 7.07993265e-01 3.14021657e+02]
```
从打印结果可以看出有的特征的均值和标准差都比较大例如如最后一个特征。如果现在用k近邻算法来对这样的数据进行分类的话k近邻算法会认为最后一个特征比较重要。因为假设有两个样本的最后一个特征值分别为`1`和`100`那么这两个样本之间的距离可能就被这最后一个特征决定了。这样就很有可能会影响k近邻算法的准确度。为了解决这种问题我们可以对数据进行标准化。标准化的知识以及如何实现标准化在第三章已经向你介绍过了相信你对这些知识已经很熟悉了在这里就不多赘述了。
## 检测品质
接下来就只需要调用之前实现的`knn_clf`方法就可以对测试集中的红数据进行品质检测了:
```python
predict = knn_clf(3,train_feature,train_label,test_feature)
predict
>>>array([6, 5, 4, 7, 4, 3, 6, 4, 5, 4, 9, 2, 7, 8, 4, 6, 9, 5, 7, 4, 7, 5,
8, 6, 0, 9, 6, 4, 5, 7, 5, 9, 8, 6, 4, 8, 8, 5, 5, 6, 7, 9, 4, 6,
8, 7, 6, 7, 4, 5, 4, 5, 4, 2, 4, 7, 0, 5, 5, 5, 4, 6, 7, 8, 5, 6,
8, 5, 5, 4, 6, 3, 7, 4, 3, 4, 6, 9, 7, 7, 8, 5, 6, 5, 6, 5, 5, 4,
9, 7, 7, 6, 8, 8, 6, 8, 5, 7, 4, 6, 8, 8, 5, 4, 6, 5, 6, 9, 9, 5])
```
再根据测试集标签即真实分类结果,计算出正确率:
```python
acc = np.mean(predict==test_label)
acc
>>>0.994
```
可以看到使用k近邻算法检测红酒品质正确率能达到`99%`以上。此时此刻,我相信,红酒鉴定师的烦恼要从活太多干不完,转变成以后可能没活干了。