【数据分析师-数据分析项目案例】大数据预处理

大数据预处理

作者:Be_melting

0 前言

在进行数据分析项目案例之前,需要了解数据的情况,有时候拿到的数据并不是想象中的完美数据,那么就需要进行预处理后才能使用。为了系统的缕清预处理的一般的步骤,这里进行详细的梳理,采用sklearn工具包和手写代码验证的方式进行。

1 数据标准化

1.1 标准化定义

标准化的定义:又被称为均值移除(mean removal),对不同样本的同一特征值进行处理,最终均值为0,标准差为1,采用此种方式我们只需要使用如下公式即可。

  • 数据均值
  • 数据标准差

1.2 为什么要进行数据标准化?

在机器学习中,许多评估模型的算法和方法都是基于距离(残差)处理的,即在进行随机数据采样时,应避免不同距离对模型的影响。因此,有必要进行标准化处理,以确保随机选择的数据等距。例如,说人类语言,借用一幅图像,就是将不同的椭圆处理成正圆,这样圆圈上的任何值都等于到原点的距离。

[En]

In machine learning, many algorithms and methods for evaluating models are based on distance (residual) processing, that is, or, therefore, the impact of different distances on the model should be avoided when random data sampling is carried out. Therefore, it is necessary to carry out standardized processing to ensure that the randomly selected data are equidistant. To speak human language, to borrow an image, for example, is to treat different ellipses into positive circles, so that any value on the circle is equal to the distance from the origin.

【数据分析师-数据分析项目案例】大数据预处理

1.3 实例操作

import numpy as npfrom sklearn import preprocessingdata = np.array([[3, -1.5,  2, -5.4], [0,  4,  -0.3, 2.1], [1,  3.3, -1.9, -4.3]])

输出结果为:(数据随机设置,便于以后人工验证)

[En]

The output result is: (the data is randomly set to facilitate manual verification later)

【数据分析师-数据分析项目案例】大数据预处理

按照标准化的公式,要先计算均值和方差,那么有个问题就来了:计算的数据是横向(一行数据,axis = 1),还是纵向(一列数据,axis = 0)的呢?每一列(纵向)都代表着一个字段的数据,而每一行却包含了所有字段中的一个数据,而在计算均值和方差时候应该选取的是某个字段进行,也就是需要计算纵向的数据

print('均值: ',data.mean(axis = 0))print('标准差: ', data.std(axis = 0))

输出结果为:

【数据分析师-数据分析项目案例】大数据预处理

然后您可以进行手动验证。

[En]

Then you can do manual verification.

import mathmath.sqrt(((3-1.33333333)**2+(0-1.33333333)**2+(1-1.33333333)**2)/3)

输出结果为:1.247219128924647(这里只进行第一列的标准差的验证,其余列也是一样的,均值可以口算)

【数据分析师-数据分析项目案例】大数据预处理

最终标准化结果为:(与第一列第一行数据一起显示)

[En]

The final standardized result is: (displayed with the data of the first row of the first column)

【数据分析师-数据分析项目案例】大数据预处理

以上的过程虽然原理很简单,操作起来也不是很难,但是要是每次进行数据处理之前都得一个数据一个数据的挨个处理,就显着很浪费时间,因此就可以使用​ ​preprocessing​​函数进行处理

核心代码:​ ​preprocessing.scale()​

data_standarized = preprocessing.scale(data)print('均值: ',data_standarized.mean(axis = 0))print('标准差: ', data_standarized.std(axis = 0))

输出的结果为:(一行代码搞定标准化。由于python计算精度的问题,均值这里实际上是为0的,10的负17次方,相当于很微小的数值了)

【数据分析师-数据分析项目案例】大数据预处理

2 数据缩放化

2.1 0-1缩放

对于不同样本的相同特征值,减去其最小值,除以(最大-最小值),最终的原始最大值为1,原始最小值为0,这样在数据分析中可以有效地消除不同单元大小对最终结构的权重影响。(例如,如果股价在5元至7元之间波动,但日成交量在100万左右,如果不按比例衡量,成交量的数据权重将比股价高出数万倍,导致最终预测数据异常)

