机器学习 —— 数据预处理
对于学习机器学习算法来说,肯定会涉及到数据的处理,因此一开始,对数据的预处理进行学习
对于数据的预处理,大概有如下几步:
步骤1 —— 导入所需库
导入处理数据所需要的python库,有如下两个库是非常重要的两个库,每次必导入
- numpy
该库包含数学函数功能的库
- pandas
该库用于导入和管理数据集
步骤2 —— 导入数据集
数据集通常以 .csv 格式进行保存,csv文件是以普通文本的形式存储列表数据,文件中每一行是一个数据记录。
对于csv文件,使用pandas模块中的 read_cvs 方法进行读取。
步骤3 —— 处理丢失数据
由于实际获取到的数据很少是同一类型的,由于各种原因会导致数据丢失,因此需要处理,以便不会降低机器学习模型的性能。
我们可以使用整列数据中的均值或者中值来替换丢失的数据, python中使用sklearn.preprocessing中的imputer类来完成该任务。
步骤4 —— 编码分类数据
分类数据通常包括的分类类型是标签值,例如是”Yes”或”No”, 而不是数值,例如0或1。
由于标签值是不能用在机器学习模型的数学等式中的,因此,需要把标签值转换为数值。
python中使用sklearn.preprocessing库中的LabelEncoder类可以完成该任务。
步骤5 —— 划分数据为训练集和测试集
机器学习中,需要把数据集划分为两部分,用于训练机器学习模式的称之为 训练集, 用于测试训练出来的模型性能的称之为 测试集。通常按80/20比例把需数据集划分为训练集和测试集。
python中使用sklearn.crossvalidation库中的train_test_split()方法进行划分。
步骤6 —— 特征缩放
大部分的机器学习算法在计算过程中使用两个数据点之间的欧几里德距离。如果数据集中的特征值的变化范围比较大的话, 大的数值比小的数值在计算距离上会导致不同的权重。因此需要进行特征标准化或Z-score正规化。
python中可以使用sklearn.preprocessing库中的StandardScalar
代码实现
# -*- coding: utf-8 -*-
"""
Author: wxer
"""
# step 1 - import the libraries
import numpy as np
import pandas as pd
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
# step 2 - import dataset
dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[: 大专栏 机器学习 —— 数据预处理, 3].values
# step 3 - handing the missing data
imputer = Imputer(missing_values='NaN', strategy='mean', axis=0)
imputer = imputer.fit(X[:, 1: 3])
X[:, 1: 3] = imputer.transform(X[:, 1: 3])
# step 4 - encoding categorical data
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features=[0])
X = onehotencoder.fit_transform(X).toarray()
labelencoder_Y = LabelEncoder()
Y = labelencoder_Y.fit_transform(Y)
# step 5 - splitting the datasets into training sets and test sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
# step 6 - feature scaling
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)
参考
机器学习 —— 数据预处理的更多相关文章
- [机器学习]-[数据预处理]-中心化 缩放 KNN(二)
上次我们使用精度评估得到的成绩是 61%,成绩并不理想,再使 recall 和 f1 看下成绩如何? 首先我们先了解一下 召回率和 f1. 真实结果 预测结果 预测结果 正例 反例 正例 TP 真 ...
- python大战机器学习——数据预处理
数据预处理的常用流程: 1)去除唯一属性 2)处理缺失值 3)属性编码 4)数据标准化.正则化 5)特征选择 6)主成分分析 1.去除唯一属性 如id属性,是唯一属性,直接去除就好 2.处理缺失值 ( ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取SelectPercentile模型
from sklearn.feature_selection import SelectPercentile,f_classif #数据预处理过滤式特征选取SelectPercentile模型 def ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型
from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...
- 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型
from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...
- 吴裕雄 python 机器学习——数据预处理标准化MaxAbsScaler模型
from sklearn.preprocessing import MaxAbsScaler #数据预处理标准化MaxAbsScaler模型 def test_MaxAbsScaler(): X=[[ ...
- 吴裕雄 python 机器学习——数据预处理标准化StandardScaler模型
from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...
- 吴裕雄 python 机器学习——数据预处理标准化MinMaxScaler模型
from sklearn.preprocessing import MinMaxScaler #数据预处理标准化MinMaxScaler模型 def test_MinMaxScaler(): X=[[ ...
- 吴裕雄 python 机器学习——数据预处理二元化OneHotEncoder模型
from sklearn.preprocessing import OneHotEncoder #数据预处理二元化OneHotEncoder模型 def test_OneHotEncoder(): X ...
随机推荐
- arg min,arg max, e.g ,i.e
数学中常见的arg min,arg max 是什么意思 arg 是变元(即自变量argument)的英文缩写 arg min 就是使后面这个式子到达最小值时的变量的取值 arg max 就是使后面这个 ...
- js变量的相关要点
如果变量在函数内没有声明(没有使用 var 关键字),该变量为全局变量. JavaScript 变量生命周期在它声明时初始化. 局部变量在函数执行完毕后销毁. 全局变量在页面关闭后销毁.
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 再来看看Java的新特性——Stream流
半年前开始试着使用Java的新特性,给我印象最深的就是Stream流和Optional.其中Stream提高了看法效率,让代码看起来十分清爽. 为什么要使用流? 摘要中已经说明了,为了提高开发效率.流 ...
- python学习笔记(29)-操作excel
操作excel #存到excel里面,python去操作excel文件 #只支持这种后缀,xlsx ,openpyxl只支持这种格式 # from openpyxl import load_workb ...
- 802.11X
LSW1; interface Vlanif100 ip address 192.168.121.2 255.255.255.0连接云的地址 interface GigabitEthernet0/0/ ...
- Codeforces Round #576 (Div. 2) D. Welfare State
http://codeforces.com/contest/1199/problem/D Examples input1 output1 input2 output2 Note In the firs ...
- 腾讯云 Serverless 首发 1ms 计费粒度,立省 70% 费用
云函数 SCF 采用按需付费的方式,并首次发布 1ms 计费粒度,真正实现按使用多少计算能力来计费. 云函数(Serverless Cloud Function,SCF)是腾讯云为企业和开发者们提供的 ...
- GSON转换成Long型变为科学计数法及时间格式转换异常的解决方案
直接上工具类了,简单实用 public class GsonUtils { private static Gson gson = null; static { if (gson == null) { ...
- left join on注意点
右侧表的条件参数需要放在on后面 where 后面进放置左表的条件参数 比如消息表和用户消息表 消息表里存在类型为<系统消息>的消息是发送给全部用户 我们发送给系统消息时,不直接插入用户消 ...