zoukankan      html  css  js  c++  java
  • 【笔记】KNN之分类准确度

    分类准确度

    分类准确度

    以sklearn中的手写数字datasets.load_digits为例,其是8*8的图形,具有64个特征值,类别由0到9

    我们挑选出666这个图形,将其可视化

      X = digits.data
      some_digit = X[666]
      some_digit_image = some_digit.reshape(8,8)
      plt.imshow(some_digit_image,cmap = matplotlib.cm.binary)
    

    我们使用自己的算法(见前笔记)
    将比例设置成0.2,k=3

    X_train,X_test,y_train,y_test=train_test_split(X,y,test_ratio=0.2)
    

    将预测到的结果放到y_predict中

      y_predict = my_knn_clf.predict(X_test)
    

    最后通过预测和测试进行比对,得到预测准确度

    在pc中写入metrics.py,其中写入代码将准确度计算也封装起来

      import numpy as np
    
      def accuracy_score(y_true, y_predict):
          "“”计算准确率“”"
          assert y_true.shape[0] == y_predict.shape[0], \
              "this size of y_true must be equal to the size of y_predict"
    
          return sum(y_true == y_predict) / len(y_true)
    

    运行结果无误

    方便起见,我们直接将准确度计算放到knn算法中一并封装

    由于我们对算法进行了改动,可能导致现在再运行的话,算法里面并没有现在我们新添加的内容,因此我们需要重新启动并运行所有代码块

    我们就可以直接使用score得到准确度,而不是需要先对数据进行处理

    scikit-learn中的accuracy_score

    与前笔记中对sklearn数据集操作同理
    注意:随机种子设置为666

      test_size=0.2, random_state=666
    

    您能读到这儿,我呢是发自真心的感谢您,若要转载,还望请您带上链接
  • 相关阅读:
    Unicode与汉字
    URL编码
    安装apk到虚拟的device
    nginx的conf文件的详细配置
    Linux下MySQL安装及命令使用
    转——iptables详细配置
    Linux下vim文件未正常退出,修复文件
    locate命令的安装
    安装Jenkins后 启动时失败的问题解决
    ——转 token 介绍
  • 原文地址:https://www.cnblogs.com/jokingremarks/p/14276553.html
Copyright © 2011-2022 走看看