from sklearn import preprocessing
import numpy as np

X_train = np.array([[ 1., -1.,  2.],
                    [ 2.,  0.,  0.],
                    [ 0.,  1., -1.]])
X_scaled = preprocessing.scale(X_train)

X_scaled

X_scaled.mean(axis=0), X_scaled.std(axis=0)

"""
Explanation: Standardization, or mean removal and variance scaling

scale function
End of explanation
"""


scaler = preprocessing.StandardScaler().fit(X_train)
scaler

scaler.mean_, scaler.scale_, scaler.transform(X_train)

"""
Explanation: StandardScaler
End of explanation
"""


X_test = [[-1., 1., 0.]]
scaler.transform(X_test)

"""
Explanation: Once a scaler instance has been fitted, it can perform the same transformation on new data:
End of explanation
"""


X_train = np.array([[ 1., -1.,  2.],
                    [ 2.,  0.,  0.],
                    [ 0.,  1., -1.]])

min_max_scaler = preprocessing.MinMaxScaler()
X_train_minmax = min_max_scaler.fit_transform(X_train)
X_train_minmax

X_test = np.array([[ -3., -1.,  4.]])
X_test_minmax = min_max_scaler.transform(X_test)
X_test_minmax

min_max_scaler.scale_, min_max_scaler.min_   

"""
Explanation: Scaling features to a range

MinMaxScaler
End of explanation
"""


X_train = np.array([[ 1., -1.,  2.],
                    [ 2.,  0.,  0.],
                    [ 0.,  1., -1.]])

max_abs_scaler = preprocessing.MaxAbsScaler()
X_train_maxabs = max_abs_scaler.fit_transform(X_train)
X_train_maxabs                # doctest +NORMALIZE_WHITESPACE^ 

X_test = np.array([[ -3., -1.,  4.]])
X_test_maxabs = max_abs_scaler.transform(X_test)
X_test_maxabs

max_abs_scaler.scale_

"""
Explanation: MaxAbsScaler
End of explanation
"""


preprocessing.minmax_scale(X_train)

"""
Explanation: minmax_scale function
End of explanation
"""


preprocessing.maxabs_scale(X_train)

"""
Explanation: maxabs_scale function
End of explanation
"""


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()

X, y = iris.data, iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

np.percentile(X_train[:, 0], [0, 25, 50, 75, 100])

quantile_transformer = preprocessing.QuantileTransformer(random_state=0)

X_train_trans = quantile_transformer.fit_transform(X_train)

X_test_trans = quantile_transformer.transform(X_test)

np.percentile(X_train_trans[:, 0], [0, 25, 50, 75, 100])

np.percentile(X_test[:, 0], [0, 25, 50, 75, 100]), np.percentile(X_test_trans[:, 0], [0, 25, 50, 75, 100])

"""
Explanation: Scaling Sparse Data
Centering sparse data destroys the sparseness which may be desirable. It might make sense to scale the sparse data with the other data--especially if features contain different scales. Use MaxAbsScaler in this situation.
Non-Linear Transformation

QuantileTransformer
End of explanation
"""


quantile_transformer = preprocessing.QuantileTransformer(output_distribution='normal', random_state=0)

X_trans = quantile_transformer.fit_transform(X)

quantile_transformer.quantiles_ 

"""
Explanation: You can map the data to a normal distribution:
End of explanation
"""


