From 0d988ad4fced15738e78292be1a6f2bbcc39b643 Mon Sep 17 00:00:00 2001 From: mhjensen Date: Tue, 29 May 2018 08:18:42 -0400 Subject: [PATCH] adding more code examples --- .../How2ReadData/html/How2ReadData-bs.html | 242 +++++++++++------- .../html/How2ReadData-reveal.html | 169 +++++++----- .../html/How2ReadData-solarized.html | 206 +++++++++------ doc/pub/How2ReadData/html/How2ReadData.html | 206 +++++++++------ doc/pub/How2ReadData/ipynb/How2ReadData.ipynb | 165 ++++++++---- .../ipynb/ipynb-How2ReadData-src.tar.gz | Bin 113365 -> 113365 bytes .../pdf/How2ReadData-beamer-handouts2x3.pdf | Bin 413826 -> 415209 bytes .../How2ReadData/pdf/How2ReadData-beamer.pdf | Bin 389631 -> 391001 bytes .../How2ReadData/pdf/How2ReadData-minted.pdf | Bin 411726 -> 413932 bytes doc/src/How2ReadData/How2ReadData.do.txt | 124 ++++++--- doc/web/course.do.txt | 2 - doc/web/course.html | 66 ----- 12 files changed, 705 insertions(+), 475 deletions(-) diff --git a/doc/pub/How2ReadData/html/How2ReadData-bs.html b/doc/pub/How2ReadData/html/How2ReadData-bs.html index 3c6bc2386..9b92fb382 100644 --- a/doc/pub/How2ReadData/html/How2ReadData-bs.html +++ b/doc/pub/How2ReadData/html/How2ReadData-bs.html @@ -49,33 +49,33 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec5'), - ('Predator-Prey model from ecology', 2, None, '___sec6'), - ('Case study from Hudson bay', 2, None, '___sec7'), - ('Hudson bay data', 2, None, '___sec8'), - ('Plotting the data', 2, None, '___sec9'), + ('Non-Linear Least squares in R', 2, None, '___sec6'), + ('Predator-Prey model from ecology', 2, None, '___sec7'), + ('Case study from Hudson bay', 2, None, '___sec8'), + ('Hudson bay data', 2, None, '___sec9'), + ('Plotting the data', 2, None, '___sec10'), ('Hares and lynx in Hudson bay from 1900 to 1920', 2, None, - '___sec10'), + '___sec11'), ('Why now create a computer model for the hare and lynx ' 'populations?', 2, None, - '___sec11'), - ('The traditional (top-down) approach', 2, None, '___sec12'), - ('Basic mathematics notation', 2, None, '___sec13'), + '___sec12'), + ('The traditional (top-down) approach', 2, None, '___sec13'), + ('Basic mathematics notation', 2, None, '___sec14'), ('Basic dynamics of the population of hares', 2, None, - '___sec14'), - ('Basic dynamics of the population of lynx', 2, None, '___sec15'), - ('Evolution equations', 2, None, '___sec16'), - ('Adapt the model to the Hudson Bay case', 2, None, '___sec17'), - ('The program', 2, None, '___sec18'), - ('The plot', 2, None, '___sec19'), - ('Linear regression in Python', 2, None, '___sec20'), - ('Linear Least squares in R', 2, None, '___sec21'), - ('Non-Linear Least squares in R', 2, None, '___sec22'), + '___sec15'), + ('Basic dynamics of the population of lynx', 2, None, '___sec16'), + ('Evolution equations', 2, None, '___sec17'), + ('Adapt the model to the Hudson Bay case', 2, None, '___sec18'), + ('The program', 2, None, '___sec19'), + ('The plot', 2, None, '___sec20'), + ('Linear regression in Python', 2, None, '___sec21'), + ('Linear Least squares in R', 2, None, '___sec22'), ('Example: ecoli lab experiment', 2, None, '___sec23'), ('The program', 2, None, '___sec24'), ('The output', 2, None, '___sec25'), @@ -133,23 +133,23 @@ MathJax.Hub.Config({
  • Installing R, C++, cython or Julia
  • Installing R, C++, cython, Numba etc
  • Simple linear regression model using scikit-learn
  • -
  • Predator-Prey model from ecology
  • -
  • Case study from Hudson bay
  • -
  • Hudson bay data
  • -
  • Plotting the data
  • -
  • Hares and lynx in Hudson bay from 1900 to 1920
  • -
  • Why now create a computer model for the hare and lynx populations?
  • -
  • The traditional (top-down) approach
  • -
  • Basic mathematics notation
  • -
  • Basic dynamics of the population of hares
  • -
  • Basic dynamics of the population of lynx
  • -
  • Evolution equations
  • -
  • Adapt the model to the Hudson Bay case
  • -
  • The program
  • -
  • The plot
  • -
  • Linear regression in Python
  • -
  • Linear Least squares in R
  • -
  • Non-Linear Least squares in R
  • +
  • Non-Linear Least squares in R
  • +
  • Predator-Prey model from ecology
  • +
  • Case study from Hudson bay
  • +
  • Hudson bay data
  • +
  • Plotting the data
  • +
  • Hares and lynx in Hudson bay from 1900 to 1920
  • +
  • Why now create a computer model for the hare and lynx populations?
  • +
  • The traditional (top-down) approach
  • +
  • Basic mathematics notation
  • +
  • Basic dynamics of the population of hares
  • +
  • Basic dynamics of the population of lynx
  • +
  • Evolution equations
  • +
  • Adapt the model to the Hudson Bay case
  • +
  • The program
  • +
  • The plot
  • +
  • Linear regression in Python
  • +
  • Linear Least squares in R
  • Example: ecoli lab experiment
  • The program
  • The output
  • @@ -191,7 +191,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    May 28, 2018

    +

    May 29, 2018


    @@ -224,16 +224,20 @@ then use machine learning algorithms included in for example scikit-learn.

    -These examples will serve us the purpose of getting started. Furthermore, they -allow us to catch more than two birds with a stone. They will allow us -to bring in some programming specific topics and tools as well as -showing the power of various Python (and R) packages for machine -learning and statistical data analysis. In the lectures on linear -algebra we cover in more detail various programming features of languages like Python and C++ (and other), we will also look into more specific linear functions which -are relevant for the various algorithms we will discuss. Here, we will -mainly focus on two specific Python packages for Machine Learning, -scikit-learn and tensorflow (see below for links etc). -Moreover, the examples we introduce will serve as inputs to many of our discussions later, as well as allowing you to set up models and produce your own data and get started with programming. +These examples will serve us the purpose of getting +started. Furthermore, they allow us to catch more than two birds with +a stone. They will allow us to bring in some programming specific +topics and tools as well as showing the power of various Python (and +R) packages for machine learning and statistical data analysis. In the +lectures on linear algebra we cover in more detail various programming +features of languages like Python and C++ (and other), we will also +look into more specific linear functions which are relevant for the +various algorithms we will discuss. Here, we will mainly focus on two +specific Python packages for Machine Learning, scikit-learn and +tensorflow (see below for links etc). Moreover, the examples we +introduce will serve as inputs to many of our discussions later, as +well as allowing you to set up models and produce your own data and +get started with programming.

    @@ -618,7 +622,86 @@ years etc.

    We will discuss in more -detail these and more function in the various lectures. +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +

    + + +

    import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import Ridge
    +from sklearn.preprocessing import PolynomialFeatures
    +from sklearn.pipeline import make_pipeline
    +
    +def f(x):
    +    """ function to approximate by polynomial interpolation"""
    +    return x*x*x
    +
    +# generate points used to plot                                                  
    +x_plot = np.linspace(0, 10, 100)
    +
    +# generate points and keep a subset of them                                     
    +x = np.linspace(0, 10, 100)
    +rng = np.random.RandomState(0)
    +rng.shuffle(x)
    +x = np.sort(x[:20])
    +y = f(x)
    +# create matrix versions of these arrays                                        
    +X = x[:, np.newaxis]
    +X_plot = x_plot[:, np.newaxis]
    +
    +colors = ['teal', 'yellowgreen', 'gold']
    +lw = 2
    +plt.plot(x_plot, f(x_plot), color='cornflowerblue', linewidth=lw,
    +         label="ground truth")
    +plt.scatter(x, y, color='navy', s=30, marker='o', label="training points")
    +
    +for count, degree in enumerate([3, 4, 5]):
    +    model = make_pipeline(PolynomialFeatures(degree), Ridge())
    +    model.fit(X, y)
    +    y_plot = model.predict(X_plot)
    +    plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
    +             label="degree %d" % degree)
    +
    +plt.legend(loc='lower left')
    +
    +plt.show()
    +
    +

    + + +

    Non-Linear Least squares in R

    +
    +
    +

    +

    + + +

    set.seed(1485)
    +len = 24
    +x = runif(len)
    +y = x^3+rnorm(len, 0,0.06)
    +ds = data.frame(x = x, y = y)
    +str(ds)
    +plot( y ~ x, main ="Known cubic with noise")
    +s  = seq(0,1,length =100)
    +lines(s, s^3, lty =2, col ="green")
    +m = nls(y ~ I(x^power), data = ds, start = list(power=1), trace = T)
    +class(m)
    +summary(m)
    +power = round(summary(m)$coefficients[1], 3)
    +power.se = round(summary(m)$coefficients[2], 3)
    +plot(y ~ x, main = "Fitted power model", sub = "Blue: fit; green: known")
    +s = seq(0, 1, length = 100)
    +lines(s, s^3, lty = 2, col = "green")
    +lines(s, predict(m, list(x = s)), lty = 1, col = "blue")
    +text(0, 0.5, paste("y =x^ (", power, " +/- ", power.se, ")", sep = ""), pos = 4)
    +
    +

    +

    +
    +

    Another useful Python package is @@ -638,7 +721,7 @@ display(data_pandas)

    -

    Predator-Prey model from ecology

    +

    Predator-Prey model from ecology

    @@ -663,7 +746,7 @@ scientific method:

    -

    Case study from Hudson bay

    +

    Case study from Hudson bay

    @@ -685,7 +768,7 @@ Here we start by

    -

    Hudson bay data

    +

    Hudson bay data

    @@ -740,7 +823,7 @@ One reason that this particular system has been so extensively studied is that t

    -

    Plotting the data

    +

    Plotting the data

    @@ -777,7 +860,7 @@ plt.show()

    -

    Hares and lynx in Hudson bay from 1900 to 1920

    +

    Hares and lynx in Hudson bay from 1900 to 1920





    @@ -785,7 +868,7 @@ plt.show()

    -

    Why now create a computer model for the hare and lynx populations?

    +

    Why now create a computer model for the hare and lynx populations?

    @@ -819,7 +902,7 @@ climate and other complicating factors. How significant are these?

    -

    The traditional (top-down) approach

    +

    The traditional (top-down) approach

    @@ -853,7 +936,7 @@ ODEs (which cannot be solved)

    -

    Basic mathematics notation

    +

    Basic mathematics notation

    @@ -874,7 +957,7 @@ ODEs (which cannot be solved)

    -

    Basic dynamics of the population of hares

    +

    Basic dynamics of the population of hares

    @@ -903,7 +986,7 @@ $$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$

    -

    Basic dynamics of the population of lynx

    +

    Basic dynamics of the population of lynx

    @@ -934,7 +1017,7 @@ $$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$

    -

    Evolution equations

    +

    Evolution equations

    @@ -964,7 +1047,7 @@ Note:

    -

    Adapt the model to the Hudson Bay case

    +

    Adapt the model to the Hudson Bay case

    @@ -988,7 +1071,7 @@ Note:

    -

    The program

    +

    The program

    @@ -1049,7 +1132,7 @@ plt.show()

    -

    The plot

    +

    The plot





    @@ -1060,7 +1143,7 @@ If we perform a least-square fitting, we can find optimal values for the paramet

    -

    Linear regression in Python

    +

    Linear regression in Python

    @@ -1094,7 +1177,7 @@ plt.show()

    -

    Linear Least squares in R

    +

    Linear Least squares in R

    @@ -1126,41 +1209,6 @@ predict(linearMod,data.frame(Year -

    - - -

    Non-Linear Least squares in R

    -
    -
    -

    -

    - - -

    set.seed(1485)
    -len = 24
    -x = runif(len)
    -y = x^3+rnorm(len, 0,0.06)
    -ds = data.frame(x = x, y = y)
    -str(ds)
    -plot( y ~ x, main ="Known cubic with noise")
    -s  = seq(0,1,length =100)
    -lines(s, s^3, lty =2, col ="green")
    -m = nls(y ~ I(x^power), data = ds, start = list(power=1), trace = T)
    -class(m)
    -summary(m)
    -power = round(summary(m)$coefficients[1], 3)
    -power.se = round(summary(m)$coefficients[2], 3)
    -plot(y ~ x, main = "Fitted power model", sub = "Blue: fit; green: known")
    -s = seq(0, 1, length = 100)
    -lines(s, s^3, lty = 2, col = "green")
    -lines(s, predict(m, list(x = s)), lty = 1, col = "blue")
    -text(0, 0.5, paste("y =x^ (", power, " +/- ", power.se, ")", sep = ""), pos = 4)
    -
    -

    -

    -
    - -

    diff --git a/doc/pub/How2ReadData/html/How2ReadData-reveal.html b/doc/pub/How2ReadData/html/How2ReadData-reveal.html index 1aa9ac1b8..519fc4df6 100644 --- a/doc/pub/How2ReadData/html/How2ReadData-reveal.html +++ b/doc/pub/How2ReadData/html/How2ReadData-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({

    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

     
    -

    May 28, 2018

    +

    May 29, 2018


    @@ -184,16 +184,20 @@ then use machine learning algorithms included in for example scikit-learn.

    -These examples will serve us the purpose of getting started. Furthermore, they -allow us to catch more than two birds with a stone. They will allow us -to bring in some programming specific topics and tools as well as -showing the power of various Python (and R) packages for machine -learning and statistical data analysis. In the lectures on linear -algebra we cover in more detail various programming features of languages like Python and C++ (and other), we will also look into more specific linear functions which -are relevant for the various algorithms we will discuss. Here, we will -mainly focus on two specific Python packages for Machine Learning, -scikit-learn and tensorflow (see below for links etc). -Moreover, the examples we introduce will serve as inputs to many of our discussions later, as well as allowing you to set up models and produce your own data and get started with programming. +These examples will serve us the purpose of getting +started. Furthermore, they allow us to catch more than two birds with +a stone. They will allow us to bring in some programming specific +topics and tools as well as showing the power of various Python (and +R) packages for machine learning and statistical data analysis. In the +lectures on linear algebra we cover in more detail various programming +features of languages like Python and C++ (and other), we will also +look into more specific linear functions which are relevant for the +various algorithms we will discuss. Here, we will mainly focus on two +specific Python packages for Machine Learning, scikit-learn and +tensorflow (see below for links etc). Moreover, the examples we +introduce will serve as inputs to many of our discussions later, as +well as allowing you to set up models and produce your own data and +get started with programming. @@ -601,7 +605,84 @@ years etc.

    We will discuss in more -detail these and more function in the various lectures. +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +

    + + +

    import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import Ridge
    +from sklearn.preprocessing import PolynomialFeatures
    +from sklearn.pipeline import make_pipeline
    +
    +def f(x):
    +    """ function to approximate by polynomial interpolation"""
    +    return x*x*x
    +
    +# generate points used to plot                                                  
    +x_plot = np.linspace(0, 10, 100)
    +
    +# generate points and keep a subset of them                                     
    +x = np.linspace(0, 10, 100)
    +rng = np.random.RandomState(0)
    +rng.shuffle(x)
    +x = np.sort(x[:20])
    +y = f(x)
    +# create matrix versions of these arrays                                        
    +X = x[:, np.newaxis]
    +X_plot = x_plot[:, np.newaxis]
    +
    +colors = ['teal', 'yellowgreen', 'gold']
    +lw = 2
    +plt.plot(x_plot, f(x_plot), color='cornflowerblue', linewidth=lw,
    +         label="ground truth")
    +plt.scatter(x, y, color='navy', s=30, marker='o', label="training points")
    +
    +for count, degree in enumerate([3, 4, 5]):
    +    model = make_pipeline(PolynomialFeatures(degree), Ridge())
    +    model.fit(X, y)
    +    y_plot = model.predict(X_plot)
    +    plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
    +             label="degree %d" % degree)
    +
    +plt.legend(loc='lower left')
    +
    +plt.show()
    +
    + + + +
    +

    Non-Linear Least squares in R

    +
    + +

    + + +

    set.seed(1485)
    +len = 24
    +x = runif(len)
    +y = x^3+rnorm(len, 0,0.06)
    +ds = data.frame(x = x, y = y)
    +str(ds)
    +plot( y ~ x, main ="Known cubic with noise")
    +s  = seq(0,1,length =100)
    +lines(s, s^3, lty =2, col ="green")
    +m = nls(y ~ I(x^power), data = ds, start = list(power=1), trace = T)
    +class(m)
    +summary(m)
    +power = round(summary(m)$coefficients[1], 3)
    +power.se = round(summary(m)$coefficients[2], 3)
    +plot(y ~ x, main = "Fitted power model", sub = "Blue: fit; green: known")
    +s = seq(0, 1, length = 100)
    +lines(s, s^3, lty = 2, col = "green")
    +lines(s, predict(m, list(x = s)), lty = 1, col = "blue")
    +text(0, 0.5, paste("y =x^ (", power, " +/- ", power.se, ")", sep = ""), pos = 4)
    +
    + +

    Another useful Python package is @@ -622,7 +703,7 @@ display(data_pandas)

    -

    Predator-Prey model from ecology

    +

    Predator-Prey model from ecology

    @@ -645,7 +726,7 @@ scientific method:
    -

    Case study from Hudson bay

    +

    Case study from Hudson bay

    @@ -665,7 +746,7 @@ Here we start by
    -

    Hudson bay data

    +

    Hudson bay data

    @@ -711,7 +792,7 @@ One reason that this particular system has been so extensively studied is that t
    -

    Plotting the data

    +

    Plotting the data

    @@ -745,7 +826,7 @@ plt.show()
    -

    Hares and lynx in Hudson bay from 1900 to 1920

    +

    Hares and lynx in Hudson bay from 1900 to 1920





    @@ -753,7 +834,7 @@ plt.show()
    -

    Why now create a computer model for the hare and lynx populations?

    +

    Why now create a computer model for the hare and lynx populations?

    @@ -785,7 +866,7 @@ climate and other complicating factors. How significant are these?

    -

    The traditional (top-down) approach

    +

    The traditional (top-down) approach

    @@ -820,7 +901,7 @@ ODEs (which cannot be solved)
    -

    Basic mathematics notation

    +

    Basic mathematics notation

    -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -219,12 +213,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -255,12 +243,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -291,12 +273,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -327,12 +303,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -363,12 +333,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -399,12 +363,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -435,12 +393,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -471,12 +423,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -507,12 +453,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML:
  • @@ -543,12 +483,6 @@ formulas in HTML or ipython notebook files.
  • Standard one-page format
  • -
  • For screen viewing:
  • - - -
  • HTML: