zoukankan      html  css  js  c++  java
  • 2.x ESL第二章习题 2.8

    题目

    代码

    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
    import pandas as pd
    import numpy as np
    train_full=pd.read_csv('../zip.train',sep=' ',engine='c',header=None).values[:,0:-1]
    test_full=pd.read_csv('../zip.test',sep=' ',engine='c',header=None).values
       
    train = np.vstack((train_full[train_full[:,0]==2],train_full[train_full[:,0]==3]))
    test = np.vstack((test_full[test_full[:,0]==2],test_full[test_full[:,0]==3]))
       
    train_x=train[:,1:]
    train_y=train[:,0]
    test_x=test[:,1:]
    test_y=test[:,0]
       
    dc = []
    from sklearn.linear_model import LinearRegression
    lrcf = LinearRegression()
    lrcf.fit(train_x, train_y)
    dc.append(('linear regression',lrcf))
       
    from sklearn.neighbors import KNeighborsClassifier
    for i in [1,3,5,7,15]:
        knn = KNeighborsClassifier(n_neighbors=i)
        knn.fit(train_x,train_y)
        dc.append(('%d-nearest neighbor' %(i),knn))
       
    def acc(clf,x,y):
        res = clf.predict(x)
        if type(clf)==LinearRegression:
            res[res>2.5]=3
            res[res<2.5]=2
        n = y.shape[0]
        r = res[(res==y)].shape[0]
        return r*1.0/n
       
    for i in dc:
        accr = acc(i[1],test_x,test_y)
        print '%s:  %.5f' %(i[0],accr)

    结果

    linear regression:  0.95879

    1-nearest neighbor:  0.97527

    3-nearest neighbor:  0.96978

    5-nearest neighbor:  0.96978

    7-nearest neighbor:  0.96703

    15-nearest neighbor:  0.96154



  • 相关阅读:
    JS中的一些遍历方法
    JS中关于引用类型数据及函数的参数传递
    JS中关于数组的操作
    CSS中的一些伪类
    JS中的回调函数
    JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性
    JDK的安装及环境变量配置
    JS中的this指针
    JS中的数据类型
    Word中页码及目录、参考文献的制做方法
  • 原文地址:https://www.cnblogs.com/porco/p/4724940.html
Copyright © 2011-2022 走看看