diff --git a/doc/BookChapters/linalg.do.txt b/doc/BookChapters/linalg.do.txt index e794b1941..c4e890255 100644 --- a/doc/BookChapters/linalg.do.txt +++ b/doc/BookChapters/linalg.do.txt @@ -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 diff --git a/doc/LectureNotes/_build/.doctrees/environment.pickle b/doc/LectureNotes/_build/.doctrees/environment.pickle index cc9a2b39f..df3041187 100644 Binary files a/doc/LectureNotes/_build/.doctrees/environment.pickle and b/doc/LectureNotes/_build/.doctrees/environment.pickle differ diff --git a/doc/LectureNotes/_build/.doctrees/linalg.doctree b/doc/LectureNotes/_build/.doctrees/linalg.doctree index 78ecc225b..4b36bc12e 100644 Binary files a/doc/LectureNotes/_build/.doctrees/linalg.doctree and b/doc/LectureNotes/_build/.doctrees/linalg.doctree differ diff --git a/doc/LectureNotes/_build/.doctrees/schedule.doctree b/doc/LectureNotes/_build/.doctrees/schedule.doctree index 193a5f18e..0847c5db0 100644 Binary files a/doc/LectureNotes/_build/.doctrees/schedule.doctree and b/doc/LectureNotes/_build/.doctrees/schedule.doctree differ diff --git a/doc/LectureNotes/_build/html/_sources/linalg.ipynb b/doc/LectureNotes/_build/html/_sources/linalg.ipynb index 874a51b2e..e915954aa 100644 --- a/doc/LectureNotes/_build/html/_sources/linalg.ipynb +++ b/doc/LectureNotes/_build/html/_sources/linalg.ipynb @@ -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": {}, diff --git a/doc/LectureNotes/_build/html/_sources/schedule.md b/doc/LectureNotes/_build/html/_sources/schedule.md index 48b07ccca..47ff4b9c3 100644 --- a/doc/LectureNotes/_build/html/_sources/schedule.md +++ b/doc/LectureNotes/_build/html/_sources/schedule.md @@ -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. diff --git a/doc/LectureNotes/_build/html/linalg.html b/doc/LectureNotes/_build/html/linalg.html index 22b1aea4e..4a2a5bbb9 100644 --- a/doc/LectureNotes/_build/html/linalg.html +++ b/doc/LectureNotes/_build/html/linalg.html @@ -54,7 +54,7 @@ - + @@ -139,17 +139,17 @@ @@ -171,12 +176,12 @@ @@ -188,12 +193,12 @@ @@ -205,12 +210,12 @@ @@ -304,14 +309,19 @@ 2.4. Numpy and arrays +
  • + + 2.5. Other Matrix and Vector Operations + +
  • - 2.5. Gaussian Elimination + 2.6. Gaussian Elimination @@ -440,8 +450,8 @@ matrices and vectors.

    -
    [-0.32938847 -2.07110274 -0.2627588   0.68616263  1.65238878 -0.03750367
    -  1.653702    0.71442781  0.2983233  -1.10327559]
    +
    [ 0.63628911  0.14168589  0.80381774 -1.0333934  -0.30509508  0.43222125
    +  2.19605877 -0.10357077 -0.15968414  0.70924636]
     
    @@ -690,8 +700,50 @@ covariance matrix through the np.linalg.eig() function.

    +
    +

    2.5. 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'))
    +
    +
    +
    +
    +
    -

    2.5. Gaussian Elimination

    +

    2.6. Gaussian Elimination

    We start with the linear set of equations

    \[ @@ -1045,7 +1097,7 @@ needed to solve the set of \(n\)

    Thereafter you call the function lubksb(double a, int n, int indx, double w) which uses the LU decomposed matrix \(\bf A\) and the vector \(\bf w\) and returns \(\bf x\) in the same place as \(\bf w\). Upon exit the original content in \(\bf w\) is destroyed. If you wish to keep this information, you should make a backup of it in your calling function.

  • -

    2.5.1. LU Decomposition, the inverse of a matrix

    +

    2.6.1. LU Decomposition, the inverse of a matrix

    If the inverse exists then

    \[ @@ -1128,7 +1180,7 @@ can be written as a vector with unknown entries

    diff --git a/doc/LectureNotes/_build/html/schedule.html b/doc/LectureNotes/_build/html/schedule.html index 6b1d8d26f..00804f141 100644 --- a/doc/LectureNotes/_build/html/schedule.html +++ b/doc/LectureNotes/_build/html/schedule.html @@ -535,7 +535,7 @@

    Week 40 October 4-8