首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【机器学习与实现】kNN分类算法示例

【机器学习与实现】kNN分类算法示例

作者头像
Francek Chen
发布于 2025-01-22 13:23:01
发布于 2025-01-22 13:23:01
10600
代码可运行
举报
运行总次数:0
代码可运行

一、混淆矩阵相关概念示例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from sklearn.metrics import confusion_matrix,accuracy_score,precision_score,recall_score,f1_score
from sklearn.metrics import classification_report
y_true = [1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0]
y_pred = [1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0]
#使用help(confusion_matrix)命令,可以查看混淆矩阵的联机帮助
#在该联机帮助的示例中,是把1看作Positive,0看作Negative,并且混淆矩阵的右下角是tp
print("混淆矩阵:\n",confusion_matrix(y_true,y_pred))    
tn,fp,fn,tp=confusion_matrix(y_true,y_pred).ravel()
print("混淆矩阵中tn,fp,fn,tp值分别为:",(tn,fp,fn,tp))
print("正例(类别为1)的准确率:",accuracy_score(y_true,y_pred))
print("正例(类别为1)的查准率:",precision_score(y_true, y_pred))
print("正例(类别为1)的查全率:",recall_score(y_true, y_pred))
print("正例(类别为1)的f1分:",f1_score(y_true, y_pred))
print("\n也可以使用classification_report分类别查看除了accuracy_score之外的其他性能指标:")
print("分类性能报告:",classification_report(y_true, y_pred, labels=[0,1]))

对比上述输出结果,可以发现: accuracy_score/precision_score/recall_score/f1_score函数只显示正例 (类别为1) 的性能指标; 而classification_report既可以显示正例(类别为1)、还可以显示出反例 (类别为0) 的性能指标。

对上面运行结果的分析如下图所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
y_true = [1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0]
y_pred = [1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0]

  查准率和查全率是一对矛盾的度量,一般来说,查准率高时,查全率往往会偏低,查全率高时,查准率往往会偏低。而F1-Score指标综合了Precision与Recall的产出的结果,是F1是基于查准率与查重率的调和平均。F1-Score的取值范围从0到1,1代表模型的输出最好0代表模型的输出结果最差

准确率ACC=\frac{2+3}{2+4+3+3}=\frac{5}{12}
查准率Precision=\frac{3}{4+3}=\frac{3}{7}
查全率Recall=\frac{3}{3+3}=\frac{3}{6}
\frac{1}{F1\_score}=\frac{1}{2}\cdot(\frac{1}{P}+\frac{1}{R})\Rightarrow F1\_score=\frac{2\cdot P\cdot R}{P+R}
F1\_score=\frac{2×\frac{3}{7}×\frac{3}{6}}{\frac{3}{7}+\frac{3}{6}}=\frac{6}{13}

二、使用kNN(k近邻)算法对鸢尾花数据集进行分类

(一)加载并查看鸢尾花数据集

