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)
机械学系之---数据预处理
最新推荐文章于 2024-08-29 14:47:26 发布