dim red
This commit is contained in:
@@ -24,18 +24,68 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca
|
||||
!bblock
|
||||
|
||||
Before we proceed however, we will discuss how to preprocess our
|
||||
data. Till now and in connection with project 1 not met so many cases
|
||||
data. Till now and in connection with our previous examples we have not met so many cases
|
||||
where we are too sensitive to the scaling of our data. Normally the
|
||||
data may need a rescaling and/or may be sensitive to extreme
|
||||
values. Scaling the data renders our inputs much more suitable for the
|
||||
algorithms we want to emply.
|
||||
algorithms we want to employ.
|
||||
|
||||
_Scikit-Learn_ has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The _StandardScaler_ function in _Scikit-Learn_ ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix).
|
||||
This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in _Scikit-Learn_ is the _MinMaxScaler_ which ensures that all features are exactly between $0$ and $1$. The _Normalizer_ function scale each column of the design matrix so that
|
||||
This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in _Scikit-Learn_ is the _MinMaxScaler_ which ensures that all features are exactly between $0$ and $1$. The _Normalizer_ function scales each column of the design matrix by its Euclidean norm.
|
||||
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
===== Simple preprocessing examples =====
|
||||
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
|
||||
!bc pycod
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.svm import SVC
|
||||
cancer = load_breast_cancer()
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
svm = SVC(C=100)
|
||||
svm.fit(X_train, y_train)
|
||||
print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
|
||||
scaler = MinMaxScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
print("Feature min values before scaling:\n {}".format(X_train.min(axis=0)))
|
||||
print("Feature max values before scaling:\n {}".format(X_train.max(axis=0)))
|
||||
|
||||
print("Feature min values before scaling:\n {}".format(X_train_scaled.min(axis=0)))
|
||||
print("Feature max values before scaling:\n {}".format(X_train_scaled.max(axis=0)))
|
||||
|
||||
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
!split
|
||||
===== Principal Component Analysis =====
|
||||
|
||||
Reference in New Issue
Block a user