zoukankan      html  css  js  c++  java
  • 数据升维

    我们在实际应用中会遇到数据集特征不足的情况,要解决这个问题,就需要对数据集的特征进行扩充,

    一般使用两种方法:

    • 交互式特征(Interaction Features)
    • 多项式特征(Ploynomial Features)

    1.准备数据集

    #导入numpy
    import numpy as np
    #导入画图工具
    import matplotlib.pyplot as plt
    
    #导入神经网络
    from sklearn.neural_network import MLPRegressor
    #生成随机数列
    rnd = np.random.RandomState(38)
    x = rnd.uniform(-5,5,size=50)
    #向数据中添加噪声
    y_no_noise = (np.cos(6*x) + x)
    X = x.reshape(-1,1)
    y = (y_no_noise + rnd.normal(size=len(x)))/2
    
    #设置箱体数为11
    bins = np.linspace(-5,5,11)
    #将数据进行装箱操作
    target_bin = np.digitize(X,bins=bins)
    #导入独热编码
    from sklearn.preprocessing import OneHotEncoder
    onehot = OneHotEncoder(sparse = False,categories='auto')
    onehot.fit(target_bin)
    #使用独热编码转化数据
    X_in_bin = onehot.transform(target_bin)
    #生成一个等差数列
    line = np.linspace(-5,5,1000,endpoint=False).reshape(-1,1)
    #使用独热编码进行数据表达
    new_line = onehot.transform(np.digitize(line,bins=bins))
    

    2.向数据集中添加交互式特征

      交互式特征是指在原始数据特征中添加交互项,使特征数量增加.

    #############################  向数据集添加交互式特征 #######################################
    #手工生成两个数组
    array_1 = [1,2,3,4,5]
    array_2 = [6,7,8,9,0]
    #使用hstack将两个数组进行堆叠
    array_3 = np.hstack((array_1,array_2))
    #打印结果
    print('将数组2添加到数据1中后得到:{}'.format(array_3))
    
    将数组2添加到数据1中后得到:[1 2 3 4 5 6 7 8 9 0]
    #将原始数据和装箱后的数据进行堆叠
    X_stack = np.hstack([X,X_in_bin])
    print(X_stack.shape)
    
    (50, 11)
    #将数据进行堆叠
    line_stack = np.hstack([line,new_line])
    #重新训练模型
    mlpr_interact = MLPRegressor().fit(X_stack,y)
    #绘制图形
    plt.plot(line,mlpr_interact.predict(line_stack),label='MLP for interaction')
    plt.ylim(-4,4)
    for vline in bins:
             plt.plot([vline,vline],[-5,5],':',c='gray')
    plt.plot(X,y,'o',c='r')
    plt.legend(loc='lower right')
    #显示图形
    plt.show()
    

    #使用新的堆叠方式处理数据
    X_multi = np.hstack([X_in_bin,X*X_in_bin])
    #打印结果
    print(X_multi.shape)
    print(X_multi[0])
    
    (50, 20)
    [ 0.         0.         0.         1.         0.         0.
      0.         0.         0.         0.        -0.        -0.
     -0.        -1.1522688 -0.        -0.        -0.        -0.
     -0.        -0.       ]
    #重新训练模型
    mlpr_multi = MLPRegressor().fit(X_multi,y)
    line_multi = np.hstack([new_line,line * new_line])
    #绘制图形
    plt.plot(line,mlpr_multi.predict(line_multi),label = 'MLP Regressor')
    for vline in bins:
        plt.plot([vline,vline],[-5,5],':',c='gray')
    plt.plot(X,y,'o',c='r')
    plt.legend(loc='lower right')
    #显示图形
    plt.show()
    

    3.向数据集中添加多项式特征

    #############################  向数据集添加多项式特征 #######################################
    #导入多项式特征工具
    from sklearn.preprocessing import PolynomialFeatures
    #向数据集添加多项式特征
    poly = PolynomialFeatures(degree=20,include_bias = False)
    X_poly = poly.fit_transform(X)
    #打印结果
    print(X_poly.shape)
    
    (50, 20)
    #打印结果
    print('原始数据集中的第一个样本特征:
    {}'.format(X[0]))
    print('
    处理后的数据集中第一个样本特征:
    {}'.format(X_poly[0]))
    
    #打印结果
    print('PolynomialFeatures对原始数据的处理:
    {}'.format(poly.get_feature_names()))
    
    原始数据集中的第一个样本特征:
    [-1.1522688]
    
    处理后的数据集中第一个样本特征:
    [ -1.1522688    1.3277234   -1.52989425   1.76284942  -2.0312764
       2.34057643  -2.6969732    3.10763809  -3.58083443   4.1260838
      -4.75435765   5.47829801  -6.3124719    7.27366446  -8.38121665
       9.65741449 -11.12793745  12.82237519 -14.77482293  17.02456756]
    PolynomialFeatures对原始数据的处理:
    ['x0', 'x0^2', 'x0^3', 'x0^4', 'x0^5', 'x0^6', 'x0^7', 'x0^8', 'x0^9', 'x0^10', 'x0^11', 'x0^12', 'x0^13', 'x0^14', 'x0^15', 'x0^16', 'x0^17', 'x0^18', 'x0^19', 'x0^20']
    #导入线性回归
    from sklearn.linear_model import LinearRegression
    #使用处理后的数据训练线性回归模型
    LNR_poly = LinearRegression().fit(X_poly,y)
    line_poly = poly.transform(line)
    #绘制图形
    plt.plot(line,LNR_poly.predict(line_poly),label='Linear Regressor')
    plt.xlim(np.min(X)-0.5,np.max(X)+0.5)
    plt.ylim(np.min(y)-0.5,np.max(y)+0.5)
    plt.plot(X,y,'o',c='r')
    plt.legend(loc='lower right')
    #显示图形
    plt.show()
    

    总结 : 

      线性模型在高维数据集中有良好的性能,但是在低维数据集中却表现一般,因此我们需要用向数据集中添加交互式特征或多项式特征等方法来对数据集进行特征扩充,以便给数据集升维,从而提高线性模型的准确率.所以可以在一定程度上解决线性模型在低维数据出现欠拟合的问题,

    文章引自 ; 《深入浅出python机器学习》

  • 相关阅读:
    Monkey面试整理
    Monkey测试环境搭建
    软件测试之Monkey 初步了解(入门级)
    接口测试Post和Get区别(面试题)
    软件测试测试人员遇到的问题及解决方法(面试)
    接口测试用例的基本测试点
    兼容性测试主要测的浏览器
    【转】Web测试中定位bug方法
    使用Postman做接口测试
    head中的title显示在body中
  • 原文地址:https://www.cnblogs.com/weijiazheng/p/10958602.html
Copyright © 2011-2022 走看看