数据预处理
调整数据尺寸
- 让所有的属性按照相同的尺度来度量数据;
- 梯度下降算法
- 神经网络
- SVM
- 回归算法
- K 近邻算法
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
filename = 'pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
scaler = MinMaxScaler(feature_range=(0, 1))
rescaledX = scaler.fit_transform(X)
np.set_printoptions(precision=3)
print(rescaledX)
正态化数据
import numpy as np
import pandas as pd
from sklearn.preprocessing import Normalizer
filename = 'pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
scaler = Normalizer().fit(X)
rescaledX = scaler.transform(X)
np.set_printoptions(precision=3)
print(rescaledX)
标准化数据
- 把每一行数据的距离处理成 1;
- 适合处理稀疏数据(有很多为 0 的数据);
- 对使用权重输入的神经网络和使用距离输入的 K 近邻算法的准确度的提升有显著作用。
import numpy as np
import pandas as pd
from sklearn.preprocessing import Normalizer
filename = 'pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
scaler = Normalizer().fit(X)
rescaledX = scaler.transform(X)
np.set_printoptions(precision=3)
print(rescaledX)
二值化数据
- 大于阈值设置为 1 ,小于阈值的设置为 0;
- 在生产明确值或特征工程增加属性的时候使用;
import numpy as np
import pandas as pd
from sklearn.preprocessing import Binarizer
filename = 'pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
transform = Binarizer(threshold=0.0).fit(X)
newX = transform.transform(X)
np.set_printoptions(precision=3)
print(newX)