四种数据预处理方式的区别?

数据预处理可以提高模型的准确率

首先导入numy模块,和画图模块matplotlib
使用sklearn模块中的make_blobs手工生成一些数据集
本列数据集中的两个特征分别对应于x轴和y轴

示例代码以及不同的数据预处理结果如下:

import numpy as np
import matplotlib.pyplot as plt
#导入数据集生成工具
from sklearn.datasets import make_blobs
X,y=make_blobs(n_samples=40,centers=2,random_state=50,cluster_std=2)
#用散点图画图
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.cool)
#显示图像
plt.show()

在这里插入图片描述

print(’========================’)

#对数据进行预处理

#导入数据预处理模块,第一种方法
#导入StandardScaler

from sklearn.preprocessing import StandardScaler
#使用上述模块进行数据预处理
X_1=StandardScaler().fit_transform(X)

#打印出预处理后的图像
plt.scatter(X_1[:,0],X_1[:,1],c=y,cmap=plt.cm.cool)
plt.show()

在这里插入图片描述

print(’====================’)
#第二种方法:使用MinMaxScaler进行数据预处理
#daoru MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
#使用MinMaxScaler进行数据预处理
X_2=MinMaxScaler().fit_transform(X)
plt.scatter(X_2[:,0],X_2[:,1],c=y,cmap=plt.cm.cool)
plt.show()
在这里插入图片描述

print(’========================’)

#第三种方式,使用RobustScaler进行数据预处理

#导入RobustScaler
from sklearn.preprocessing import RobustScaler
#使用上述模块进行数据预处理
X_3=RobustScaler().fit_transform(X)
plt.scatter(X_3[:,0],X_3[:,1],c=y,cmap=plt.cm.cool)
plt.show()
在这里插入图片描述

print(’=================’)

#第四种方式,使用Normalizaer进行数据预处理
#导入Normalizaer
from sklearn.preprocessing import Normalizer
#使用上述模块进行数据预处理
X_4=Normalizer().fit_transform(X)
plt.scatter(X_4[:,0],X_4[:,1],c=y,cmap=plt.cm.cool)
plt.show()
在这里插入图片描述

print(’======================’)

猜你喜欢

转载自blog.csdn.net/qq_40258282/article/details/84834349