机器学习 —— 数据预处理
对于学习机器学习算法来说,肯定会涉及到数据的处理,因此一开始,对数据的预处理进行学习
对于数据的预处理,大概有如下几步:
步骤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 ...
随机推荐
- CodeForces 994B Knights of a Polygonal Table(STL、贪心)
http://codeforces.com/problemset/problem/994/B 题意: 给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的 ...
- systemd[1]: mariadb.service: Can't open PID file /data/mariadb/mysql/30-mariadb-1.pid (yet?) after start: No such file or directory
环境:Centos8 编译安装Mariadb-10.4.11,安装到make install都没有问题,添加服务启动脚本到/lib/systemd/system/,服务启动脚本名为mariadb.se ...
- Laravel 操作指令
php artisan migrate —path=database/migrations/v1 更新表数据 php artisan make:migration create_channels_ta ...
- 基础篇五:Nginx的目录和基础配置
Yum安装目录:yum的方式安装 rpm -ql nginx 下面开始安装目录详解
- elastic search记录
安装与启动 插件安装 中文分词器 https://github.com/medcl/elasticsearch-analysis-ik elastic api GET _search { " ...
- day43-线程概念
#1.进程:程序不能单独运行,要将程序加载到内存当中,系统为它分配资源才能运行,而这种执行的程序就是进程. #程序和进程的区别在于:程序是指令的集合,它是进程运行的静态描述文本:进程是程序的一次执行活 ...
- T-shirt
题目描述 JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-shi ...
- python语法基础-基础-控制语句
############### if条件控制语句 ############### # 以下实例 x 为 0-99 取一个数,y 为 0-199 取一个数,如果 x>y 则输出 x,如 ...
- elasticsearch5.4安装
1.从官网下载ES 安装包: elasticsearch-.tar.gz 2.解压到要安装的目录 注意:一定要切换用户,不能用root用户解压,不能用root用户启动 tar -zxvf elasti ...
- 数位dp——BZOJ1026 Windy数
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MB Description windy定义了一种windy数.不含前导零且相邻 ...