update
This commit is contained in:
@@ -140,7 +140,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Dec 25, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Aug 19, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
|
||||
<h2 id="___sec0">Introduction </h2>
|
||||
@@ -534,7 +534,7 @@ Here follows a simple example where we set up an array of ten elements, all dete
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
We defined a vector \( x \) with \( n=10 \) elements with its values given by the Normal distribution \( N(0,1) \).
|
||||
@@ -544,7 +544,7 @@ Another alternative is to declare a vector as follows
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">1</span>, <span style="color: #666666">2</span>, <span style="color: #666666">3</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
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++
|
||||
@@ -554,7 +554,7 @@ start numbering array elements from \( 0 \) and on. This means that a vector wit
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([<span style="color: #666666">4</span>, <span style="color: #666666">7</span>, <span style="color: #666666">8</span>]))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the last example we used Numpy's unary function \( np.log \). This function is
|
||||
@@ -573,7 +573,7 @@ logarithms of a vector would be to write
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">4</span>, <span style="color: #666666">7</span>, <span style="color: #666666">8</span>])
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>, <span style="color: #008000">len</span>(x)):
|
||||
x[i] <span style="color: #666666">=</span> log(x[i])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note that our code is much longer already and we need to import the <b>log</b> function from the <b>math</b> module.
|
||||
@@ -583,7 +583,7 @@ The attentive reader will also notice that the output is \( [1, 1, 2] \). Python
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([<span style="color: #666666">4</span>, <span style="color: #666666">7</span>, <span style="color: #666666">8</span>], dtype <span style="color: #666666">=</span> np<span style="color: #666666">.</span>float64))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is
|
||||
@@ -592,7 +592,7 @@ or simply write them as double precision numbers (Python uses 64 bits as default
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x)
|
||||
<span style="color: #008000">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the <b>itemsize</b> functionality (the array \( x \) is actually an object which inherits the functionalities defined in Numpy) as
|
||||
@@ -601,7 +601,7 @@ To check the number of bytes (remember that one byte contains eight bits for dou
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>])
|
||||
<span style="color: #008000; font-weight: bold">print</span>(x<span style="color: #666666">.</span>itemsize)
|
||||
<span style="color: #008000">print</span>(x<span style="color: #666666">.</span>itemsize)
|
||||
</pre></div>
|
||||
|
||||
<h2 id="___sec13">Matrices in Python </h2>
|
||||
@@ -616,7 +616,7 @@ lowercase letters for vectors and uppercase letters for matrices)
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A)
|
||||
<span style="color: #008000">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
If we use the <b>shape</b> 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
|
||||
@@ -626,7 +626,7 @@ If we use the <b>shape</b> function we would get \( (3, 3) \) as output, that is
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
||||
<span style="color: #408080; font-style: italic"># print the first column, row-major order and elements start with 0</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A[:,<span style="color: #666666">0</span>])
|
||||
<span style="color: #008000">print</span>(A[:,<span style="color: #666666">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
We can continue this was by printing out other columns or rows. The example here prints out the second column
|
||||
@@ -636,7 +636,7 @@ We can continue this was by printing out other columns or rows. The example here
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
||||
<span style="color: #408080; font-style: italic"># print the first column, row-major order and elements start with 0</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A[<span style="color: #666666">1</span>,:])
|
||||
<span style="color: #008000">print</span>(A[<span style="color: #666666">1</span>,:])
|
||||
</pre></div>
|
||||
<p>
|
||||
Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the <a href="http://www.numpy.org/" target="_blank">Numpy website for more details</a>. Useful functions when defining a matrix are the <b>np.zeros</b> function which declares a matrix of a given dimension and sets all elements to zero
|
||||
@@ -647,7 +647,7 @@ Numpy contains many other functionalities that allow us to slice, subdivide etc
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to zero</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros( (n, n) )
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A)
|
||||
<span style="color: #008000">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
or initializing all elements to
|
||||
@@ -658,7 +658,7 @@ or initializing all elements to
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to one</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones( (n, n) )
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A)
|
||||
<span style="color: #008000">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
or as unitarily distributed random numbers (see the material on random number generators in the statistics part)
|
||||
@@ -669,7 +669,7 @@ or as unitarily distributed random numbers (see the material on random number ge
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]</span>
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n, n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(A)
|
||||
<span style="color: #008000">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
@@ -714,16 +714,16 @@ covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
z <span style="color: #666666">=</span> x<span style="color: #666666">**3+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(z))
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(z))
|
||||
W <span style="color: #666666">=</span> np<span style="color: #666666">.</span>vstack((x, y, z))
|
||||
Sigma <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cov(W)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Sigma)
|
||||
<span style="color: #008000">print</span>(Sigma)
|
||||
Eigvals, Eigvecs <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(Sigma)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Eigvals)
|
||||
<span style="color: #008000">print</span>(Eigvals)
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
@@ -732,9 +732,9 @@ Eigvals, Eigvecs <span style="color: #666666">=</span> np<span style="color: #66
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy</span> <span style="color: #008000; font-weight: bold">import</span> sparse
|
||||
eye <span style="color: #666666">=</span> np<span style="color: #666666">.</span>eye(<span style="color: #666666">4</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(eye)
|
||||
<span style="color: #008000">print</span>(eye)
|
||||
sparse_mtx <span style="color: #666666">=</span> sparse<span style="color: #666666">.</span>csr_matrix(eye)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(sparse_mtx)
|
||||
<span style="color: #008000">print</span>(sparse_mtx)
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-10</span>,<span style="color: #666666">10</span>,<span style="color: #666666">100</span>)
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sin(x)
|
||||
plt<span style="color: #666666">.</span>plot(x,y,marker<span style="color: #666666">=</span><span style="color: #BA2121">'x'</span>)
|
||||
@@ -817,8 +817,8 @@ cols <span style="color: #666666">=</span> <span style="color: #666666">5</span>
|
||||
a <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(rows,cols)
|
||||
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(a)
|
||||
display(df)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>mean())
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>std())
|
||||
<span style="color: #008000">print</span>(df<span style="color: #666666">.</span>mean())
|
||||
<span style="color: #008000">print</span>(df<span style="color: #666666">.</span>std())
|
||||
display(df<span style="color: #666666">**2</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
@@ -830,9 +830,9 @@ Thereafter we can select specific columns only and plot final results
|
||||
df<span style="color: #666666">.</span>index <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">10</span>)
|
||||
|
||||
display(df)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df[<span style="color: #BA2121">'Second'</span>]<span style="color: #666666">.</span>mean() )
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>info())
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>describe())
|
||||
<span style="color: #008000">print</span>(df[<span style="color: #BA2121">'Second'</span>]<span style="color: #666666">.</span>mean() )
|
||||
<span style="color: #008000">print</span>(df<span style="color: #666666">.</span>info())
|
||||
<span style="color: #008000">print</span>(df<span style="color: #666666">.</span>describe())
|
||||
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pylab</span> <span style="color: #008000; font-weight: bold">import</span> plt, mpl
|
||||
plt<span style="color: #666666">.</span>style<span style="color: #666666">.</span>use(<span style="color: #BA2121">'seaborn'</span>)
|
||||
@@ -851,9 +851,9 @@ We can produce a \( 4\times 4 \) matrix
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>b <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">16</span>)<span style="color: #666666">.</span>reshape((<span style="color: #666666">4</span>,<span style="color: #666666">4</span>))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(b)
|
||||
<span style="color: #008000">print</span>(b)
|
||||
df1 <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(b)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(df1)
|
||||
<span style="color: #008000">print</span>(df1)
|
||||
</pre></div>
|
||||
<p>
|
||||
and many other operations.
|
||||
@@ -1065,7 +1065,7 @@ ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666
|
||||
plt<span style="color: #666666">.</span>plot(x, np<span style="color: #666666">.</span>abs(ypredict<span style="color: #666666">-</span>y)<span style="color: #666666">/</span><span style="color: #008000">abs</span>(y), <span style="color: #BA2121">"ro"</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">0.0</span>, <span style="color: #666666">0.5</span>])
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r'$x$'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r'$\epsilon_{\mathrm{relative}}$'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r'$\epsilon_{\mathrm</span><span style="color: #BB6688; font-weight: bold">{relative}</span><span style="color: #BA2121">}$'</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r'Relative error'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
@@ -1097,16 +1097,16 @@ y <span style="color: #666666">=</span> <span style="color: #666666">2.0+</span>
|
||||
linreg <span style="color: #666666">=</span> LinearRegression()
|
||||
linreg<span style="color: #666666">.</span>fit(x,y)
|
||||
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(x)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'The intercept alpha: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">'</span>, linreg<span style="color: #666666">.</span>intercept_)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Coefficient beta : </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">'</span>, linreg<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'The intercept alpha: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">'</span>, linreg<span style="color: #666666">.</span>intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Coefficient beta : </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">'</span>, linreg<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #408080; font-style: italic"># The mean squared error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> mean_squared_error(y, ypredict))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> mean_squared_error(y, ypredict))
|
||||
<span style="color: #408080; font-style: italic"># Explained variance score: 1 is perfect prediction </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> r2_score(y, ypredict))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> r2_score(y, ypredict))
|
||||
<span style="color: #408080; font-style: italic"># Mean squared log error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Mean squared log error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_squared_log_error(y, ypredict) )
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Mean squared log error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_squared_log_error(y, ypredict) )
|
||||
<span style="color: #408080; font-style: italic"># Mean absolute error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Mean absolute error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_absolute_error(y, ypredict))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Mean absolute error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_absolute_error(y, ypredict))
|
||||
plt<span style="color: #666666">.</span>plot(x, ypredict, <span style="color: #BA2121">"r-"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">'ro'</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0.0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">1.5</span>, <span style="color: #666666">7.0</span>])
|
||||
@@ -1217,7 +1217,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
err<span style="color: #666666">=</span>(y<span style="color: #666666">-</span>yn)<span style="color: #666666">/</span>yn
|
||||
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">abs</span>(np<span style="color: #666666">.</span>sum(err))<span style="color: #666666">/</span><span style="color: #008000">len</span>(err)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span> (error(y))
|
||||
<span style="color: #008000">print</span> (error(y))
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec17">To our real data: nuclear binding energies. Brief reminder on masses and binding energies </h3>
|
||||
@@ -1340,7 +1340,7 @@ DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, format<span style="color: #666666">=</span><span style="color: #BA2121">'png'</span>)
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, <span style="color: #008000">format</span><span style="color: #666666">=</span><span style="color: #BA2121">'png'</span>)
|
||||
|
||||
infile <span style="color: #666666">=</span> <span style="color: #008000">open</span>(data_path(<span style="color: #BA2121">"MassEval2016.dat"</span>),<span style="color: #BA2121">'r'</span>)
|
||||
</pre></div>
|
||||
@@ -1398,7 +1398,7 @@ Masses <span style="color: #666666">=</span> pd<span style="color: #666666">.</s
|
||||
names<span style="color: #666666">=</span>(<span style="color: #BA2121">'N'</span>, <span style="color: #BA2121">'Z'</span>, <span style="color: #BA2121">'A'</span>, <span style="color: #BA2121">'Element'</span>, <span style="color: #BA2121">'Ebinding'</span>),
|
||||
widths<span style="color: #666666">=</span>(<span style="color: #666666">1</span>,<span style="color: #666666">3</span>,<span style="color: #666666">5</span>,<span style="color: #666666">5</span>,<span style="color: #666666">5</span>,<span style="color: #666666">1</span>,<span style="color: #666666">3</span>,<span style="color: #666666">4</span>,<span style="color: #666666">1</span>,<span style="color: #666666">13</span>,<span style="color: #666666">11</span>,<span style="color: #666666">11</span>,<span style="color: #666666">9</span>,<span style="color: #666666">1</span>,<span style="color: #666666">2</span>,<span style="color: #666666">11</span>,<span style="color: #666666">9</span>,<span style="color: #666666">1</span>,<span style="color: #666666">3</span>,<span style="color: #666666">1</span>,<span style="color: #666666">12</span>,<span style="color: #666666">11</span>,<span style="color: #666666">1</span>),
|
||||
header<span style="color: #666666">=39</span>,
|
||||
index_col<span style="color: #666666">=</span><span style="color: #008000">False</span>)
|
||||
index_col<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Extrapolated values are indicated by '#' in place of the decimal place, so</span>
|
||||
<span style="color: #408080; font-style: italic"># the Ebinding column won't be numeric. Coerce to float and drop these entries.</span>
|
||||
@@ -1432,7 +1432,7 @@ Z <span style="color: #666666">=</span> Masses[<span style="color: #BA2121">'
|
||||
N <span style="color: #666666">=</span> Masses[<span style="color: #BA2121">'N'</span>]
|
||||
Element <span style="color: #666666">=</span> Masses[<span style="color: #BA2121">'Element'</span>]
|
||||
Energies <span style="color: #666666">=</span> Masses[<span style="color: #BA2121">'Ebinding'</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Masses)
|
||||
<span style="color: #008000">print</span>(Masses)
|
||||
</pre></div>
|
||||
<p>
|
||||
The next step, and we will define this mathematically later, is to set up the so-called <b>design matrix</b>. We will throughout call this matrix \( \boldsymbol{X} \).
|
||||
@@ -1463,18 +1463,18 @@ Now we can print measures of how our fit is doing, the coefficients from the fit
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># The mean squared error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> mean_squared_error(Energies, fity))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> mean_squared_error(Energies, fity))
|
||||
<span style="color: #408080; font-style: italic"># Explained variance score: 1 is perfect prediction </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> r2_score(Energies, fity))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> r2_score(Energies, fity))
|
||||
<span style="color: #408080; font-style: italic"># Mean absolute error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Mean absolute error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_absolute_error(Energies, fity))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(clf<span style="color: #666666">.</span>coef_, clf<span style="color: #666666">.</span>intercept_)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Mean absolute error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_absolute_error(Energies, fity))
|
||||
<span style="color: #008000">print</span>(clf<span style="color: #666666">.</span>coef_, clf<span style="color: #666666">.</span>intercept_)
|
||||
|
||||
Masses[<span style="color: #BA2121">'Eapprox'</span>] <span style="color: #666666">=</span> fity
|
||||
<span style="color: #408080; font-style: italic"># Generate a plot comparing the experimental with the fitted values values.</span>
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots()
|
||||
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">r'$A = N + Z$'</span>)
|
||||
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">r'$E_\mathrm{bind}\,/\mathrm{MeV}$'</span>)
|
||||
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">r'$E_\mathrm</span><span style="color: #BB6688; font-weight: bold">{bind}</span><span style="color: #BA2121">\,/\mathrm</span><span style="color: #BB6688; font-weight: bold">{MeV}</span><span style="color: #BA2121">$'</span>)
|
||||
ax<span style="color: #666666">.</span>plot(Masses[<span style="color: #BA2121">'A'</span>], Masses[<span style="color: #BA2121">'Ebinding'</span>], alpha<span style="color: #666666">=0.7</span>, lw<span style="color: #666666">=2</span>,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">'Ame2016'</span>)
|
||||
ax<span style="color: #666666">.</span>plot(Masses[<span style="color: #BA2121">'A'</span>], Masses[<span style="color: #BA2121">'Eapprox'</span>], alpha<span style="color: #666666">=0.7</span>, lw<span style="color: #666666">=2</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">'m'</span>,
|
||||
@@ -1519,8 +1519,8 @@ plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
save_fig(<span style="color: #BA2121">"Masses2016Trees"</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Masses)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean( (Energies<span style="color: #666666">-</span>y_1)<span style="color: #666666">**2</span>))
|
||||
<span style="color: #008000">print</span>(Masses)
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean( (Energies<span style="color: #666666">-</span>y_1)<span style="color: #666666">**2</span>))
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec20">And what about using neural networks? </h3>
|
||||
@@ -1554,7 +1554,7 @@ sns<span style="color: #666666">.</span>set()
|
||||
train_accuracy[i][j] <span style="color: #666666">=</span> dnn<span style="color: #666666">.</span>score(X_train, Y_train)
|
||||
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
|
||||
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">"viridis"</span>)
|
||||
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">"viridis"</span>)
|
||||
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">"Training Accuracy"</span>)
|
||||
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">"$\eta$"</span>)
|
||||
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">"$\lambda$"</span>)
|
||||
@@ -1581,7 +1581,7 @@ Now it is time to dive more into the details of various methods. We will start w
|
||||
|
||||
|
||||
<center style="font-size:80%">
|
||||
<!-- copyright --> © 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
||||
<!-- copyright --> © 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
||||
</center>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user