update
This commit is contained in:
@@ -135,7 +135,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>
|
||||
@@ -529,7 +529,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 "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>n = <span style="color: #B452CD">10</span>
|
||||
x = np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">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) \).
|
||||
@@ -539,7 +539,7 @@ Another alternative is to declare a vector as follows
|
||||
<!-- 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>
|
||||
x = np.array([<span style="color: #B452CD">1</span>, <span style="color: #B452CD">2</span>, <span style="color: #B452CD">3</span>])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">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++
|
||||
@@ -549,7 +549,7 @@ start numbering array elements from \( 0 \) and on. This means that a vector wit
|
||||
<!-- 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>
|
||||
x = np.log(np.array([<span style="color: #B452CD">4</span>, <span style="color: #B452CD">7</span>, <span style="color: #B452CD">8</span>]))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">print</span>(x)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the last example we used Numpy's unary function \( np.log \). This function is
|
||||
@@ -568,7 +568,7 @@ logarithms of a vector would be to write
|
||||
x = np.array([<span style="color: #B452CD">4</span>, <span style="color: #B452CD">7</span>, <span style="color: #B452CD">8</span>])
|
||||
<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">0</span>, <span style="color: #658b00">len</span>(x)):
|
||||
x[i] = log(x[i])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">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.
|
||||
@@ -578,7 +578,7 @@ The attentive reader will also notice that the output is \( [1, 1, 2] \). Python
|
||||
<!-- 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>
|
||||
x = np.log(np.array([<span style="color: #B452CD">4</span>, <span style="color: #B452CD">7</span>, <span style="color: #B452CD">8</span>], dtype = np.float64))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">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
|
||||
@@ -587,7 +587,7 @@ or simply write them as double precision numbers (Python uses 64 bits as default
|
||||
<!-- 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>
|
||||
x = np.log(np.array([<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">7.0</span>, <span style="color: #B452CD">8.0</span>])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x)
|
||||
<span style="color: #658b00">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
|
||||
@@ -596,7 +596,7 @@ To check the number of bytes (remember that one byte contains eight bits for dou
|
||||
<!-- 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>
|
||||
x = np.log(np.array([<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">7.0</span>, <span style="color: #B452CD">8.0</span>])
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(x.itemsize)
|
||||
<span style="color: #658b00">print</span>(x.itemsize)
|
||||
</pre></div>
|
||||
|
||||
<h2 id="___sec13">Matrices in Python </h2>
|
||||
@@ -611,7 +611,7 @@ lowercase letters for vectors and uppercase letters for matrices)
|
||||
<!-- 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>
|
||||
A = np.log(np.array([ [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">7.0</span>, <span style="color: #B452CD">8.0</span>], [<span style="color: #B452CD">3.0</span>, <span style="color: #B452CD">10.0</span>, <span style="color: #B452CD">11.0</span>], [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">5.0</span>, <span style="color: #B452CD">7.0</span>] ]))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A)
|
||||
<span style="color: #658b00">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
|
||||
@@ -621,7 +621,7 @@ If we use the <b>shape</b> function we would get \( (3, 3) \) as output, that is
|
||||
<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>
|
||||
A = np.log(np.array([ [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">7.0</span>, <span style="color: #B452CD">8.0</span>], [<span style="color: #B452CD">3.0</span>, <span style="color: #B452CD">10.0</span>, <span style="color: #B452CD">11.0</span>], [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">5.0</span>, <span style="color: #B452CD">7.0</span>] ]))
|
||||
<span style="color: #228B22"># print the first column, row-major order and elements start with 0</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A[:,<span style="color: #B452CD">0</span>])
|
||||
<span style="color: #658b00">print</span>(A[:,<span style="color: #B452CD">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
|
||||
@@ -631,7 +631,7 @@ We can continue this was by printing out other columns or rows. The example here
|
||||
<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>
|
||||
A = np.log(np.array([ [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">7.0</span>, <span style="color: #B452CD">8.0</span>], [<span style="color: #B452CD">3.0</span>, <span style="color: #B452CD">10.0</span>, <span style="color: #B452CD">11.0</span>], [<span style="color: #B452CD">4.0</span>, <span style="color: #B452CD">5.0</span>, <span style="color: #B452CD">7.0</span>] ]))
|
||||
<span style="color: #228B22"># print the first column, row-major order and elements start with 0</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A[<span style="color: #B452CD">1</span>,:])
|
||||
<span style="color: #658b00">print</span>(A[<span style="color: #B452CD">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
|
||||
@@ -642,7 +642,7 @@ Numpy contains many other functionalities that allow us to slice, subdivide etc
|
||||
n = <span style="color: #B452CD">10</span>
|
||||
<span style="color: #228B22"># define a matrix of dimension 10 x 10 and set all elements to zero</span>
|
||||
A = np.zeros( (n, n) )
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A)
|
||||
<span style="color: #658b00">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
or initializing all elements to
|
||||
@@ -653,7 +653,7 @@ or initializing all elements to
|
||||
n = <span style="color: #B452CD">10</span>
|
||||
<span style="color: #228B22"># define a matrix of dimension 10 x 10 and set all elements to one</span>
|
||||
A = np.ones( (n, n) )
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A)
|
||||
<span style="color: #658b00">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
or as unitarily distributed random numbers (see the material on random number generators in the statistics part)
|
||||
@@ -664,7 +664,7 @@ or as unitarily distributed random numbers (see the material on random number ge
|
||||
n = <span style="color: #B452CD">10</span>
|
||||
<span style="color: #228B22"># define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]</span>
|
||||
A = np.random.rand(n, n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(A)
|
||||
<span style="color: #658b00">print</span>(A)
|
||||
</pre></div>
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
@@ -709,16 +709,16 @@ covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
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))
|
||||
<span style="color: #658b00">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))
|
||||
<span style="color: #658b00">print</span>(np.mean(y))
|
||||
z = x**<span style="color: #B452CD">3</span>+np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(z))
|
||||
<span style="color: #658b00">print</span>(np.mean(z))
|
||||
W = np.vstack((x, y, z))
|
||||
Sigma = np.cov(W)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Sigma)
|
||||
<span style="color: #658b00">print</span>(Sigma)
|
||||
Eigvals, Eigvecs = np.linalg.eig(Sigma)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Eigvals)
|
||||
<span style="color: #658b00">print</span>(Eigvals)
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
@@ -727,9 +727,9 @@ Eigvals, Eigvecs = np.linalg.eig(Sigma)
|
||||
<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">from</span> <span style="color: #008b45; text-decoration: underline">scipy</span> <span style="color: #8B008B; font-weight: bold">import</span> sparse
|
||||
eye = np.eye(<span style="color: #B452CD">4</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(eye)
|
||||
<span style="color: #658b00">print</span>(eye)
|
||||
sparse_mtx = sparse.csr_matrix(eye)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(sparse_mtx)
|
||||
<span style="color: #658b00">print</span>(sparse_mtx)
|
||||
x = np.linspace(-<span style="color: #B452CD">10</span>,<span style="color: #B452CD">10</span>,<span style="color: #B452CD">100</span>)
|
||||
y = np.sin(x)
|
||||
plt.plot(x,y,marker=<span style="color: #CD5555">'x'</span>)
|
||||
@@ -812,8 +812,8 @@ 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())
|
||||
<span style="color: #658b00">print</span>(df.mean())
|
||||
<span style="color: #658b00">print</span>(df.std())
|
||||
display(df**<span style="color: #B452CD">2</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
@@ -825,9 +825,9 @@ Thereafter we can select specific columns only and plot final results
|
||||
df.index = np.arange(<span style="color: #B452CD">10</span>)
|
||||
|
||||
display(df)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(df[<span style="color: #CD5555">'Second'</span>].mean() )
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(df.info())
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(df.describe())
|
||||
<span style="color: #658b00">print</span>(df[<span style="color: #CD5555">'Second'</span>].mean() )
|
||||
<span style="color: #658b00">print</span>(df.info())
|
||||
<span style="color: #658b00">print</span>(df.describe())
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">pylab</span> <span style="color: #8B008B; font-weight: bold">import</span> plt, mpl
|
||||
plt.style.use(<span style="color: #CD5555">'seaborn'</span>)
|
||||
@@ -846,9 +846,9 @@ We can produce a \( 4\times 4 \) matrix
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>b = np.arange(<span style="color: #B452CD">16</span>).reshape((<span style="color: #B452CD">4</span>,<span style="color: #B452CD">4</span>))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(b)
|
||||
<span style="color: #658b00">print</span>(b)
|
||||
df1 = pd.DataFrame(b)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(df1)
|
||||
<span style="color: #658b00">print</span>(df1)
|
||||
</pre></div>
|
||||
<p>
|
||||
and many other operations.
|
||||
@@ -1092,16 +1092,16 @@ y = <span style="color: #B452CD">2.0</span>+ <span style="color: #B452CD">5</spa
|
||||
linreg = LinearRegression()
|
||||
linreg.fit(x,y)
|
||||
ypredict = linreg.predict(x)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'The intercept alpha: \n'</span>, linreg.intercept_)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Coefficient beta : \n'</span>, linreg.coef_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'The intercept alpha: \n'</span>, linreg.intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Coefficient beta : \n'</span>, linreg.coef_)
|
||||
<span style="color: #228B22"># The mean squared error </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Mean squared error: %.2f"</span> % mean_squared_error(y, ypredict))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Mean squared error: %.2f"</span> % mean_squared_error(y, ypredict))
|
||||
<span style="color: #228B22"># Explained variance score: 1 is perfect prediction </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Variance score: %.2f'</span> % r2_score(y, ypredict))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Variance score: %.2f'</span> % r2_score(y, ypredict))
|
||||
<span style="color: #228B22"># Mean squared log error </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Mean squared log error: %.2f'</span> % mean_squared_log_error(y, ypredict) )
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Mean squared log error: %.2f'</span> % mean_squared_log_error(y, ypredict) )
|
||||
<span style="color: #228B22"># Mean absolute error </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Mean absolute error: %.2f'</span> % mean_absolute_error(y, ypredict))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Mean absolute error: %.2f'</span> % mean_absolute_error(y, ypredict))
|
||||
plt.plot(x, ypredict, <span style="color: #CD5555">"r-"</span>)
|
||||
plt.plot(x, y ,<span style="color: #CD5555">'ro'</span>)
|
||||
plt.axis([<span style="color: #B452CD">0.0</span>,<span style="color: #B452CD">1.0</span>,<span style="color: #B452CD">1.5</span>, <span style="color: #B452CD">7.0</span>])
|
||||
@@ -1212,7 +1212,7 @@ plt.show()
|
||||
err=(y-yn)/yn
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">abs</span>(np.sum(err))/<span style="color: #658b00">len</span>(err)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span> (error(y))
|
||||
<span style="color: #658b00">print</span> (error(y))
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec17">To our real data: nuclear binding energies. Brief reminder on masses and binding energies </h3>
|
||||
@@ -1335,7 +1335,7 @@ DATA_ID = <span style="color: #CD5555">"DataFiles/"</span>
|
||||
<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>)
|
||||
plt.savefig(image_path(fig_id) + <span style="color: #CD5555">".png"</span>, <span style="color: #658b00">format</span>=<span style="color: #CD5555">'png'</span>)
|
||||
|
||||
infile = <span style="color: #658b00">open</span>(data_path(<span style="color: #CD5555">"MassEval2016.dat"</span>),<span style="color: #CD5555">'r'</span>)
|
||||
</pre></div>
|
||||
@@ -1393,7 +1393,7 @@ Masses = pd.read_fwf(infile, usecols=(<span style="color: #B452CD">2</span>,<spa
|
||||
names=(<span style="color: #CD5555">'N'</span>, <span style="color: #CD5555">'Z'</span>, <span style="color: #CD5555">'A'</span>, <span style="color: #CD5555">'Element'</span>, <span style="color: #CD5555">'Ebinding'</span>),
|
||||
widths=(<span style="color: #B452CD">1</span>,<span style="color: #B452CD">3</span>,<span style="color: #B452CD">5</span>,<span style="color: #B452CD">5</span>,<span style="color: #B452CD">5</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">3</span>,<span style="color: #B452CD">4</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">13</span>,<span style="color: #B452CD">11</span>,<span style="color: #B452CD">11</span>,<span style="color: #B452CD">9</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">11</span>,<span style="color: #B452CD">9</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">3</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">12</span>,<span style="color: #B452CD">11</span>,<span style="color: #B452CD">1</span>),
|
||||
header=<span style="color: #B452CD">39</span>,
|
||||
index_col=<span style="color: #658b00">False</span>)
|
||||
index_col=<span style="color: #8B008B; font-weight: bold">False</span>)
|
||||
|
||||
<span style="color: #228B22"># Extrapolated values are indicated by '#' in place of the decimal place, so</span>
|
||||
<span style="color: #228B22"># the Ebinding column won't be numeric. Coerce to float and drop these entries.</span>
|
||||
@@ -1427,7 +1427,7 @@ Z = Masses[<span style="color: #CD5555">'Z'</span>]
|
||||
N = Masses[<span style="color: #CD5555">'N'</span>]
|
||||
Element = Masses[<span style="color: #CD5555">'Element'</span>]
|
||||
Energies = Masses[<span style="color: #CD5555">'Ebinding'</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Masses)
|
||||
<span style="color: #658b00">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} \).
|
||||
@@ -1458,12 +1458,12 @@ Now we can print measures of how our fit is doing, the coefficients from the fit
|
||||
|
||||
<!-- 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"># The mean squared error </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Mean squared error: %.2f"</span> % mean_squared_error(Energies, fity))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Mean squared error: %.2f"</span> % mean_squared_error(Energies, fity))
|
||||
<span style="color: #228B22"># Explained variance score: 1 is perfect prediction </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Variance score: %.2f'</span> % r2_score(Energies, fity))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Variance score: %.2f'</span> % r2_score(Energies, fity))
|
||||
<span style="color: #228B22"># Mean absolute error </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">'Mean absolute error: %.2f'</span> % mean_absolute_error(Energies, fity))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(clf.coef_, clf.intercept_)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'Mean absolute error: %.2f'</span> % mean_absolute_error(Energies, fity))
|
||||
<span style="color: #658b00">print</span>(clf.coef_, clf.intercept_)
|
||||
|
||||
Masses[<span style="color: #CD5555">'Eapprox'</span>] = fity
|
||||
<span style="color: #228B22"># Generate a plot comparing the experimental with the fitted values values.</span>
|
||||
@@ -1514,8 +1514,8 @@ plt.title(<span style="color: #CD5555">"Decision Tree Regression"</spa
|
||||
plt.legend()
|
||||
save_fig(<span style="color: #CD5555">"Masses2016Trees"</span>)
|
||||
plt.show()
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Masses)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean( (Energies-y_1)**<span style="color: #B452CD">2</span>))
|
||||
<span style="color: #658b00">print</span>(Masses)
|
||||
<span style="color: #658b00">print</span>(np.mean( (Energies-y_1)**<span style="color: #B452CD">2</span>))
|
||||
</pre></div>
|
||||
|
||||
<h3 id="___sec20">And what about using neural networks? </h3>
|
||||
@@ -1549,7 +1549,7 @@ sns.set()
|
||||
train_accuracy[i][j] = dnn.score(X_train, Y_train)
|
||||
|
||||
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
|
||||
sns.heatmap(train_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">"viridis"</span>)
|
||||
sns.heatmap(train_accuracy, annot=<span style="color: #8B008B; font-weight: bold">True</span>, ax=ax, cmap=<span style="color: #CD5555">"viridis"</span>)
|
||||
ax.set_title(<span style="color: #CD5555">"Training Accuracy"</span>)
|
||||
ax.set_ylabel(<span style="color: #CD5555">"$\eta$"</span>)
|
||||
ax.set_xlabel(<span style="color: #CD5555">"$\lambda$"</span>)
|
||||
@@ -1576,7 +1576,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