参考资源: https://blog.csdn.net/weixin_45014385/article/details/123618841 https://www.cnblogs.com/gemine/p/11130032.html

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from sklearn.datasets import load_iris
#加载sklearn自带的鸢尾花数据集
iris = load_iris()
#查看该数据集的说明信息,该说明信息是一个字典
iris
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
{'DESCR': 'Iris Plants Database\n====================\n\nNotes\n-----\nData Set Characteristics:\n    :Number of Instances: 150 (50 in each of three classes)\n    :Number of Attributes: 4 numeric, predictive attributes and the class\n    :Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm\n        - class:\n                - Iris-Setosa\n                - Iris-Versicolour\n                - Iris-Virginica\n    :Summary Statistics:\n\n    ============== ==== ==== ======= ===== ====================\n                    Min  Max   Mean    SD   Class Correlation\n    ============== ==== ==== ======= ===== ====================\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n    petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)\n    ============== ==== ==== ======= ===== ====================\n\n    :Missing Attribute Values: None\n    :Class Distribution: 33.3% for each of 3 classes.\n    :Creator: R.A. Fisher\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n    :Date: July, 1988\n\nThis is a copy of UCI ML iris datasets.\nhttp://archive.ics.uci.edu/ml/datasets/Iris\n\nThe famous Iris database, first used by Sir R.A Fisher\n\nThis is perhaps the best known database to be found in the\npattern recognition literature.  Fisher\'s paper is a classic in the field and\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant.  One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\nReferences\n----------\n   - Fisher,R.A. "The use of multiple measurements in taxonomic problems"\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n     Mathematical Statistics" (John Wiley, NY, 1950).\n   - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n     Structure and Classification Rule for Recognition in Partially Exposed\n     Environments".  IEEE Transactions on Pattern Analysis and Machine\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions\n     on Information Theory, May 1972, 431-433.\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II\n     conceptual clustering system finds 3 classes in the data.\n   - Many, many more ...\n',
 'data': array([[ 5.1,  3.5,  1.4,  0.2],
        [ 4.9,  3. ,  1.4,  0.2],
        [ 4.7,  3.2,  1.3,  0.2],
        [ 4.6,  3.1,  1.5,  0.2],
        [ 5. ,  3.6,  1.4,  0.2],
        [ 5.4,  3.9,  1.7,  0.4],
        [ 4.6,  3.4,  1.4,  0.3],
        [ 5. ,  3.4,  1.5,  0.2],
        [ 4.4,  2.9,  1.4,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 5.4,  3.7,  1.5,  0.2],
        [ 4.8,  3.4,  1.6,  0.2],
        [ 4.8,  3. ,  1.4,  0.1],
        [ 4.3,  3. ,  1.1,  0.1],
        [ 5.8,  4. ,  1.2,  0.2],
        [ 5.7,  4.4,  1.5,  0.4],
        [ 5.4,  3.9,  1.3,  0.4],
        [ 5.1,  3.5,  1.4,  0.3],
        [ 5.7,  3.8,  1.7,  0.3],
        [ 5.1,  3.8,  1.5,  0.3],
        [ 5.4,  3.4,  1.7,  0.2],
        [ 5.1,  3.7,  1.5,  0.4],
        [ 4.6,  3.6,  1. ,  0.2],
        [ 5.1,  3.3,  1.7,  0.5],
        [ 4.8,  3.4,  1.9,  0.2],
        [ 5. ,  3. ,  1.6,  0.2],
        [ 5. ,  3.4,  1.6,  0.4],
        [ 5.2,  3.5,  1.5,  0.2],
        [ 5.2,  3.4,  1.4,  0.2],
        [ 4.7,  3.2,  1.6,  0.2],
        [ 4.8,  3.1,  1.6,  0.2],
        [ 5.4,  3.4,  1.5,  0.4],
        [ 5.2,  4.1,  1.5,  0.1],
        [ 5.5,  4.2,  1.4,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 5. ,  3.2,  1.2,  0.2],
        [ 5.5,  3.5,  1.3,  0.2],
        [ 4.9,  3.1,  1.5,  0.1],
        [ 4.4,  3. ,  1.3,  0.2],
        [ 5.1,  3.4,  1.5,  0.2],
        [ 5. ,  3.5,  1.3,  0.3],
        [ 4.5,  2.3,  1.3,  0.3],
        [ 4.4,  3.2,  1.3,  0.2],
        [ 5. ,  3.5,  1.6,  0.6],
        [ 5.1,  3.8,  1.9,  0.4],
        [ 4.8,  3. ,  1.4,  0.3],
        [ 5.1,  3.8,  1.6,  0.2],
        [ 4.6,  3.2,  1.4,  0.2],
        [ 5.3,  3.7,  1.5,  0.2],
        [ 5. ,  3.3,  1.4,  0.2],
        [ 7. ,  3.2,  4.7,  1.4],
        [ 6.4,  3.2,  4.5,  1.5],
        [ 6.9,  3.1,  4.9,  1.5],
        [ 5.5,  2.3,  4. ,  1.3],
        [ 6.5,  2.8,  4.6,  1.5],
        [ 5.7,  2.8,  4.5,  1.3],
        [ 6.3,  3.3,  4.7,  1.6],
        [ 4.9,  2.4,  3.3,  1. ],
        [ 6.6,  2.9,  4.6,  1.3],
        [ 5.2,  2.7,  3.9,  1.4],
        [ 5. ,  2. ,  3.5,  1. ],
        [ 5.9,  3. ,  4.2,  1.5],
        [ 6. ,  2.2,  4. ,  1. ],
        [ 6.1,  2.9,  4.7,  1.4],
        [ 5.6,  2.9,  3.6,  1.3],
        [ 6.7,  3.1,  4.4,  1.4],
        [ 5.6,  3. ,  4.5,  1.5],
        [ 5.8,  2.7,  4.1,  1. ],
        [ 6.2,  2.2,  4.5,  1.5],
        [ 5.6,  2.5,  3.9,  1.1],
        [ 5.9,  3.2,  4.8,  1.8],
        [ 6.1,  2.8,  4. ,  1.3],
        [ 6.3,  2.5,  4.9,  1.5],
        [ 6.1,  2.8,  4.7,  1.2],
        [ 6.4,  2.9,  4.3,  1.3],
        [ 6.6,  3. ,  4.4,  1.4],
        [ 6.8,  2.8,  4.8,  1.4],
        [ 6.7,  3. ,  5. ,  1.7],
        [ 6. ,  2.9,  4.5,  1.5],
        [ 5.7,  2.6,  3.5,  1. ],
        [ 5.5,  2.4,  3.8,  1.1],
        [ 5.5,  2.4,  3.7,  1. ],
        [ 5.8,  2.7,  3.9,  1.2],
        [ 6. ,  2.7,  5.1,  1.6],
        [ 5.4,  3. ,  4.5,  1.5],
        [ 6. ,  3.4,  4.5,  1.6],
        [ 6.7,  3.1,  4.7,  1.5],
        [ 6.3,  2.3,  4.4,  1.3],
        [ 5.6,  3. ,  4.1,  1.3],
        [ 5.5,  2.5,  4. ,  1.3],
        [ 5.5,  2.6,  4.4,  1.2],
        [ 6.1,  3. ,  4.6,  1.4],
        [ 5.8,  2.6,  4. ,  1.2],
        [ 5. ,  2.3,  3.3,  1. ],
        [ 5.6,  2.7,  4.2,  1.3],
        [ 5.7,  3. ,  4.2,  1.2],
        [ 5.7,  2.9,  4.2,  1.3],
        [ 6.2,  2.9,  4.3,  1.3],
        [ 5.1,  2.5,  3. ,  1.1],
        [ 5.7,  2.8,  4.1,  1.3],
        [ 6.3,  3.3,  6. ,  2.5],
        [ 5.8,  2.7,  5.1,  1.9],
        [ 7.1,  3. ,  5.9,  2.1],
        [ 6.3,  2.9,  5.6,  1.8],
        [ 6.5,  3. ,  5.8,  2.2],
        [ 7.6,  3. ,  6.6,  2.1],
        [ 4.9,  2.5,  4.5,  1.7],
        [ 7.3,  2.9,  6.3,  1.8],
        [ 6.7,  2.5,  5.8,  1.8],
        [ 7.2,  3.6,  6.1,  2.5],
        [ 6.5,  3.2,  5.1,  2. ],
        [ 6.4,  2.7,  5.3,  1.9],
        [ 6.8,  3. ,  5.5,  2.1],
        [ 5.7,  2.5,  5. ,  2. ],
        [ 5.8,  2.8,  5.1,  2.4],
        [ 6.4,  3.2,  5.3,  2.3],
        [ 6.5,  3. ,  5.5,  1.8],
        [ 7.7,  3.8,  6.7,  2.2],
        [ 7.7,  2.6,  6.9,  2.3],
        [ 6. ,  2.2,  5. ,  1.5],
        [ 6.9,  3.2,  5.7,  2.3],
        [ 5.6,  2.8,  4.9,  2. ],
        [ 7.7,  2.8,  6.7,  2. ],
        [ 6.3,  2.7,  4.9,  1.8],
        [ 6.7,  3.3,  5.7,  2.1],
        [ 7.2,  3.2,  6. ,  1.8],
        [ 6.2,  2.8,  4.8,  1.8],
        [ 6.1,  3. ,  4.9,  1.8],
        [ 6.4,  2.8,  5.6,  2.1],
        [ 7.2,  3. ,  5.8,  1.6],
        [ 7.4,  2.8,  6.1,  1.9],
        [ 7.9,  3.8,  6.4,  2. ],
        [ 6.4,  2.8,  5.6,  2.2],
        [ 6.3,  2.8,  5.1,  1.5],
        [ 6.1,  2.6,  5.6,  1.4],
        [ 7.7,  3. ,  6.1,  2.3],
        [ 6.3,  3.4,  5.6,  2.4],
        [ 6.4,  3.1,  5.5,  1.8],
        [ 6. ,  3. ,  4.8,  1.8],
        [ 6.9,  3.1,  5.4,  2.1],
        [ 6.7,  3.1,  5.6,  2.4],
        [ 6.9,  3.1,  5.1,  2.3],
        [ 5.8,  2.7,  5.1,  1.9],
        [ 6.8,  3.2,  5.9,  2.3],
        [ 6.7,  3.3,  5.7,  2.5],
        [ 6.7,  3. ,  5.2,  2.3],
        [ 6.3,  2.5,  5. ,  1.9],
        [ 6.5,  3. ,  5.2,  2. ],
        [ 6.2,  3.4,  5.4,  2.3],
        [ 5.9,  3. ,  5.1,  1.8]]),
 'feature_names': ['sepal length (cm)',
  'sepal width (cm)',
  'petal length (cm)',
  'petal width (cm)'],
 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 'target_names': array(['setosa', 'versicolor', 'virginica'],
       dtype='<U10')}

