zoukankan      html  css  js  c++  java
  • 分类和回归

    了解

    分类【classification】

    预测男女,预测是否通过考试等

      分类是求topk中出现最多的类别

    回归【regression】

    预测体重、房价、损失等

      回归是求topk的value的平均值

    与knn的关系

    在对一个问题进行预测前,要分清这个问题属于哪一类,每一类有对应的数学模型区解决。

    knn刚好都是适用于这两个模型

    案例

    房价预测

    1、手动填充数据预测

    import numpy as np
    
    feature = np.array([
        [84,83],
        [84.1,85],
        [84.2,84],
        [84.3,84.5],
        [85,83.6]
    ])
    label = np.array([
        200,250,234,246,243
    ])
    
    # 预测点
    predictPoint = np.array([84.2,85.2])
    
    """
    欧氏距离计算
    """
    # 矩阵减向量(广播)
    matritemp = (feature - predictPoint)
    
    matritemp2 = np.square(matritemp)
    
    # axis=1 :逐行相加
    sortindex = np.argsort(np.sqrt(np.sum(matritemp2,axis=1)))
    
    sortlabel = label[sortindex]
    
    # k:训练集的开平方
    k = 2
    predictPrice = np.sum(sortlabel[0:k]) / k
    print(f'位置是{predictPoint}处预测的房价是{predictPrice}万')
    
    """
    位置是[84.2 85.2]处预测的房价是248.0万
    """

    2、自动加载数据集

    选用 boston.csv数据集介绍 

    import numpy as np
    
    def knn(k,predictPoint,feature,label):
        """
        欧氏距离计算
        """
        # 矩阵减向量(广播)
        matritemp = (feature - predictPoint)
    
        matritemp2 = np.square(matritemp)
    
        # axis=1 :逐行相加
        sortindex = np.argsort(np.sqrt(np.sum(matritemp2,axis=1)))
    
        sortlabel = label[sortindex]
    
        predictPrice = np.sum(sortlabel[0:k]) / k
        return predictPrice
    
    if __name__ == '__main__':
        feature = np.loadtxt("boston.csv",delimiter=",",skiprows=1,usecols=(0,1,2,3,8,10))
        label  = np.loadtxt("boston.csv",delimiter=",",skiprows=1,usecols=(12))
        predictPoint = np.array([0.02055,85,0.74,0,2,17.3])
        print(knn(22,predictPoint,feature,label))
    
    """
    原来的:24.7
    预测的:30.822727272727263
    """

    3、数据归一化和标准化

    归一化

    import numpy as np
    
    test = np.array([10,11,121,15,2,1555,65])
    
    print((test - np.min(test)) / (np.max(test) - np.min(test)))
    
    """
    [0.00515132 0.00579524 0.07662589 0.0083709  0.         1.
     0.04056665]
    """

    标准化

    使数据成正态分布

    import numpy as np
    
    test = np.array([
        [1,8],
        [8,5],
        [2,9]
    ])
    
    mean = (np.mean(test[:,1]))
    std = np.std(test[:,1])
    
    print((test[:,1] - mean) / std)
    
    """
    [-0.86266219  1.40182605 -0.53916387]
    [ 0.39223227 -1.37281295  0.98058068]
    """
  • 相关阅读:
    C#反射中Assembly.Load及Assembly.Load.CreateInstance
    ASP.NET知识点(一):面向接口,工厂模式的程序结构
    表格布局规范
    山塞一个PetShop 4.0(01)——最简单的数据库连接
    ASP.NET知识点(二):数据访问层的基础[SQLHelper]
    山塞一个PetShop ——源代码下载、安装、配置及体验
    OD基本快捷键及功能
    OD使用教程 调试篇01|解密系列
    OD基本快捷键及功能
    OD基本快捷键及功能
  • 原文地址:https://www.cnblogs.com/pam-sh/p/13192293.html
Copyright © 2011-2022 走看看