数学建模老哥-python基础和机器学习(五)数据预处理

该笔记为个人学习笔记,看的课程是B站-数学建模老哥:5 Python数据预处理_哔哩哔哩_bilibili

 数据集的来源在 数学建模老哥-python基础和机器学习(四)数据导入+数据理解+数据可视化-CSDN博客

目录

2.3.2数据预处理

2.3.2.1数据缩放(等比例缩放)

2.3.2.2正态化数据

2.3.2.3标准化数据(零-均值)

2.3.2.4二值数据

2.3.2.5总代码


2.3.2数据预处理

 

2.3.2.1数据缩放(等比例缩放)

使用算法:K邻近算法

#归一化缩放数据
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
# 将DataFrame对象转换为numpy数组
array = data.values  # DataFrame的.values属性会返回一个numpy数组,包含了DataFrame中的所有数据
X = array[:, 0:8]
Y = array[:, 8]
transformer = MinMaxScaler(feature_range=(0, 1))  # 归一化
newX = transformer.fit_transform(X)
set_printoptions(precision=3)  # 调整数据尺度 小数点后保留三位
print(newX)
2.3.2.2正态化数据

使用算法:线性回归、逻辑回归、判别分析

#正态化数据
from sklearn.preprocessing import StandardScaler
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
# 将DataFrame对象转换为numpy数组
array = data.values  # DataFrame的.values属性会返回一个numpy数组,包含了DataFrame中的所有数据
X = array[:, 0:8]
Y = array[:, 8]
transformer = StandardScaler().fit(X)  # 使用fit方法计算X的均值和标准差,这些统计量将用于后续的转换
# 使用transform方法将X转换为标准化后的数据
# 标准化意味着每个特征将减去其均值并除以其标准差,使得转换后的数据具有均值为0和标准差为1的特性
newX = transformer.transform(X)
# 设置numpy数组的打印选项,这里设置打印的精度为3位小数
set_printoptions(precision=3)
print(newX)
2.3.2.3标准化数据(零-均值)

使用算法:K邻近算法、神经网络

#标准化数据
from sklearn.preprocessing import Normalizer
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
transformer = Normalizer().fit(X)
newX = transformer.transform(X)
set_printoptions(precision=3)
print(newX)
2.3.2.4二值数据

使用算法:分类算法

#二值数据
from sklearn.preprocessing import Binarizer
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
transformer = Binarizer(threshold=0.0).fit(X)
newX = transformer.transform(X)
set_printoptions(precision=3)
print(newX)
2.3.2.5总代码
from sklearn.preprocessing import MinMaxScaler  # 导入MinMaxScaler类,用于数据的归一化处理
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
from pandas import read_csv

#归一化缩放数据
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
# 将DataFrame对象转换为numpy数组
array = data.values  # DataFrame的.values属性会返回一个numpy数组,包含了DataFrame中的所有数据
X = array[:, 0:8]
Y = array[:, 8]
transformer = MinMaxScaler(feature_range=(0, 1))  # 归一化
newX = transformer.fit_transform(X)
set_printoptions(precision=3)  # 调整数据尺度 小数点后保留三位
print(newX)

#正态化数据
from sklearn.preprocessing import StandardScaler
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
# 将DataFrame对象转换为numpy数组
array = data.values  # DataFrame的.values属性会返回一个numpy数组,包含了DataFrame中的所有数据
X = array[:, 0:8]
Y = array[:, 8]
transformer = StandardScaler().fit(X)  # 使用fit方法计算X的均值和标准差,这些统计量将用于后续的转换
# 使用transform方法将X转换为标准化后的数据
# 标准化意味着每个特征将减去其均值并除以其标准差,使得转换后的数据具有均值为0和标准差为1的特性
newX = transformer.transform(X)
# 设置numpy数组的打印选项,这里设置打印的精度为3位小数
set_printoptions(precision=3)
print(newX)

#标准化数据
from sklearn.preprocessing import Normalizer
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
transformer = Normalizer().fit(X)
newX = transformer.transform(X)
set_printoptions(precision=3)
print(newX)

#二值数据
from sklearn.preprocessing import Binarizer
from numpy import set_printoptions  # 导入set_printoptions函数,用于设置numpy数组的打印格式
filename = 'pima.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
transformer = Binarizer(threshold=0.0).fit(X)
newX = transformer.transform(X)
set_printoptions(precision=3)
print(newX)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值