为了更清晰的查看该字典的信息,下面的代码通过循环输出字典对应的键值对。

可以看到:

  • iris数据集中的鸢尾花样本共分为3个类别 (取值为0、1、2),类别名来自于target_names键 (取值为 ‘setosa’ ‘versicolor’ ‘virginica’);
  • 每个类别各有50个样本,一共150个样本;
  • 每个样本有4个属性,分别是花萼长度 (sepal length)、花萼宽度 (sepal width)、花瓣长度 (petal length) 和花瓣宽度 (petal width);
  • 每个样本的真实类别 (取值为0、1、2中的一个) 需要从target键对应的值中去找寻。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
print("鸢尾花字典的键:",iris.keys())
for key in iris.keys():
    print(key)
    print(iris[key])
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
鸢尾花字典的键: dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
data
[[ 5.1  3.5  1.4  0.2]
 [ 4.9  3.   1.4  0.2]
 [ 4.7  3.2  1.3  0.2]
 [ 4.6  3.1  1.5  0.2]
 [ 5.   3.6  1.4  0.2]
 [ 5.4  3.9  1.7  0.4]
 [ 4.6  3.4  1.4  0.3]
 [ 5.   3.4  1.5  0.2]
 [ 4.4  2.9  1.4  0.2]
 [ 4.9  3.1  1.5  0.1]
 [ 5.4  3.7  1.5  0.2]
 [ 4.8  3.4  1.6  0.2]
 [ 4.8  3.   1.4  0.1]
 [ 4.3  3.   1.1  0.1]
 [ 5.8  4.   1.2  0.2]
 [ 5.7  4.4  1.5  0.4]
 [ 5.4  3.9  1.3  0.4]
 [ 5.1  3.5  1.4  0.3]
 [ 5.7  3.8  1.7  0.3]
 [ 5.1  3.8  1.5  0.3]
 [ 5.4  3.4  1.7  0.2]
 [ 5.1  3.7  1.5  0.4]
 [ 4.6  3.6  1.   0.2]
 [ 5.1  3.3  1.7  0.5]
 [ 4.8  3.4  1.9  0.2]
 [ 5.   3.   1.6  0.2]
 [ 5.   3.4  1.6  0.4]
 [ 5.2  3.5  1.5  0.2]
 [ 5.2  3.4  1.4  0.2]
 [ 4.7  3.2  1.6  0.2]
 [ 4.8  3.1  1.6  0.2]
 [ 5.4  3.4  1.5  0.4]
 [ 5.2  4.1  1.5  0.1]
 [ 5.5  4.2  1.4  0.2]
 [ 4.9  3.1  1.5  0.1]
 [ 5.   3.2  1.2  0.2]
 [ 5.5  3.5  1.3  0.2]
 [ 4.9  3.1  1.5  0.1]
 [ 4.4  3.   1.3  0.2]
 [ 5.1  3.4  1.5  0.2]
 [ 5.   3.5  1.3  0.3]
 [ 4.5  2.3  1.3  0.3]
 [ 4.4  3.2  1.3  0.2]
 [ 5.   3.5  1.6  0.6]
 [ 5.1  3.8  1.9  0.4]
 [ 4.8  3.   1.4  0.3]
 [ 5.1  3.8  1.6  0.2]
 [ 4.6  3.2  1.4  0.2]
 [ 5.3  3.7  1.5  0.2]
 [ 5.   3.3  1.4  0.2]
 [ 7.   3.2  4.7  1.4]
 [ 6.4  3.2  4.5  1.5]
 [ 6.9  3.1  4.9  1.5]
 [ 5.5  2.3  4.   1.3]
 [ 6.5  2.8  4.6  1.5]
 [ 5.7  2.8  4.5  1.3]
 [ 6.3  3.3  4.7  1.6]
 [ 4.9  2.4  3.3  1. ]
 [ 6.6  2.9  4.6  1.3]
 [ 5.2  2.7  3.9  1.4]
 [ 5.   2.   3.5  1. ]
 [ 5.9  3.   4.2  1.5]
 [ 6.   2.2  4.   1. ]
 [ 6.1  2.9  4.7  1.4]
 [ 5.6  2.9  3.6  1.3]
 [ 6.7  3.1  4.4  1.4]
 [ 5.6  3.   4.5  1.5]
 [ 5.8  2.7  4.1  1. ]
 [ 6.2  2.2  4.5  1.5]
 [ 5.6  2.5  3.9  1.1]
 [ 5.9  3.2  4.8  1.8]
 [ 6.1  2.8  4.   1.3]
 [ 6.3  2.5  4.9  1.5]
 [ 6.1  2.8  4.7  1.2]
 [ 6.4  2.9  4.3  1.3]
 [ 6.6  3.   4.4  1.4]
 [ 6.8  2.8  4.8  1.4]
 [ 6.7  3.   5.   1.7]
 [ 6.   2.9  4.5  1.5]
 [ 5.7  2.6  3.5  1. ]
 [ 5.5  2.4  3.8  1.1]
 [ 5.5  2.4  3.7  1. ]
 [ 5.8  2.7  3.9  1.2]
 [ 6.   2.7  5.1  1.6]
 [ 5.4  3.   4.5  1.5]
 [ 6.   3.4  4.5  1.6]
 [ 6.7  3.1  4.7  1.5]
 [ 6.3  2.3  4.4  1.3]
 [ 5.6  3.   4.1  1.3]
 [ 5.5  2.5  4.   1.3]
 [ 5.5  2.6  4.4  1.2]
 [ 6.1  3.   4.6  1.4]
 [ 5.8  2.6  4.   1.2]
 [ 5.   2.3  3.3  1. ]
 [ 5.6  2.7  4.2  1.3]
 [ 5.7  3.   4.2  1.2]
 [ 5.7  2.9  4.2  1.3]
 [ 6.2  2.9  4.3  1.3]
 [ 5.1  2.5  3.   1.1]
 [ 5.7  2.8  4.1  1.3]
 [ 6.3  3.3  6.   2.5]
 [ 5.8  2.7  5.1  1.9]
 [ 7.1  3.   5.9  2.1]
 [ 6.3  2.9  5.6  1.8]
 [ 6.5  3.   5.8  2.2]
 [ 7.6  3.   6.6  2.1]
 [ 4.9  2.5  4.5  1.7]
 [ 7.3  2.9  6.3  1.8]
 [ 6.7  2.5  5.8  1.8]
 [ 7.2  3.6  6.1  2.5]
 [ 6.5  3.2  5.1  2. ]
 [ 6.4  2.7  5.3  1.9]
 [ 6.8  3.   5.5  2.1]
 [ 5.7  2.5  5.   2. ]
 [ 5.8  2.8  5.1  2.4]
 [ 6.4  3.2  5.3  2.3]
 [ 6.5  3.   5.5  1.8]
 [ 7.7  3.8  6.7  2.2]
 [ 7.7  2.6  6.9  2.3]
 [ 6.   2.2  5.   1.5]
 [ 6.9  3.2  5.7  2.3]
 [ 5.6  2.8  4.9  2. ]
 [ 7.7  2.8  6.7  2. ]
 [ 6.3  2.7  4.9  1.8]
 [ 6.7  3.3  5.7  2.1]
 [ 7.2  3.2  6.   1.8]
 [ 6.2  2.8  4.8  1.8]
 [ 6.1  3.   4.9  1.8]
 [ 6.4  2.8  5.6  2.1]
 [ 7.2  3.   5.8  1.6]
 [ 7.4  2.8  6.1  1.9]
 [ 7.9  3.8  6.4  2. ]
 [ 6.4  2.8  5.6  2.2]
 [ 6.3  2.8  5.1  1.5]
 [ 6.1  2.6  5.6  1.4]
 [ 7.7  3.   6.1  2.3]
 [ 6.3  3.4  5.6  2.4]
 [ 6.4  3.1  5.5  1.8]
 [ 6.   3.   4.8  1.8]
 [ 6.9  3.1  5.4  2.1]
 [ 6.7  3.1  5.6  2.4]
 [ 6.9  3.1  5.1  2.3]
 [ 5.8  2.7  5.1  1.9]
 [ 6.8  3.2  5.9  2.3]
 [ 6.7  3.3  5.7  2.5]
 [ 6.7  3.   5.2  2.3]
 [ 6.3  2.5  5.   1.9]
 [ 6.5  3.   5.2  2. ]
 [ 6.2  3.4  5.4  2.3]
 [ 5.9  3.   5.1  1.8]]
