• 机器学习3判断机器算法的性能


    一、判断机器算法的性能1基本使用

    1.目的

    判断机器算法的性能(train test split)目的是帮助我们找到一个更好的模型/测试时数据,训练数据;预测的准确率(越接近1越准确):
    在这里插入图片描述
    在这里插入图片描述

    2.使用pycharm函数封装

    使用pycharm函数封装的形式运行train test split算法:

    import numpy as np
    def train_test_split(x,y,test_ratio=0.2,seed=None):
        """将数据x和y按照test_ratio分割成训练数据集和测试数据集"""
        assert x.shape[0]==y.shape[0],\
        "the size of x must be equal to the sze of y"
        assert 0.0<=test_ratio<=1.0,\
        "test_ration must be valid"
        if seed:
            np.random.seed(seed)
        shuffle_indexes = np.random.permutation(len(x))
        test_size = int(len(x) * test_ratio)
        test_indexes = shuffle_indexes[:test_size]  # 确定测试数据集对应的索引
        train_indexes = shuffle_indexes[test_size:]  # 训练数据集所对应的索引
        x_train = x[train_indexes]
        y_train = y[train_indexes]
    
        x_test = x[test_indexes]
        y_test = y[test_indexes]
        return x_train,x_test,y_train,y_test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    3.sklearn中的train test split:

    在这里插入图片描述

    4.完美调用:

    在这里插入图片描述

    二、判断机器算法的性能2分类的准确度(accuracy)

    分类的准确度(accuracy):

    准确度初步计算:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    完善KNNpy程序如下:

    import numpy as np
    from math import sqrt
    from collections import Counter
    from .metrics import accuracy_score
    
    class KNNClassifier:
    
        def __init__(self, k):
            """初始化kNN分类器"""
            assert k >= 1, "k must be valid"
            self.k = k
            self._X_train = None
            self._y_train = None
    
        def fit(self, X_train, y_train):
            """根据训练数据集X_train和y_train训练kNN分类器"""
            assert X_train.shape[0] == y_train.shape[0], \
                "the size of X_train must be equal to the size of y_train"
            assert self.k <= X_train.shape[0], \
                "the size of X_train must be at least k."
    
            self._X_train = X_train
            self._y_train = y_train
            return self
    
        def predict(self, X_predict):
            """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
            assert self._X_train is not None and self._y_train is not None, \
                    "must fit before predict!"
            assert X_predict.shape[1] == self._X_train.shape[1], \
                    "the feature number of X_predict must be equal to X_train"
    
            y_predict = [self._predict(x) for x in X_predict]
            return np.array(y_predict)
    
        def _predict(self, x):
            """给定单个待预测数据x,返回x的预测结果值"""
            assert x.shape[0] == self._X_train.shape[1], \
                "the feature number of x must be equal to X_train"
    
            distances = [sqrt(np.sum((x_train - x) ** 2))
                         for x_train in self._X_train]
            nearest = np.argsort(distances)
    
            topK_y = [self._y_train[i] for i in nearest[:self.k]]
            votes = Counter(topK_y)
    
            return votes.most_common(1)[0][0]
    
        def score(self, X_test, y_test):
            """根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
    
            y_predict = self.predict(X_test)
            return accuracy_score(y_test, y_predict)
    
        def __repr__(self):
            return "KNN(k=%d)" % self.k
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    Linux操作系统:IPTables
    android 闪屏图适配尺寸
    交通状态预测 | Python实现基于Social-LSTM的轨迹预测方法
    将大量文件的拓展名中大写字母改为小写:Python实现
    # STM32定时器
    MySQL:面试问的范式设计
    [思维]The Enchanted Forest Codeforces1688D
    Python中list列表的常见操作
    初识 Linux 文件系统
    什么是机器学习力场
  • 原文地址:https://blog.csdn.net/m0_57297999/article/details/128139564