diff --git a/doc/pub/week34/html/week34-bs.html b/doc/pub/week34/html/week34-bs.html index e67babf57..6a5add6b4 100644 --- a/doc/pub/week34/html/week34-bs.html +++ b/doc/pub/week34/html/week34-bs.html @@ -1,11 +1,11 @@
- + @@ -41,67 +41,94 @@ Automatically generated HTML file from DocOnce source @@ -139,43 +166,43 @@ MathJax.Hub.Config({-
@@ -246,13 +273,13 @@ MathJax.Hub.Config({
-
Video of Lecture.
@@ -187,14 +187,14 @@ MathJax.Hub.Config({
Basic knowledge in programming and mathematics, with an emphasis on
@@ -320,7 +317,7 @@ Python is the recurring programming language.
The course has two central parts
@@ -360,7 +357,7 @@ These topics will be scattered thorughout the course and may not necessarily be
The following topics will be covered
@@ -378,7 +375,7 @@ The following topics will be covered
The link here https://www.mn.uio.no/english/research/about/centre-focus/innovation/data-science/studies/ gives an excellent overview of courses on Machine learning at UiO.
@@ -443,7 +440,7 @@ The link here Introduction
+
Our emphasis throughout this series of lectures
@@ -485,7 +482,7 @@ get started with programming.
Statistics, data science and machine learning form important fields of
@@ -558,7 +555,7 @@ of algorithms and methods we will discuss.
The approaches to machine learning are many, but are often split into
@@ -600,7 +597,7 @@ At the heart of basically all ML algorithms there are so-called minimization alg
We will make extensive use of Python as programming language and its
@@ -646,7 +643,7 @@ etc etc.
If you don't want to perform these operations separately and venture
@@ -682,7 +679,7 @@ no setup and runs entirely in the cloud. Try it out!
You will also find it convenient to utilize R. We will mainly
@@ -723,7 +720,7 @@ To install R with Jupyter notebook
For the C++ aficionados, Jupyter/IPython notebook allows you also to
@@ -747,7 +744,7 @@ further processing. For example, convert to latex as
-
And to add more versatility, the Python package SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python.
@@ -760,7 +757,7 @@ formats, ipython notebooks, latex files, pdf files etc with minimal edits. These
There are several central software libraries for linear algebra and eigenvalue problems. Several of the more
@@ -780,11 +777,11 @@ developed in the 1970s, namely EISPACK and LINPACK. We describe them shortly he
For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent
@@ -885,22 +882,22 @@ For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all
-
Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution,
-
We defined a vector \( x \) with \( n=10 \) elements with its values given by the Normal distribution \( N(0,1) \).
@@ -908,9 +905,9 @@ Another alternative is to declare a vector as follows
-
Here we have defined a vector with three elements, with \( x_0=1 \), \( x_1=2 \) and \( x_2=3 \). Note that both Python and C++
@@ -918,9 +915,9 @@ start numbering array elements from \( 0 \) and on. This means that a vector wit
-
In the last example we used Numpy's unary function \( np.log \). This function is
@@ -934,12 +931,12 @@ logarithms of a vector would be to write
-
We note that our code is much longer already and we need to import the log function from the math module.
@@ -947,33 +944,33 @@ The attentive reader will also notice that the output is \( [1, 1, 2] \). Python
-
or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is
-
To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the itemsize functionality (the array \( x \) is actually an object which inherits the functionalities defined in Numpy) as
-
Having defined vectors, we are now ready to try out matrices. We can
@@ -983,62 +980,62 @@ lowercase letters for vectors and uppercase letters for matrices)
-
If we use the shape function we would get \( (3, 3) \) as output, that is verifying that our matrix is a \( 3\times 3 \) matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row-major order, see below) as
-
We can continue this was by printing out other columns or rows. The example here prints out the second column
-
Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the Numpy website for more details. Useful functions when defining a matrix are the np.zeros function which declares a matrix of a given dimension and sets all elements to zero
-
or initializing all elements to
-
or as unitarily distributed random numbers (see the material on random number generators in the statistics part)
-
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
@@ -1084,32 +1081,32 @@ covariance matrix through the np.linalg.eig() function.
-
-
-
-
@@ -1164,14 +1161,14 @@ Thereafter we display the content of the row which begins with the index Arag
-
We can easily append data to this, for example
-
-
@@ -1204,13 +1201,13 @@ Thereafter we can select specific columns only and plot final results
-
-
and many other operations.
@@ -1247,7 +1244,7 @@ For multidimensional arrays, we recommend strongly Friday August 21
+
Video of Lecture and Handwritten notes
@@ -1255,7 +1252,7 @@ For multidimensional arrays, we recommend strongly Reading Data and fitting
+
In order to study various Machine Learning algorithms, we need to
@@ -1294,13 +1291,13 @@ But before we really start with nuclear physics data, let's just look at some si
We start with perhaps our simplest possible example, using Scikit-Learn to perform linear regression analysis on a data set produced by us.
@@ -1356,7 +1353,7 @@ The Python code follows here.
-
-
-
-
Let us now dive into nuclear physics and remind ourselves briefly about some basic features about binding
@@ -1732,7 +1729,7 @@ We could also add a so-called pairing term, which is a correction term that
arises from the tendency of proton pairs and neutron pairs to
occur. An even number of particles is more stable than an odd number.
-
Let us start with reading and organizing our data.
@@ -1744,7 +1741,7 @@ We start with preparing folders for storing our calculations and the data file o
-
-
-
-
-
The next step, and we will define this mathematically later, is to set up the so-called design matrix. We will throughout call this matrix \( \boldsymbol{X} \).
@@ -1874,7 +1871,7 @@ It has dimensionality \( p\times n \), where \( n \) is the number of data point
-
-
@@ -1896,13 +1893,13 @@ Now we can print measures of how our fit is doing, the coefficients from the fit
-
As a teaser, let us now see how we can do this with decision trees using scikit-learn. Later we will switch to so-called random forests!
@@ -1926,7 +1923,7 @@ As a teaser, let us now see how we can do this with decision trees using scik
-
-
The aim behind these introductory words was to present to you various
diff --git a/doc/pub/week34/html/week34-solarized.html b/doc/pub/week34/html/week34-solarized.html
index 836ca766c..9761d1a77 100644
--- a/doc/pub/week34/html/week34-solarized.html
+++ b/doc/pub/week34/html/week34-solarized.html
@@ -1,19 +1,19 @@
-
Video of Lecture.
@@ -195,7 +222,7 @@ MathJax.Hub.Config({
Basic knowledge in programming and mathematics, with an emphasis on
@@ -325,7 +348,7 @@ Python is the recurring programming language.
The course has two central parts
@@ -367,7 +390,7 @@ These topics will be scattered thorughout the course and may not necessarily be
@@ -388,7 +411,7 @@ The following topics will be covered
The link here https://www.mn.uio.no/english/research/about/centre-focus/innovation/data-science/studies/ gives an excellent overview of courses on Machine learning at UiO.
@@ -453,7 +476,7 @@ The link here Introduction
+
Our emphasis throughout this series of lectures
@@ -495,7 +518,7 @@ get started with programming.
Statistics, data science and machine learning form important fields of
@@ -568,7 +591,7 @@ of algorithms and methods we will discuss.
The approaches to machine learning are many, but are often split into
@@ -606,7 +629,7 @@ At the heart of basically all ML algorithms there are so-called minimization alg
We will make extensive use of Python as programming language and its
@@ -649,7 +672,7 @@ etc etc.
If you don't want to perform these operations separately and venture
@@ -683,7 +706,7 @@ no setup and runs entirely in the cloud. Try it out!
You will also find it convenient to utilize R. We will mainly
@@ -723,7 +746,7 @@ To install R with Jupyter notebook
For the C++ aficionados, Jupyter/IPython notebook allows you also to
@@ -747,7 +770,7 @@ further processing. For example, convert to latex as
-
And to add more versatility, the Python package SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python.
@@ -760,7 +783,7 @@ formats, ipython notebooks, latex files, pdf files etc with minimal edits. These
There are several central software libraries for linear algebra and eigenvalue problems. Several of the more
@@ -776,11 +799,11 @@ developed in the 1970s, namely EISPACK and LINPACK. We describe them shortly he
$$
\mathbf{A} =
@@ -824,7 +847,7 @@ $$
For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all equivalent
@@ -862,22 +885,22 @@ For an \( N\times N \) matrix \( \mathbf{A} \) the following properties are all
-
Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution,
-
We defined a vector \( x \) with \( n=10 \) elements with its values given by the Normal distribution \( N(0,1) \).
@@ -885,9 +908,9 @@ Another alternative is to declare a vector as follows
-
Here we have defined a vector with three elements, with \( x_0=1 \), \( x_1=2 \) and \( x_2=3 \). Note that both Python and C++
@@ -895,9 +918,9 @@ start numbering array elements from \( 0 \) and on. This means that a vector wit
-
In the last example we used Numpy's unary function \( np.log \). This function is
@@ -911,12 +934,12 @@ logarithms of a vector would be to write
-
We note that our code is much longer already and we need to import the log function from the math module.
@@ -924,32 +947,32 @@ The attentive reader will also notice that the output is \( [1, 1, 2] \). Python
-
or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is
-
To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the itemsize functionality (the array \( x \) is actually an object which inherits the functionalities defined in Numpy) as
-
Having defined vectors, we are now ready to try out matrices. We can
@@ -959,62 +982,62 @@ lowercase letters for vectors and uppercase letters for matrices)
-
If we use the shape function we would get \( (3, 3) \) as output, that is verifying that our matrix is a \( 3\times 3 \) matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row-major order, see below) as
-
We can continue this was by printing out other columns or rows. The example here prints out the second column
-
Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the Numpy website for more details. Useful functions when defining a matrix are the np.zeros function which declares a matrix of a given dimension and sets all elements to zero
-
or initializing all elements to
-
or as unitarily distributed random numbers (see the material on random number generators in the statistics part)
-
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
@@ -1054,32 +1077,32 @@ covariance matrix through the np.linalg.eig() function.
-
-
-
-
@@ -1133,14 +1156,14 @@ Thereafter we display the content of the row which begins with the index Arag
-
We can easily append data to this, for example
-
-
@@ -1173,13 +1196,13 @@ Thereafter we can select specific columns only and plot final results
-
-
and many other operations.
@@ -1216,7 +1239,7 @@ For multidimensional arrays, we recommend strongly Friday August 21
+
Video of Lecture and Handwritten notes
@@ -1224,7 +1247,7 @@ For multidimensional arrays, we recommend strongly Reading Data and fitting
+
In order to study various Machine Learning algorithms, we need to
@@ -1263,12 +1286,12 @@ But before we really start with nuclear physics data, let's just look at some si
We start with perhaps our simplest possible example, using Scikit-Learn to perform linear regression analysis on a data set produced by us.
@@ -1322,7 +1345,7 @@ The Python code follows here.
-
-
-
-
Let us now dive into nuclear physics and remind ourselves briefly about some basic features about binding
@@ -1664,7 +1687,7 @@ We could also add a so-called pairing term, which is a correction term that
arises from the tendency of proton pairs and neutron pairs to
occur. An even number of particles is more stable than an odd number.
-
Let us start with reading and organizing our data.
@@ -1676,7 +1699,7 @@ We start with preparing folders for storing our calculations and the data file o
-
-
-Lectures and ComputerLab
+Lectures and ComputerLab
-
Course Format
+Course Format
Teachers
+Teachers
Deadlines for projects (tentative)
+Deadlines for projects (tentative)
Recommended textbooks
+Recommended textbooks
Prerequisites
+Prerequisites
Learning outcomes
+Learning outcomes
Topics covered in this course: Statistical analysis and optimization of data
+Topics covered in this course: Statistical analysis and optimization of data
Topics covered in this course: Machine Learning
+Topics covered in this course: Machine Learning
Extremely useful tools, strongly recommended
+Extremely useful tools, strongly recommended
Other courses on Data science and Machine Learning at UiO
+Other courses on Data science and Machine Learning at UiO
Introduction
What is Machine Learning?
+What is Machine Learning?
Types of Machine Learning
+Types of Machine Learning
Software and needed installations
+Software and needed installations
Python installers
+Python installers
Useful Python libraries
+Useful Python libraries
Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there)
@@ -702,7 +699,7 @@ Here we list several useful Python libraries we strongly recommend (if you use a
Installing R, C++, cython or Julia
+Installing R, C++, cython or Julia
Installing R, C++, cython, Numba etc
+Installing R, C++, cython, Numba etc
pycod jupyter nbconvert filename.ipynb --to latex
+
pycod jupyter nbconvert filename.ipynb --to latex
Numpy examples and Important Matrix and vector handling packages
+Numpy examples and Important Matrix and vector handling packages
Basic Matrix Features
+Basic Matrix Features
$$
\mathbf{A} =
@@ -831,7 +828,7 @@ $$
Some famous Matrices
+Some famous Matrices
@@ -858,11 +855,11 @@ $$
More Basic Matrix Features
+More Basic Matrix Features
Numpy and arrays
+Numpy and arrays
Numpy provides an easy way to handle arrays in Python. The standard way to import this library is as
import numpy as np
+
import numpy as np
n = 10
+
n = 10
x = np.random.normal(size=n)
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.array([1, 2, 3])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4, 7, 8]))
-print(x)
+print(x)
import numpy as np
+
import numpy as np
from math import log
x = np.array([4, 7, 8])
for i in range(0, len(x)):
x[i] = log(x[i])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
-print(x.itemsize)
+print(x.itemsize)
Matrices in Python
+Matrices in Python
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
-print(A)
+print(A)
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
-print(A[:,0])
+print(A[:,0])
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
-print(A[1,:])
+print(A[1,:])
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to zero
A = np.zeros( (n, n) )
-print(A)
+print(A)
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to one
A = np.ones( (n, n) )
-print(A)
+print(A)
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]
A = np.random.rand(n, n)
-print(A)
+print(A)
# Importing various packages
+
# Importing various packages
import numpy as np
n = 100
x = np.random.normal(size=n)
-print(np.mean(x))
+print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
-print(np.mean(y))
+print(np.mean(y))
z = x**3+np.random.normal(size=n)
-print(np.mean(z))
+print(np.mean(z))
W = np.vstack((x, y, z))
Sigma = np.cov(W)
-print(Sigma)
+print(Sigma)
Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+print(Eigvals)
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
eye = np.eye(4)
-print(eye)
+print(eye)
sparse_mtx = sparse.csr_matrix(eye)
-print(sparse_mtx)
+print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
@@ -1119,7 +1116,7 @@ plt.show()
Meet the Pandas
+Meet the Pandas

