个性化阅读
专注于IT技术分析

Python中机器学习的数据预处理

点击下载

•预处理是指在将数据馈送到算法之前应用于我们的数据的转换。

•数据预处理是一种用于将原始数据转换为干净数据集的技术。换句话说, 无论何时从不同来源收集数据, 数据都以原始格式收集, 这对于分析是不可行的。

Python中机器学习的数据预处理1

需要数据预处理

•为了从Machine Learning项目中的应用模型中获得更好的结果, 数据格式必须采用适当的方式。某些指定的机器学习模型需要使用指定格式的信息, 例如, 随机森林算法不支持空值, 因此要执行随机森林算法, 必须从原始原始数据集中管理空值。

•另一个方面是, 数据集的格式应确保在一个数据集中执行多个机器学习和深度学习算法, 并从中选择最好的算法。

本文包含3种用于机器学习的不同数据预处理技术。

每种技术都使用Pima印度糖尿病数据集。这是一个二进制分类问题, 其中所有属性都是数字并且具有不同的比例。这是可以从预处理中受益的数据集的一个很好的例子。你可以在UCI机器学习存储库网页上找到此数据集。请注意, 该程序可能无法在srcmini IDE上运行, 但可以在你本地的python解释器上轻松运行, 前提是你已安装了所需的库。

1.重新缩放数据

•当我们的数据由具有不同比例的属性组成时, 许多机器学习算法都可以从将属性重新缩放为所有具有相同比例的方法中受益。

•这对于在机器学习算法(例如梯度下降)的核心中使用的优化算法很有用。

•对于加权输入(例如回归和神经网络)的算法以及使用距离度量(例如K最近邻)的算法也很有用。

•我们可以使用scikit-learn通过MinMaxScaler类。

# Python code to Rescale data (between 0 and 1)
import pandas
import scipy
import numpy
from sklearn.preprocessing import MinMaxScaler
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
names = [ 'preg' , 'plas' , 'pres' , 'skin' , 'test' , 'mass' , 'pedi' , 'age' , 'class' ]
dataframe = pandas.read_csv(url, names = names)
array = dataframe.values
  
# separate array into input and output components
X = array[:, 0 : 8 ]
Y = array[:, 8 ]
scaler = MinMaxScaler(feature_range = ( 0 , 1 ))
rescaledX = scaler.fit_transform(X)
  
# summarize transformed data
numpy.set_printoptions(precision = 3 )
print (rescaledX[ 0 : 5 , :])

重新缩放后, 请注意所有值都在0到1之间。

Output
[[ 0.353  0.744  0.59   0.354  0.0    0.501  0.234  0.483]
 [ 0.059  0.427  0.541  0.293  0.0    0.396  0.117  0.167]
 [ 0.471  0.92   0.525  0.     0.0    0.347  0.254  0.183]
 [ 0.059  0.447  0.541  0.232  0.111  0.419  0.038  0.0  ]
 [ 0.0    0.688  0.328  0.354  0.199  0.642  0.944  0.2  ]]

2.二进制化数据(二进制)

•我们可以使用二进制阈值转换数据。所有高于阈值的值都标记为1, 所有等于或低于阈值的值都标记为0。

•这称为二进制化数据或阈值数据。当你有可能要做出明晰的值的概率时, 此功能很有用。当进行要素工程并且你想添加表示有意义的新要素时, 该功能也很有用。

•我们可以使用scikit-learn在Python中创建新的二进制属性,二值化器类。

# Python code for binarization
from sklearn.preprocessing import Binarizer
import pandas
import numpy
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
names = [ 'preg' , 'plas' , 'pres' , 'skin' , 'test' , 'mass' , 'pedi' , 'age' , 'class' ]
dataframe = pandas.read_csv(url, names = names)
array = dataframe.values
  
# separate array into input and output components
X = array[:, 0 : 8 ]
Y = array[:, 8 ]
binarizer = Binarizer(threshold = 0.0 ).fit(X)
binaryX = binarizer.transform(X)
  
# summarize transformed data
numpy.set_printoptions(precision = 3 )
print (binaryX[ 0 : 5 , :])

我们可以看到所有等于或小于0的值都标记为0, 所有大于0的值都标记为1。

Output
[[ 1.  1.  1.  1.  0.  1.  1.  1.]
 [ 1.  1.  1.  1.  0.  1.  1.  1.]
 [ 1.  1.  1.  0.  0.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.]
 [ 0.  1.  1.  1.  1.  1.  1.  1.]]

3.标准化数据

•标准化是一种有用的技术, 可以将具有高斯分布, 均值和标准差不同的属性转换为平均值为0和标准差为1的标准高斯分布。

•我们可以使用scikit-learn和标准缩放器类。

# Python code to Standardize data (0 mean, 1 stdev)
from sklearn.preprocessing import StandardScaler
import pandas
import numpy
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
names = [ 'preg' , 'plas' , 'pres' , 'skin' , 'test' , 'mass' , 'pedi' , 'age' , 'class' ]
dataframe = pandas.read_csv(url, names = names)
array = dataframe.values
  
# separate array into input and output components
X = array[:, 0 : 8 ]
Y = array[:, 8 ]
scaler = StandardScaler().fit(X)
rescaledX = scaler.transform(X)
  
# summarize transformed data
numpy.set_printoptions(precision = 3 )
print (rescaledX[ 0 : 5 , :])

现在, 每个属性的值的平均值为0, 标准差为1。

Output
[[ 0.64   0.848  0.15   0.907 -0.693  0.204  0.468  1.426]
 [-0.845 -1.123 -0.161  0.531 -0.693 -0.684 -0.365 -0.191]
 [ 1.234  1.944 -0.264 -1.288 -0.693 -1.103  0.604 -0.106]
 [-0.845 -0.998 -0.161  0.155  0.123 -0.494 -0.921 -1.042]
 [-1.142  0.504 -1.505  0.907  0.766  1.41   5.485 -0.02 ]]

参考文献:

Practical Guide on Data Preprocessing in Python using Scikit Learn

https://www.xenonstack.com/blog/data-preprocessing-data-wrangling-in-machine-learning-deep-learning

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

赞(0)
未经允许不得转载:srcmini » Python中机器学习的数据预处理

评论 抢沙发

评论前必须登录!