target
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
target_names
['setosa' 'versicolor' 'virginica']
DESCR
Iris Plants Database
====================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
    :Summary Statistics:

    ============== ==== ==== ======= ===== ====================
                    Min  Max   Mean    SD   Class Correlation
    ============== ==== ==== ======= ===== ====================
    sepal length:   4.3  7.9   5.84   0.83    0.7826
    sepal width:    2.0  4.4   3.05   0.43   -0.4194
    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
    petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)
    ============== ==== ==== ======= ===== ====================

    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris

The famous Iris database, first used by Sir R.A Fisher

This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

References
----------
   - Fisher,R.A. "The use of multiple measurements in taxonomic problems"
     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
     Mathematical Statistics" (John Wiley, NY, 1950).
   - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.
     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
     Structure and Classification Rule for Recognition in Partially Exposed
     Environments".  IEEE Transactions on Pattern Analysis and Machine
     Intelligence, Vol. PAMI-2, No. 1, 67-71.
   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
     on Information Theory, May 1972, 431-433.
   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
     conceptual clustering system finds 3 classes in the data.
   - Many, many more ...

feature_names
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

为方便后续处理,把该字典信息加载到DataFrame对象中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import pandas as pd
#下面语句用于提取每个样本的4个属性值
#下面语句也可写成df=pd.DataFrame(iris.data,columns=iris.feature_names),这是把字典的键看成是iris的属性
df=pd.DataFrame(iris['data'],columns=iris['feature_names'])