[En]

For the same eigenvalue of different samples, subtract its minimum value, divided by (maximum-minimum value), the final original maximum value is 1, and the original minimum value is 0, so that the weight influence of different unit sizes on the final structure can be effectively eliminated in data analysis. (for example, if the stock price fluctuates between 5 and 7 yuan, but the daily trading volume is about 1 million, without scaling, the data weight of the trading volume will be tens of thousands of times higher than the stock price, resulting in abnormal final forecast data)

2.2 实例操作

如果直接使用numpy进行操作,依据计算的公式求解如下

【数据分析师-数据分析项目案例】大数据预处理

除了手动计算外,也可以直接调用sklearn中的模块,也是在​ ​preprocessing​​​函数中,使用​ ​MinMaxScaler​​方法

核心代码:​ ​preprocessing.MinMaxScaler()​

data_scaler = preprocessing.MinMaxScaler(feature_range = (0,2))data_scaled = data_scaler.fit_transform(data)

输出结果为:(​ ​MinMaxScaler​​括号里可以进行参数的调整,根据自己的需求进行设置,比如将区间缩放至0-2,得到的结果就是刚刚利用numpy求解的2倍)

【数据分析师-数据分析项目案例】大数据预处理

3 数据归一化

3.1 数据归一化定义

如果要调整特征向量中的值时,可以使用数据归一化,以便可以使用通用比例尺对其进行测量。机器学习中最常用的规范化形式之一是调整特征向量的值,使其总和为1(方便查找重要特征)。常见的处理方式有如下几种:L1模式, L2模式。

L1模式:理解就是加了绝对值的,也是常见的一种模式,还有其他很多的名字称呼,比如熟悉的曼哈顿距离,最小最对误差等。使用L1模式可以度量两个向量间的差异,如绝对误差和(Sum of Absolute Difference)

L2模式:理解就是加了平方的,是最常见最常用的一种模式,也有其他的称呼,比如使用最多的欧氏距离就是L2模式。通常用来做优化目标函数的正则化项,放置模型为了迎合训练集而过于复杂造成的过拟合情况,从而提高模型的泛化能力(可以参考一下逻辑回归进行信用卡欺诈预测​案例中,如果惩罚项参数选择L2,结果会如何)

3.2 实例操作

核心代码:​ ​preprocessing.normalize()​

L1 模式

data_normalized = preprocessing.normalize(data,'l1',axis = 0)print(data_normalized)data_norm_abs = np.abs(data_normalized)print(data_norm_abs.sum(axis=0))

输出结果为:(您可以看到每列转换后的数据之和为1)

[En]

The result of the output is: (you can see that the sum of the converted data of each column is 1)

【数据分析师-数据分析项目案例】大数据预处理

L2模式

data_normalized = preprocessing.normalize(data,'l2',axis = 0)print(data_normalized)#0.31622777*3 = 0.9486833...(data_normalized*data_normalized).sum(axis = 0)

输出结果为:(每列的变换数据也按公式计算为1,比例保持不变。)

[En]

The output result is: (the transformed data of each column is also calculated to be 1 according to the formula, and the proportion remains the same.)

【数据分析师-数据分析项目案例】大数据预处理

4 二值化

4.1 二值化定义

当我们要将数字特征向量转换为布尔向量时,我们可以使用二值化(即根据我们自己指定的阈值,如果超过该阈值,则为1,如果小于该阈值,则为0)。在数字图像处理领域,图像二值化是将彩色或灰度图像转换为二值图像(即只有两种颜色(通常是黑色和白色)的图像)的过程。

[En]