@@ -1138,7 +1135,7 @@ The following simple example shows how we can, in an easy way make tables of our
import pandas as pd
+
import pandas as pd
from IPython.display import display
data = {'First Name': ["Frodo", "Bilbo", "Aragorn II", "Samwise"],
'Last Name': ["Baggins", "Baggins","Elessar","Gamgee"],
@@ -1156,7 +1153,7 @@ Displaying these results, we see that the indices are given by the default numbe
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
+
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
display(data_pandas)
display(data_pandas.loc['Aragorn'])
+
display(data_pandas.loc['Aragorn'])
new_hobbit = {'First Name': ["Peregrin"],
+new_hobbit = {'First Name': ["Peregrin"],
'Last Name': ["Took"],
'Place of birth': ["Shire"],
'Date of Birth T.A.': [2990]
@@ -1185,7 +1182,7 @@ of dimensionality \( 10\times 5 \) and compute the mean value and standard devia
import numpy as np
+
import numpy as np
import pandas as pd
from IPython.display import display
np.random.seed(100)
@@ -1195,8 +1192,8 @@ cols = 5
a = np.random.randn(rows,cols)
df = pd.DataFrame(a)
display(df)
-print(df.mean())
-print(df.std())
+print(df.mean())
+print(df.std())
display(df**2)
df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
+
df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
df.index = np.arange(10)
display(df)
-print(df['Second'].mean() )
-print(df.info())
-print(df.describe())
+print(df['Second'].mean() )
+print(df.info())
+print(df.describe())
from pylab import plt, mpl
plt.style.use('seaborn')
@@ -1228,10 +1225,10 @@ We can produce a \( 4\times 4 \) matrix
b = np.arange(16).reshape((4,4))
-print(b)
+
b = np.arange(16).reshape((4,4))
+print(b)
df1 = pd.DataFrame(b)
-print(df1)
+print(df1)
Friday August 21
Reading Data and fitting
Friday August 21
+Friday August 21
Simple linear regression model using scikit-learn
+Simple linear regression model using scikit-learn
# Importing various packages
+
# Importing various packages
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
@@ -1458,7 +1455,7 @@ We can modify easily the above Python code and plot the relative error instead
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
@@ -1493,7 +1490,7 @@ example of the functionality of Scikit-Learn.
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
@@ -1503,16 +1500,16 @@ y = 2.0+ 5print('The intercept alpha: \n', linreg.intercept_)
-print('Coefficient beta : \n', linreg.coef_)
+print('The intercept alpha: \n', linreg.intercept_)
+print('Coefficient beta : \n', linreg.coef_)
# The mean squared error
-print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
+print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
# Explained variance score: 1 is perfect prediction
-print('Variance score: %.2f' % r2_score(y, ypredict))
+print('Variance score: %.2f' % r2_score(y, ypredict))
# Mean squared log error
-print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
+print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
# Mean absolute error
-print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
+print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
plt.plot(x, ypredict, "r-")
plt.plot(x, y ,'ro')
plt.axis([0.0,1.0,1.5, 7.0])
@@ -1606,7 +1603,7 @@ a linear \( x \)-dependence we study now a cubic polynomial and use the polynomi
import matplotlib.pyplot as plt
+
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn.linear_model import Ridge
@@ -1635,10 +1632,10 @@ plt.show()
err=(y-yn)/yn
return abs(np.sum(err))/len(err)
-print (error(y))
+print (error(y))
To our real data: nuclear binding energies. Brief reminder on masses and binding energies
+To our real data: nuclear binding energies. Brief reminder on masses and binding energies
Organizing our data
+Organizing our data
# Common imports
+
# Common imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
@@ -1774,7 +1771,7 @@ DATA_ID = "DataFiles/"
return os.path.join(DATA_ID, dat_id)
def save_fig(fig_id):
- plt.savefig(image_path(fig_id) + ".png", format='png')
+ plt.savefig(image_path(fig_id) + ".png", format='png')
infile = open(data_path("MassEval2016.dat"),'r')
from pylab import plt, mpl
+
from pylab import plt, mpl
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
@@ -1807,7 +1804,7 @@ In particular, the program that outputs the final nuclear masses is written in F
"""
+
"""
This is taken from the data file of the mass 2016 evaluation.
All files are 3436 lines long with 124 character per line.
Headers are 39 lines long.
@@ -1827,12 +1824,12 @@ covert them into the pandas DataFrame structure.
# Read the experimental data with Pandas
+
# Read the experimental data with Pandas
Masses = pd.read_fwf(infile, usecols=(2,3,4,6,11),
names=('N', 'Z', 'A', 'Element', 'Ebinding'),
widths=(1,3,5,5,5,1,3,4,1,13,11,11,9,1,2,11,9,1,3,1,12,11,1),
header=39,
- index_col=False)
+ index_col=False)
# Extrapolated values are indicated by '#' in place of the decimal place, so
# the Ebinding column won't be numeric. Coerce to float and drop these entries.
@@ -1861,12 +1858,12 @@ the number of nucleons \( A \), the number of protons \( Z \) and the number of
A = Masses['A']
+
A = Masses['A']
Z = Masses['Z']
N = Masses['N']
Element = Masses['Element']
Energies = Masses['Ebinding']
-print(Masses)
+print(Masses)
# Now we set up the design matrix X
+
# Now we set up the design matrix X
X = np.zeros((len(A),5))
X[:,0] = 1
X[:,1] = A
@@ -1887,7 +1884,7 @@ With scikitlearn we are now ready to use linear regression and fit our da
clf = skl.LinearRegression().fit(X, Energies)
+
clf = skl.LinearRegression().fit(X, Energies)
fity = clf.predict(X)
# The mean squared error
-print("Mean squared error: %.2f" % mean_squared_error(Energies, fity))
+
# The mean squared error
+print("Mean squared error: %.2f" % mean_squared_error(Energies, fity))
# Explained variance score: 1 is perfect prediction
-print('Variance score: %.2f' % r2_score(Energies, fity))
+print('Variance score: %.2f' % r2_score(Energies, fity))
# Mean absolute error
-print('Mean absolute error: %.2f' % mean_absolute_error(Energies, fity))
-print(clf.coef_, clf.intercept_)
+print('Mean absolute error: %.2f' % mean_absolute_error(Energies, fity))
+print(clf.coef_, clf.intercept_)
Masses['Eapprox'] = fity
# Generate a plot comparing the experimental with the fitted values values.
@@ -1918,7 +1915,7 @@ save_fig("Masses2016")
plt.show()
Seeing the wood for the trees
+Seeing the wood for the trees
#Decision Tree Regression
+
#Decision Tree Regression
from sklearn.tree import DecisionTreeRegressor
regr_1=DecisionTreeRegressor(max_depth=5)
regr_2=DecisionTreeRegressor(max_depth=7)
@@ -1953,18 +1950,18 @@ plt.title("Decision Tree Regression""Masses2016Trees")
plt.show()
-print(Masses)
-print(np.mean( (Energies-y_1)**2))
+print(Masses)
+print(np.mean( (Energies-y_1)**2))
And what about using neural networks?
+And what about using neural networks?
The seaborn package allows us to visualize data in an efficient way. Note that we use scikit-learn's multi-layer perceptron (or feed forward neural network)
functionality.
from sklearn.neural_network import MLPRegressor
+
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import accuracy_score
import seaborn as sns
@@ -1988,14 +1985,14 @@ sns.set()
train_accuracy[i][j] = dnn.score(X_train, Y_train)
fig, ax = plt.subplots(figsize = (10, 10))
-sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
+sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
ax.set_title("Training Accuracy")
ax.set_ylabel("$\eta$")
ax.set_xlabel("$\lambda$")
plt.show()
A first summary
+A first summary
Sep 16, 2020
Aug 8, 2021
-Overview of first week
+Overview of first week
-
-Thursday August 20
+Thursday August 26
-Lectures and ComputerLab
+Lectures and ComputerLab
-
-Course Format
+Course Format
-Teachers
+Teachers
-Deadlines for projects (tentative)
+Deadlines for projects (tentative)
-Recommended textbooks
+Recommended textbooks
-Prerequisites
+Prerequisites
-Learning outcomes
+Learning outcomes
-Topics covered in this course: Statistical analysis and optimization of data
+Topics covered in this course: Statistical analysis and optimization of data
-Topics covered in this course: Machine Learning
+Topics covered in this course: Machine Learning
-Extremely useful tools, strongly recommended
+Extremely useful tools, strongly recommended
@@ -432,7 +455,7 @@ Hands-on demonstrations, exercises and projects aim at deepening your understand
-Other courses on Data science and Machine Learning at UiO
+Other courses on Data science and Machine Learning at UiO
Introduction
-What is Machine Learning?
+What is Machine Learning?
-Types of Machine Learning
+Types of Machine Learning
-Software and needed installations
+Software and needed installations
-Python installers
+Python installers
-Useful Python libraries
+Useful Python libraries
Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there)
@@ -702,7 +725,7 @@ Here we list several useful Python libraries we strongly recommend (if you use a
-Installing R, C++, cython or Julia
+Installing R, C++, cython or Julia
-Installing R, C++, cython, Numba etc
+Installing R, C++, cython, Numba etc
pycod jupyter nbconvert filename.ipynb --to latex
+
pycod jupyter nbconvert filename.ipynb --to latex
-Numpy examples and Important Matrix and vector handling packages
+Numpy examples and Important Matrix and vector handling packages
-Basic Matrix Features
+Basic Matrix Features
-Some famous Matrices
+Some famous Matrices
-More Basic Matrix Features
+More Basic Matrix Features
-Numpy and arrays
+Numpy and arrays
Numpy provides an easy way to handle arrays in Python. The standard way to import this library is as
import numpy as np
+
import numpy as np
n = 10
+
n = 10
x = np.random.normal(size=n)
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.array([1, 2, 3])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4, 7, 8]))
-print(x)
+print(x)
import numpy as np
+
import numpy as np
from math import log
x = np.array([4, 7, 8])
for i in range(0, len(x)):
x[i] = log(x[i])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
-print(x)
+print(x)
import numpy as np
+
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
-print(x.itemsize)
+print(x.itemsize)
-Matrices in Python
+Matrices in Python
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
-print(A)
+print(A)
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
-print(A[:,0])
+print(A[:,0])
import numpy as np
+
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
-print(A[1,:])
+print(A[1,:])
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to zero
A = np.zeros( (n, n) )
-print(A)
+print(A)
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to one
A = np.ones( (n, n) )
-print(A)
+print(A)
import numpy as np
+
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]
A = np.random.rand(n, n)
-print(A)
+print(A)
# Importing various packages
+
# Importing various packages
import numpy as np
n = 100
x = np.random.normal(size=n)
-print(np.mean(x))
+print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
-print(np.mean(y))
+print(np.mean(y))
z = x**3+np.random.normal(size=n)
-print(np.mean(z))
+print(np.mean(z))
W = np.vstack((x, y, z))
Sigma = np.cov(W)
-print(Sigma)
+print(Sigma)
Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+print(Eigvals)
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
eye = np.eye(4)
-print(eye)
+print(eye)
sparse_mtx = sparse.csr_matrix(eye)
-print(sparse_mtx)
+print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
@@ -1088,7 +1111,7 @@ plt.show()
-Meet the Pandas
+Meet the Pandas