#下面语句提取每个样本的真实类别标签,该语句也可以写成d['target']=iris.target
df['target']=iris['target']
df.sample(10)       #sample(10)函数作用:随机选择10个样本进行显示
在这里插入图片描述
在这里插入图片描述
(二)进行z-score标准化处理

为减小不同特征/属性的量纲差距,下面的代码进行了z-score标准化处理。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from sklearn.preprocessing import StandardScaler
ss=StandardScaler()
new_data=ss.fit_transform(iris.data)
new_df=pd.DataFrame(new_data,columns=iris.feature_names)
#提取每个样本的真实类别标签
new_df['target']=iris.target
new_df.sample(10)
(三)划分训练集和测试集

下面的train_test_split函数将把全部150个样本划分成训练集和测试集,其中测试集占30% (对应于test_size=0.3)。划分时使用了stratify参数进行分层采样 (参考:https://zhuanlan.zhihu.com/p/49991313) ,这样可以保证每个类别的训练样本都是35个。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(new_df.iloc[:,0:4],new_df['target'],stratify=new_df['target'],random_state=33,test_size=0.3)
print(y_train.value_counts())

如果不使用stratify参数进行分层采样,有可能使得不同类别的训练样本数量不同,进而影响分类准确性。下面的划分没有使用分层采样,可以发现出现了样本不平衡现象,这会影响后面的分类效果 (去掉注释后可以对比分析)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#X_train,X_test,y_train,y_test=train_test_split(new_df.iloc[:,0:4],new_df['target'],random_state=33,test_size=0.3)
#print(y_train.value_counts())
(四)进行类别预测和模型性能评估
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#https://scikit-learn.org/stable/modules/classes.html#module-sklearn.neighbors

from sklearn.neighbors import KNeighborsClassifier

#下面的语句创建kNN分类估计器,这里指定超参数k等于9,即为了分类,需要找到测试样本的9个近邻去投票
neigh = KNeighborsClassifier(n_neighbors=9)  #n_neighbors默认值是5
#下面的语句进行数据拟合(类似于模型学习),对kNN分类器而言就是构建一颗kd_tree
neigh.fit(X_train,y_train)
#下面的语句进行测试样本的类别预测,对kNN分类器而言就是在kd_tree上找到k个近邻并投票确定测试样本的类别
y_pred=neigh.predict(X_test)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#下面的代码用于查看分类性能:
#注意:多分类问题可以使用accuracy_score查看分类的准确性
ACC = metrics.accuracy_score(y_test,y_pred)
print("分类的准确率:",ACC)

#注意:对于多分类问题,却无法使用precision_score/recall_score/f1_score函数
#precision=metrics.precision_score(y_test,y_pred)
#print("分类的查准率:",precision)

#对于多分类问题,却可以使用classification_report函数查看相关性能指标
print("分类性能报告:")
print(classification_report(y_test,y_pred))
(五)利用网格搜索来优化分类模型的性能

为了找到可能更好的超参数k提升分类的性能,下面的代码演示了交叉验证配合网格搜索。

超参数说明:模型参数 (如果有的话,例如SVM中分离超平面的法向量w和截距b) 是通过fit方法从数据中学习到的,而超参数则是人工配置的,因而创建模型对象时指定的参数是超参数。

为了评估不同超参数组合配置下的模型性能,可以采用网格搜索的方法,并且常常与训练数据集的交叉验证进行搭配。网格搜索的目的是为了优化超参数配置,交叉验证的目的是为了更准确的评估特定超参数配置下的模型性能的可信度,两者作用不同。

关于交叉验证,可以参考: https://www.cnblogs.com/qiu-hua/p/14904992.html https://www.ngui.cc/el/1515667.html?action=onClick

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from sklearn.model_selection import GridSearchCV,KFold
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report

#下面的代码指定了网格搜索的范围,即把从39范围内的整数一个个尝试作为超参数k的取值
params_knn={'n_neighbors':range(3,16,1)}
#下面的代码指定进行3折交叉验证,交叉验证的好处(可以自行百度):
#充分利用所有数据、更好地评估模型稳定性、有利于超参数调优、防止过拟合等
kf=KFold(n_splits=3,shuffle=False)

#下面的语句创建kNN分类估计器,创建时所有参数使用默认值
neigh = KNeighborsClassifier()
#下面的语句创建网格搜索估计器,并依次指定分类算法、超参数k的取值范围、交叉验证的设置
grid_search_knn=GridSearchCV(neigh,params_knn,cv=kf)
#下面的语句用创建网格搜索估计器进行训练数据的拟合(类似于模型学习)
grid_search_knn.fit(X_train,y_train)
#下面的语句用拟合好的网格搜索估计器(类似于学习到的模型)进行测试集中所有样本的分类预测
grid_search_y_pred=grid_search_knn.predict(X_test)
#下面的代码用于分类的性能评价
print("分类准确率:",grid_search_knn.score(X_test,y_test))
print("网格搜索得到的最佳超参数:",grid_search_knn.best_params_)
print("输出分类性能报告:")
print(classification_report(y_test,grid_search_y_pred))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
机器学习-实现简单神经网络(差代码) 原
(5)如果正确则输出电信号,得到最终结果;如果错误,就把错误结果根据前面所描述得步骤返回来,对权重向量进行跟新,再把其他原有的训练样本或者是新的徐连样本再重新输入到感知器中。
晓歌
2018/08/15
3950
机器学习-实现简单神经网络(差代码)
                                                                            原
KNN算法应用
Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理。Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性。可通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类。
foochane
2019/05/23
7730
KNN算法应用
利用skl实现KNN
本文通过SKL的中KNN分类器,实现了对iris数据的的分类预测,主要涉及的内容包含:
皮大大
2021/03/01
3050
利用skl实现KNN
Python3入门机器学习(三)- matplotlib基础与简单应用
matplotlib基础 matplotlib 基础 import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np # 对0到10分成100份 x = np.linspace(0,10,100) x array([ 0. , 0.1010101 , 0.2020202 , 0.3030303 , 0.4040404 , 0.50505051, 0.60606061, 0.707
Meet相识
2018/09/12
1.1K0
Python3入门机器学习(三)- matplotlib基础与简单应用
【一起从0开始学习人工智能】0x01机器学习基础+初次实践
人工智能------机器学习-------深度学习 应用:网络安全、交通网络、社交网络…
20岁爱吃必胜客
2023/01/01
3400
【一起从0开始学习人工智能】0x01机器学习基础+初次实践
「R」数据操作(四):初学者学习tidyverse
tidyverse是一组处理与可视化R包的集合(人称“极乐净土”,但我并不喜欢这个称呼),其中ggplot2与dplyr最广为人知。
王诗翔呀
2020/07/06
1.8K0
「R」数据操作(四):初学者学习tidyverse
dplyr_new version-across
2020年5月29日,dplyr迎来来大更新,作为大版本的更新,还是有必要进行学习一下
火星娃统计
2020/09/15
4300
前端搞AI:在浏览器中训练模型
本文将在浏览器中定义、训练和运行模型。为了实现这一功能,我将构建一个识别鸢尾花的案例。
前端修罗场
2022/07/29
8460
前端搞AI:在浏览器中训练模型
从Iris数据集开始---机器学习入门
代码多来自《Introduction to Machine Learning with Python》. 该文集主要是自己的一个阅读笔记以及一些小思考,小总结。 #前言 在开始进行模型训练之前,
用户1631856
2018/04/12
2.1K0
从Iris数据集开始---机器学习入门
A.机器学习入门算法(三):K近邻(k-nearest neighbors),鸢尾花KNN分类,马绞痛数据--kNN数据预处理+kNN分类pipeline
kNN(k-nearest neighbors),中文翻译K近邻。我们常常听到一个故事:如果要了解一个人的经济水平,只需要知道他最好的5个朋友的经济能力, 对他的这五个人的经济水平求平均就是这个人的经济水平。这句话里面就包含着kNN的算法思想。
汀丶人工智能
2023/03/22
1.8K0
A.机器学习入门算法(三):K近邻(k-nearest neighbors),鸢尾花KNN分类,马绞痛数据--kNN数据预处理+kNN分类pipeline
利用Matlab对经典鸢尾花数据集实现决策树算法分类,并绘图
最近在学习数据挖掘,其实决策树分类看过去好久了,但是最近慢慢的想都实现一下,加深一下理解。 知道决策树有很多现成的算法(ID3,C4.5、CART),但是毕竟核心思想就是那几点,所以本篇博客就是我随便实现的,没有参考现有的决策树算法。考虑到实现分类起码需要一个数据集,所以我选择了经典的鸢尾花数据集,下载地址:Iris
全栈程序员站长
2022/07/23
2.7K1
利用Matlab对经典鸢尾花数据集实现决策树算法分类,并绘图
可以用来自己写函数的function函数
--- title: "可以用来自己写函数的function函数" output: html_document date: "2023-03-11" --- 我们在学习R语言的过程中需要学习、使用各种大神已经写好的函数,那我们能不能也写出一个属于自己的函数呢?使用function函数就可以轻松做到! 1.function函数的简介——写函数的函数 # 我们想要求两个数的平方,可以设计以下函数 jimmy_sq <- function(a,b,m = 2){ (a+b)^m
小叮当aka
2023/03/22
6650
Day6——R包的学习
菜单栏-Tools-Packages-Primary CRAN repository-选择国内镜像
掩扉先生
2023/10/25
2380
R海拾遗-tidyverse
tidyverse函数高效,代码简洁,受过专业训练的一般都用这个,除非记不住,能记一点是一点吧。 love&peace
火星娃统计
2020/09/15
1.2K0
机器学习之决策树三-CART原理与代码实现
本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9482885.html
用户7225427
2020/09/03
6790
机器学习之决策树三-CART原理与代码实现
R语言常见的内置数据
---title: "R语言常见的内置数据"output: html_documentdate: "2023-03-09"---在学习R语言的过程中,合理运用内置数据可以帮助我们更好地学习,下面介绍几种不同数据类型的内置数据1.数据框x <- irisx## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 s
小叮当aka
2023/03/18
4500
Day-6:学习R包
BiocManager::install(“包”)(R包来自Bioconductor)
用户11039713
2024/03/27
1220
R语言入门
最近在复习python的科学计算,突然心血来潮,想看看R的数据处理和python的区别在哪,所以就有了这篇文章。 R语言简介 四十多年前, R 语言的始祖诞生了 , John Chambers 在贝尔实验室中开发出S语言 ,用于快速地进行数据探索, 统计分析和可视化 。十几年后 , 新西兰奥克兰大学的 Robert Gentleman 和 Ross Ihaka 在 S 语言的基础上发明了 R 语言 。 R 语言流淌着统计学的血液 , 它内置了海量的统计函数 ,使用者可以利用其对数据进行快速交互分析 。
若与
2018/04/25
2.5K0
R语言入门
Python基础(十三) | 机器学习sklearn库详解与应用
可见,花瓣的长度和宽度有非常好的相关性。而花萼的长宽效果不好,因此考虑对他们丢弃。
timerring
2022/10/27
7290
学习小组day6笔记-R包
all_of(): Matches variable names in a character vector. All names must be present, otherwise an out-of-bounds error is thrown.
清南
2023/04/13
4100
相关推荐
机器学习-实现简单神经网络(差代码) 原
更多 >
LV.1
这个人很懒,什么都没有留下~
目录
  • 一、混淆矩阵相关概念示例
  • 二、使用kNN(k近邻)算法对鸢尾花数据集进行分类
    • (一)加载并查看鸢尾花数据集
    • (二)进行z-score标准化处理
    • (三)划分训练集和测试集
    • (四)进行类别预测和模型性能评估
    • (五)利用网格搜索来优化分类模型的性能
加入讨论
的问答专区 >
1KOL擅长5个领域
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档