1303 lines
73 KiB
HTML
1303 lines
73 KiB
HTML
<!--
|
|
Automatically generated HTML file from DocOnce source
|
|
(https://github.com/hplgit/doconce/)
|
|
-->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Data Analysis and Machine Learning: Preprocessing and Dimensionality Reduction">
|
|
|
|
<title>Data Analysis and Machine Learning: Preprocessing and Dimensionality Reduction</title>
|
|
|
|
|
|
<link href="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/>
|
|
<script src="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/js/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
<link href="https://thomasf.github.io/solarized-css/solarized-light.min.css" rel="stylesheet">
|
|
<style type="text/css">
|
|
h1 {color: #b58900;} /* yellow */
|
|
/* h1 {color: #cb4b16;} orange */
|
|
/* h1 {color: #d33682;} magenta, the original choice of thomasf */
|
|
code { padding: 0px; background-color: inherit; }
|
|
pre {
|
|
border: 0pt solid #93a1a1;
|
|
box-shadow: none;
|
|
}
|
|
.alert-text-small { font-size: 80%; }
|
|
.alert-text-large { font-size: 130%; }
|
|
.alert-text-normal { font-size: 90%; }
|
|
.alert {
|
|
padding:8px 35px 8px 14px; margin-bottom:18px;
|
|
text-shadow:0 1px 0 rgba(255,255,255,0.5);
|
|
border:1px solid #93a1a1;
|
|
border-radius: 4px;
|
|
-webkit-border-radius: 4px;
|
|
-moz-border-radius: 4px;
|
|
color: #555;
|
|
background-color: #eee8d5;
|
|
background-position: 10px 5px;
|
|
background-repeat: no-repeat;
|
|
background-size: 38px;
|
|
padding-left: 55px;
|
|
width: 75%;
|
|
}
|
|
.alert-block {padding-top:14px; padding-bottom:14px}
|
|
.alert-block > p, .alert-block > ul {margin-bottom:1em}
|
|
.alert li {margin-top: 1em}
|
|
.alert-block p+p {margin-top:5px}
|
|
.alert-notice { background-image: url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_notice.png); }
|
|
.alert-summary { background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_summary.png); }
|
|
.alert-warning { background-image: url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_warning.png); }
|
|
.alert-question {background-image:url(https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_images/small_yellow_question.png); }
|
|
|
|
div { text-align: justify; text-justify: inter-word; }
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Reducing the number of degrees of freedom, overarching view',
|
|
2,
|
|
None,
|
|
'___sec0'),
|
|
('Preprocessing our data', 2, None, '___sec1'),
|
|
('More preprocessing', 2, None, '___sec2'),
|
|
('Simple preprocessing examples, Franke function and regression',
|
|
2,
|
|
None,
|
|
'___sec3'),
|
|
('Simple preprocessing examples, breast cancer data and '
|
|
'classification, Support Vector Machines',
|
|
2,
|
|
None,
|
|
'___sec4'),
|
|
('More on Cancer Data, now with Logistic Regression',
|
|
2,
|
|
None,
|
|
'___sec5'),
|
|
('Why should we think of reducing the dimensionality',
|
|
2,
|
|
None,
|
|
'___sec6'),
|
|
('Basic ideas of the Principal Component Analysis (PCA)',
|
|
2,
|
|
None,
|
|
'___sec7'),
|
|
('Introducing the Covariance and Correlation functions',
|
|
2,
|
|
None,
|
|
'___sec8'),
|
|
('Correlation Function and Design/Feature Matrix',
|
|
2,
|
|
None,
|
|
'___sec9'),
|
|
('Covariance Matrix Examples', 2, None, '___sec10'),
|
|
('Correlation Matrix', 2, None, '___sec11'),
|
|
('Correlation Matrix with Pandas', 2, None, '___sec12'),
|
|
('Correlation Matrix with Pandas and the Franke function',
|
|
2,
|
|
None,
|
|
'___sec13'),
|
|
('Rewriting the Covariance and/or Correlation Matrix',
|
|
2,
|
|
None,
|
|
'___sec14'),
|
|
('Towards the PCA theorem', 2, None, '___sec15'),
|
|
('The Algorithm before the Theorem', 2, None, '___sec16'),
|
|
('Classical PCA Theorem', 2, None, '___sec17'),
|
|
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
|
('PCA Proof continued', 2, None, '___sec19'),
|
|
('The final step', 2, None, '___sec20'),
|
|
('PCA and Scikit-Learn Functionality', 2, None, '___sec21'),
|
|
('Principal Component Analysis', 2, None, '___sec22'),
|
|
('PCA and scikit-learn', 2, None, '___sec23'),
|
|
('More on the PCA', 2, None, '___sec24'),
|
|
('Incremental PCA', 2, None, '___sec25'),
|
|
('Randomized PCA', 2, None, '___sec26'),
|
|
('Kernel PCA', 2, None, '___sec27'),
|
|
('LLE', 2, None, '___sec28'),
|
|
('Other techniques', 2, None, '___sec29')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
TeX: {
|
|
equationNumbers: { autoNumber: "AMS" },
|
|
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
|
}
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- ------------------- main content ---------------------- -->
|
|
|
|
|
|
|
|
<center><h1>Data Analysis and Machine Learning: Preprocessing and Dimensionality Reduction</h1></center> <!-- document title -->
|
|
|
|
<p>
|
|
<!-- author(s): Morten Hjorth-Jensen -->
|
|
|
|
<center>
|
|
<b>Morten Hjorth-Jensen</b> [1, 2]
|
|
</center>
|
|
|
|
<p>
|
|
<!-- institution(s) -->
|
|
|
|
<center>[1] <b>Department of Physics, University of Oslo</b></center>
|
|
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
|
<br>
|
|
<p>
|
|
<center><h4>Oct 24, 2019</h4></center> <!-- date -->
|
|
<br>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec0">Reducing the number of degrees of freedom, overarching view </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
|
|
<p>
|
|
Many Machine Learning problems involve thousands or even millions of
|
|
features for each training instance. Not only does this make training
|
|
extremely slow, it can also make it much harder to find a good
|
|
solution, as we will see. This problem is often referred to as the
|
|
curse of dimensionality. Fortunately, in real-world problems, it is
|
|
often possible to reduce the number of features considerably, turning
|
|
an intractable problem into a tractable one.
|
|
|
|
<p>
|
|
Here we will discuss some of the most popular dimensionality reduction
|
|
techniques: the principal component analysis PCA, Kernel PCA, and
|
|
Locally Linear Embedding (LLE). Furthermore, we will start by looking
|
|
at some simple preprocessing of the data which allow us to rescale the
|
|
data.
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec1">Preprocessing our data </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
|
|
<p>
|
|
Before we proceed however, we will discuss how to preprocess our
|
|
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 employ.
|
|
|
|
<p>
|
|
<b>Scikit-Learn</b> has several functions which allow us to rescale the
|
|
data, normally resulting in much better results in terms of various
|
|
accuracy scores. The <b>StandardScaler</b> function in <b>Scikit-Learn</b>
|
|
ensures that for each feature/predictor we study the mean value is
|
|
zero and the variance is one (every column in the design/feature
|
|
matrix). This scaling has the drawback that it does not ensure that
|
|
we have a particular maximum or minimum in our data set. Another
|
|
function included in <b>Scikit-Learn</b> is the <b>MinMaxScaler</b> which
|
|
ensures that all features are exactly between \( 0 \) and \( 1 \). The
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec2">More preprocessing </h2>
|
|
|
|
<p>
|
|
The <b>Normalizer</b> scales each data
|
|
point such that the feature vector has a euclidean length of one. In other words, it
|
|
projects a data point on the circle (or sphere in the case of higher dimensions) with a
|
|
radius of 1. This means every data point is scaled by a different number (by the
|
|
inverse of it’s length).
|
|
This normalization is often used when only the direction (or angle) of the data matters,
|
|
not the length of the feature vector.
|
|
|
|
<p>
|
|
The <b>RobustScaler</b> works similarly to the StandardScaler in that it
|
|
ensures statistical properties for each feature that guarantee that
|
|
they are on the same scale. However, the RobustScaler uses the median
|
|
and quartiles, instead of mean and variance. This makes the
|
|
RobustScaler ignore data points that are very different from the rest
|
|
(like measurement errors). These odd data points are also called
|
|
outliers, and might often lead to trouble for other scaling
|
|
techniques.
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec3">Simple preprocessing examples, Franke function and regression </h2>
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Common imports</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skl</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> MinMaxScaler, StandardScaler, Normalizer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVR
|
|
|
|
<span style="color: #228B22"># Where to save the figures and data files</span>
|
|
PROJECT_ROOT_DIR = <span style="color: #CD5555">"Results"</span>
|
|
FIGURE_ID = <span style="color: #CD5555">"Results/FigureFiles"</span>
|
|
DATA_ID = <span style="color: #CD5555">"DataFiles/"</span>
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(PROJECT_ROOT_DIR):
|
|
os.mkdir(PROJECT_ROOT_DIR)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(FIGURE_ID):
|
|
os.makedirs(FIGURE_ID)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(DATA_ID):
|
|
os.makedirs(DATA_ID)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">image_path</span>(fig_id):
|
|
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(FIGURE_ID, fig_id)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">data_path</span>(dat_id):
|
|
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(DATA_ID, dat_id)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">save_fig</span>(fig_id):
|
|
plt.savefig(image_path(fig_id) + <span style="color: #CD5555">".png"</span>, format=<span style="color: #CD5555">'png'</span>)
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">FrankeFunction</span>(x,y):
|
|
term1 = <span style="color: #B452CD">0.75</span>*np.exp(-(<span style="color: #B452CD">0.25</span>*(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>) - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>))
|
|
term2 = <span style="color: #B452CD">0.75</span>*np.exp(-((<span style="color: #B452CD">9</span>*x+<span style="color: #B452CD">1</span>)**<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">49.0</span> - <span style="color: #B452CD">0.1</span>*(<span style="color: #B452CD">9</span>*y+<span style="color: #B452CD">1</span>))
|
|
term3 = <span style="color: #B452CD">0.5</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>/<span style="color: #B452CD">4.0</span> - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">3</span>)**<span style="color: #B452CD">2</span>))
|
|
term4 = -<span style="color: #B452CD">0.2</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">4</span>)**<span style="color: #B452CD">2</span> - (<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>)
|
|
<span style="color: #8B008B; font-weight: bold">return</span> term1 + term2 + term3 + term4
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_X</span>(x, y, n ):
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(x.shape) > <span style="color: #B452CD">1</span>:
|
|
x = np.ravel(x)
|
|
y = np.ravel(y)
|
|
|
|
N = <span style="color: #658b00">len</span>(x)
|
|
l = <span style="color: #658b00">int</span>((n+<span style="color: #B452CD">1</span>)*(n+<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">2</span>) <span style="color: #228B22"># Number of elements in beta</span>
|
|
X = np.ones((N,l))
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">1</span>,n+<span style="color: #B452CD">1</span>):
|
|
q = <span style="color: #658b00">int</span>((i)*(i+<span style="color: #B452CD">1</span>)/<span style="color: #B452CD">2</span>)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> k <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(i+<span style="color: #B452CD">1</span>):
|
|
X[:,q+k] = (x**(i-k))*(y**k)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">return</span> X
|
|
|
|
|
|
<span style="color: #228B22"># Making meshgrid of datapoints and compute Franke's function</span>
|
|
n = <span style="color: #B452CD">5</span>
|
|
N = <span style="color: #B452CD">1000</span>
|
|
x = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
|
y = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
|
z = FrankeFunction(x, y)
|
|
X = create_X(x, y, n=n)
|
|
<span style="color: #228B22"># split in training and test data</span>
|
|
X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=<span style="color: #B452CD">0.2</span>)
|
|
|
|
|
|
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
|
svm.fit(X_train, y_train)
|
|
|
|
<span style="color: #228B22"># The mean squared error and R2 score</span>
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE before scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test), y_test)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score before scaling {:.2f}"</span>.format(svm.score(X_test,y_test)))
|
|
|
|
scaler = StandardScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values before scaling:\n {}"</span>.format(X_train.min(axis=<span style="color: #B452CD">0</span>)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values before scaling:\n {}"</span>.format(X_train.max(axis=<span style="color: #B452CD">0</span>)))
|
|
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values after scaling:\n {}"</span>.format(X_train_scaled.min(axis=<span style="color: #B452CD">0</span>)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values after scaling:\n {}"</span>.format(X_train_scaled.max(axis=<span style="color: #B452CD">0</span>)))
|
|
|
|
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
|
svm.fit(X_train_scaled, y_train)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE after scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score for scaled data: {:.2f}"</span>.format(svm.score(X_test_scaled,y_test)))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec4">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines </h2>
|
|
|
|
<p>
|
|
We show here how we can use a simple regression case on the breast
|
|
cancer data using support vector machines (SVM) as algorithm for
|
|
classification.
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVC
|
|
cancer = load_breast_cancer()
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(X_train.shape)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(X_test.shape)
|
|
|
|
svm = SVC(C=<span style="color: #B452CD">100</span>)
|
|
svm.fit(X_train, y_train)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy: {:.2f}"</span>.format(svm.score(X_test,y_test)))
|
|
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> MinMaxScaler, StandardScaler
|
|
scaler = MinMaxScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values before scaling:\n {}"</span>.format(X_train.min(axis=<span style="color: #B452CD">0</span>)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values before scaling:\n {}"</span>.format(X_train.max(axis=<span style="color: #B452CD">0</span>)))
|
|
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values before scaling:\n {}"</span>.format(X_train_scaled.min(axis=<span style="color: #B452CD">0</span>)))
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values before scaling:\n {}"</span>.format(X_train_scaled.max(axis=<span style="color: #B452CD">0</span>)))
|
|
|
|
|
|
svm.fit(X_train_scaled, y_train)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy scaled data with Min-Max scaling: {:.2f}"</span>.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)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy scaled data with Standar Scaler: {:.2f}"</span>.format(svm.score(X_test_scaled,y_test)))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec5">More on Cancer Data, now with Logistic Regression </h2>
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
|
cancer = load_breast_cancer()
|
|
|
|
<span style="color: #228B22"># Set up training data</span>
|
|
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
|
|
logreg = LogisticRegression()
|
|
logreg.fit(X_train, y_train)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
|
|
|
<span style="color: #228B22"># Scale data</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
|
scaler = StandardScaler()
|
|
scaler.fit(X_train)
|
|
X_train_scaled = scaler.transform(X_train)
|
|
X_test_scaled = scaler.transform(X_test)
|
|
logreg.fit(X_train_scaled, y_train)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec6">Why should we think of reducing the dimensionality </h2>
|
|
|
|
<p>
|
|
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
|
We use also <b>Pandas</b> to compute the correlation matrix.
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
|
cancer = load_breast_cancer()
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
<span style="color: #228B22"># Making a data frame</span>
|
|
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
|
|
|
fig, axes = plt.subplots(<span style="color: #B452CD">15</span>,<span style="color: #B452CD">2</span>,figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">20</span>))
|
|
malignant = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
|
|
benign = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
|
|
ax = axes.ravel()
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">30</span>):
|
|
_, bins = np.histogram(cancer.data[:,i], bins =<span style="color: #B452CD">50</span>)
|
|
ax[i].hist(malignant[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
|
ax[i].hist(benign[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
|
ax[i].set_title(cancer.feature_names[i])
|
|
ax[i].set_yticks(())
|
|
ax[<span style="color: #B452CD">0</span>].set_xlabel(<span style="color: #CD5555">"Feature magnitude"</span>)
|
|
ax[<span style="color: #B452CD">0</span>].set_ylabel(<span style="color: #CD5555">"Frequency"</span>)
|
|
ax[<span style="color: #B452CD">0</span>].legend([<span style="color: #CD5555">"Malignant"</span>, <span style="color: #CD5555">"Benign"</span>], loc =<span style="color: #CD5555">"best"</span>)
|
|
fig.tight_layout()
|
|
plt.show()
|
|
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
|
|
correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
|
<span style="color: #228B22"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
|
<span style="color: #228B22"># annot = True to print the values inside the square</span>
|
|
sns.heatmap(data=correlation_matrix, annot=<span style="color: #658b00">True</span>)
|
|
plt.show()
|
|
|
|
<span style="color: #228B22">#print eigvalues of correlation matrix</span>
|
|
EigValues, EigVectors = np.linalg.eig(correlation_matrix)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(EigValues)
|
|
</pre></div>
|
|
<p>
|
|
In the above example we note two things. In the first plot we display
|
|
the overlap of benign and malignant tumors as functions of the various
|
|
features in the Wisconsing breast cancer data set. We see that for
|
|
some of the features we can distinguish clearly the benign and
|
|
malignant cases while for other features we cannot. This can point to
|
|
us which features may be of greater interest when we wish to classify
|
|
a benign or not benign tumour.
|
|
|
|
<p>
|
|
In the second figure we have computed the so-called correlation
|
|
matrix, which in our case with thirty features becomes a \( 30\times 30 \)
|
|
matrix.
|
|
|
|
<p>
|
|
We constructed this matrix using <b>pandas</b> via the statements
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
|
</pre></div>
|
|
<p>
|
|
and then
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
|
</pre></div>
|
|
<p>
|
|
Diagonalizing this matrix we can in turn say something about which
|
|
features are of relevance and which are not. But before we proceed we
|
|
need to define covariance and correlation matrices. This leads us to
|
|
the classical Principal Component Analysis (PCA) theorem with
|
|
applications.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec7">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
|
|
|
<p>
|
|
We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see below for its definition)
|
|
|
|
<ul>
|
|
<li> Each data point is determined by \( p \) extrinsic (measurement) variables</li>
|
|
<li> We may want to ask the following question: Are there fewer intrinsic variables (say \( d < < p \)) that still approximately describe the data?</li>
|
|
<li> If so, these intrinsic variables may tell us something important and finding these intrinsic variables is what dimension reduction methods do.</li>
|
|
</ul>
|
|
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec8">Introducing the Covariance and Correlation functions </h2>
|
|
|
|
<p>
|
|
Before we discuss the PCA theorem, we need to remind ourselves about
|
|
the definition of the covariance and the correlation function.
|
|
|
|
<p>
|
|
Suppose we have defined two vectors
|
|
\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
|
\mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
where for example
|
|
$$
|
|
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
|
|
$$
|
|
|
|
With this definition and recalling that the variance is defined as
|
|
$$
|
|
\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2,
|
|
$$
|
|
|
|
we can rewrite the covariance matrix as
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
|
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\
|
|
\end{bmatrix}.
|
|
$$
|
|
|
|
<p>
|
|
The covariance takes values between zero and infinity and may thus
|
|
lead to problems with loss of numerical precision for particularly
|
|
large values. It is common to scale the covariance matrix by
|
|
introducing instead the correlation matrix defined via the so-called
|
|
correlation function
|
|
|
|
$$
|
|
\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}.
|
|
$$
|
|
|
|
<p>
|
|
The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]
|
|
\in [-1,1] \). This avoids eventual problems with too large values. We
|
|
can then define the correlation matrix for the two vectors \( \boldsymbol{x} \)
|
|
and \( \boldsymbol{y} \) as
|
|
|
|
$$
|
|
\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\
|
|
\mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
<p>
|
|
In the above example this is the function we constructed using <b>pandas</b>.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec9">Correlation Function and Design/Feature Matrix </h2>
|
|
|
|
<p>
|
|
In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression we defined the design/feature matrix \( \boldsymbol{X} \) as
|
|
$$
|
|
\boldsymbol{X}=\begin{bmatrix}
|
|
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
|
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
|
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
|
\dots & \dots & \dots & \dots \dots & \dots \\
|
|
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
|
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the
|
|
entries \( n \) being the row elements.
|
|
We can rewrite the design/feature matrix in terms of its column vectors as
|
|
$$
|
|
\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_0 & \boldsymbol{x}_0 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix},
|
|
$$
|
|
|
|
with a given vector
|
|
$$
|
|
\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}.
|
|
$$
|
|
|
|
<p>
|
|
With these definitions, we can now rewrite our \( 2\times 2 \) correaltion/covariance matrix in terms of a moe general design/feature matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i =0,1,\dots,p-1 \)
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix}
|
|
\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
|
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
|
\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
|
\dots & \dots & \dots & \dots & \dots & \dots \\
|
|
\dots & \dots & \dots & \dots & \dots & \dots \\
|
|
\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
and the correlation matrix
|
|
$$
|
|
\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix}
|
|
1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
|
\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
|
\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
|
\dots & \dots & \dots & \dots & \dots & \dots \\
|
|
\dots & \dots & \dots & \dots & \dots & \dots \\
|
|
\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec10">Covariance Matrix Examples </h2>
|
|
|
|
<p>
|
|
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
|
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
|
|
the exact mean values. The following simple function uses the
|
|
<b>np.vstack</b> function which takes each vector of dimension \( 1\times n \)
|
|
and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
|
|
|
|
$$
|
|
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
|
|
x_1 & y_1 \\
|
|
x_2 & y_2\\
|
|
\dots & \dots \\
|
|
x_{n-2} & y_{n-2}\\
|
|
x_{n-1} & y_{n-1} &
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
<p>
|
|
which in turn is converted into into the \( 2\times 2 \) covariance matrix
|
|
\( \boldsymbol{C} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
|
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
|
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
|
covariance matrix through the <b>np.linalg.eig()</b> function.
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
n = <span style="color: #B452CD">100</span>
|
|
x = np.random.normal(size=n)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(x))
|
|
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(y))
|
|
W = np.vstack((x, y))
|
|
C = np.cov(W)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec11">Correlation Matrix </h2>
|
|
|
|
<p>
|
|
The previous example can be converted into the correlation matrix by
|
|
simply scaling the matrix elements with the variances. We should also
|
|
subtract the mean values for each column. This leads to the following
|
|
code which sets up the correlations matrix for the previous example in
|
|
a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
n = <span style="color: #B452CD">100</span>
|
|
<span style="color: #228B22"># define two vectors </span>
|
|
x = np.random.random(size=n)
|
|
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
|
<span style="color: #228B22">#scaling the x and y vectors </span>
|
|
x = x - np.mean(x)
|
|
y = y - np.mean(y)
|
|
variance_x = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
|
variance_y = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(variance_x)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(variance_y)
|
|
cov_xy = np.sum(x<span style="color: #707a7c">@y</span>)/n
|
|
cov_xx = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
|
cov_yy = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
|
C = np.zeros((<span style="color: #B452CD">2</span>,<span style="color: #B452CD">2</span>))
|
|
C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>]= cov_xx/variance_x
|
|
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>]= cov_yy/variance_y
|
|
C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]= cov_xy/np.sqrt(variance_y*variance_x)
|
|
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]= C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
|
</pre></div>
|
|
<p>
|
|
We see that the matrix elements along the diagonal are one as they
|
|
should be and that the matrix is symmetric. Furthermore, diagonalizing
|
|
this matrix we easily see that it is a positive definite matrix.
|
|
|
|
<p>
|
|
The above procedure with <b>numpy</b> can be made more compact if we use <b>pandas</b>.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec12">Correlation Matrix with Pandas </h2>
|
|
|
|
<p>
|
|
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
n = <span style="color: #B452CD">10</span>
|
|
x = np.random.normal(size=n)
|
|
x = x - np.mean(x)
|
|
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
|
y = y - np.mean(y)
|
|
X = (np.vstack((x, y))).T
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(X)
|
|
Xpd = pd.DataFrame(X)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(Xpd)
|
|
correlation_matrix = Xpd.corr()
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(correlation_matrix)
|
|
</pre></div>
|
|
<p>
|
|
We expand this model to the Franke function discussed above.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec13">Correlation Matrix with Pandas and the Franke function </h2>
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Common imports</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">FrankeFunction</span>(x,y):
|
|
term1 = <span style="color: #B452CD">0.75</span>*np.exp(-(<span style="color: #B452CD">0.25</span>*(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>) - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>))
|
|
term2 = <span style="color: #B452CD">0.75</span>*np.exp(-((<span style="color: #B452CD">9</span>*x+<span style="color: #B452CD">1</span>)**<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">49.0</span> - <span style="color: #B452CD">0.1</span>*(<span style="color: #B452CD">9</span>*y+<span style="color: #B452CD">1</span>))
|
|
term3 = <span style="color: #B452CD">0.5</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>/<span style="color: #B452CD">4.0</span> - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">3</span>)**<span style="color: #B452CD">2</span>))
|
|
term4 = -<span style="color: #B452CD">0.2</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">4</span>)**<span style="color: #B452CD">2</span> - (<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>)
|
|
<span style="color: #8B008B; font-weight: bold">return</span> term1 + term2 + term3 + term4
|
|
|
|
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_X</span>(x, y, n ):
|
|
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(x.shape) > <span style="color: #B452CD">1</span>:
|
|
x = np.ravel(x)
|
|
y = np.ravel(y)
|
|
|
|
N = <span style="color: #658b00">len</span>(x)
|
|
l = <span style="color: #658b00">int</span>((n+<span style="color: #B452CD">1</span>)*(n+<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">2</span>) <span style="color: #228B22"># Number of elements in beta</span>
|
|
X = np.ones((N,l))
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">1</span>,n+<span style="color: #B452CD">1</span>):
|
|
q = <span style="color: #658b00">int</span>((i)*(i+<span style="color: #B452CD">1</span>)/<span style="color: #B452CD">2</span>)
|
|
<span style="color: #8B008B; font-weight: bold">for</span> k <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(i+<span style="color: #B452CD">1</span>):
|
|
X[:,q+k] = (x**(i-k))*(y**k)
|
|
|
|
<span style="color: #8B008B; font-weight: bold">return</span> X
|
|
|
|
|
|
<span style="color: #228B22"># Making meshgrid of datapoints and compute Franke's function</span>
|
|
n = <span style="color: #B452CD">4</span>
|
|
N = <span style="color: #B452CD">100</span>
|
|
x = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
|
y = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
|
z = FrankeFunction(x, y)
|
|
X = create_X(x, y, n=n)
|
|
|
|
Xpd = pd.DataFrame(X)
|
|
<span style="color: #228B22"># subtract the mean values and set up the covariance matrix</span>
|
|
Xpd = Xpd - Xpd.mean()
|
|
covariance_matrix = Xpd.cov()
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(covariance_matrix)
|
|
</pre></div>
|
|
<p>
|
|
We note here that the covariance is zero for the first rows and
|
|
columns since all matrix elements in the design matrix were set to one
|
|
(we are fitting the function in terms of a polynomial of degree \( n \)).
|
|
|
|
<p>
|
|
This means that the variance for these elements will be zero and will
|
|
cause problems when we set up the correlation matrix. We can simply
|
|
drop these elements as follows and then construct the correlation
|
|
matrix.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec14">Rewriting the Covariance and/or Correlation Matrix </h2>
|
|
|
|
<p>
|
|
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
|
$$
|
|
|
|
<p>
|
|
To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)
|
|
$$
|
|
\boldsymbol{X}=\begin{bmatrix}
|
|
x_{00} & x_{01}\\
|
|
x_{10} & x_{11}\\
|
|
\end{bmatrix}=\begin{bmatrix}
|
|
\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\
|
|
\end{bmatrix}.
|
|
$$
|
|
|
|
<p>
|
|
If we then compute the expectation value
|
|
$$
|
|
\mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T=\begin{bmatrix}
|
|
x_{00}^2+x_{01}^2 & x_{00}x_{10}+x_{01}x_{11}\\
|
|
x_{10}x_{00}+x_{01}x_{11} & x_{10}^2+x_{11}^2\\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
which is just
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\
|
|
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).
|
|
|
|
<p>
|
|
It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec15">Towards the PCA theorem </h2>
|
|
|
|
<p>
|
|
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
|
$$
|
|
|
|
Let us now assume that we can perform a series of orthogonal transformations where we employ some orthogonal matrices \( \boldsymbol{S} \).
|
|
These matrices are defined as \( \boldsymbol{S}\in {\mathbb{R}}^{p\times p} \) and obey the orthogonality requirements \( \boldsymbol{S}\boldsymbol{S}^T=\boldsymbol{S}^T\boldsymbol{S}=\boldsymbol{I} \). The matrix can be written out in terms of the column vectors \( \boldsymbol{s}_i \) as \( \boldsymbol{S}=[\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \) and \( \boldsymbol{s}_i \in {\mathbb{R}}^{p} \).
|
|
|
|
<p>
|
|
Assume also that there is a transformation \( \boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T=\boldsymbol{C}[\boldsymbol{y}] \) such that the new matrix \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal with elements \( [\lambda_0,\lambda_1,\lambda_2,\dots,\lambda_{p-1}] \).
|
|
|
|
<p>
|
|
That is we have
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{y}] = \mathbb{E}[\boldsymbol{S}\boldsymbol{X}\boldsymbol{X}^T\boldsymbol{S}^T]=\boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
|
$$
|
|
|
|
since the matrix \( \boldsymbol{S} \) is not a data dependent matrix. Multiplying with \( \boldsymbol{S}^T \) from the left we have
|
|
$$
|
|
\boldsymbol{S}^T\boldsymbol{C}[\boldsymbol{y}] = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
|
$$
|
|
|
|
and since \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal we have for a given eigenvalue \( i \) of the covariance matrix that
|
|
|
|
$$
|
|
\boldsymbol{S}^T_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T_i.
|
|
$$
|
|
|
|
<p>
|
|
In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is
|
|
\( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
|
|
|
|
<p>
|
|
The eigenvalues tell us then how much we need to stretch the
|
|
corresponding eigenvectors. Dimensions with large eigenvalues have
|
|
thus large variations (large variance) and define therefore useful
|
|
dimensions. The data points are more spread out in the direction of
|
|
these eigenvectors. Smaller eigenvalues mean on the other hand that
|
|
the corresponding eigenvectors are shrunk accordingly and the data
|
|
points are tightly bunched together and there is not much variation in
|
|
these specific directions. Hopefully then we could leave it out
|
|
dimensions where the eigenvalues are very small. If \( p \) is very large,
|
|
we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \)
|
|
features/predictors.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec16">The Algorithm before theorem </h2>
|
|
|
|
<p>
|
|
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
|
|
|
<ul>
|
|
<li> Set up the datapoints for the design/feature matrix \( \boldsymbol{X} \) with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) referring to the column numbers and the entries \( n \) being the row elements.</li>
|
|
</ul>
|
|
|
|
$$
|
|
\boldsymbol{X}=\begin{bmatrix}
|
|
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
|
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
|
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
|
\dots & \dots & \dots & \dots \dots & \dots \\
|
|
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
|
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
|
|
<ul>
|
|
<li> Center the data by subtracting the mean value for each column. This leads to a new matrix \( \boldsymbol{X}\rightarrow \overline{\boldsymbol{X}} \).</li>
|
|
<li> Compute then the covariance/correlation matrix \( \mathbb{E}[\overline{\boldsymbol{X}}\overline{\boldsymbol{X}}^T] \).</li>
|
|
<li> Find the eigenpairs of \( \boldsymbol{C} \) with eigenvalues \( [\lambda_0,\lambda_1,\dots,\lambda_{p-1}] \) and eigenvectors \( [\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \).</li>
|
|
<li> Order the eigenvalue (and the eigenvectors accordingly) in order of decreasing eigenvalues.</li>
|
|
<li> Keep only those \( l \) eigenvalues larger than a selected threshold value, discarding thus \( p-l \) features since we expect small variations in the data here.</li>
|
|
</ul>
|
|
|
|
After this we ask ourselves how do we prove the link between the maximum variance and the feature reduction.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec17">Classical PCA Theorem </h2>
|
|
|
|
<p>
|
|
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been centered as discussed above. For the sake of simplicity we skip the overline symbol. The matrix is defined in terms of the various column vectors \( [\boldsymbol{x}_0,\boldsymbol{x}_1,\dots, \boldsymbol{x}_{p-1}] \)
|
|
each with dimension \( \boldsymbol{x}\in {\mathbb{R}}^{n} \).
|
|
|
|
<p>
|
|
We assume also that we have an orthogonal transformation \( \boldsymbol{W}\in {\mathbb{R}}^{p\times p} \). We define the reconstruction error (which is similar to the mean squared error we have seen before) as
|
|
$$
|
|
J(\boldsymbol{W},\boldsymbol{Z}) = \frac{1}{p}\sum_i (\boldsymbol{x}_i - \overline{\boldsymbol{x}_i})^2,
|
|
$$
|
|
|
|
with \( \overline{\boldsymbol{x}_i} = \boldsymbol{W}\boldsymbol{z}_i \), where \( \boldsymbol{z}_i \) is a row vector with dimension \( {\mathbb{R}}^{n} \) of the matrix
|
|
\( \boldsymbol{Z}\in {\mathbb{R}}^{p\times n} \). When doing PCA we want to reduce this dimensionality.
|
|
|
|
<p>
|
|
The PCA theorem states that minimizing the above reconstruction error corresponds to setting \( \boldsymbol{W}=\boldsymbol{S} \), the orthogonal matrix which diagonalizes the empirical covariance(correlation) matrix. The optimal low-dimensional encoding of the data is then given by a set of vectors \( l \) $\boldsymbol{z}_i$, with \( l < < p \), defined by the orthogonal projection of the data onto the columns spanned by the eigenvectors of the covariance(correlations matrix).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec18">Proof of the PCA Theorem </h2>
|
|
|
|
<p>
|
|
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
|
$$
|
|
J(\boldsymbol{w}_0,\boldsymbol{z}_0)= \frac{1}{p}\sum_i (\boldsymbol{x}_i - z_{i0}\boldsymbol{w}_0)^2=\frac{1}{p}\sum_i (\boldsymbol{x}_^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2\boldsymbol{w}_0^T\boldsymbol{w}_0),
|
|
$$
|
|
|
|
which we can rewrite due to the orthogonality of \( \boldsymbol{w}_i \) as
|
|
$$
|
|
J(\boldsymbol{w}_0,\boldsymbol{z}_0)=\frac{1}{p}\sum_i (\boldsymbol{x}_^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2).
|
|
$$
|
|
|
|
Minimizing \( J \) with respect to the unknown parameters \( z_{0i} \) we obtain that
|
|
$$
|
|
z_{i0}=\boldsymbol{w}_0^T\boldsymbol{x}_i,
|
|
$$
|
|
|
|
where the vectors on the rhs are known.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec19">PCA Proof continued </h2>
|
|
|
|
<p>
|
|
We have now found the unknown parameters \( z_{i0} \). These correspond to the project coordinates and we can write
|
|
$$
|
|
J(\boldsymbol{w}_0)= \frac{1}{p}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - z_{i0}^2)=\mathrm{const}-\frac{1}{p}\sum_i z_{i0}^2.
|
|
$$
|
|
|
|
<p>
|
|
We can show that the variance of the project coordinates defined by \( \boldsymbol{w}_0^T\boldsymbol{x}_i \) are given by
|
|
$$
|
|
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{p}\sum_i z_{i0}^2,
|
|
$$
|
|
|
|
since the expectation value of
|
|
$$
|
|
\mathbb{E}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \mathbb{E}[z_{i0}]= \boldsymbol{w}_0^T\mathbb{E}[\boldsymbol{x}_i]=0,
|
|
$$
|
|
|
|
where we have used the fact that our data are centered.
|
|
|
|
<p>
|
|
Recalling our definition of the covariance as
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T],
|
|
$$
|
|
|
|
we have thus that
|
|
$$
|
|
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{p}\sum_i z_{i0}^2=\boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0.
|
|
$$
|
|
|
|
<p>
|
|
We are almost there, we have obtained a relation between minimizing the reconstruction error and the variance and the covariance matrix. Minimizing the error is equivalent to maximizing the variance of the projected data.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec20">The final step </h2>
|
|
|
|
<p>
|
|
We could trivially maximize the variance of the projection (and
|
|
thereby minimize the error in the reconstruction function) by letting
|
|
the norm-2 of \( \boldsymbol{w}_0 \) go to infinity. However, this norm since we
|
|
want the matrix \( \boldsymbol{W} \) to be an orthogonal matrix, is constrained by
|
|
\( $\vert\vert \boldsymbol{w}_0 \vert\vert_2^2=1 \). Imposing this condition via a
|
|
Lagrange multiplier we can then in turn maximize
|
|
|
|
$$
|
|
J(\boldsymbol{w}_0)= \boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0+\lambda_0(1-\boldsymbol{w}_0^T\boldsymbol{w}_0).
|
|
$$
|
|
|
|
Taking the derivative with respect to \( \boldsymbol{w}_0 \) we obtain
|
|
|
|
$$
|
|
\frac{\partial J(\boldsymbol{w}_0)}{\partial \boldsymbol{w}_0}= 2\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0-2\lambda_0\boldsymbol{w}_0=0,
|
|
$$
|
|
|
|
meaning that
|
|
$$
|
|
\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0\boldsymbol{w}_0.
|
|
$$
|
|
|
|
<b>The direction that maximizes the variance (or minimizes the construction error) is an eigenvector of the covariance matrix</b>! If we left multiply with \( \boldsymbol{w}_0^T \) we have the variance of the projected data is
|
|
$$
|
|
\boldsymbol{w}_^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0.
|
|
$$
|
|
|
|
If we want to maximize the variance (minimize the construction error) we simply pick the eigenvector of the covariance matrix with the largest eigenvalue. This establishes the link between the minimization of the reconstruction function \( J \) in terms of an orthogonal matrix and the maximization of the variance and thereby the covariance of our observations encoded in the design/feature matrix \( \boldsymbol{X} \). The proof for the other eigenvectors \( \boldsymbol{w}_1,\boldsymbol{w}_2,\dots \) cna be established by applying the above arguments and using the fact that basis of eigenvectors is orthogonal, see <a href="https://mitpress.mit.edu/books/machine-learning-1" target="_blank">Murphy chapter 12.2</a>. The discussion in chapter 12.2 of Murphy's text has also a nice link with the Singular Value Decomposition theorem. For categorical data, see chapter 12.4 and discussion therein.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec21">PCA and Scikit-Learn Functionality </h2>
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Now add PCA</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
|
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
|
pca.fit(X_train_scaled)
|
|
|
|
X_pca = pca.transform(X_train_scaled)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec22">Principal Component Analysis </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
|
First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it.
|
|
|
|
<p>
|
|
The following Python code uses NumPy’s <b>svd()</b> function to obtain all the principal components of the
|
|
training set, then extracts the first two principal components. First we center the data
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span>X_centered = X - X.mean(axis=<span style="color: #B452CD">0</span>)
|
|
U, s, V = np.linalg.svd(X_centered)
|
|
c1 = V.T[:, <span style="color: #B452CD">0</span>]
|
|
c2 = V.T[:, <span style="color: #B452CD">1</span>]
|
|
</pre></div>
|
|
<p>
|
|
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
|
the data for you. However, if you implement PCA yourself (as in the preceding example), or if you use other libraries, don’t
|
|
forget to center the data first.
|
|
|
|
<p>
|
|
Once you have identified all the principal components, you can reduce the dimensionality of the dataset
|
|
down to \( d \) dimensions by projecting it onto the hyperplane defined by the first \( d \) principal components.
|
|
Selecting this hyperplane ensures that the projection will preserve as much variance as possible.
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span>W2 = V.T[:, :<span style="color: #B452CD">2</span>]
|
|
X2D = X_centered.dot(W2)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split -->
|
|
|
|
<h2 id="___sec23">PCA and scikit-learn </h2>
|
|
|
|
<p>
|
|
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
|
following code applies PCA to reduce the dimensionality of the dataset down to two dimensions (note
|
|
that it automatically takes care of centering the data):
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
|
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
|
X2D = pca.fit_transform(X)
|
|
</pre></div>
|
|
<p>
|
|
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
|
components variable (note that it contains the PCs as horizontal vectors, so, for example, the first
|
|
principal component is equal to
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span>pca.components_.T[:, <span style="color: #B452CD">0</span>]).
|
|
</pre></div>
|
|
<p>
|
|
Another very useful piece of information is the explained variance ratio of each principal component,
|
|
available via the \( explained\_variance\_ratio \) variable. It indicates the proportion of the dataset’s
|
|
variance that lies along the axis of each principal component.
|
|
More material to come here.
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec24">More on the PCA </h2>
|
|
|
|
<p>
|
|
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
|
choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%).
|
|
Unless, of course, you are reducing dimensionality for data visualization — in that case you will
|
|
generally want to reduce the dimensionality down to 2 or 3.
|
|
The following code computes PCA without reducing dimensionality, then computes the minimum number
|
|
of dimensions required to preserve 95% of the training set’s variance:
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span>pca = PCA()
|
|
pca.fit(X)
|
|
cumsum = np.cumsum(pca.explained_variance_ratio_)
|
|
d = np.argmax(cumsum >= <span style="color: #B452CD">0.95</span>) + <span style="color: #B452CD">1</span>
|
|
</pre></div>
|
|
<p>
|
|
You could then set \( n\_components=d \) and run PCA again. However, there is a much better option: instead
|
|
of specifying the number of principal components you want to preserve, you can set \( n\_components \) to be
|
|
a float between 0.0 and 1.0, indicating the ratio of variance you wish to preserve:
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span>pca = PCA(n_components=<span style="color: #B452CD">0.95</span>)
|
|
X_reduced = pca.fit_transform(X)
|
|
</pre></div>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec25">Incremental PCA </h2>
|
|
|
|
<p>
|
|
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
|
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
|
|
been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch
|
|
at a time. This is useful for large training sets, and also to apply PCA online (i.e., on the fly, as new
|
|
instances arrive).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec26">Randomized PCA </h2>
|
|
|
|
<p>
|
|
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
|
algorithm that quickly finds an approximation of the first d principal components. Its computational
|
|
complexity is \( O(m \times d^2)+O(d^3) \), instead of \( O(m \times n^2) + O(n^3) \), so it is dramatically faster than the
|
|
previous algorithms when \( d \) is much smaller than \( n \).
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec27">Kernel PCA </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
|
|
<p>
|
|
The kernel trick is a mathematical technique that implicitly maps instances into a
|
|
very high-dimensional space (called the feature space), enabling nonlinear classification and regression
|
|
with Support Vector Machines. Recall that a linear decision boundary in the high-dimensional feature
|
|
space corresponds to a complex nonlinear decision boundary in the original space.
|
|
It turns out that the same trick can be applied to PCA, making it possible to perform complex nonlinear
|
|
projections for dimensionality reduction. This is called Kernel PCA (kPCA). It is often good at
|
|
preserving clusters of instances after projection, or sometimes even unrolling datasets that lie close to a
|
|
twisted manifold.
|
|
For example, the following code uses Scikit-Learn’s KernelPCA class to perform kPCA with an
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> KernelPCA
|
|
rbf_pca = KernelPCA(n_components = <span style="color: #B452CD">2</span>, kernel=<span style="color: #CD5555">"rbf"</span>, gamma=<span style="color: #B452CD">0.04</span>)
|
|
X_reduced = rbf_pca.fit_transform(X)
|
|
</pre></div>
|
|
|
|
</div>
|
|
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec28">LLE </h2>
|
|
|
|
<p>
|
|
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
|
(NLDR) technique. It is a Manifold Learning technique that does not rely on projections like the previous
|
|
algorithms. In a nutshell, LLE works by first measuring how each training instance linearly relates to its
|
|
closest neighbors (c.n.), and then looking for a low-dimensional representation of the training set where
|
|
these local relationships are best preserved (more details shortly).
|
|
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec29">Other techniques </h2>
|
|
|
|
<p>
|
|
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
|
|
|
<p>
|
|
Here are some of the most popular:
|
|
|
|
<ul>
|
|
<li> <b>Multidimensional Scaling (MDS)</b> reduces dimensionality while trying to preserve the distances between the instances.</li>
|
|
<li> <b>Isomap</b> creates a graph by connecting each instance to its nearest neighbors, then reduces dimensionality while trying to preserve the geodesic distances between the instances.</li>
|
|
<li> <b>t-Distributed Stochastic Neighbor Embedding</b> (t-SNE) reduces dimensionality while trying to keep similar instances close and dissimilar instances apart. It is mostly used for visualization, in particular to visualize clusters of instances in high-dimensional space (e.g., to visualize the MNIST images in 2D).</li>
|
|
<li> Linear Discriminant Analysis (LDA) is actually a classification algorithm, but during training it learns the most discriminative axes between the classes, and these axes can then be used to define a hyperplane onto which to project the data. The benefit is that the projection will keep classes as far apart as possible, so LDA is a good technique to reduce dimensionality before running another classification algorithm such as a Support Vector Machine (SVM) classifier discussed in the SVM lectures.</li>
|
|
</ul>
|
|
|
|
Here are other examples where we use the <b>DataFrame</b> functionality to handle arrays, now with more interesting features for us, namely numbers. We set up a matrix
|
|
of dimensionality \( 10\times 5 \) and compute the mean value and standard deviation of each column. Similarly, we can perform mathematial operations like squaring the matrix elements and many other operations.
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">IPython.display</span> <span style="color: #8B008B; font-weight: bold">import</span> display
|
|
np.random.seed(<span style="color: #B452CD">100</span>)
|
|
<span style="color: #228B22"># setting up a 10 x 5 matrix</span>
|
|
rows = <span style="color: #B452CD">10</span>
|
|
cols = <span style="color: #B452CD">5</span>
|
|
a = np.random.randn(rows,cols)
|
|
df = pd.DataFrame(a)
|
|
display(df)
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(df.mean())
|
|
<span style="color: #8B008B; font-weight: bold">print</span>(df.std())
|
|
display(df**<span style="color: #B452CD">2</span>)
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- ------------------- end of main content --------------- -->
|
|
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
|
</center>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|
|
|