When we want to convert a digital feature vector into a Boolean vector, we can use binarization (that is, according to the threshold specified by ourselves, if the threshold is exceeded, it is 1, and if it is less than this threshold, it is 0). In the field of digital image processing, image binarization is the process of converting a color or grayscale image into a binary image (that is, an image with only two colors (usually black and white).

此技术用于识别对象、形状,尤其是字符。通过二值化,可以将感兴趣的对象从找到的对象的背景区域中分离出来,例如常见的黑白肖像。

[En]

This technique is used to identify objects, shapes, especially characters. Through binarization, the object of interest can be separated from the background area of the found object, such as common black-and-white portraits.

【数据分析师-数据分析项目案例】大数据预处理

4.2 实际操作

核心代码:​ ​preprocessing.Binarizer()​

data_binarized = preprocessing.Binarizer(threshold = 1.4).transform(data)print(data_binarized)print(data)

输出结果为:(可以设置阈值,大于此值的数据为1,不大于0的为0,最终数据为0和1)

[En]

The result of the output is: (you can set a threshold, the data greater than this value is 1, those not greater than 0 are 0, and the final data are 0 and 1)

【数据分析师-数据分析项目案例】大数据预处理

5 独热编码

5.1 独热编码定义

在许多计算模型中,只能进行数值处理,但数据中经常会遇到各种分类数据,因此有必要对分类数据进行数值编码。单热编码将分类数据转换为0-1编码,单热编码器可以将一键编码作为增强特征向量的工具。基于该方案对特征向量中的每个特征进行编码。这有助于我们提高空间效率。编码过程如下

[En]

In many computing models, numerical processing can only be carried out, but all kinds of classified data are often encountered in the data, so it is necessary to encode the classified data numerically. Mono-thermal coding converts classified data into 0-1 coding, and the single-hot encoder can regard * one-key coding * as a tool to enhance the feature vector. Each feature in the feature vector is encoded based on this scheme. This helps us to improve space efficiency. The coding process is as follows

【数据分析师-数据分析项目案例】大数据预处理

关于 独热的理解:经过编码后,一行数据中,只有一个数据是1,其余的都是0,对比红路灯,在一个时刻,该指示灯只能显示一种颜色

【数据分析师-数据分析项目案例】大数据预处理

5.2 实例操作

核心代码:​ ​preprocessing.OneHotEncoder()​

给出举例数据

data = np.array([[1, 1, 2], [0, 2, 3], [1, 0, 1], [0, 1, 0]])print(data)

输出的结果为:(数据共四行三列,假设这三列的名称分别为ABC,可以发现A中共有两个特征0和1,B中有三个特征,C中有四个特征)

【数据分析师-数据分析项目案例】大数据预处理

那么接下来进行独热编码,根据上面的设定,经过独热编码后的结果应该有9个特征,前两个对应A字段,中间三个对应B字段,最后四个对应C字段

encoder = preprocessing.OneHotEncoder()encoder.fit(data)encoder_vector = encoder.transform([[0,0,0]]).toarray()print(encoder_vector)encoder_vector = encoder.transform([[1,0,3]]).toarray()print(encoder_vector)

输出结果为:(这里以[[0,0,0]]举例,A中两个特征,0在前面第一个位置,B中三个特征,0在第一个位置,C中三个特征,0也在第一个位置。对比这种位置关系[[1,0,3]]独热编码后就不难理解了)

【数据分析师-数据分析项目案例】大数据预处理

如果这些数字很难理解,那么这里就是张三李思的介绍。

[En]

If the numbers are difficult to understand, then here is the introduction of Zhang San Li Si.

data = np.array([['张三', '张三', '李四'], ['王五', '李四', '赵六'], ['张三', '王五', '张三'], ['王五','张三', '王五']])print(data)encoder = preprocessing.OneHotEncoder()encoder.fit(data)encoder_vector = encoder.transform([['张三','张三','张三']]).toarray()print(encoder_vector)

输出结果为:(原理与上面相同,但使用中文文本数据进行显示,易于理解。)

[En]

The output result is: (here is the same principle as above, but the Chinese text data is used for display, which is easy to understand.)

【数据分析师-数据分析项目案例】大数据预处理

6、标签编码

6.1 标签编码定义

除了热编码,还可以对分类变量进行标记,即不仅可以标记0和1,还可以标记其他值。标签编码的常见名称是给数据加标签,并找到相应的数据。它也可以手动操作。

[En]

In addition to thermal coding, classification variables can be tagged, that is, not only 0 and 1, but also other values. The common title of tag coding is to “tag” the data and find the corresponding data. It can also be operated manually.

【数据分析师-数据分析项目案例】大数据预处理

6.2 实例操作

核心代码:​ ​preprocessing.LabelEncoder()​

input_classes = ['audi', 'ford', 'audi', 'toyota', 'ford', 'bmw']label_encoder = preprocessing.LabelEncoder()label_encoder.fit(input_classes)for i, item in enumerate(label_encoder.classes_):    print(item, "-->", i)

输出结果为:(回顾基础部分讲解​ ​for​​​循环时讲到的​ ​for-enumerate​​的配合使用,可以直接输出对应的标签信息和标签值)

【数据分析师-数据分析项目案例】大数据预处理

经过​ ​fit​​训练之后的标签信息,就可以对部分数据甚至全部的数据的标签信息甚至逆标签信息查询,比如指定原列表中的部分数据,就可以直接获得对应数据的编码

【数据分析师-数据分析项目案例】大数据预处理

同时,还可以反向查询标签信息,指定编码值,输出相应的标签信息。

[En]

At the same time, you can also reverse the tag information query, specify the encoded value, and output the corresponding tag information.

【数据分析师-数据分析项目案例】大数据预处理

7 缺失值处理

7.1 处理方式

最常见的数据情况是部分数据缺失,那么如何处理缺失值呢?有固定的配方吗?治疗方法如下:

[En]

The most common data situation is missing part of the data, so how to deal with missing values? Is there a fixed formula? The treatment is as follows:

  • 删除:缺失样本量 非常大,删除整个字段;如果缺失量较少,且 难以填充 则删除缺失样本
  • 填充:缺失量 小于10%,根据缺失变量的数据分布采取 均值(正态分布)中位数(偏态分布) 进行填充
  • 模拟或预测缺失样本:根据样本的数据分布生成随机值填充(内插);使用与缺失相关性高的特征建立模型来预测缺失值
    [En]

    simulate or predict missing samples: generate * random value filling (interpolation) * according to the data distribution of the sample; use features with high correlation to the missing to establish a model to predict the missing value*

在第二章已经详细介绍了使用pandas对缺失值进行处理,接下来介绍sklearn中如何对大数据样本进行缺失值处理

7.2 实例操作

采用sklearn模块中的​ ​SimpleImputer​​函数进行处理,使用的步骤共四步

  • (1)从模块中导入函数
  • (2)设定填充的对象和填充的方式
  • (3)选取数据
  • (4)处理数据

读取测试数据,其中​ ​Age​​​和​ ​Salary​​字段有缺失值

【数据分析师-数据分析项目案例】大数据预处理

按列或坐标提取相应的字段

[En]

Extract the corresponding fields, either by column or coordinate

#采用坐标的方式进行提取X = dataset.iloc[:, :-1].valuesY = dataset.iloc[:, -1].values

输出结果如下:(一般会把所有的特征字段提取后赋值为​ ​X​​​,标签字段提取后赋值为​ ​Y​​)

【数据分析师-数据分析项目案例】大数据预处理

严格按照sklearn处理缺失值的步骤进行,代码如下

#导入处理模块from sklearn.impute import SimpleImputer  #设定要处理的对象和处理规则imputer=SimpleImputer(missing_values=np.nan,strategy='mean')#选取数据fit训练imputer= imputer.fit(X[:,1:3])#处理数据X[:,1:3]=imputer.transform(X[:,1:3])

输出结果如下:

【数据分析师-数据分析项目案例】大数据预处理

最后,关于可以设置的规则,通过调用文档,可以发现可以使用的类型如下:(Mean、Medium、MODE和Constant)

[En]

Finally, with regard to the rules that can be set, by calling the documentation, you can find that the types that can be used are as follows: (mean, median, mode and constant)

【数据分析师-数据分析项目案例】大数据预处理

至此,大数据的前置介绍完成,✿✿Flowers(°▽°)✿

[En]

At this point, the introduction of the pretreatment of big data is finished, and the ✿✿ flowers (°▽ °) ✿

Original: https://blog.51cto.com/u_15713987/5462745
Author: 百木从森
Title: 【数据分析师-数据分析项目案例】大数据预处理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508319/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球