• 测试机器学习算法的好坏


    一:当我们训练出一个模型,我们不知道这个模型是好还是坏,我们如果应用到真实环境,结果预测的没有可信度,所以在输入样例之前先对模型进行测试。这时,我们就要将我们所有的数据分为两部分,一大部分用来训练,一小部分用来测试,一般比例在8:2。也就是我们会有两套数据集,一个是训练集,另一个是测试集。

    二:鸢尾花举例说明

    #测试机器学习算法的好坏
    
    import numpy as np
    from sklearn import datasets
    from sklearn.neighbors import KNeighborsClassifier
    
    iris = datasets.load_iris()#读取数据
    x = iris.data #获取特征数据
    y = iris.target#获取样本标记
    
    shuffle_indexs = np.random.permutation(len(x)) #将索引打乱顺序,因为这里面的标记都是排好序的,不打乱顺序,不能将所有的标记加载到训练集中
    
    test_radio = 0.2
    test_size = int(test_radio*len(x))
    
    #获取训练的下标和测试的下标
    train_indexs = shuffle_indexs[test_size:]
    test_indexs = shuffle_indexs[:test_size]
    
    #获取训练集的数据和测试集的数据
    x_train = x[train_indexs]
    y_train = y[train_indexs]
    
    x_test = x[test_indexs]
    y_test = y[test_indexs]
    
    #KNN算法执行
    knn = KNeighborsClassifier(n_neighbors=5)
    knn.fit(x_train,y_train)
    y_predict = knn.predict(x_test) #预测结果
    rate = sum(y_predict==y_test)/len(y_test) #求比例
    
    print(rate)#准确率
    
  • 相关阅读:
    预览图片功能 直接复制就OK
    记录:express返回自定义http状态吗
    Git tag 给当前分支打标签
    css双飞翼和圣杯布局
    简单模拟MVVM数据双向绑定
    JS的自定义事件(观察者模式)
    js模拟触发事件
    QueryString和BASE64
    WebResource.axd文件的配置和使用
    处理json中的异常字符
  • 原文地址:https://www.cnblogs.com/lyr999736/p/10654374.html
Copyright © 2020-2023  润新知