import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
一般泛指把数据压缩到0到1之间的浮点数。
data = np.random.randint(10,100,size=(100,2))
target = [0]*50 + [1]*50
plt.scatter(data[:,0],data[:,1],c=target)
class MinMax(object):
#构造函数
def __init__(self,feature_range=(0,1)):
self.mi = feature_range[0]
self.mx = feature_range[1]
#提取每一列的max和min
def fit(self,X):
self.train = np.array(X)
self.data_max_ = self.train.max(axis=0)
self.data_min_ = self.train.min(axis=0)
#转换
def transform(self,X):
data = np.array(X)
#0-1
x = np.divide(np.subtract(data,self.data_min_),np.subtract(self.data_max_,self.data_min_))
#修改范围的 x*(mx-mi) + mi
return np.add(np.multiply(x,np.subtract(self.mx,self.mi)),self.mi)
def fit_transform(self,X):
self.fit(X)
return self.transform(X)
#把归一化的数据还原 不能 浮点运算
def inverse_transform(self,X):
pass
mm = MinMax().fit_transform(data)
plt.scatter(data[:,0],data[:,1],c=target)
plt.scatter(mm[:,0],mm[:,1],c=target)
把每一列的数据尽可能的接近标准的高斯分布(均值为0,标准差为1)
class Stand(object):
#提取每一列的mean和std
def fit(self,X):
self.train = np.array(X)
self.data_mean_ = self.train.mean(axis=0)
self.data_std_ = self.train.std(axis=0)
#转换
def transform(self,X):
data = np.array(X)
# item-mean / std
return np.divide(np.subtract(data,self.data_mean_),self.data_std_)
def fit_transform(self,X):
self.fit(X)
return self.transform(X)
def inverse_transform(self,X):
pass
#去中心化
avg = data.mean(axis=0)
new_data = data-avg
plt.scatter(new_data[:,0],new_data[:,1],c=target)
std_ = Stand().fit_transform(data)
plt.scatter(std_[:,0],std_[:,1],c=target)