updated book
This commit is contained in:
@@ -283,6 +283,46 @@ plt.show()
|
||||
!ec
|
||||
|
||||
|
||||
===== Other Matrix and Vector Operations =====
|
||||
|
||||
The following examples show how to compute various quantities like the _mean_ value of a matrix or a vector and how to use functions like _reshape_ and _ravel_. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance.
|
||||
!bc pycod
|
||||
"""
|
||||
Simple code that tests various numpy functions
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
# Simple test-matrix of dim 3 x 4
|
||||
a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)
|
||||
print(f"The test matrix:{a}")
|
||||
# This is the total mean summed over all elements, which here has to be 6.5
|
||||
print(f"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}")
|
||||
# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector
|
||||
print(f"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}")
|
||||
# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if
|
||||
# keepdims=True. Else it return a row-like vector
|
||||
# Try setting keepdims=False
|
||||
print(f"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}")
|
||||
# We print then the mean value for each row by setting keepdims=False
|
||||
print(f"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}")
|
||||
|
||||
# Ravel return a contiguous flattened array.
|
||||
print(f"Flatten the matrix:{np.ravel(a)}")
|
||||
# It is the same as reshaping the matrix into a one-dimensional array
|
||||
print(f"Reshape the matrix to a one-dim array:{a.reshape(-1)}")
|
||||
# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.
|
||||
# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest
|
||||
print(np.ravel(a, order='F'))
|
||||
# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering
|
||||
# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.
|
||||
# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.
|
||||
# Transposing it
|
||||
print(np.ravel(a.T))
|
||||
print(np.ravel(a.T, order='A'))
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
===== Gaussian Elimination =====
|
||||
|
||||
We start with the linear set of equations
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -558,6 +558,58 @@
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Other Matrix and Vector Operations\n",
|
||||
"\n",
|
||||
"The following examples show how to compute various quantities like the **mean** value of a matrix or a vector and how to use functions like **reshape** and **ravel**. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"\n",
|
||||
"Simple code that tests various numpy functions\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"# Simple test-matrix of dim 3 x 4\n",
|
||||
"a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)\n",
|
||||
"print(f\"The test matrix:{a}\")\n",
|
||||
"# This is the total mean summed over all elements, which here has to be 6.5\n",
|
||||
"print(f\"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}\")\n",
|
||||
"# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector\n",
|
||||
"print(f\"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if\n",
|
||||
"# keepdims=True. Else it return a row-like vector\n",
|
||||
"# Try setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# We print then the mean value for each row by setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}\")\n",
|
||||
"\n",
|
||||
"# Ravel return a contiguous flattened array.\n",
|
||||
"print(f\"Flatten the matrix:{np.ravel(a)}\")\n",
|
||||
"# It is the same as reshaping the matrix into a one-dimensional array\n",
|
||||
"print(f\"Reshape the matrix to a one-dim array:{a.reshape(-1)}\")\n",
|
||||
"# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.\n",
|
||||
"# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest \n",
|
||||
"print(np.ravel(a, order='F'))\n",
|
||||
"# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering\n",
|
||||
"# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.\n",
|
||||
"# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.\n",
|
||||
"# Transposing it\n",
|
||||
"print(np.ravel(a.T))\n",
|
||||
"print(np.ravel(a.T, order='A'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
||||
@@ -101,7 +101,7 @@ For the reading assignments we use the following abbreviations:
|
||||
|
||||
### Week 40 October 4-8
|
||||
- Lab Wednesday: Wrap up project 1
|
||||
- Lecture Thursday: Writing a feed-forward Neural Network code for regression and classification
|
||||
- Lecture Thursday: Stochastic gradient descent, automatic differentiation and start discussion of feed-forward Neural Network code for regression and classification
|
||||
- Lecture Friday: Deep Learning and Neural Networks
|
||||
- Reading recommendations:
|
||||
- See lecture notes for week 40 at https://compphysics.github.io/MachineLearning/doc/web/course.html.
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
<script async="async" src="_static/sphinx-thebe.js"></script>
|
||||
<link rel="index" title="Index" href="genindex.html" />
|
||||
<link rel="search" title="Search" href="search.html" />
|
||||
<link rel="next" title="3. Linear Regression, basic Elements" href="chapter1.html" />
|
||||
<link rel="next" title="3. Linear Regression" href="chapter1.html" />
|
||||
<link rel="prev" title="1. Elements of Probability Theory and Statistical Data Analysis" href="statistics.html" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="docsearch:language" content="en" />
|
||||
@@ -139,17 +139,17 @@
|
||||
<ul class="nav bd-sidenav">
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter1.html">
|
||||
3. Linear Regression, basic Elements
|
||||
3. Linear Regression
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter2.html">
|
||||
4. Resampling Methods
|
||||
4. Ridge and Lasso Regression
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter3.html">
|
||||
5. Ridge and Lasso Regression
|
||||
5. Resampling Methods
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
@@ -157,9 +157,14 @@
|
||||
6. Logistic Regression
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapteroptimization.html">
|
||||
7. Optimization, the central part of any Machine Learning algortithm
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter5.html">
|
||||
7. Support Vector Machines, overarching aims
|
||||
8. Support Vector Machines, overarching aims
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -171,12 +176,12 @@
|
||||
<ul class="nav bd-sidenav">
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter6.html">
|
||||
8. Decision trees, overarching aims
|
||||
9. Decision trees, overarching aims
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter7.html">
|
||||
9. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods
|
||||
10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -188,12 +193,12 @@
|
||||
<ul class="nav bd-sidenav">
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter8.html">
|
||||
10. Basic ideas of the Principal Component Analysis (PCA)
|
||||
11. Basic ideas of the Principal Component Analysis (PCA)
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="Clustering.html">
|
||||
11. Clustering Analysis
|
||||
12. Clustering Analysis
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -205,12 +210,12 @@
|
||||
<ul class="nav bd-sidenav">
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter9.html">
|
||||
12. Neural networks
|
||||
13. Neural networks
|
||||
</a>
|
||||
</li>
|
||||
<li class="toctree-l1">
|
||||
<a class="reference internal" href="chapter10.html">
|
||||
13. Building a Feed Forward Neural Network
|
||||
14. Building a Feed Forward Neural Network
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -304,14 +309,19 @@
|
||||
2.4. Numpy and arrays
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
<a class="reference internal nav-link" href="#other-matrix-and-vector-operations">
|
||||
2.5. Other Matrix and Vector Operations
|
||||
</a>
|
||||
</li>
|
||||
<li class="toc-h2 nav-item toc-entry">
|
||||
<a class="reference internal nav-link" href="#gaussian-elimination">
|
||||
2.5. Gaussian Elimination
|
||||
2.6. Gaussian Elimination
|
||||
</a>
|
||||
<ul class="nav section-nav flex-column">
|
||||
<li class="toc-h3 nav-item toc-entry">
|
||||
<a class="reference internal nav-link" href="#lu-decomposition-the-inverse-of-a-matrix">
|
||||
2.5.1. LU Decomposition, the inverse of a matrix
|
||||
2.6.1. LU Decomposition, the inverse of a matrix
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -440,8 +450,8 @@ matrices and vectors.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[-0.32938847 -2.07110274 -0.2627588 0.68616263 1.65238878 -0.03750367
|
||||
1.653702 0.71442781 0.2983233 -1.10327559]
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[ 0.63628911 0.14168589 0.80381774 -1.0333934 -0.30509508 0.43222125
|
||||
2.19605877 -0.10357077 -0.15968414 0.70924636]
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -690,8 +700,50 @@ covariance matrix through the <strong>np.linalg.eig()</strong> function.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="other-matrix-and-vector-operations">
|
||||
<h2><span class="section-number">2.5. </span>Other Matrix and Vector Operations<a class="headerlink" href="#other-matrix-and-vector-operations" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The following examples show how to compute various quantities like the <strong>mean</strong> value of a matrix or a vector and how to use functions like <strong>reshape</strong> and <strong>ravel</strong>. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
|
||||
<span class="sd">Simple code that tests various numpy functions</span>
|
||||
<span class="sd">"""</span>
|
||||
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="c1"># Simple test-matrix of dim 3 x 4</span>
|
||||
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]],</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The test matrix:</span><span class="si">{</span><span class="n">a</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># This is the total mean summed over all elements, which here has to be 6.5</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"This is the total mean summed over all elements:</span><span class="si">{</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"This is the mean for each column:</span><span class="si">{</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if</span>
|
||||
<span class="c1"># keepdims=True. Else it return a row-like vector</span>
|
||||
<span class="c1"># Try setting keepdims=False</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"This is the mean value for each row:</span><span class="si">{</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># We print then the mean value for each row by setting keepdims=False</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"This is the mean value for each row with keepdims false:</span><span class="si">{</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Ravel return a contiguous flattened array.</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Flatten the matrix:</span><span class="si">{</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># It is the same as reshaping the matrix into a one-dimensional array</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Reshape the matrix to a one-dim array:</span><span class="si">{</span><span class="n">a</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="c1"># ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.</span>
|
||||
<span class="c1"># ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest </span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">'F'</span><span class="p">))</span>
|
||||
<span class="c1"># When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering</span>
|
||||
<span class="c1"># ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.</span>
|
||||
<span class="c1"># ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.</span>
|
||||
<span class="c1"># Transposing it</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">'A'</span><span class="p">))</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="gaussian-elimination">
|
||||
<h2><span class="section-number">2.5. </span>Gaussian Elimination<a class="headerlink" href="#gaussian-elimination" title="Permalink to this headline">¶</a></h2>
|
||||
<h2><span class="section-number">2.6. </span>Gaussian Elimination<a class="headerlink" href="#gaussian-elimination" title="Permalink to this headline">¶</a></h2>
|
||||
<p>We start with the linear set of equations</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
@@ -1045,7 +1097,7 @@ needed to solve the set of <span class="math notranslate nohighlight">\(n\)</spa
|
||||
<li><p>Thereafter you call the function <code class="docutils literal notranslate"><span class="pre">lubksb(double</span> <span class="pre">a,</span> <span class="pre">int</span> <span class="pre">n,</span> <span class="pre">int</span> <span class="pre">indx,</span> <span class="pre">double</span> <span class="pre">w)</span></code> which uses the LU decomposed matrix <span class="math notranslate nohighlight">\(\bf A\)</span> and the vector <span class="math notranslate nohighlight">\(\bf w\)</span> and returns <span class="math notranslate nohighlight">\(\bf x\)</span> in the same place as <span class="math notranslate nohighlight">\(\bf w\)</span>. Upon exit the original content in <span class="math notranslate nohighlight">\(\bf w\)</span> is destroyed. If you wish to keep this information, you should make a backup of it in your calling function.</p></li>
|
||||
</ul>
|
||||
<div class="section" id="lu-decomposition-the-inverse-of-a-matrix">
|
||||
<h3><span class="section-number">2.5.1. </span>LU Decomposition, the inverse of a matrix<a class="headerlink" href="#lu-decomposition-the-inverse-of-a-matrix" title="Permalink to this headline">¶</a></h3>
|
||||
<h3><span class="section-number">2.6.1. </span>LU Decomposition, the inverse of a matrix<a class="headerlink" href="#lu-decomposition-the-inverse-of-a-matrix" title="Permalink to this headline">¶</a></h3>
|
||||
<p>If the inverse exists then</p>
|
||||
<div class="math notranslate nohighlight">
|
||||
\[
|
||||
@@ -1128,7 +1180,7 @@ can be written as a vector with unknown entries</p>
|
||||
<div class='prev-next-bottom'>
|
||||
|
||||
<a class='left-prev' id="prev-link" href="statistics.html" title="previous page"><span class="section-number">1. </span>Elements of Probability Theory and Statistical Data Analysis</a>
|
||||
<a class='right-next' id="next-link" href="chapter1.html" title="next page"><span class="section-number">3. </span>Linear Regression, basic Elements</a>
|
||||
<a class='right-next' id="next-link" href="chapter1.html" title="next page"><span class="section-number">3. </span>Linear Regression</a>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@@ -535,7 +535,7 @@
|
||||
<h3>Week 40 October 4-8<a class="headerlink" href="#week-40-october-4-8" title="Permalink to this headline">¶</a></h3>
|
||||
<ul class="simple">
|
||||
<li><p>Lab Wednesday: Wrap up project 1</p></li>
|
||||
<li><p>Lecture Thursday: Writing a feed-forward Neural Network code for regression and classification</p></li>
|
||||
<li><p>Lecture Thursday: Stochastic gradient descent, automatic differentiation and start discussion of feed-forward Neural Network code for regression and classification</p></li>
|
||||
<li><p>Lecture Friday: Deep Learning and Neural Networks</p></li>
|
||||
<li><p>Reading recommendations:</p>
|
||||
<ul>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -159,8 +159,8 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[-0.32938847 -2.07110274 -0.2627588 0.68616263 1.65238878 -0.03750367\n",
|
||||
" 1.653702 0.71442781 0.2983233 -1.10327559]\n"
|
||||
"[ 0.63628911 0.14168589 0.80381774 -1.0333934 -0.30509508 0.43222125\n",
|
||||
" 2.19605877 -0.10357077 -0.15968414 0.70924636]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -608,6 +608,58 @@
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Other Matrix and Vector Operations\n",
|
||||
"\n",
|
||||
"The following examples show how to compute various quantities like the **mean** value of a matrix or a vector and how to use functions like **reshape** and **ravel**. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"\n",
|
||||
"Simple code that tests various numpy functions\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"# Simple test-matrix of dim 3 x 4\n",
|
||||
"a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)\n",
|
||||
"print(f\"The test matrix:{a}\")\n",
|
||||
"# This is the total mean summed over all elements, which here has to be 6.5\n",
|
||||
"print(f\"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}\")\n",
|
||||
"# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector\n",
|
||||
"print(f\"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if\n",
|
||||
"# keepdims=True. Else it return a row-like vector\n",
|
||||
"# Try setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# We print then the mean value for each row by setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}\")\n",
|
||||
"\n",
|
||||
"# Ravel return a contiguous flattened array.\n",
|
||||
"print(f\"Flatten the matrix:{np.ravel(a)}\")\n",
|
||||
"# It is the same as reshaping the matrix into a one-dimensional array\n",
|
||||
"print(f\"Reshape the matrix to a one-dim array:{a.reshape(-1)}\")\n",
|
||||
"# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.\n",
|
||||
"# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest \n",
|
||||
"print(np.ravel(a, order='F'))\n",
|
||||
"# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering\n",
|
||||
"# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.\n",
|
||||
"# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.\n",
|
||||
"# Transposing it\n",
|
||||
"print(np.ravel(a.T))\n",
|
||||
"print(np.ravel(a.T, order='A'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
||||
@@ -286,6 +286,43 @@ y = np.sin(x)
|
||||
plt.plot(x,y,marker='x')
|
||||
plt.show()
|
||||
|
||||
## Other Matrix and Vector Operations
|
||||
|
||||
The following examples show how to compute various quantities like the **mean** value of a matrix or a vector and how to use functions like **reshape** and **ravel**. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance.
|
||||
|
||||
"""
|
||||
Simple code that tests various numpy functions
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
# Simple test-matrix of dim 3 x 4
|
||||
a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)
|
||||
print(f"The test matrix:{a}")
|
||||
# This is the total mean summed over all elements, which here has to be 6.5
|
||||
print(f"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}")
|
||||
# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector
|
||||
print(f"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}")
|
||||
# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if
|
||||
# keepdims=True. Else it return a row-like vector
|
||||
# Try setting keepdims=False
|
||||
print(f"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}")
|
||||
# We print then the mean value for each row by setting keepdims=False
|
||||
print(f"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}")
|
||||
|
||||
# Ravel return a contiguous flattened array.
|
||||
print(f"Flatten the matrix:{np.ravel(a)}")
|
||||
# It is the same as reshaping the matrix into a one-dimensional array
|
||||
print(f"Reshape the matrix to a one-dim array:{a.reshape(-1)}")
|
||||
# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.
|
||||
# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest
|
||||
print(np.ravel(a, order='F'))
|
||||
# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering
|
||||
# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.
|
||||
# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.
|
||||
# Transposing it
|
||||
print(np.ravel(a.T))
|
||||
print(np.ravel(a.T, order='A'))
|
||||
|
||||
## Gaussian Elimination
|
||||
|
||||
We start with the linear set of equations
|
||||
|
||||
@@ -558,6 +558,58 @@
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Other Matrix and Vector Operations\n",
|
||||
"\n",
|
||||
"The following examples show how to compute various quantities like the **mean** value of a matrix or a vector and how to use functions like **reshape** and **ravel**. These are all useful quantities when scaling the data and preparing the data for various machine learning algorithms and when calculating quantities like the mean squared error or the variance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"\n",
|
||||
"Simple code that tests various numpy functions\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"# Simple test-matrix of dim 3 x 4\n",
|
||||
"a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)\n",
|
||||
"print(f\"The test matrix:{a}\")\n",
|
||||
"# This is the total mean summed over all elements, which here has to be 6.5\n",
|
||||
"print(f\"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}\")\n",
|
||||
"# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector\n",
|
||||
"print(f\"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if\n",
|
||||
"# keepdims=True. Else it return a row-like vector\n",
|
||||
"# Try setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}\")\n",
|
||||
"# We print then the mean value for each row by setting keepdims=False\n",
|
||||
"print(f\"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}\")\n",
|
||||
"\n",
|
||||
"# Ravel return a contiguous flattened array.\n",
|
||||
"print(f\"Flatten the matrix:{np.ravel(a)}\")\n",
|
||||
"# It is the same as reshaping the matrix into a one-dimensional array\n",
|
||||
"print(f\"Reshape the matrix to a one-dim array:{a.reshape(-1)}\")\n",
|
||||
"# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.\n",
|
||||
"# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest \n",
|
||||
"print(np.ravel(a, order='F'))\n",
|
||||
"# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering\n",
|
||||
"# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.\n",
|
||||
"# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.\n",
|
||||
"# Transposing it\n",
|
||||
"print(np.ravel(a.T))\n",
|
||||
"print(np.ravel(a.T, order='A'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
||||
@@ -101,7 +101,7 @@ For the reading assignments we use the following abbreviations:
|
||||
|
||||
### Week 40 October 4-8
|
||||
- Lab Wednesday: Wrap up project 1
|
||||
- Lecture Thursday: Writing a feed-forward Neural Network code for regression and classification
|
||||
- Lecture Thursday: Stochastic gradient descent, automatic differentiation and start discussion of feed-forward Neural Network code for regression and classification
|
||||
- Lecture Friday: Deep Learning and Neural Networks
|
||||
- Reading recommendations:
|
||||
- See lecture notes for week 40 at https://compphysics.github.io/MachineLearning/doc/web/course.html.
|
||||
|
||||
Reference in New Issue
Block a user