机械学系之---数据预处理

from pandas import read_csv
import numpy as np
import matplotlib.pyplot as plt
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
# 直方图
data.hist()
plt.show()
# 密度图
data.plot(kind='density', subplots=True, layout=(3, 3), sharex=False)
plt.show()
# 箱线图
data.plot(kind='box', subplots=True, layout=(3, 3), sharex=False)
plt.show()
# 相关矩阵图
correlations = data.corr()
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(correlations, vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0, 9, 1,)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels(names)
ax.set_yticklabels(names)
plt.show()
# 散点矩阵图
from pandas import read_csv
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
scatter_matrix(data)
plt.show()

# 数据缩放
from numpy import set_printoptions
from sklearn.preprocessing import MinMaxScaler
from pandas import read_csv
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', '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 = MinMaxScaler(feature_range=(0, 1))
newX = transformer.fit_transform(X)
set_printoptions(precision=3)
print(newX)

# 正态化
from numpy import set_printoptions
from sklearn.preprocessing import StandardScaler
from pandas import read_csv
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', '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 = StandardScaler().fit(X)
newX = transformer.transform(X)
set_printoptions(precision=3)
print(newX)

# 标准化数据
from numpy import set_printoptions
from sklearn.preprocessing import Normalizer
from pandas import read_csv
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', '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 numpy import set_printoptions
from sklearn.preprocessing import Binarizer
from pandas import read_csv
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preq', '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)



  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值