数据升维
原标题:数据升维
原文来自:博客园 原文链接:https://www.cnblogs.com/weijiazheng/p/10958602.html
我们在实际应用中会遇到数据集特征不足的情况,要解决这个问题,就需要对数据集的特征进行扩充,
一般使用两种方法:
交互式特征(Interaction Features)
多项式特征(Ploynomial Features)
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 | #导入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)) |
交互式特征是指在原始数据特征中添加交互项,使特征数量增加.
1 2 3 4 5 6 7 8 | ############################# 向数据集添加交互式特征 ####################################### #手工生成两个数组 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]
1 2 3 | #将原始数据和装箱后的数据进行堆叠 X_stack = np.hstack([X,X_in_bin]) print (X_stack.shape) |
(50, 11)
1 2 3 4 5 6 7 8 9 10 11 12 13 | #将数据进行堆叠 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() |
1 2 3 4 5 | #使用新的堆叠方式处理数据 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. ]
1 2 3 4 5 6 7 8 9 10 11 | #重新训练模型 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() |
1 2 3 4 5 6 7 8 | ############################# 向数据集添加多项式特征 ####################################### #导入多项式特征工具 from sklearn.preprocessing import PolynomialFeatures #向数据集添加多项式特征 poly = PolynomialFeatures(degree = 20 ,include_bias = False ) X_poly = poly.fit_transform(X) #打印结果 print (X_poly.shape) |
(50, 20)
1 2 3 4 5 6 | #打印结果 print ( '原始数据集中的第一个样本特征:n{}' . format (X[ 0 ])) print ( 'n处理后的数据集中第一个样本特征:n{}' . format (X_poly[ 0 ])) #打印结果 print ( 'PolynomialFeatures对原始数据的处理:n{}' . 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']
1 2 3 4 5 6 7 8 9 10 11 12 13 | #导入线性回归 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机器学习》
免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。
合作及投稿邮箱:E-mail:editor@tusaishared.com
上一篇:K-近邻算法(KNN)
下一篇:数据表达
热门资源
Python 爬虫(二)...
所谓爬虫就是模拟客户端发送网络请求,获取网络响...
TensorFlow从1到2...
原文第四篇中,我们介绍了官方的入门案例MNIST,功...
TensorFlow从1到2...
“回归”这个词,既是Regression算法的名称,也代表...
机器学习中的熵、...
熵 (entropy) 这一词最初来源于热力学。1948年,克...
TensorFlow2.0(10...
前面的博客中我们说过,在加载数据和预处理数据时...
智能在线
400-630-6780
聆听.建议反馈
E-mail: support@tusaishared.com