Answers for "python preprocessing"

1

feature scaling in python

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
from sklearn.linear_model import Ridge
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data,
                                                   random_state = 0)

X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Posted by: Guest on April-27-2020
0

Scaling features to a range

# Scaling features to a range using MaxAbsScaler

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
# array([[ 0.5, -1.,  1. ],
#        [ 1. ,  0. ,  0. ],
#        [ 0. ,  1. , -0.5]])
X_test = np.array([[ -3., -1.,  4.]])
X_test_maxabs = max_abs_scaler.transform(X_test)
X_test_maxabs
# array([[-1.5, -1. ,  2. ]])
max_abs_scaler.scale_
# array([2.,  1.,  2.])
Posted by: Guest on April-16-2020
0

The function scale provides a quick and easy way to perform

# Standardization

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
# array([[ 0.  ..., -1.22...,  1.33...],
#        [ 1.22...,  0.  ..., -0.26...],
#        [-1.22...,  1.22..., -1.06...]])

# Scaled data has zero mean and unit variance:

X_scaled.mea(axis=0)
# array([0., 0., 0.])
X_scaled.std(axis=0)
# array([1., 1., 1.])

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

scaler.mean_
# array([1. ..., 0. ..., 0.33...])

scaler.scale_
# array([0.81..., 0.81..., 1.24...])

scaler.transform(X_train)
array([[ 0.  ..., -1.22...,  1.33...],
       [ 1.22...,  0.  ..., -0.26...],
       [-1.22...,  1.22..., -1.06...]])

X_test = [[-1., 1., 0.]]
scaler.transform(X_test)
# array([[-2.44...,  1.22..., -0.26...]])
Posted by: Guest on April-16-2020

Code answers related to "python preprocessing"

Python Answers by Framework

Browse Popular Code Answers by Language