This commit is contained in:
mhjensen
2018-05-27 09:43:21 -04:00
parent f90a232c25
commit 17afb32e08
2 changed files with 59 additions and 39 deletions
+42 -39
View File
@@ -11,25 +11,24 @@ DATE: today
Our emphasis throughout this series of lectures
is on understanding the mathematical aspects of
different algorithms used in the fields of data analysis and machine learning.
However, where possible we will emphasize the
importance of using available software. We start thus with a hands-on
and top-down approach to machine learning. The aim is thus to start with
relevant data and use these to introduce statistical data analysis
relevant data or data we have produced
and use these to introduce statistical data analysis
concepts and machine learning algorithms before we delve into the
algorithms themselves. The examples we will use in the beginning, start with simple
polynomials with random noise added, and using the Python
software package "Scikit-learn":"http://scikit-learn.org/stable/" we
will introduce various machine learning algorithms to make fits of
polynomials with random noise added. We will use the Python
software package "Scikit-learn":"http://scikit-learn.org/stable/" and
introduce various machine learning algorithms to make fits of
the data and predictions. We move thereafter to more interesting
cases such as the simulation of financial transactions or disease
models. These are examples where we can easily set up the data and
then use machine learning algorithms included in for example
_scikit-learn_. Another model we will consider is the so-called Ising
model. Here we will use this model to produce data for selected spin
configurations and attempt to classify the data. Finally, our last
example consists of economic data from the OECD.
_scikit-learn_.
All these examples will serve us the purpose of getting us started, furthermore, they
These examples will serve us the purpose of getting started. Furthermore, they
allow us to catch more than two birds with a stone. They will allow us
to bring in some programming specific topics and tools as well as
showing the power of various Python (and R) packages for machine
@@ -47,7 +46,7 @@ Moreover, the examples we introduce will serve as inputs to many of our discussi
===== Software and needed installations =====
We will make extensive use of Python as programming language and its
myriad of available libraries. Furthermore, you will find
myriad of available libraries. You will find
IPython/Jupyter notebooks invaluable in your work. You can run _R_
codes in the Jupyter/IPython notebooks, with the immediate benefit of
visualizing your data. You can also use compiled languages like C++,
@@ -59,17 +58,23 @@ a Jupyter notebook.
If you have Python installed (we recommend Python3) and you feel
pretty familiar with installing different packages, we recommend that
you install the following Python packages via _pip_ as o pip install
numpy scipy matplotlib ipython scikit-learn mglearn sympy pandas
pillow For Python3, replace _pip_ with _pip3_.
you install the following Python packages via _pip_ as
For OSX users we recommend also, after having installed Xcode, to
o pip install numpy scipy matplotlib ipython scikit-learn mglearn sympy pandas pillow
For Python3, replace _pip_ with _pip3_.
For OSX users we recommend, after having installed Xcode, to
install _brew_. Brew allows for a seamless installation of additional
software via for example o brew install python3
software via for example
o brew install python3
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution,
you can use _pip_ as well and simply install Python as
o sudo apt-get install python3 (or python for pyhton2.7)
etc etc.
!split
@@ -78,13 +83,19 @@ etc etc.
If you don't want to perform these operations separately and venture
into the hassle of exploring how to set up dependencies and paths, we
recommend two widely used distrubutions which set up all relevant
dependencies for Python, namely o
"Anaconda":"https://docs.anaconda.com/", which is an open source
dependencies for Python, namely
o "Anaconda":"https://docs.anaconda.com/",
which is an open source
distribution of the Python and R programming languages for large-scale
data processing, predictive analytics, and scientific computing, that
aims to simplify package management and deployment. Package versions
are managed by the package management system _conda_ o "Enthought
canopy":"https://www.enthought.com/product/canopy/" is a Python
are managed by the package management system _conda_.
o "Enthoughtmcanopy":"https://www.enthought.com/product/canopy/"
is a Python
distribution for scientific and analytic computing distribution and
analysis environment, available for free and under a commercial
license.
@@ -104,8 +115,8 @@ browser. The software library _R_ is tuned to statistically analysis
and allows for an easy usage of the tools we will discuss in these
texts.
To install _R_ with Jupyter notebook "following the link
here":"https://mpacer.org/maths/r-kernel-for-ipython-notebook"
To install _R_ with Jupyter notebook
"follow the link here":"https://mpacer.org/maths/r-kernel-for-ipython-notebook"
@@ -129,16 +140,20 @@ versatility, including symbolic operations, Python offers a unique
computational environment. Your Jupyter/IPython notebook can easily be
converted into a nicely rendered _PDF_ file or a Latex file for
further processing. For example, convert to latex as
!bc
pycod jupyter nbconvert filename.ipynb --to latex
!ec
And to add more versatility, symbolic Python package "SymPy":"http://www.sympy.org/en/index.html" is Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python.
Finally, if you wish to use the light mark-up language "doconce":"https://github.com/hplgit/doconce" you can convert a standard ascii text file into various HTML
And to add more versatility, the Python package "SymPy":"http://www.sympy.org/en/index.html" is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python.
Finally, if you wish to use the light mark-up language
"doconce":"https://github.com/hplgit/doconce" you can convert a standard ascii text file into various HTML
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
!split
===== Simple linear regression model using _scikit-learn_ =====
We start with perhaps our simplest possible example, using _scikit-learn_ to perform linear regression analysis on a data set produced by us.
What follows is a simple Python code where we have defined function $y$ in terms of the variable $x$. Both are defined as vectors of dimension $1\times 100$. The entries to the vector $\hat{x}$ are given by random numbers generated with a uniform distribution with entries $x_i \in [0,1]$ (more about probability distribution functions later). These values are then used to define a function $y(x)$ (tabulated again as a vector) with a linear dependence on $x$ plus a random noise added via the normal distribution.
@@ -176,7 +191,7 @@ this example we plot our original values of $x$ and $y$ as well as the
prediction _ypredict_ ($\tilde{y}$), which attempts at fitting our
data with a straight line.
The Python follows here.
The Python code follows here.
!bc pycod
# Importing various packages
import numpy as np
@@ -251,10 +266,11 @@ employed are various variants of _gradient_ methods. These will be
discussed in more detail later. Again, you'll be surprised to hear that
many practitioners minimize the above function ''by the eye', popularly dubbed as
'chi by the eye'. That is, change a parameter and see (visually and numerically) that
the your $\chi^2$ function becomes smaller.
the $\chi^2$ function becomes smaller.
There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define
the relative error as
!bt
\[
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
@@ -315,28 +331,15 @@ We will come to the definition of these outputs later.
Another useful Python package is
"pandas":"https://pandas.pydata.org/", which is an open source library
providing high-performance, easy-to-use data structures and data
analysis tools for Python. The following simple example shows an example on how we can, in an easy way make tables of our data. Here we define a data set which includes names, city of residence and age, and displays the data in an easy to read way. We will see repeated use of _pandas_, in particular in connection with classification of data.
analysis tools for Python. The following simple example shows how we can, in an easy way make tables of our data. Here we define a data set which includes names, city of residence and age, and displays the data in an easy to read way. We will see repeated use of _pandas_, in particular in connection with classification of data.
!bblock
!bc pycod
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
import pandas as pd
from IPython.display import display
eye = np.eye(4)
print(eye)
sparse_mtx = sparse.csr_matrix(eye)
print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
plt.show()
data = {'Name': ["John", "Anna", "Peter", "Linda"], 'Location': ["Nairobi", "Napoli", "London", "Buenos Aires"], 'Age':[51, 21, 34, 45]}
data_pandas = pd.DataFrame(data)
display(data_pandas)
!ec
!eblock
+17
View File
@@ -291,6 +291,23 @@ print(Eigvals)
!ec
!bc pycod
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
eye = np.eye(4)
print(eye)
sparse_mtx = sparse.csr_matrix(eye)
print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
plt.show()
!ec
!split
===== Matrix Handling in C/C++, Static and Dynamical allocation =====