@@ -1107,7 +1130,7 @@ The following simple example shows how we can, in an easy way make tables of our
import pandas as pd
+
import pandas as pd
from IPython.display import display
data = {'First Name': ["Frodo", "Bilbo", "Aragorn II", "Samwise"],
'Last Name': ["Baggins", "Baggins","Elessar","Gamgee"],
@@ -1125,7 +1148,7 @@ Displaying these results, we see that the indices are given by the default numbe
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
+
data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
display(data_pandas)
display(data_pandas.loc['Aragorn'])
+
display(data_pandas.loc['Aragorn'])
new_hobbit = {'First Name': ["Peregrin"],
+new_hobbit = {'First Name': ["Peregrin"],
'Last Name': ["Took"],
'Place of birth': ["Shire"],
'Date of Birth T.A.': [2990]
@@ -1154,7 +1177,7 @@ of dimensionality \( 10\times 5 \) and compute the mean value and standard devia
import numpy as np
+
import numpy as np
import pandas as pd
from IPython.display import display
np.random.seed(100)
@@ -1164,8 +1187,8 @@ cols = 5
a = np.random.randn(rows,cols)
df = pd.DataFrame(a)
display(df)
-print(df.mean())
-print(df.std())
+print(df.mean())
+print(df.std())
display(df**2)
df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
+
df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
df.index = np.arange(10)
display(df)
-print(df['Second'].mean() )
-print(df.info())
-print(df.describe())
+print(df['Second'].mean() )
+print(df.info())
+print(df.describe())
from pylab import plt, mpl
plt.style.use('seaborn')
@@ -1197,10 +1220,10 @@ We can produce a \( 4\times 4 \) matrix
b = np.arange(16).reshape((4,4))
-print(b)
+
b = np.arange(16).reshape((4,4))
+print(b)
df1 = pd.DataFrame(b)
-print(df1)
+print(df1)
Friday August 21
Reading Data and fitting
-Friday August 21
+Friday August 21
-Simple linear regression model using scikit-learn
+Simple linear regression model using scikit-learn
# Importing various packages
+
# Importing various packages
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
@@ -1418,7 +1441,7 @@ We can modify easily the above Python code and plot the relative error instead
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
@@ -1453,7 +1476,7 @@ example of the functionality of Scikit-Learn.
import numpy as np
+
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
@@ -1463,16 +1486,16 @@ y = 2.0+ 5print('The intercept alpha: \n', linreg.intercept_)
-print('Coefficient beta : \n', linreg.coef_)
+print('The intercept alpha: \n', linreg.intercept_)
+print('Coefficient beta : \n', linreg.coef_)
# The mean squared error
-print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
+print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
# Explained variance score: 1 is perfect prediction
-print('Variance score: %.2f' % r2_score(y, ypredict))
+print('Variance score: %.2f' % r2_score(y, ypredict))
# Mean squared log error
-print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
+print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
# Mean absolute error
-print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
+print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
plt.plot(x, ypredict, "r-")
plt.plot(x, y ,'ro')
plt.axis([0.0,1.0,1.5, 7.0])
@@ -1554,7 +1577,7 @@ a linear \( x \)-dependence we study now a cubic polynomial and use the polynomi
import matplotlib.pyplot as plt
+
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn.linear_model import Ridge
@@ -1583,10 +1606,10 @@ plt.show()
err=(y-yn)/yn
return abs(np.sum(err))/len(err)
-print (error(y))
+print (error(y))
To our real data: nuclear binding energies. Brief reminder on masses and binding energies
+To our real data: nuclear binding energies. Brief reminder on masses and binding energies
Organizing our data
+Organizing our data
# Common imports
+
# Common imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
@@ -1706,7 +1729,7 @@ DATA_ID = "DataFiles/"
return os.path.join(DATA_ID, dat_id)
def save_fig(fig_id):
- plt.savefig(image_path(fig_id) + ".png", format='png')
+ plt.savefig(image_path(fig_id) + ".png", format='png')
infile = open(data_path("MassEval2016.dat"),'r')
from pylab import plt, mpl
+
from pylab import plt, mpl
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
@@ -1739,7 +1762,7 @@ In particular, the program that outputs the final nuclear masses is written in F