From 40023e523f9e046f91e838850a3beeab3cc28f0b Mon Sep 17 00:00:00 2001 From: mhjensen Date: Mon, 9 Nov 2020 23:21:39 +0100 Subject: [PATCH] updating week 46 --- doc/pub/week46/html/._week46-bs000.html | 116 +++---- doc/pub/week46/html/._week46-bs001.html | 118 +++---- doc/pub/week46/html/._week46-bs002.html | 142 ++++---- doc/pub/week46/html/._week46-bs003.html | 197 ++++------- doc/pub/week46/html/._week46-bs004.html | 154 +++++---- doc/pub/week46/html/._week46-bs005.html | 225 ++++++++----- doc/pub/week46/html/._week46-bs006.html | 154 +++++---- doc/pub/week46/html/._week46-bs007.html | 152 +++++---- doc/pub/week46/html/._week46-bs008.html | 150 +++++---- doc/pub/week46/html/._week46-bs009.html | 134 ++++---- doc/pub/week46/html/._week46-bs010.html | 141 ++++---- doc/pub/week46/html/._week46-bs011.html | 134 ++++---- doc/pub/week46/html/._week46-bs012.html | 156 ++++----- doc/pub/week46/html/._week46-bs013.html | 164 ++++----- doc/pub/week46/html/._week46-bs014.html | 167 +++++----- doc/pub/week46/html/._week46-bs015.html | 165 ++++----- doc/pub/week46/html/._week46-bs016.html | 157 +++++---- doc/pub/week46/html/._week46-bs017.html | 151 +++++---- doc/pub/week46/html/._week46-bs018.html | 149 ++++----- doc/pub/week46/html/._week46-bs019.html | 160 ++++----- doc/pub/week46/html/._week46-bs020.html | 199 +++++------ doc/pub/week46/html/._week46-bs021.html | 167 +++++----- doc/pub/week46/html/._week46-bs022.html | 209 +++++++----- doc/pub/week46/html/._week46-bs023.html | 173 +++++----- doc/pub/week46/html/._week46-bs024.html | 334 +++++-------------- doc/pub/week46/html/._week46-bs025.html | 155 +++++---- doc/pub/week46/html/._week46-bs026.html | 322 ++++++++++++++---- doc/pub/week46/html/._week46-bs027.html | 181 ++++------ doc/pub/week46/html/._week46-bs028.html | 147 ++++---- doc/pub/week46/html/week46-bs.html | 116 +++---- doc/pub/week46/html/week46-reveal.html | 79 +++-- doc/pub/week46/html/week46-solarized.html | 135 ++++---- doc/pub/week46/html/week46.html | 135 ++++---- doc/pub/week46/ipynb/ipynb-week46-src.tar.gz | Bin 191 -> 191 bytes doc/pub/week46/ipynb/week46.ipynb | 15 +- doc/src/week46/week46.do.txt | 15 +- 36 files changed, 2809 insertions(+), 2659 deletions(-) diff --git a/doc/pub/week46/html/._week46-bs000.html b/doc/pub/week46/html/._week46-bs000.html index 8529411ec..3f75fff30 100644 --- a/doc/pub/week46/html/._week46-bs000.html +++ b/doc/pub/week46/html/._week46-bs000.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -174,7 +178,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Nov 8, 2020

+

Nov 9, 2020


@@ -198,7 +202,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs001.html b/doc/pub/week46/html/._week46-bs001.html index 383488ddc..f3a79a03d 100644 --- a/doc/pub/week46/html/._week46-bs001.html +++ b/doc/pub/week46/html/._week46-bs001.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -158,8 +162,8 @@ MathJax.Hub.Config({

    Overview of week 46

    Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) of Hastie et al contains also a good discussion. @@ -184,7 +188,7 @@ Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) o
  • 10
  • 11
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs002.html b/doc/pub/week46/html/._week46-bs002.html index cbafddb0b..836bc284b 100644 --- a/doc/pub/week46/html/._week46-bs002.html +++ b/doc/pub/week46/html/._week46-bs002.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,35 +159,13 @@ MathJax.Hub.Config({ -

    Support Vector Machines, overarching aims

    +

    Thursday

    -A Support Vector Machine (SVM) is a very powerful and versatile -Machine Learning method, capable of performing linear or nonlinear -classification, regression, and even outlier detection. It is one of -the most popular models in Machine Learning, and anyone interested in -Machine Learning should have it in their toolbox. SVMs are -particularly well suited for classification of complex but small-sized or -medium-sized datasets. +The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie et al. chapter 10.1-10.10.

    -The case with two well-separated classes only can be understood in an -intuitive way in terms of lines in a two-dimensional space separating -the two classes (see figure below). - -

    -The basic mathematics behind the SVM is however less familiar to most of us. -It relies on the definition of hyperplanes and the -definition of a margin which separates classes (in case of -classification problems) of variables. It is also used for regression -problems. - -

    -With SVMs we distinguish between hard margin and soft margins. The -latter introduces a so-called softening parameter to be discussed -below. We distinguish also between linear and non-linear -approaches. The latter are the most frequent ones since it is rather -unlikely that we can separate classes easily by say straight lines. +The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education.

    @@ -203,7 +185,7 @@ unlikely that we can separate classes easily by say straight lines.

  • 11
  • 12
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs003.html b/doc/pub/week46/html/._week46-bs003.html index ce315df72..04a6d876b 100644 --- a/doc/pub/week46/html/._week46-bs003.html +++ b/doc/pub/week46/html/._week46-bs003.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,90 +159,11 @@ MathJax.Hub.Config({ -

    Hyperplanes and all that

    +

    Friday

    -The theory behind support vector machines (SVM hereafter) is based on -the mathematical description of so-called hyperplanes. Let us start -with a two-dimensional case. This will also allow us to introduce our -first SVM examples. These will be tailored to the case of two specific -classes, as displayed in the figure here based on the usage of the petal data. +We start with a new topic and second-last topic covered in this course, support vector machines (SVM). -

    -We assume here that our data set can be well separated into two -domains, where a straight line does the job in the separating the two -classes. Here the two classes are represented by either squares or -circles. -

    - - -

    from sklearn import datasets
    -from sklearn.svm import SVC, LinearSVC
    -from sklearn.linear_model import SGDClassifier
    -from sklearn.preprocessing import StandardScaler
    -import matplotlib
    -import matplotlib.pyplot as plt
    -plt.rcParams['axes.labelsize'] = 14
    -plt.rcParams['xtick.labelsize'] = 12
    -plt.rcParams['ytick.labelsize'] = 12
    -
    -
    -iris = datasets.load_iris()
    -X = iris["data"][:, (2, 3)]  # petal length, petal width
    -y = iris["target"]
    -
    -setosa_or_versicolor = (y == 0) | (y == 1)
    -X = X[setosa_or_versicolor]
    -y = y[setosa_or_versicolor]
    -
    -
    -
    -C = 5
    -alpha = 1 / (C * len(X))
    -
    -lin_clf = LinearSVC(loss="hinge", C=C, random_state=42)
    -svm_clf = SVC(kernel="linear", C=C)
    -sgd_clf = SGDClassifier(loss="hinge", learning_rate="constant", eta0=0.001, alpha=alpha,
    -                        max_iter=100000, random_state=42)
    -
    -scaler = StandardScaler()
    -X_scaled = scaler.fit_transform(X)
    -
    -lin_clf.fit(X_scaled, y)
    -svm_clf.fit(X_scaled, y)
    -sgd_clf.fit(X_scaled, y)
    -
    -print("LinearSVC:                   ", lin_clf.intercept_, lin_clf.coef_)
    -print("SVC:                         ", svm_clf.intercept_, svm_clf.coef_)
    -print("SGDClassifier(alpha={:.5f}):".format(sgd_clf.alpha), sgd_clf.intercept_, sgd_clf.coef_)
    -
    -# Compute the slope and bias of each decision boundary
    -w1 = -lin_clf.coef_[0, 0]/lin_clf.coef_[0, 1]
    -b1 = -lin_clf.intercept_[0]/lin_clf.coef_[0, 1]
    -w2 = -svm_clf.coef_[0, 0]/svm_clf.coef_[0, 1]
    -b2 = -svm_clf.intercept_[0]/svm_clf.coef_[0, 1]
    -w3 = -sgd_clf.coef_[0, 0]/sgd_clf.coef_[0, 1]
    -b3 = -sgd_clf.intercept_[0]/sgd_clf.coef_[0, 1]
    -
    -# Transform the decision boundary lines back to the original scale
    -line1 = scaler.inverse_transform([[-10, -10 * w1 + b1], [10, 10 * w1 + b1]])
    -line2 = scaler.inverse_transform([[-10, -10 * w2 + b2], [10, 10 * w2 + b2]])
    -line3 = scaler.inverse_transform([[-10, -10 * w3 + b3], [10, 10 * w3 + b3]])
    -
    -# Plot all three decision boundaries
    -plt.figure(figsize=(11, 4))
    -plt.plot(line1[:, 0], line1[:, 1], "k:", label="LinearSVC")
    -plt.plot(line2[:, 0], line2[:, 1], "b--", linewidth=2, label="SVC")
    -plt.plot(line3[:, 0], line3[:, 1], "r-", label="SGDClassifier")
    -plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs") # label="Iris-Versicolor"
    -plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo") # label="Iris-Setosa"
    -plt.xlabel("Petal length", fontsize=14)
    -plt.ylabel("Petal width", fontsize=14)
    -plt.legend(loc="upper center", fontsize=14)
    -plt.axis([0, 5.5, 0, 2])
    -
    -plt.show()
    -

    @@ -258,7 +183,7 @@ plt.show()

  • 12
  • 13
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs004.html b/doc/pub/week46/html/._week46-bs004.html index 6481b6c70..d00514c58 100644 --- a/doc/pub/week46/html/._week46-bs004.html +++ b/doc/pub/week46/html/._week46-bs004.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,33 +159,35 @@ MathJax.Hub.Config({ -

    What is a hyperplane?

    +

    Support Vector Machines, overarching aims

    -The aim of the SVM algorithm is to find a hyperplane in a -\( p \)-dimensional space, where \( p \) is the number of features that -distinctly classifies the data points. +A Support Vector Machine (SVM) is a very powerful and versatile +Machine Learning method, capable of performing linear or nonlinear +classification, regression, and even outlier detection. It is one of +the most popular models in Machine Learning, and anyone interested in +Machine Learning should have it in their toolbox. SVMs are +particularly well suited for classification of complex but small-sized or +medium-sized datasets.

    -In a \( p \)-dimensional space, a hyperplane is what we call an affine subspace of dimension of \( p-1 \). -As an example, in two dimension, a hyperplane is simply as straight line while in three dimensions it is -a two-dimensional subspace, or stated simply, a plane. +The case with two well-separated classes only can be understood in an +intuitive way in terms of lines in a two-dimensional space separating +the two classes (see figure below).

    -In two dimensions, with the variables \( x_1 \) and \( x_2 \), the hyperplane is defined as -$$ -b+w_1x_1+w_2x_2=0, -$$ +The basic mathematics behind the SVM is however less familiar to most of us. +It relies on the definition of hyperplanes and the +definition of a margin which separates classes (in case of +classification problems) of variables. It is also used for regression +problems.

    -where \( b \) is the intercept and \( w_1 \) and \( w_2 \) define the elements of a vector orthogonal to the line -\( b+w_1x_1+w_2x_2=0 \). -In two dimensions we define the vectors \( \boldsymbol{x} =[x1,x2] \) and \( \boldsymbol{w}=[w1,w2] \). -We can then rewrite the above equation as - -$$ -\boldsymbol{x}^T\boldsymbol{w}+b=0. -$$ +With SVMs we distinguish between hard margin and soft margins. The +latter introduces a so-called softening parameter to be discussed +below. We distinguish also between linear and non-linear +approaches. The latter are the most frequent ones since it is rather +unlikely that we can separate classes easily by say straight lines.

    @@ -203,7 +209,7 @@ $$

  • 13
  • 14
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs005.html b/doc/pub/week46/html/._week46-bs005.html index bed17adb3..8fe7957b6 100644 --- a/doc/pub/week46/html/._week46-bs005.html +++ b/doc/pub/week46/html/._week46-bs005.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,45 +159,90 @@ MathJax.Hub.Config({ -

    A \( p \)-dimensional space of features

    +

    Hyperplanes and all that

    -We limit ourselves to two classes of outputs \( y_i \) and assign these classes the values \( y_i = \pm 1 \). -In a \( p \)-dimensional space of say \( p \) features we have a hyperplane defines as -$$ -b+wx_1+w_2x_2+\dots +w_px_p=0. -$$ - -If we define a -matrix \( \boldsymbol{X}=\left[\boldsymbol{x}_1,\boldsymbol{x}_2,\dots, \boldsymbol{x}_p\right] \) -of dimension \( n\times p \), where \( n \) represents the observations for each feature and each vector \( x_i \) is a column vector of the matrix \( \boldsymbol{X} \), -$$ -\boldsymbol{x}_i = \begin{bmatrix} x_{i1} \\ x_{i2} \\ \dots \\ \dots \\ x_{ip} \end{bmatrix}. -$$ - -If the above condition is not met for a given vector \( \boldsymbol{x}_i \) we have -$$ -b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip} >0, -$$ - -if our output \( y_i=1 \). -In this case we say that \( \boldsymbol{x}_i \) lies on one of the sides of the hyperplane and if -$$ -b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip} < 0, -$$ - -for the class of observations \( y_i=-1 \), -then \( \boldsymbol{x}_i \) lies on the other side. +The theory behind support vector machines (SVM hereafter) is based on +the mathematical description of so-called hyperplanes. Let us start +with a two-dimensional case. This will also allow us to introduce our +first SVM examples. These will be tailored to the case of two specific +classes, as displayed in the figure here based on the usage of the petal data.

    -Equivalently, for the two classes of observations we have -$$ -y_i\left(b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip}\right) > 0. -$$ - +We assume here that our data set can be well separated into two +domains, where a straight line does the job in the separating the two +classes. Here the two classes are represented by either squares or +circles.

    -When we try to separate hyperplanes, if it exists, we can use it to construct a natural classifier: a test observation is assigned a given class depending on which side of the hyperplane it is located. + +

    from sklearn import datasets
    +from sklearn.svm import SVC, LinearSVC
    +from sklearn.linear_model import SGDClassifier
    +from sklearn.preprocessing import StandardScaler
    +import matplotlib
    +import matplotlib.pyplot as plt
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +
    +iris = datasets.load_iris()
    +X = iris["data"][:, (2, 3)]  # petal length, petal width
    +y = iris["target"]
    +
    +setosa_or_versicolor = (y == 0) | (y == 1)
    +X = X[setosa_or_versicolor]
    +y = y[setosa_or_versicolor]
    +
    +
    +
    +C = 5
    +alpha = 1 / (C * len(X))
    +
    +lin_clf = LinearSVC(loss="hinge", C=C, random_state=42)
    +svm_clf = SVC(kernel="linear", C=C)
    +sgd_clf = SGDClassifier(loss="hinge", learning_rate="constant", eta0=0.001, alpha=alpha,
    +                        max_iter=100000, random_state=42)
    +
    +scaler = StandardScaler()
    +X_scaled = scaler.fit_transform(X)
    +
    +lin_clf.fit(X_scaled, y)
    +svm_clf.fit(X_scaled, y)
    +sgd_clf.fit(X_scaled, y)
    +
    +print("LinearSVC:                   ", lin_clf.intercept_, lin_clf.coef_)
    +print("SVC:                         ", svm_clf.intercept_, svm_clf.coef_)
    +print("SGDClassifier(alpha={:.5f}):".format(sgd_clf.alpha), sgd_clf.intercept_, sgd_clf.coef_)
    +
    +# Compute the slope and bias of each decision boundary
    +w1 = -lin_clf.coef_[0, 0]/lin_clf.coef_[0, 1]
    +b1 = -lin_clf.intercept_[0]/lin_clf.coef_[0, 1]
    +w2 = -svm_clf.coef_[0, 0]/svm_clf.coef_[0, 1]
    +b2 = -svm_clf.intercept_[0]/svm_clf.coef_[0, 1]
    +w3 = -sgd_clf.coef_[0, 0]/sgd_clf.coef_[0, 1]
    +b3 = -sgd_clf.intercept_[0]/sgd_clf.coef_[0, 1]
    +
    +# Transform the decision boundary lines back to the original scale
    +line1 = scaler.inverse_transform([[-10, -10 * w1 + b1], [10, 10 * w1 + b1]])
    +line2 = scaler.inverse_transform([[-10, -10 * w2 + b2], [10, 10 * w2 + b2]])
    +line3 = scaler.inverse_transform([[-10, -10 * w3 + b3], [10, 10 * w3 + b3]])
    +
    +# Plot all three decision boundaries
    +plt.figure(figsize=(11, 4))
    +plt.plot(line1[:, 0], line1[:, 1], "k:", label="LinearSVC")
    +plt.plot(line2[:, 0], line2[:, 1], "b--", linewidth=2, label="SVC")
    +plt.plot(line3[:, 0], line3[:, 1], "r-", label="SGDClassifier")
    +plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs") # label="Iris-Versicolor"
    +plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo") # label="Iris-Setosa"
    +plt.xlabel("Petal length", fontsize=14)
    +plt.ylabel("Petal width", fontsize=14)
    +plt.legend(loc="upper center", fontsize=14)
    +plt.axis([0, 5.5, 0, 2])
    +
    +plt.show()
    +

    @@ -215,7 +264,7 @@ When we try to separate hyperplanes, if it exists, we can use it to construct a

  • 14
  • 15
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs006.html b/doc/pub/week46/html/._week46-bs006.html index a4374aaa2..b94a948ca 100644 --- a/doc/pub/week46/html/._week46-bs006.html +++ b/doc/pub/week46/html/._week46-bs006.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -153,31 +157,35 @@ MathJax.Hub.Config({

     

     

     

    - + -

    The two-dimensional case

    +

    What is a hyperplane?

    -Let us try to develop our intuition about SVMs by limiting ourselves to a two-dimensional -plane. To separate the two classes of data points, there are many -possible lines (hyperplanes if you prefer a more strict naming) -that could be chosen. Our objective is to find a -plane that has the maximum margin, i.e the maximum distance between -data points of both classes. Maximizing the margin distance provides -some reinforcement so that future data points can be classified with -more confidence. +The aim of the SVM algorithm is to find a hyperplane in a +\( p \)-dimensional space, where \( p \) is the number of features that +distinctly classifies the data points.

    -What a linear classifier attempts to accomplish is to split the -feature space into two half spaces by placing a hyperplane between the -data points. This hyperplane will be our decision boundary. All -points on one side of the plane will belong to class one and all points -on the other side of the plane will belong to the second class two. +In a \( p \)-dimensional space, a hyperplane is what we call an affine subspace of dimension of \( p-1 \). +As an example, in two dimension, a hyperplane is simply as straight line while in three dimensions it is +a two-dimensional subspace, or stated simply, a plane.

    -Unfortunately there are many ways in which we can place a hyperplane -to divide the data. Below is an example of two candidate hyperplanes -for our data sample. +In two dimensions, with the variables \( x_1 \) and \( x_2 \), the hyperplane is defined as +$$ +b+w_1x_1+w_2x_2=0, +$$ + +

    +where \( b \) is the intercept and \( w_1 \) and \( w_2 \) define the elements of a vector orthogonal to the line +\( b+w_1x_1+w_2x_2=0 \). +In two dimensions we define the vectors \( \boldsymbol{x} =[x1,x2] \) and \( \boldsymbol{w}=[w1,w2] \). +We can then rewrite the above equation as + +$$ +\boldsymbol{x}^T\boldsymbol{w}+b=0. +$$

    @@ -201,7 +209,7 @@ for our data sample.

  • 15
  • 16
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs007.html b/doc/pub/week46/html/._week46-bs007.html index 9653726c2..bb66f674c 100644 --- a/doc/pub/week46/html/._week46-bs007.html +++ b/doc/pub/week46/html/._week46-bs007.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,24 +159,44 @@ MathJax.Hub.Config({ -

    Getting into the details

    +

    A \( p \)-dimensional space of features

    -Let us define the function +We limit ourselves to two classes of outputs \( y_i \) and assign these classes the values \( y_i = \pm 1 \). +In a \( p \)-dimensional space of say \( p \) features we have a hyperplane defines as $$ -f(x) = \boldsymbol{w}^T\boldsymbol{x}+b = 0, +b+wx_1+w_2x_2+\dots +w_px_p=0. $$ -as the function that determines the line \( L \) that separates two classes (our two features), see the figure here. +If we define a +matrix \( \boldsymbol{X}=\left[\boldsymbol{x}_1,\boldsymbol{x}_2,\dots, \boldsymbol{x}_p\right] \) +of dimension \( n\times p \), where \( n \) represents the observations for each feature and each vector \( x_i \) is a column vector of the matrix \( \boldsymbol{X} \), +$$ +\boldsymbol{x}_i = \begin{bmatrix} x_{i1} \\ x_{i2} \\ \dots \\ \dots \\ x_{ip} \end{bmatrix}. +$$ + +If the above condition is not met for a given vector \( \boldsymbol{x}_i \) we have +$$ +b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip} >0, +$$ + +if our output \( y_i=1 \). +In this case we say that \( \boldsymbol{x}_i \) lies on one of the sides of the hyperplane and if +$$ +b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip} < 0, +$$ + +for the class of observations \( y_i=-1 \), +then \( \boldsymbol{x}_i \) lies on the other side.

    -Any point defined by \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_2 \) on the line \( L \) will satisfy \( \boldsymbol{w}^T(\boldsymbol{x}_1-\boldsymbol{x}_2)=0 \). +Equivalently, for the two classes of observations we have +$$ +y_i\left(b+w_1x_{i1}+w_2x_{i2}+\dots +w_px_{ip}\right) > 0. +$$

    -The signed distance \( \delta \) from any point defined by a vector \( \boldsymbol{x} \) and a point \( \boldsymbol{x}_0 \) on the line \( L \) is then -$$ -\delta = \frac{1}{\vert\vert \boldsymbol{w}\vert\vert}(\boldsymbol{w}^T\boldsymbol{x}+b). -$$ +When we try to separate hyperplanes, if it exists, we can use it to construct a natural classifier: a test observation is assigned a given class depending on which side of the hyperplane it is located.

    @@ -197,7 +221,7 @@ $$

  • 16
  • 17
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs008.html b/doc/pub/week46/html/._week46-bs008.html index b5f05a054..7738a1bc4 100644 --- a/doc/pub/week46/html/._week46-bs008.html +++ b/doc/pub/week46/html/._week46-bs008.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -153,29 +157,31 @@ MathJax.Hub.Config({

     

     

     

    - + -

    First attempt at a minimization approach

    +

    The two-dimensional case

    -How do we find the parameter \( b \) and the vector \( \boldsymbol{w} \)? What we could -do is to define a cost function which now contains the set of all -misclassified points \( M \) and attempt to minimize this function - -$$ -C(\boldsymbol{w},b) = -\sum_{i\in M} y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b). -$$ +Let us try to develop our intuition about SVMs by limiting ourselves to a two-dimensional +plane. To separate the two classes of data points, there are many +possible lines (hyperplanes if you prefer a more strict naming) +that could be chosen. Our objective is to find a +plane that has the maximum margin, i.e the maximum distance between +data points of both classes. Maximizing the margin distance provides +some reinforcement so that future data points can be classified with +more confidence.

    -We could now for example define all values \( y_i =1 \) as misclassified in case we have \( \boldsymbol{w}^T\boldsymbol{x}_i+b < 0 \) and the opposite if we have \( y_i=-1 \). Taking the derivatives gives us -$$ -\frac{\partial C}{\partial b} = -\sum_{i\in M} y_i, -$$ +What a linear classifier attempts to accomplish is to split the +feature space into two half spaces by placing a hyperplane between the +data points. This hyperplane will be our decision boundary. All +points on one side of the plane will belong to class one and all points +on the other side of the plane will belong to the second class two. -and -$$ -\frac{\partial C}{\partial \boldsymbol{w}} = -\sum_{i\in M} y_ix_i. -$$ +

    +Unfortunately there are many ways in which we can place a hyperplane +to divide the data. Below is an example of two candidate hyperplanes +for our data sample.

    @@ -201,7 +207,7 @@ $$

  • 17
  • 18
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs009.html b/doc/pub/week46/html/._week46-bs009.html index b7f27e42c..c73260c74 100644 --- a/doc/pub/week46/html/._week46-bs009.html +++ b/doc/pub/week46/html/._week46-bs009.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,20 +159,24 @@ MathJax.Hub.Config({ -

    Solving the equations

    +

    Getting into the details

    -We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations +Let us define the function $$ -b \leftarrow b +\eta \frac{\partial C}{\partial b}, +f(x) = \boldsymbol{w}^T\boldsymbol{x}+b = 0, $$ -and -$$ -\boldsymbol{w} \leftarrow \boldsymbol{w} +\eta \frac{\partial C}{\partial \boldsymbol{w}}, -$$ +as the function that determines the line \( L \) that separates two classes (our two features), see the figure here. -where \( \eta \) is our by now well-known learning rate. +

    +Any point defined by \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_2 \) on the line \( L \) will satisfy \( \boldsymbol{w}^T(\boldsymbol{x}_1-\boldsymbol{x}_2)=0 \). + +

    +The signed distance \( \delta \) from any point defined by a vector \( \boldsymbol{x} \) and a point \( \boldsymbol{x}_0 \) on the line \( L \) is then +$$ +\delta = \frac{1}{\vert\vert \boldsymbol{w}\vert\vert}(\boldsymbol{w}^T\boldsymbol{x}+b). +$$

    @@ -195,7 +203,7 @@ where \( \eta \) is our by now well-known learning rate.

  • 18
  • 19
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs010.html b/doc/pub/week46/html/._week46-bs010.html index 764b4d033..614025bad 100644 --- a/doc/pub/week46/html/._week46-bs010.html +++ b/doc/pub/week46/html/._week46-bs010.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,17 +159,28 @@ MathJax.Hub.Config({ -

    Code Example

    +

    First attempt at a minimization approach

    -The equations we discussed above can be coded rather easily (the -framework is similar to what we developed for logistic -regression). We are going to set up a simple case with two classes only and we want to find a line which separates them the best possible way. -

    +How do we find the parameter \( b \) and the vector \( \boldsymbol{w} \)? What we could +do is to define a cost function which now contains the set of all +misclassified points \( M \) and attempt to minimize this function + +$$ +C(\boldsymbol{w},b) = -\sum_{i\in M} y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b). +$$ + +

    +We could now for example define all values \( y_i =1 \) as misclassified in case we have \( \boldsymbol{w}^T\boldsymbol{x}_i+b < 0 \) and the opposite if we have \( y_i=-1 \). Taking the derivatives gives us +$$ +\frac{\partial C}{\partial b} = -\sum_{i\in M} y_i, +$$ + +and +$$ +\frac{\partial C}{\partial \boldsymbol{w}} = -\sum_{i\in M} y_ix_i. +$$ - -

    
    -

    @@ -192,7 +207,7 @@ regression). We are going to set up a simple case with two classes only and we w

  • 19
  • 20
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs011.html b/doc/pub/week46/html/._week46-bs011.html index 6809c0f25..f5a747095 100644 --- a/doc/pub/week46/html/._week46-bs011.html +++ b/doc/pub/week46/html/._week46-bs011.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,18 +159,20 @@ MathJax.Hub.Config({ -

    Problems with the Simpler Approach

    +

    Solving the equations

    -There are however problems with this approach, although it looks -pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes. +We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations +$$ +b \leftarrow b +\eta \frac{\partial C}{\partial b}, +$$ -

    -For small -gaps between the entries, we may also end up needing many iterations -before the solutions converge and if the data cannot be separated -properly into two distinct classes, we may not experience a converge -at all. +and +$$ +\boldsymbol{w} \leftarrow \boldsymbol{w} +\eta \frac{\partial C}{\partial \boldsymbol{w}}, +$$ + +where \( \eta \) is our by now well-known learning rate.

    @@ -194,7 +200,7 @@ at all.

  • 20
  • 21
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs012.html b/doc/pub/week46/html/._week46-bs012.html index a49e84157..aa0d49ec2 100644 --- a/doc/pub/week46/html/._week46-bs012.html +++ b/doc/pub/week46/html/._week46-bs012.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,45 +159,17 @@ MathJax.Hub.Config({ -

    A better approach

    +

    Code Example

    -A better approach is rather to try to define a large margin between -the two classes (if they are well separated from the beginning). - +The equations we discussed above can be coded rather easily (the +framework is similar to what we developed for logistic +regression). We are going to set up a simple case with two classes only and we want to find a line which separates them the best possible way.

    -Thus, we wish to find a margin \( M \) with \( \boldsymbol{w} \) normalized to -\( \vert\vert \boldsymbol{w}\vert\vert =1 \) subject to the condition - -$$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p. -$$ - -All points are thus at a signed distance from the decision boundary defined by the line \( L \). The parameters \( b \) and \( w_1 \) and \( w_2 \) define this line. - -

    -We seek thus the largest value \( M \) defined by -$$ -\frac{1}{\vert \vert \boldsymbol{w}\vert\vert}y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n, -$$ - -or just -$$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M\vert \vert \boldsymbol{w}\vert\vert \hspace{0.1cm}\forall i. -$$ - -If we scale the equation so that \( \vert \vert \boldsymbol{w}\vert\vert = 1/M \), we have to find the minimum of -\( \boldsymbol{w}^T\boldsymbol{w}=\vert \vert \boldsymbol{w}\vert\vert \) (the norm) subject to the condition -$$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i. -$$ - -

    -We have thus defined our margin as the invers of the norm of -\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as -possible margin \( M \). Before we proceed, we need to remind ourselves -about Lagrangian multipliers. + +

    
    +

    @@ -220,7 +196,7 @@ about Lagrangian multipliers.

  • 21
  • 22
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs013.html b/doc/pub/week46/html/._week46-bs013.html index 91be97a28..ecdc93942 100644 --- a/doc/pub/week46/html/._week46-bs013.html +++ b/doc/pub/week46/html/._week46-bs013.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,52 +159,18 @@ MathJax.Hub.Config({ -

    A quick Reminder on Lagrangian Multipliers

    +

    Problems with the Simpler Approach

    -Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an -extreme we have -$$ -df=0. -$$ - -A necessary and sufficient condition is -$$ -\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0, -$$ - -due to -$$ -df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz. -$$ - -In many problems the variables \( x,y,z \) are often subject to constraints (such as those above for the margin) -so that they are no longer all independent. It is possible at least in principle to use each -constraint to eliminate one variable -and to proceed with a new and smaller set of independent varables. +There are however problems with this approach, although it looks +pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.

    -The use of so-called Lagrangian multipliers is an alternative technique when the elimination -of variables is incovenient or undesirable. Assume that we have an equation of constraint on -the variables \( x,y,z \) -$$ -\phi(x,y,z) = 0, -$$ - - resulting in -$$ -d\phi = \frac{\partial \phi}{\partial x}dx+\frac{\partial \phi}{\partial y}dy+\frac{\partial \phi}{\partial z}dz =0. -$$ - -Now we cannot set anymore -$$ -\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0, -$$ - -if \( df=0 \) is wanted -because there are now only two independent variables! Assume \( x \) and \( y \) are the independent -variables. -Then \( dz \) is no longer arbitrary. +For small +gaps between the entries, we may also end up needing many iterations +before the solutions converge and if the data cannot be separated +properly into two distinct classes, we may not experience a converge +at all.

    @@ -228,7 +198,7 @@ Then \( dz \) is no longer arbitrary.

  • 22
  • 23
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs014.html b/doc/pub/week46/html/._week46-bs014.html index 75259e9ee..b7a16397f 100644 --- a/doc/pub/week46/html/._week46-bs014.html +++ b/doc/pub/week46/html/._week46-bs014.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,45 +159,44 @@ MathJax.Hub.Config({ -

    Adding the Multiplier

    +

    A better approach

    -However, we can add to +A better approach is rather to try to define a large margin between +the two classes (if they are well separated from the beginning). + +

    +Thus, we wish to find a margin \( M \) with \( \boldsymbol{w} \) normalized to +\( \vert\vert \boldsymbol{w}\vert\vert =1 \) subject to the condition + $$ -df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz, +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p. $$ -a multiplum of \( d\phi \), viz. \( \lambda d\phi \), resulting in +All points are thus at a signed distance from the decision boundary defined by the line \( L \). The parameters \( b \) and \( w_1 \) and \( w_2 \) define this line. + +

    +We seek thus the largest value \( M \) defined by $$ -df+\lambda d\phi = (\frac{\partial f}{\partial z}+\lambda -\frac{\partial \phi}{\partial x})dx+(\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y})dy+ -(\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z})dz =0. +\frac{1}{\vert \vert \boldsymbol{w}\vert\vert}y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n, $$ -Our multiplier is chosen so that +or just $$ -\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z} =0. +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M\vert \vert \boldsymbol{w}\vert\vert \hspace{0.1cm}\forall i. +$$ + +If we scale the equation so that \( \vert \vert \boldsymbol{w}\vert\vert = 1/M \), we have to find the minimum of +\( \boldsymbol{w}^T\boldsymbol{w}=\vert \vert \boldsymbol{w}\vert\vert \) (the norm) subject to the condition +$$ +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i. $$

    -We need to remember that we took \( dx \) and \( dy \) to be arbitrary and thus we must have -$$ -\frac{\partial f}{\partial x}+\lambda\frac{\partial \phi}{\partial x} =0, -$$ - -and -$$ -\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y} =0. -$$ - -When all these equations are satisfied, \( df=0 \). We have four unknowns, \( x,y,z \) and -\( \lambda \). Actually we want only \( x,y,z \), \( \lambda \) needs not to be determined, -it is therefore often called -Lagrange's undetermined multiplier. -If we have a set of constraints \( \phi_k \) we have the equations -$$ -\frac{\partial f}{\partial x_i}+\sum_k\lambda_k\frac{\partial \phi_k}{\partial x_i} =0. -$$ +We have thus defined our margin as the invers of the norm of +\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as +possible margin \( M \). Before we proceed, we need to remind ourselves +about Lagrangian multipliers.

    @@ -221,7 +224,7 @@ $$

  • 23
  • 24
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs015.html b/doc/pub/week46/html/._week46-bs015.html index fc035f660..10748aac8 100644 --- a/doc/pub/week46/html/._week46-bs015.html +++ b/doc/pub/week46/html/._week46-bs015.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,43 +159,52 @@ MathJax.Hub.Config({ -

    Setting up the Problem

    -In order to solve the above problem, we define the following Lagrangian function to be minimized -$$ -{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right], -$$ - -where \( \lambda_i \) is a so-called Lagrange multiplier subject to the condition \( \lambda_i \geq 0 \). +

    A quick Reminder on Lagrangian Multipliers

    -Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain +Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an +extreme we have $$ -\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0, +df=0. $$ -and +A necessary and sufficient condition is $$ -\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i. +\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0, $$ -Inserting these constraints into the equation for \( {\cal L} \) we obtain +due to $$ -{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, +df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz. $$ -subject to the constraints \( \lambda_i\geq 0 \) and \( \sum_i\lambda_iy_i=0 \). -We must in addition satisfy the Karush-Kuhn-Tucker (KKT) condition +In many problems the variables \( x,y,z \) are often subject to constraints (such as those above for the margin) +so that they are no longer all independent. It is possible at least in principle to use each +constraint to eliminate one variable +and to proceed with a new and smaller set of independent varables. + +

    +The use of so-called Lagrangian multipliers is an alternative technique when the elimination +of variables is incovenient or undesirable. Assume that we have an equation of constraint on +the variables \( x,y,z \) $$ -\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -1\right] \hspace{0.1cm}\forall i. +\phi(x,y,z) = 0, $$ + resulting in +$$ +d\phi = \frac{\partial \phi}{\partial x}dx+\frac{\partial \phi}{\partial y}dy+\frac{\partial \phi}{\partial z}dz =0. +$$ -

      -
    1. If \( \lambda_i > 0 \), then \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) and we say that \( x_i \) is on the boundary.
    2. -
    3. If \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)> 1 \), we say \( x_i \) is not on the boundary and we set \( \lambda_i=0 \).
    4. -
    +Now we cannot set anymore +$$ +\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0, +$$ -When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin \( M \). +if \( df=0 \) is wanted +because there are now only two independent variables! Assume \( x \) and \( y \) are the independent +variables. +Then \( dz \) is no longer arbitrary.

    @@ -219,7 +232,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support

  • 24
  • 25
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs016.html b/doc/pub/week46/html/._week46-bs016.html index c00ab470f..37b045007 100644 --- a/doc/pub/week46/html/._week46-bs016.html +++ b/doc/pub/week46/html/._week46-bs016.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,26 +159,45 @@ MathJax.Hub.Config({ -

    The problem to solve

    +

    Adding the Multiplier

    -We can rewrite +However, we can add to $$ -{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, +df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz, $$ -and its constraints in terms of a matrix-vector problem where we minimize w.r.t. \( \lambda \) the following problem +a multiplum of \( d\phi \), viz. \( \lambda d\phi \), resulting in $$ -\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1\boldsymbol{x}_1^T\boldsymbol{x}_1 & y_1y_2\boldsymbol{x}_1^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_1^T\boldsymbol{x}_n \\ -y_2y_1\boldsymbol{x}_2^T\boldsymbol{x}_1 & y_2y_2\boldsymbol{x}_2^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_2^T\boldsymbol{x}_n \\ -\dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots \\ -y_ny_1\boldsymbol{x}_n^T\boldsymbol{x}_1 & y_ny_2\boldsymbol{x}_n^T\boldsymbol{x}_2 & \dots & \dots & y_ny_n\boldsymbol{x}_n^T\boldsymbol{x}_n \\ -\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda}, +df+\lambda d\phi = (\frac{\partial f}{\partial z}+\lambda +\frac{\partial \phi}{\partial x})dx+(\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y})dy+ +(\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z})dz =0. $$ -subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and -\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \). +Our multiplier is chosen so that +$$ +\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z} =0. +$$ + +

    +We need to remember that we took \( dx \) and \( dy \) to be arbitrary and thus we must have +$$ +\frac{\partial f}{\partial x}+\lambda\frac{\partial \phi}{\partial x} =0, +$$ + +and +$$ +\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y} =0. +$$ + +When all these equations are satisfied, \( df=0 \). We have four unknowns, \( x,y,z \) and +\( \lambda \). Actually we want only \( x,y,z \), \( \lambda \) needs not to be determined, +it is therefore often called +Lagrange's undetermined multiplier. +If we have a set of constraints \( \phi_k \) we have the equations +$$ +\frac{\partial f}{\partial x_i}+\sum_k\lambda_k\frac{\partial \phi_k}{\partial x_i} =0. +$$

    @@ -202,7 +225,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec

  • 25
  • 26
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs017.html b/doc/pub/week46/html/._week46-bs017.html index 8b8e28c1d..d25f1cba3 100644 --- a/doc/pub/week46/html/._week46-bs017.html +++ b/doc/pub/week46/html/._week46-bs017.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,36 +159,43 @@ MathJax.Hub.Config({ -

    The last steps

    +

    Setting up the Problem

    +In order to solve the above problem, we define the following Lagrangian function to be minimized +$$ +{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right], +$$ + +where \( \lambda_i \) is a so-called Lagrange multiplier subject to the condition \( \lambda_i \geq 0 \).

    -Solving the above problem, yields the values of \( \lambda_i \). -To find the coefficients of your hyperplane we need simply to compute +Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain $$ -\boldsymbol{w}=\sum_{i} \lambda_iy_i\boldsymbol{x}_i. +\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0, $$ -With our vector \( \boldsymbol{w} \) we can in turn find the value of the intercept \( b \) (here in two dimensions) via +and $$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1, +\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i. $$ -resulting in +Inserting these constraints into the equation for \( {\cal L} \) we obtain $$ -b = \frac{1}{y_i}-\boldsymbol{w}^T\boldsymbol{x}_i, +{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, $$ -or if we write it out in terms of the support vectors only, with \( N_s \) being their number, we have +subject to the constraints \( \lambda_i\geq 0 \) and \( \sum_i\lambda_iy_i=0 \). +We must in addition satisfy the Karush-Kuhn-Tucker (KKT) condition $$ -b = \frac{1}{N_s}\sum_{j\in N_s}\left(y_j-\sum_{i=1}^n\lambda_iy_i\boldsymbol{x}_i^T\boldsymbol{x}_j\right). +\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -1\right] \hspace{0.1cm}\forall i. $$ -With our hyperplane coefficients we can use our classifier to assign any observation by simply using -$$ -y_i = \mathrm{sign}(\boldsymbol{w}^T\boldsymbol{x}_i+b). -$$ -Below we discuss how to find the optimal values of \( \lambda_i \). Before we proceed however, we discuss now the so-called soft classifier. +

      +
    1. If \( \lambda_i > 0 \), then \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) and we say that \( x_i \) is on the boundary.
    2. +
    3. If \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)> 1 \), we say \( x_i \) is not on the boundary and we set \( \lambda_i=0 \).
    4. +
    + +When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin \( M \).

    @@ -212,7 +223,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr

  • 26
  • 27
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs018.html b/doc/pub/week46/html/._week46-bs018.html index 2ab36a77a..c81546e41 100644 --- a/doc/pub/week46/html/._week46-bs018.html +++ b/doc/pub/week46/html/._week46-bs018.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,37 +159,26 @@ MathJax.Hub.Config({ -

    A soft classifier

    +

    The problem to solve

    -Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined. - -

    -Suppose now that classes overlap in feature space, as shown in the -figure here. One way to deal with this problem before we define the -so-called kernel approach, is to allow a kind of slack in the sense -that we allow some points to be on the wrong side of the margin. - -

    -We introduce thus the so-called slack variables \( \boldsymbol{\xi} =[\xi_1,x_2,\dots,x_n] \) and -modify our previous equation +We can rewrite $$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1, +{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, $$ -to +and its constraints in terms of a matrix-vector problem where we minimize w.r.t. \( \lambda \) the following problem $$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i, +\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1\boldsymbol{x}_1^T\boldsymbol{x}_1 & y_1y_2\boldsymbol{x}_1^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_1^T\boldsymbol{x}_n \\ +y_2y_1\boldsymbol{x}_2^T\boldsymbol{x}_1 & y_2y_2\boldsymbol{x}_2^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_2^T\boldsymbol{x}_n \\ +\dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots \\ +y_ny_1\boldsymbol{x}_n^T\boldsymbol{x}_1 & y_ny_2\boldsymbol{x}_n^T\boldsymbol{x}_2 & \dots & \dots & y_ny_n\boldsymbol{x}_n^T\boldsymbol{x}_n \\ +\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda}, $$ -with the requirement \( \xi_i\geq 0 \). The total violation is now \( \sum_i\xi \). -The value \( \xi_i \) in the constraint the last constraint corresponds to the amount by which the prediction -\( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) is on the wrong side of its margin. Hence by bounding the sum \( \sum_i \xi_i \), -we bound the total amount by which predictions fall on the wrong side of their margins. - -

    -Misclassifications occur when \( \xi_i > 1 \). Thus bounding the total sum by some value \( C \) bounds in turn the total number of -misclassifications. +subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and +\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).

    @@ -213,7 +206,7 @@ misclassifications.

  • 27
  • 28
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs019.html b/doc/pub/week46/html/._week46-bs019.html index 2478e0739..1d740ccb0 100644 --- a/doc/pub/week46/html/._week46-bs019.html +++ b/doc/pub/week46/html/._week46-bs019.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,56 +159,36 @@ MathJax.Hub.Config({ -

    Soft optmization problem

    +

    The last steps

    -This has in turn the consequences that we change our optmization problem to finding the minimum of +Solving the above problem, yields the values of \( \lambda_i \). +To find the coefficients of your hyperplane we need simply to compute $$ -{\cal L}=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i, +\boldsymbol{w}=\sum_{i} \lambda_iy_i\boldsymbol{x}_i. $$ -subject to +With our vector \( \boldsymbol{w} \) we can in turn find the value of the intercept \( b \) (here in two dimensions) via $$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i, +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1, $$ -with the requirement \( \xi_i\geq 0 \). - -

    -Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain +resulting in $$ -\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0, +b = \frac{1}{y_i}-\boldsymbol{w}^T\boldsymbol{x}_i, $$ -and +or if we write it out in terms of the support vectors only, with \( N_s \) being their number, we have $$ -\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i, +b = \frac{1}{N_s}\sum_{j\in N_s}\left(y_j-\sum_{i=1}^n\lambda_iy_i\boldsymbol{x}_i^T\boldsymbol{x}_j\right). $$ -and +With our hyperplane coefficients we can use our classifier to assign any observation by simply using $$ -\lambda_i = C-\gamma_i \hspace{0.1cm}\forall i. +y_i = \mathrm{sign}(\boldsymbol{w}^T\boldsymbol{x}_i+b). $$ -Inserting these constraints into the equation for \( {\cal L} \) we obtain the same equation as before -$$ -{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, -$$ - -but now subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) and \( 0\leq\lambda_i \leq C \). -We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads -$$ -\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i, -$$ - -$$ -\gamma_i\xi_i = 0, -$$ - -and -$$ -y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i. -$$ +Below we discuss how to find the optimal values of \( \lambda_i \). Before we proceed however, we discuss now the so-called soft classifier.

    @@ -231,6 +215,8 @@ $$

  • 27
  • 28
  • 29
  • +
  • ...
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs020.html b/doc/pub/week46/html/._week46-bs020.html index 5cd032a42..2af51a96f 100644 --- a/doc/pub/week46/html/._week46-bs020.html +++ b/doc/pub/week46/html/._week46-bs020.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,76 +159,38 @@ MathJax.Hub.Config({ -

    Kernels and non-linearity

    +

    A soft classifier

    -The cases we have studied till now, were all characterized by two classes -with a close to linear separability. The classifiers we have described -so far find linear boundaries in our input feature space. It is -possible to make our procedure more flexible by exploring the feature -space using other basis expansions such as higher-order polynomials, -wavelets, splines etc. +Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.

    -If our feature space is not easy to separate, as shown in the figure -here, we can achieve a better separation by introducing more complex -basis functions. The ideal would be, as shown in the next figure, to, via a specific transformation to -obtain a separation between the classes which is almost linear. +Suppose now that classes overlap in feature space, as shown in the +figure here. One way to deal with this problem before we define the +so-called kernel approach, is to allow a kind of slack in the sense +that we allow some points to be on the wrong side of the margin.

    -The change of basis, from \( x\rightarrow z=\phi(x) \) leads to the same type of equations to be solved, except that -we need to introduce for example a polynomial transformation to a two-dimensional training set. +We introduce thus the so-called slack variables \( \boldsymbol{\xi} =[\xi_1,x_2,\dots,x_n] \) and +modify our previous equation +$$ +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1, +$$ + +to +$$ +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i, +$$ + +with the requirement \( \xi_i\geq 0 \). The total violation is now \( \sum_i\xi \). +The value \( \xi_i \) in the constraint the last constraint corresponds to the amount by which the prediction +\( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) is on the wrong side of its margin. Hence by bounding the sum \( \sum_i \xi_i \), +we bound the total amount by which predictions fall on the wrong side of their margins.

    +Misclassifications occur when \( \xi_i > 1 \). Thus bounding the total sum by some value \( C \) bounds in turn the total number of +misclassifications. - -

    import numpy as np
    -import os
    -
    -np.random.seed(42)
    -
    -# To plot pretty figures
    -import matplotlib
    -import matplotlib.pyplot as plt
    -plt.rcParams['axes.labelsize'] = 14
    -plt.rcParams['xtick.labelsize'] = 12
    -plt.rcParams['ytick.labelsize'] = 12
    -
    -
    -from sklearn.svm import SVC
    -from sklearn import datasets
    -
    -
    -
    -X1D = np.linspace(-4, 4, 9).reshape(-1, 1)
    -X2D = np.c_[X1D, X1D**2]
    -y = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0])
    -
    -plt.figure(figsize=(11, 4))
    -
    -plt.subplot(121)
    -plt.grid(True, which='both')
    -plt.axhline(y=0, color='k')
    -plt.plot(X1D[:, 0][y==0], np.zeros(4), "bs")
    -plt.plot(X1D[:, 0][y==1], np.zeros(5), "g^")
    -plt.gca().get_yaxis().set_ticks([])
    -plt.xlabel(r"$x_1$", fontsize=20)
    -plt.axis([-4.5, 4.5, -0.2, 0.2])
    -
    -plt.subplot(122)
    -plt.grid(True, which='both')
    -plt.axhline(y=0, color='k')
    -plt.axvline(x=0, color='k')
    -plt.plot(X2D[:, 0][y==0], X2D[:, 1][y==0], "bs")
    -plt.plot(X2D[:, 0][y==1], X2D[:, 1][y==1], "g^")
    -plt.xlabel(r"$x_1$", fontsize=20)
    -plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
    -plt.gca().get_yaxis().set_ticks([0, 4, 8, 12, 16])
    -plt.plot([-4.5, 4.5], [6.5, 6.5], "r--", linewidth=3)
    -plt.axis([-4.5, 4.5, -1, 17])
    -plt.subplots_adjust(right=1)
    -plt.show()
    -

    @@ -249,6 +215,9 @@ plt.show()

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • ...
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs021.html b/doc/pub/week46/html/._week46-bs021.html index 12d5d2129..a065e67c3 100644 --- a/doc/pub/week46/html/._week46-bs021.html +++ b/doc/pub/week46/html/._week46-bs021.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,47 +159,56 @@ MathJax.Hub.Config({ -

    The equations

    +

    Soft optmization problem

    -Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables) +This has in turn the consequences that we change our optmization problem to finding the minimum of $$ -z = \phi(x_i) =\left(x_i^2, y_i^2, \sqrt{2}x_iy_i\right). +{\cal L}=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i, $$ +subject to +$$ +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i, +$$ + +with the requirement \( \xi_i\geq 0 \). +

    -With our new basis, the equations we solved earlier are basically the same, that is we have now (without the slack option for simplicity) +Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain $$ -{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{z}_i^T\boldsymbol{z}_j, +\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0, $$ -subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \), and for the support vectors +and $$ -y_i(\boldsymbol{w}^T\boldsymbol{z}_i+b)= 1 \hspace{0.1cm}\forall i, +\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i, $$ -from which we also find \( b \). -To compute \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we define the kernel \( K(\boldsymbol{x}_i,\boldsymbol{x}_j) \) as +and $$ -K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\boldsymbol{z}_i^T\boldsymbol{z}_j= \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j). +\lambda_i = C-\gamma_i \hspace{0.1cm}\forall i. $$ -For the above example, the kernel reads +Inserting these constraints into the equation for \( {\cal L} \) we obtain the same equation as before $$ -K(\boldsymbol{x}_i,\boldsymbol{x}_j)=[x_i^2, y_i^2, \sqrt{2}x_iy_i]^T\begin{bmatrix} x_j^2 \\ y_j^2 \\ \sqrt{2}x_jy_j \end{bmatrix}=x_i^2x_j^2+2x_ix_jy_iy_j+y_i^2y_j^2. +{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j, $$ -

    -We note that this is nothing but the dot product of the two original -vectors \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). Instead of thus computing the -product in the Lagrangian of \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we simply compute -the dot product \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). +but now subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) and \( 0\leq\lambda_i \leq C \). +We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads +$$ +\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i, +$$ -

    -This leads to the so-called -kernel trick and the result leads to the same as if we went through -the trouble of performing the transformation -\( \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j) \) during the SVM calculations. +$$ +\gamma_i\xi_i = 0, +$$ + +and +$$ +y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i. +$$

    @@ -220,6 +233,8 @@ the trouble of performing the transformation

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs022.html b/doc/pub/week46/html/._week46-bs022.html index d86d8b0f3..f96672e91 100644 --- a/doc/pub/week46/html/._week46-bs022.html +++ b/doc/pub/week46/html/._week46-bs022.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,39 +159,76 @@ MathJax.Hub.Config({ -

    The problem to solve

    -Using our definition of the kernel We can rewrite again the Lagrangian -$$ -{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j, -$$ - -subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) in terms of a convex optimization problem -$$ -\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\ -y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\ -\dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots \\ -y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\ -\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda}, -$$ - -subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and -\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \). -If we add the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \). +

    Kernels and non-linearity

    -We can rewrite this (see the solutions below) in terms of a convex optimization problem of the type -$$ -\begin{align*} - &\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber - &\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \hspace{0.2cm} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f. -\end{align*} -$$ +The cases we have studied till now, were all characterized by two classes +with a close to linear separability. The classifiers we have described +so far find linear boundaries in our input feature space. It is +possible to make our procedure more flexible by exploring the feature +space using other basis expansions such as higher-order polynomials, +wavelets, splines etc. -Below we discuss how to solve these equations. Here we note that the matrix \( \boldsymbol{P} \) has matrix elements \( p_{ij}=y_iy_jK(\boldsymbol{x}_i,\boldsymbol{x}_j) \). -Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up. The constraint \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \) leads to \( f=0 \) and \( \boldsymbol{A}=\boldsymbol{y} \). How to set up the matrix \( \boldsymbol{G} \) is discussed later. Here note that the inequalities \( 0\leq \lambda_i \leq C \) can be split up into -\( 0\leq \lambda_i \) and \( \lambda_i \leq C \). These two inequalities define then the matrix \( \boldsymbol{G} \) and the vector \( \boldsymbol{h} \). +

    +If our feature space is not easy to separate, as shown in the figure +here, we can achieve a better separation by introducing more complex +basis functions. The ideal would be, as shown in the next figure, to, via a specific transformation to +obtain a separation between the classes which is almost linear. +

    +The change of basis, from \( x\rightarrow z=\phi(x) \) leads to the same type of equations to be solved, except that +we need to introduce for example a polynomial transformation to a two-dimensional training set. + +

    + + +

    import numpy as np
    +import os
    +
    +np.random.seed(42)
    +
    +# To plot pretty figures
    +import matplotlib
    +import matplotlib.pyplot as plt
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +
    +from sklearn.svm import SVC
    +from sklearn import datasets
    +
    +
    +
    +X1D = np.linspace(-4, 4, 9).reshape(-1, 1)
    +X2D = np.c_[X1D, X1D**2]
    +y = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0])
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.grid(True, which='both')
    +plt.axhline(y=0, color='k')
    +plt.plot(X1D[:, 0][y==0], np.zeros(4), "bs")
    +plt.plot(X1D[:, 0][y==1], np.zeros(5), "g^")
    +plt.gca().get_yaxis().set_ticks([])
    +plt.xlabel(r"$x_1$", fontsize=20)
    +plt.axis([-4.5, 4.5, -0.2, 0.2])
    +
    +plt.subplot(122)
    +plt.grid(True, which='both')
    +plt.axhline(y=0, color='k')
    +plt.axvline(x=0, color='k')
    +plt.plot(X2D[:, 0][y==0], X2D[:, 1][y==0], "bs")
    +plt.plot(X2D[:, 0][y==1], X2D[:, 1][y==1], "g^")
    +plt.xlabel(r"$x_1$", fontsize=20)
    +plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
    +plt.gca().get_yaxis().set_ticks([0, 4, 8, 12, 16])
    +plt.plot([-4.5, 4.5], [6.5, 6.5], "r--", linewidth=3)
    +plt.axis([-4.5, 4.5, -1, 17])
    +plt.subplots_adjust(right=1)
    +plt.show()
    +

    @@ -210,6 +251,8 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs023.html b/doc/pub/week46/html/._week46-bs023.html index 041e9881f..c37d34c40 100644 --- a/doc/pub/week46/html/._week46-bs023.html +++ b/doc/pub/week46/html/._week46-bs023.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,40 +159,47 @@ MathJax.Hub.Config({ -

    Different kernels and Mercer's theorem

    +

    The equations

    -There are several popular kernels being used. These are - -

      -
    1. Linear: \( K(\boldsymbol{x},\boldsymbol{y})=\boldsymbol{x}^T\boldsymbol{y} \),
    2. -
    3. Polynomial: \( K(\boldsymbol{x},\boldsymbol{y})=(\boldsymbol{x}^T\boldsymbol{y}+\gamma)^d \),
    4. -
    5. Gaussian Radial Basis Function: \( K(\boldsymbol{x},\boldsymbol{y})=\exp{\left(-\gamma\vert\vert\boldsymbol{x}-\boldsymbol{y}\vert\vert^2\right)} \),
    6. -
    7. Tanh: \( K(\boldsymbol{x},\boldsymbol{y})=\tanh{(\boldsymbol{x}^T\boldsymbol{y}+\gamma)} \),
    8. -
    - -and many other ones. - -

    -An important theorem for us is Mercer's -theorem. The -theorem states that if a kernel function \( K \) is symmetric, continuous -and leads to a positive semi-definite matrix \( \boldsymbol{P} \) then there -exists a function \( \phi \) that maps \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_j \) into -another space (possibly with much higher dimensions) such that - +Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables) $$ -K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j). +z = \phi(x_i) =\left(x_i^2, y_i^2, \sqrt{2}x_iy_i\right). $$

    -So you can use \( K \) as a kernel since you know \( \phi \) exists, even if -you don’t know what \( \phi \) is. +With our new basis, the equations we solved earlier are basically the same, that is we have now (without the slack option for simplicity) +$$ +{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{z}_i^T\boldsymbol{z}_j, +$$ + +subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \), and for the support vectors +$$ +y_i(\boldsymbol{w}^T\boldsymbol{z}_i+b)= 1 \hspace{0.1cm}\forall i, +$$ + +from which we also find \( b \). +To compute \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we define the kernel \( K(\boldsymbol{x}_i,\boldsymbol{x}_j) \) as +$$ +K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\boldsymbol{z}_i^T\boldsymbol{z}_j= \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j). +$$ + +For the above example, the kernel reads +$$ +K(\boldsymbol{x}_i,\boldsymbol{x}_j)=[x_i^2, y_i^2, \sqrt{2}x_iy_i]^T\begin{bmatrix} x_j^2 \\ y_j^2 \\ \sqrt{2}x_jy_j \end{bmatrix}=x_i^2x_j^2+2x_ix_jy_iy_j+y_i^2y_j^2. +$$

    -Note that some frequently used kernels (such as the Sigmoid kernel) -don’t respect all of Mercer’s conditions, yet they generally work well -in practice. +We note that this is nothing but the dot product of the two original +vectors \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). Instead of thus computing the +product in the Lagrangian of \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we simply compute +the dot product \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). + +

    +This leads to the so-called +kernel trick and the result leads to the same as if we went through +the trouble of performing the transformation +\( \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j) \) during the SVM calculations.

    @@ -211,6 +222,8 @@ in practice.

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs024.html b/doc/pub/week46/html/._week46-bs024.html index 9339c1aea..90175a244 100644 --- a/doc/pub/week46/html/._week46-bs024.html +++ b/doc/pub/week46/html/._week46-bs024.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,199 +159,39 @@ MathJax.Hub.Config({ -

    The moons example

    +

    The problem to solve

    +Using our definition of the kernel We can rewrite again the Lagrangian +$$ +{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j, +$$ + +subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) in terms of a convex optimization problem +$$ +\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\ +y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\ +\dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots \\ +y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\ +\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda}, +$$ + +subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and +\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \). +If we add the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \). +

    +We can rewrite this (see the solutions below) in terms of a convex optimization problem of the type +$$ +\begin{align*} + &\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber + &\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \hspace{0.2cm} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f. +\end{align*} +$$ - -

    from __future__ import division, print_function, unicode_literals
    +Below we discuss how to solve these equations. Here we note that the matrix \( \boldsymbol{P} \) has matrix elements \( p_{ij}=y_iy_jK(\boldsymbol{x}_i,\boldsymbol{x}_j) \).
    +Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up. The constraint \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \) leads to \( f=0 \) and \( \boldsymbol{A}=\boldsymbol{y} \). How to set up the matrix \( \boldsymbol{G} \) is discussed later. Here note that the inequalities \( 0\leq \lambda_i \leq C \) can be split up into
    +\( 0\leq \lambda_i \) and \( \lambda_i \leq C \). These two inequalities define then the matrix \( \boldsymbol{G} \) and the vector \( \boldsymbol{h} \).
     
    -import numpy as np
    -np.random.seed(42)
    -
    -import matplotlib
    -import matplotlib.pyplot as plt
    -plt.rcParams['axes.labelsize'] = 14
    -plt.rcParams['xtick.labelsize'] = 12
    -plt.rcParams['ytick.labelsize'] = 12
    -
    -
    -from sklearn.svm import SVC
    -from sklearn import datasets
    -
    -
    -
    -from sklearn.pipeline import Pipeline
    -from sklearn.preprocessing import StandardScaler
    -from sklearn.svm import LinearSVC
    -
    -
    -from sklearn.datasets import make_moons
    -X, y = make_moons(n_samples=100, noise=0.15, random_state=42)
    -
    -def plot_dataset(X, y, axes):
    -    plt.plot(X[:, 0][y==0], X[:, 1][y==0], "bs")
    -    plt.plot(X[:, 0][y==1], X[:, 1][y==1], "g^")
    -    plt.axis(axes)
    -    plt.grid(True, which='both')
    -    plt.xlabel(r"$x_1$", fontsize=20)
    -    plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
    -
    -plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    -plt.show()
    -
    -from sklearn.datasets import make_moons
    -from sklearn.pipeline import Pipeline
    -from sklearn.preprocessing import PolynomialFeatures
    -
    -polynomial_svm_clf = Pipeline([
    -        ("poly_features", PolynomialFeatures(degree=3)),
    -        ("scaler", StandardScaler()),
    -        ("svm_clf", LinearSVC(C=10, loss="hinge", random_state=42))
    -    ])
    -
    -polynomial_svm_clf.fit(X, y)
    -
    -def plot_predictions(clf, axes):
    -    x0s = np.linspace(axes[0], axes[1], 100)
    -    x1s = np.linspace(axes[2], axes[3], 100)
    -    x0, x1 = np.meshgrid(x0s, x1s)
    -    X = np.c_[x0.ravel(), x1.ravel()]
    -    y_pred = clf.predict(X).reshape(x0.shape)
    -    y_decision = clf.decision_function(X).reshape(x0.shape)
    -    plt.contourf(x0, x1, y_pred, cmap=plt.cm.brg, alpha=0.2)
    -    plt.contourf(x0, x1, y_decision, cmap=plt.cm.brg, alpha=0.1)
    -
    -plot_predictions(polynomial_svm_clf, [-1.5, 2.5, -1, 1.5])
    -plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    -
    -plt.show()
    -
    -
    -from sklearn.svm import SVC
    -
    -poly_kernel_svm_clf = Pipeline([
    -        ("scaler", StandardScaler()),
    -        ("svm_clf", SVC(kernel="poly", degree=3, coef0=1, C=5))
    -    ])
    -poly_kernel_svm_clf.fit(X, y)
    -
    -poly100_kernel_svm_clf = Pipeline([
    -        ("scaler", StandardScaler()),
    -        ("svm_clf", SVC(kernel="poly", degree=10, coef0=100, C=5))
    -    ])
    -poly100_kernel_svm_clf.fit(X, y)
    -
    -plt.figure(figsize=(11, 4))
    -
    -plt.subplot(121)
    -plot_predictions(poly_kernel_svm_clf, [-1.5, 2.5, -1, 1.5])
    -plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    -plt.title(r"$d=3, r=1, C=5$", fontsize=18)
    -
    -plt.subplot(122)
    -plot_predictions(poly100_kernel_svm_clf, [-1.5, 2.5, -1, 1.5])
    -plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    -plt.title(r"$d=10, r=100, C=5$", fontsize=18)
    -
    -plt.show()
    -
    -def gaussian_rbf(x, landmark, gamma):
    -    return np.exp(-gamma * np.linalg.norm(x - landmark, axis=1)**2)
    -
    -gamma = 0.3
    -
    -x1s = np.linspace(-4.5, 4.5, 200).reshape(-1, 1)
    -x2s = gaussian_rbf(x1s, -2, gamma)
    -x3s = gaussian_rbf(x1s, 1, gamma)
    -
    -XK = np.c_[gaussian_rbf(X1D, -2, gamma), gaussian_rbf(X1D, 1, gamma)]
    -yk = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0])
    -
    -plt.figure(figsize=(11, 4))
    -
    -plt.subplot(121)
    -plt.grid(True, which='both')
    -plt.axhline(y=0, color='k')
    -plt.scatter(x=[-2, 1], y=[0, 0], s=150, alpha=0.5, c="red")
    -plt.plot(X1D[:, 0][yk==0], np.zeros(4), "bs")
    -plt.plot(X1D[:, 0][yk==1], np.zeros(5), "g^")
    -plt.plot(x1s, x2s, "g--")
    -plt.plot(x1s, x3s, "b:")
    -plt.gca().get_yaxis().set_ticks([0, 0.25, 0.5, 0.75, 1])
    -plt.xlabel(r"$x_1$", fontsize=20)
    -plt.ylabel(r"Similarity", fontsize=14)
    -plt.annotate(r'$\mathbf{x}$',
    -             xy=(X1D[3, 0], 0),
    -             xytext=(-0.5, 0.20),
    -             ha="center",
    -             arrowprops=dict(facecolor='black', shrink=0.1),
    -             fontsize=18,
    -            )
    -plt.text(-2, 0.9, "$x_2$", ha="center", fontsize=20)
    -plt.text(1, 0.9, "$x_3$", ha="center", fontsize=20)
    -plt.axis([-4.5, 4.5, -0.1, 1.1])
    -
    -plt.subplot(122)
    -plt.grid(True, which='both')
    -plt.axhline(y=0, color='k')
    -plt.axvline(x=0, color='k')
    -plt.plot(XK[:, 0][yk==0], XK[:, 1][yk==0], "bs")
    -plt.plot(XK[:, 0][yk==1], XK[:, 1][yk==1], "g^")
    -plt.xlabel(r"$x_2$", fontsize=20)
    -plt.ylabel(r"$x_3$  ", fontsize=20, rotation=0)
    -plt.annotate(r'$\phi\left(\mathbf{x}\right)$',
    -             xy=(XK[3, 0], XK[3, 1]),
    -             xytext=(0.65, 0.50),
    -             ha="center",
    -             arrowprops=dict(facecolor='black', shrink=0.1),
    -             fontsize=18,
    -            )
    -plt.plot([-0.1, 1.1], [0.57, -0.1], "r--", linewidth=3)
    -plt.axis([-0.1, 1.1, -0.1, 1.1])
    -    
    -plt.subplots_adjust(right=1)
    -
    -plt.show()
    -
    -
    -x1_example = X1D[3, 0]
    -for landmark in (-2, 1):
    -    k = gaussian_rbf(np.array([[x1_example]]), np.array([[landmark]]), gamma)
    -    print("Phi({}, {}) = {}".format(x1_example, landmark, k))
    -
    -rbf_kernel_svm_clf = Pipeline([
    -        ("scaler", StandardScaler()),
    -        ("svm_clf", SVC(kernel="rbf", gamma=5, C=0.001))
    -    ])
    -rbf_kernel_svm_clf.fit(X, y)
    -
    -
    -from sklearn.svm import SVC
    -
    -gamma1, gamma2 = 0.1, 5
    -C1, C2 = 0.001, 1000
    -hyperparams = (gamma1, C1), (gamma1, C2), (gamma2, C1), (gamma2, C2)
    -
    -svm_clfs = []
    -for gamma, C in hyperparams:
    -    rbf_kernel_svm_clf = Pipeline([
    -            ("scaler", StandardScaler()),
    -            ("svm_clf", SVC(kernel="rbf", gamma=gamma, C=C))
    -        ])
    -    rbf_kernel_svm_clf.fit(X, y)
    -    svm_clfs.append(rbf_kernel_svm_clf)
    -
    -plt.figure(figsize=(11, 7))
    -
    -for i, svm_clf in enumerate(svm_clfs):
    -    plt.subplot(221 + i)
    -    plot_predictions(svm_clf, [-1.5, 2.5, -1, 1.5])
    -    plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    -    gamma, C = hyperparams[i]
    -    plt.title(r"$\gamma = {}, C = {}$".format(gamma, C), fontsize=16)
    -
    -plt.show()
    -

    @@ -368,6 +212,8 @@ plt.show()

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs025.html b/doc/pub/week46/html/._week46-bs025.html index fe9b72791..d6575f49a 100644 --- a/doc/pub/week46/html/._week46-bs025.html +++ b/doc/pub/week46/html/._week46-bs025.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,27 +159,40 @@ MathJax.Hub.Config({ -

    Mathematical optimization of convex functions

    +

    Different kernels and Mercer's theorem

    -A mathematical (quadratic) optimization problem, or just optimization problem, has the form +There are several popular kernels being used. These are + +

      +
    1. Linear: \( K(\boldsymbol{x},\boldsymbol{y})=\boldsymbol{x}^T\boldsymbol{y} \),
    2. +
    3. Polynomial: \( K(\boldsymbol{x},\boldsymbol{y})=(\boldsymbol{x}^T\boldsymbol{y}+\gamma)^d \),
    4. +
    5. Gaussian Radial Basis Function: \( K(\boldsymbol{x},\boldsymbol{y})=\exp{\left(-\gamma\vert\vert\boldsymbol{x}-\boldsymbol{y}\vert\vert^2\right)} \),
    6. +
    7. Tanh: \( K(\boldsymbol{x},\boldsymbol{y})=\tanh{(\boldsymbol{x}^T\boldsymbol{y}+\gamma)} \),
    8. +
    + +and many other ones. + +

    +An important theorem for us is Mercer's +theorem. The +theorem states that if a kernel function \( K \) is symmetric, continuous +and leads to a positive semi-definite matrix \( \boldsymbol{P} \) then there +exists a function \( \phi \) that maps \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_j \) into +another space (possibly with much higher dimensions) such that + $$ -\begin{align*} - &\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber - &\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f. -\end{align*} +K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j). $$ -subject to some constraints for say a selected set \( i=1,2,\dots, n \). -In our case we are optimizing with respect to the Lagrangian multipliers \( \lambda_i \), and the -vector \( \boldsymbol{\lambda}=[\lambda_1, \lambda_2,\dots, \lambda_n] \) is the optimization variable we are dealing with. +

    +So you can use \( K \) as a kernel since you know \( \phi \) exists, even if +you don’t know what \( \phi \) is.

    -In our case we are particularly interested in a class of optimization problems called convex optmization problems. -In our discussion on gradient descent methods we discussed at length the definition of a convex function. - -

    -Convex optimization problems play a central role in applied mathematics and we recommend strongly Boyd and Vandenberghe's text on the topics. +Note that some frequently used kernels (such as the Sigmoid kernel) +don’t respect all of Mercer’s conditions, yet they generally work well +in practice.

    @@ -196,6 +213,8 @@ Convex optimization problems play a central role in applied mathematics and we r

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs026.html b/doc/pub/week46/html/._week46-bs026.html index 41e5739cc..0368265a3 100644 --- a/doc/pub/week46/html/._week46-bs026.html +++ b/doc/pub/week46/html/._week46-bs026.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,29 +159,199 @@ MathJax.Hub.Config({ -

    How do we solve these problems?

    - -

    -If we use Python as programming language and wish to venture beyond -scikit-learn, tensorflow and similar software which makes our -lives so much easier, we need to dive into the wonderful world of -quadratic programming. We can, if we wish, solve the minimization -problem using say standard gradient methods or conjugate gradient -methods. However, these methods tend to exhibit a rather slow -converge. So, welcome to the promised land of quadratic programming. - -

    -The functions we need are contained in the quadratic programming package CVXOPT and we need to import it together with numpy as - +

    The moons example

    -

    import numpy
    -import cvxopt
    -
    -

    -This will make our life much easier. You don't need t write your own optimizer. +

    from __future__ import division, print_function, unicode_literals
     
    +import numpy as np
    +np.random.seed(42)
    +
    +import matplotlib
    +import matplotlib.pyplot as plt
    +plt.rcParams['axes.labelsize'] = 14
    +plt.rcParams['xtick.labelsize'] = 12
    +plt.rcParams['ytick.labelsize'] = 12
    +
    +
    +from sklearn.svm import SVC
    +from sklearn import datasets
    +
    +
    +
    +from sklearn.pipeline import Pipeline
    +from sklearn.preprocessing import StandardScaler
    +from sklearn.svm import LinearSVC
    +
    +
    +from sklearn.datasets import make_moons
    +X, y = make_moons(n_samples=100, noise=0.15, random_state=42)
    +
    +def plot_dataset(X, y, axes):
    +    plt.plot(X[:, 0][y==0], X[:, 1][y==0], "bs")
    +    plt.plot(X[:, 0][y==1], X[:, 1][y==1], "g^")
    +    plt.axis(axes)
    +    plt.grid(True, which='both')
    +    plt.xlabel(r"$x_1$", fontsize=20)
    +    plt.ylabel(r"$x_2$", fontsize=20, rotation=0)
    +
    +plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    +plt.show()
    +
    +from sklearn.datasets import make_moons
    +from sklearn.pipeline import Pipeline
    +from sklearn.preprocessing import PolynomialFeatures
    +
    +polynomial_svm_clf = Pipeline([
    +        ("poly_features", PolynomialFeatures(degree=3)),
    +        ("scaler", StandardScaler()),
    +        ("svm_clf", LinearSVC(C=10, loss="hinge", random_state=42))
    +    ])
    +
    +polynomial_svm_clf.fit(X, y)
    +
    +def plot_predictions(clf, axes):
    +    x0s = np.linspace(axes[0], axes[1], 100)
    +    x1s = np.linspace(axes[2], axes[3], 100)
    +    x0, x1 = np.meshgrid(x0s, x1s)
    +    X = np.c_[x0.ravel(), x1.ravel()]
    +    y_pred = clf.predict(X).reshape(x0.shape)
    +    y_decision = clf.decision_function(X).reshape(x0.shape)
    +    plt.contourf(x0, x1, y_pred, cmap=plt.cm.brg, alpha=0.2)
    +    plt.contourf(x0, x1, y_decision, cmap=plt.cm.brg, alpha=0.1)
    +
    +plot_predictions(polynomial_svm_clf, [-1.5, 2.5, -1, 1.5])
    +plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    +
    +plt.show()
    +
    +
    +from sklearn.svm import SVC
    +
    +poly_kernel_svm_clf = Pipeline([
    +        ("scaler", StandardScaler()),
    +        ("svm_clf", SVC(kernel="poly", degree=3, coef0=1, C=5))
    +    ])
    +poly_kernel_svm_clf.fit(X, y)
    +
    +poly100_kernel_svm_clf = Pipeline([
    +        ("scaler", StandardScaler()),
    +        ("svm_clf", SVC(kernel="poly", degree=10, coef0=100, C=5))
    +    ])
    +poly100_kernel_svm_clf.fit(X, y)
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plot_predictions(poly_kernel_svm_clf, [-1.5, 2.5, -1, 1.5])
    +plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    +plt.title(r"$d=3, r=1, C=5$", fontsize=18)
    +
    +plt.subplot(122)
    +plot_predictions(poly100_kernel_svm_clf, [-1.5, 2.5, -1, 1.5])
    +plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    +plt.title(r"$d=10, r=100, C=5$", fontsize=18)
    +
    +plt.show()
    +
    +def gaussian_rbf(x, landmark, gamma):
    +    return np.exp(-gamma * np.linalg.norm(x - landmark, axis=1)**2)
    +
    +gamma = 0.3
    +
    +x1s = np.linspace(-4.5, 4.5, 200).reshape(-1, 1)
    +x2s = gaussian_rbf(x1s, -2, gamma)
    +x3s = gaussian_rbf(x1s, 1, gamma)
    +
    +XK = np.c_[gaussian_rbf(X1D, -2, gamma), gaussian_rbf(X1D, 1, gamma)]
    +yk = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0])
    +
    +plt.figure(figsize=(11, 4))
    +
    +plt.subplot(121)
    +plt.grid(True, which='both')
    +plt.axhline(y=0, color='k')
    +plt.scatter(x=[-2, 1], y=[0, 0], s=150, alpha=0.5, c="red")
    +plt.plot(X1D[:, 0][yk==0], np.zeros(4), "bs")
    +plt.plot(X1D[:, 0][yk==1], np.zeros(5), "g^")
    +plt.plot(x1s, x2s, "g--")
    +plt.plot(x1s, x3s, "b:")
    +plt.gca().get_yaxis().set_ticks([0, 0.25, 0.5, 0.75, 1])
    +plt.xlabel(r"$x_1$", fontsize=20)
    +plt.ylabel(r"Similarity", fontsize=14)
    +plt.annotate(r'$\mathbf{x}$',
    +             xy=(X1D[3, 0], 0),
    +             xytext=(-0.5, 0.20),
    +             ha="center",
    +             arrowprops=dict(facecolor='black', shrink=0.1),
    +             fontsize=18,
    +            )
    +plt.text(-2, 0.9, "$x_2$", ha="center", fontsize=20)
    +plt.text(1, 0.9, "$x_3$", ha="center", fontsize=20)
    +plt.axis([-4.5, 4.5, -0.1, 1.1])
    +
    +plt.subplot(122)
    +plt.grid(True, which='both')
    +plt.axhline(y=0, color='k')
    +plt.axvline(x=0, color='k')
    +plt.plot(XK[:, 0][yk==0], XK[:, 1][yk==0], "bs")
    +plt.plot(XK[:, 0][yk==1], XK[:, 1][yk==1], "g^")
    +plt.xlabel(r"$x_2$", fontsize=20)
    +plt.ylabel(r"$x_3$  ", fontsize=20, rotation=0)
    +plt.annotate(r'$\phi\left(\mathbf{x}\right)$',
    +             xy=(XK[3, 0], XK[3, 1]),
    +             xytext=(0.65, 0.50),
    +             ha="center",
    +             arrowprops=dict(facecolor='black', shrink=0.1),
    +             fontsize=18,
    +            )
    +plt.plot([-0.1, 1.1], [0.57, -0.1], "r--", linewidth=3)
    +plt.axis([-0.1, 1.1, -0.1, 1.1])
    +    
    +plt.subplots_adjust(right=1)
    +
    +plt.show()
    +
    +
    +x1_example = X1D[3, 0]
    +for landmark in (-2, 1):
    +    k = gaussian_rbf(np.array([[x1_example]]), np.array([[landmark]]), gamma)
    +    print("Phi({}, {}) = {}".format(x1_example, landmark, k))
    +
    +rbf_kernel_svm_clf = Pipeline([
    +        ("scaler", StandardScaler()),
    +        ("svm_clf", SVC(kernel="rbf", gamma=5, C=0.001))
    +    ])
    +rbf_kernel_svm_clf.fit(X, y)
    +
    +
    +from sklearn.svm import SVC
    +
    +gamma1, gamma2 = 0.1, 5
    +C1, C2 = 0.001, 1000
    +hyperparams = (gamma1, C1), (gamma1, C2), (gamma2, C1), (gamma2, C2)
    +
    +svm_clfs = []
    +for gamma, C in hyperparams:
    +    rbf_kernel_svm_clf = Pipeline([
    +            ("scaler", StandardScaler()),
    +            ("svm_clf", SVC(kernel="rbf", gamma=gamma, C=C))
    +        ])
    +    rbf_kernel_svm_clf.fit(X, y)
    +    svm_clfs.append(rbf_kernel_svm_clf)
    +
    +plt.figure(figsize=(11, 7))
    +
    +for i, svm_clf in enumerate(svm_clfs):
    +    plt.subplot(221 + i)
    +    plot_predictions(svm_clf, [-1.5, 2.5, -1, 1.5])
    +    plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
    +    gamma, C = hyperparams[i]
    +    plt.title(r"$\gamma = {}, C = {}$".format(gamma, C), fontsize=16)
    +
    +plt.show()
    +

    @@ -196,6 +370,8 @@ This will make our life much easier. You don't need t write your own optimizer.

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs027.html b/doc/pub/week46/html/._week46-bs027.html index abdeff904..ab6c51552 100644 --- a/doc/pub/week46/html/._week46-bs027.html +++ b/doc/pub/week46/html/._week46-bs027.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,71 +159,28 @@ MathJax.Hub.Config({ -

    A simple example

    +

    Mathematical optimization of convex functions

    -We remind ourselves about the general problem we want to solve +A mathematical (quadratic) optimization problem, or just optimization problem, has the form $$ \begin{align*} - &\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}\boldsymbol{x}^T\boldsymbol{P}\boldsymbol{x}+\boldsymbol{q}^T\boldsymbol{x},\\ \nonumber - &\mathrm{subject\hspace{0.1cm} to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{x} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{x}=f. + &\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber + &\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f. \end{align*} $$ -

    -Let us show how to perform the optmization using a simple case. Assume we want to optimize the following problem -$$ -\begin{align*} - &\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}x^2+5x+3y \\ \nonumber - &\mathrm{subject to} \\ \nonumber - &x, y \geq 0 \\ \nonumber - &x+3y \geq 15 \\ \nonumber - &2x+5y \leq 100 \\ \nonumber - &3x+4y \leq 80. \\ \nonumber -\end{align*} -$$ - -The minimization problem can be rewritten in terms of vectors and matrices as (with \( x \) and \( y \) being the unknowns) -$$ -\frac{1}{2}\begin{bmatrix} x\\ y \end{bmatrix}^T \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix}3\\ 4 \end{bmatrix}^T \begin{bmatrix}x \\ y \end{bmatrix}. -$$ - -Similarly, we can now set up the inequalities (we need to change \( \geq \) to \( \leq \) by multiplying with \( -1 \) on bot sides) as the following matrix-vector equation -$$ -\begin{bmatrix} -1 & 0 \\ 0 & -1 \\ -1 & -3 \\ 2 & 5 \\ 3 & 4\end{bmatrix}\begin{bmatrix} x \\ y\end{bmatrix} \preceq \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}. -$$ - -We have collapsed all the inequalities into a single matrix \( \boldsymbol{G} \). We see also that our matrix -$$ -\boldsymbol{P} =\begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} -$$ - -is clearly positive semi-definite (all eigenvalues larger or equal zero). -Finally, the vector \( \boldsymbol{h} \) is defined as -$$ -\boldsymbol{h} = \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}. -$$ +subject to some constraints for say a selected set \( i=1,2,\dots, n \). +In our case we are optimizing with respect to the Lagrangian multipliers \( \lambda_i \), and the +vector \( \boldsymbol{\lambda}=[\lambda_1, \lambda_2,\dots, \lambda_n] \) is the optimization variable we are dealing with.

    -Since we don't have any equalities the matrix \( \boldsymbol{A} \) is set to zero -The following code solves the equations for us -

    +In our case we are particularly interested in a class of optimization problems called convex optmization problems. +In our discussion on gradient descent methods we discussed at length the definition of a convex function. + +

    +Convex optimization problems play a central role in applied mathematics and we recommend strongly Boyd and Vandenberghe's text on the topics. - -

    # Import the necessary packages
    -import numpy
    -from cvxopt import matrix
    -from cvxopt import solvers
    -P = matrix(numpy.diag([1,0]), tc=’d’)
    -q = matrix(numpy.array([3,4]), tc=’d’)
    -G = matrix(numpy.array([[-1,0],[0,-1],[-1,-3],[2,5],[3,4]]), tc=’d’)
    -h = matrix(numpy.array([0,0,-15,100,80]), tc=’d’)
    -# Construct the QP, invoke solver
    -sol = solvers.qp(P,q,G,h)
    -# Extract optimal value and solution
    -sol[’x’] 
    -sol[’primal objective’]
    -

    @@ -237,6 +198,8 @@ sol[’primal objective’]

  • 27
  • 28
  • 29
  • +
  • 30
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/._week46-bs028.html b/doc/pub/week46/html/._week46-bs028.html index ba3ede964..e465732bb 100644 --- a/doc/pub/week46/html/._week46-bs028.html +++ b/doc/pub/week46/html/._week46-bs028.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -155,28 +159,30 @@ MathJax.Hub.Config({ -

    Back to the more realistic cases

    +

    How do we solve these problems?

    -We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the slack parameter \( C \) we have -$$ -\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\ -y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2K(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\ -\dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots \\ -y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\ -\end{bmatrix}\boldsymbol{\lambda}-\mathbb{I}\boldsymbol{\lambda}, -$$ - -subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and -\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \). -With the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \). +If we use Python as programming language and wish to venture beyond +scikit-learn, tensorflow and similar software which makes our +lives so much easier, we need to dive into the wonderful world of +quadratic programming. We can, if we wish, solve the minimization +problem using say standard gradient methods or conjugate gradient +methods. However, these methods tend to exhibit a rather slow +converge. So, welcome to the promised land of quadratic programming.

    -code will be added +The functions we need are contained in the quadratic programming package CVXOPT and we need to import it together with numpy as

    + +

    import numpy
    +import cvxopt
    +
    +

    +This will make our life much easier. You don't need t write your own optimizer. + +

    diff --git a/doc/pub/week46/html/week46-bs.html b/doc/pub/week46/html/week46-bs.html index 8529411ec..3f75fff30 100644 --- a/doc/pub/week46/html/week46-bs.html +++ b/doc/pub/week46/html/week46-bs.html @@ -42,39 +42,41 @@ Automatically generated HTML file from DocOnce source @@ -113,33 +115,35 @@ MathJax.Hub.Config({ Contents @@ -174,7 +178,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    Nov 8, 2020

    +

    Nov 9, 2020


    @@ -198,7 +202,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 29
  • +
  • 31
  • »
  • diff --git a/doc/pub/week46/html/week46-reveal.html b/doc/pub/week46/html/week46-reveal.html index 5fbc96d91..06d8467ab 100644 --- a/doc/pub/week46/html/week46-reveal.html +++ b/doc/pub/week46/html/week46-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

     
    -

    Nov 8, 2020

    +

    Nov 9, 2020


    @@ -162,8 +162,8 @@ MathJax.Hub.Config({

    Overview of week 46

    @@ -175,7 +175,26 @@ Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) o

    -

    Support Vector Machines, overarching aims

    +

    Thursday

    + +

    +The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie et al. chapter 10.1-10.10. + +

    +The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education. +

    + + +
    +

    Friday

    + +

    +We start with a new topic and second-last topic covered in this course, support vector machines (SVM). +

    + + +
    +

    Support Vector Machines, overarching aims

    A Support Vector Machine (SVM) is a very powerful and versatile @@ -208,7 +227,7 @@ unlikely that we can separate classes easily by say straight lines.

    -

    Hyperplanes and all that

    +

    Hyperplanes and all that

    The theory behind support vector machines (SVM hereafter) is based on @@ -296,7 +315,7 @@ plt.show()

    -

    What is a hyperplane?

    +

    What is a hyperplane?

    The aim of the SVM algorithm is to find a hyperplane in a @@ -331,7 +350,7 @@ $$

    -

    A \( p \)-dimensional space of features

    +

    A \( p \)-dimensional space of features

    We limit ourselves to two classes of outputs \( y_i \) and assign these classes the values \( y_i = \pm 1 \). @@ -383,7 +402,7 @@ When we try to separate hyperplanes, if it exists, we can use it to construct a

    -

    The two-dimensional case

    +

    The two-dimensional case

    Let us try to develop our intuition about SVMs by limiting ourselves to a two-dimensional @@ -410,7 +429,7 @@ for our data sample.

    -

    Getting into the details

    +

    Getting into the details

    Let us define the function @@ -436,7 +455,7 @@ $$

    -

    First attempt at a minimization approach

    +

    First attempt at a minimization approach

    How do we find the parameter \( b \) and the vector \( \boldsymbol{w} \)? What we could @@ -467,7 +486,7 @@ $$

    -

    Solving the equations

    +

    Solving the equations

    We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations @@ -489,7 +508,7 @@ where \( \eta \) is our by now well-known learning rate.

    -

    Code Example

    +

    Code Example

    The equations we discussed above can be coded rather easily (the @@ -504,7 +523,7 @@ regression). We are going to set up a simple case with two classes only and we w

    -

    Problems with the Simpler Approach

    +

    Problems with the Simpler Approach

    There are however problems with this approach, although it looks @@ -520,7 +539,7 @@ at all.

    -

    A better approach

    +

    A better approach

    A better approach is rather to try to define a large margin between @@ -570,7 +589,7 @@ about Lagrangian multipliers.

    -

    A quick Reminder on Lagrangian Multipliers

    +

    A quick Reminder on Lagrangian Multipliers

    Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an @@ -632,7 +651,7 @@ Then \( dz \) is no longer arbitrary.

    -

    Adding the Multiplier

    +

    Adding the Multiplier

    However, we can add to @@ -687,7 +706,7 @@ $$

    -

    Setting up the Problem

    +

    Setting up the Problem

    In order to solve the above problem, we define the following Lagrangian function to be minimized

     
    $$ @@ -739,7 +758,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support

    -

    The problem to solve

    +

    The problem to solve

    We can rewrite @@ -767,7 +786,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec

    -

    The last steps

    +

    The last steps

    Solving the above problem, yields the values of \( \lambda_i \). @@ -811,7 +830,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr

    -

    A soft classifier

    +

    A soft classifier

    Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined. @@ -850,7 +869,7 @@ misclassifications.

    -

    Soft optmization problem

    +

    Soft optmization problem

    This has in turn the consequences that we change our optmization problem to finding the minimum of @@ -922,7 +941,7 @@ $$

    -

    Kernels and non-linearity

    +

    Kernels and non-linearity

    The cases we have studied till now, were all characterized by two classes @@ -996,7 +1015,7 @@ plt.show()

    -

    The equations

    +

    The equations

    Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables) @@ -1051,7 +1070,7 @@ the trouble of performing the transformation

    -

    The problem to solve

    +

    The problem to solve

    Using our definition of the kernel We can rewrite again the Lagrangian

     
    $$ @@ -1093,7 +1112,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.

    -

    Different kernels and Mercer's theorem

    +

    Different kernels and Mercer's theorem

    There are several popular kernels being used. These are @@ -1134,7 +1153,7 @@ in practice.

    -

    The moons example

    +

    The moons example

    @@ -1331,7 +1350,7 @@ plt.show()

    -

    Mathematical optimization of convex functions

    +

    Mathematical optimization of convex functions

    A mathematical (quadratic) optimization problem, or just optimization problem, has the form @@ -1358,7 +1377,7 @@ Convex optimization problems play a central role in applied mathematics and we r

    -

    How do we solve these problems?

    +

    How do we solve these problems?

    If we use Python as programming language and wish to venture beyond @@ -1384,7 +1403,7 @@ This will make our life much easier. You don't need t write your own optimizer.

    -

    A simple example

    +

    A simple example

    We remind ourselves about the general problem we want to solve @@ -1465,7 +1484,7 @@ sol[primal obj

    -

    Back to the more realistic cases

    +

    Back to the more realistic cases

    We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the slack parameter \( C \) we have diff --git a/doc/pub/week46/html/week46-solarized.html b/doc/pub/week46/html/week46-solarized.html index 25201908f..d07214093 100644 --- a/doc/pub/week46/html/week46-solarized.html +++ b/doc/pub/week46/html/week46-solarized.html @@ -36,39 +36,41 @@ div { text-align: justify; text-justify: inter-word; } @@ -110,7 +112,7 @@ MathJax.Hub.Config({

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

    -

    Nov 8, 2020

    +

    Nov 9, 2020












    @@ -118,8 +120,8 @@ MathJax.Hub.Config({

    Overview of week 46

      -
    • Thursday: Summary of Gradient Boosting and further examples of applications.
    • -
    • Friday: Support Vector Machines, classification and regression
    • +
    • Thursday: Summary of Gradient Boosting and further examples of applications, from the physical sciences to the social sciences.
    • +
    • Friday: Support Vector Machines, classification and regression.
    Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) of Hastie et al contains also a good discussion. @@ -130,7 +132,26 @@ Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) o











    -

    Support Vector Machines, overarching aims

    +

    Thursday

    + +

    +The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie et al. chapter 10.1-10.10. + +

    +The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education. + +

    +









    + +

    Friday

    + +

    +We start with a new topic and second-last topic covered in this course, support vector machines (SVM). + +

    +









    + +

    Support Vector Machines, overarching aims

    A Support Vector Machine (SVM) is a very powerful and versatile @@ -163,7 +184,7 @@ unlikely that we can separate classes easily by say straight lines.











    -

    Hyperplanes and all that

    +

    Hyperplanes and all that

    The theory behind support vector machines (SVM hereafter) is based on @@ -250,7 +271,7 @@ plt.show()











    -

    What is a hyperplane?

    +

    What is a hyperplane?

    The aim of the SVM algorithm is to find a hyperplane in a @@ -281,7 +302,7 @@ $$











    -

    A \( p \)-dimensional space of features

    +

    A \( p \)-dimensional space of features

    We limit ourselves to two classes of outputs \( y_i \) and assign these classes the values \( y_i = \pm 1 \). @@ -323,7 +344,7 @@ When we try to separate hyperplanes, if it exists, we can use it to construct a

    -

    The two-dimensional case

    +

    The two-dimensional case

    Let us try to develop our intuition about SVMs by limiting ourselves to a two-dimensional @@ -350,7 +371,7 @@ for our data sample.











    -

    Getting into the details

    +

    Getting into the details

    Let us define the function @@ -372,7 +393,7 @@ $$











    -

    First attempt at a minimization approach

    +

    First attempt at a minimization approach

    How do we find the parameter \( b \) and the vector \( \boldsymbol{w} \)? What we could @@ -397,7 +418,7 @@ $$











    -

    Solving the equations

    +

    Solving the equations

    We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations @@ -415,7 +436,7 @@ where \( \eta \) is our by now well-known learning rate.











    -

    Code Example

    +

    Code Example

    The equations we discussed above can be coded rather easily (the @@ -429,7 +450,7 @@ regression). We are going to set up a simple case with two classes only and we w











    -

    Problems with the Simpler Approach

    +

    Problems with the Simpler Approach

    There are however problems with this approach, although it looks @@ -445,7 +466,7 @@ at all.











    -

    A better approach

    +

    A better approach

    A better approach is rather to try to define a large margin between @@ -487,7 +508,7 @@ about Lagrangian multipliers.











    -

    A quick Reminder on Lagrangian Multipliers

    +

    A quick Reminder on Lagrangian Multipliers

    Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an @@ -537,7 +558,7 @@ Then \( dz \) is no longer arbitrary.











    -

    Adding the Multiplier

    +

    Adding the Multiplier

    However, we can add to @@ -580,7 +601,7 @@ $$











    -

    Setting up the Problem

    +

    Setting up the Problem

    In order to solve the above problem, we define the following Lagrangian function to be minimized $$ {\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right], @@ -621,7 +642,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support











    -

    The problem to solve

    +

    The problem to solve

    We can rewrite @@ -645,7 +666,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec











    -

    The last steps

    +

    The last steps

    Solving the above problem, yields the values of \( \lambda_i \). @@ -679,7 +700,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr











    -

    A soft classifier

    +

    A soft classifier

    Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined. @@ -714,7 +735,7 @@ misclassifications.











    -

    Soft optmization problem

    +

    Soft optmization problem

    This has in turn the consequences that we change our optmization problem to finding the minimum of @@ -768,7 +789,7 @@ $$











    -

    Kernels and non-linearity

    +

    Kernels and non-linearity

    The cases we have studied till now, were all characterized by two classes @@ -841,7 +862,7 @@ plt.show()











    -

    The equations

    +

    The equations

    Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables) @@ -886,7 +907,7 @@ the trouble of performing the transformation











    -

    The problem to solve

    +

    The problem to solve

    Using our definition of the kernel We can rewrite again the Lagrangian $$ {\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j, @@ -922,7 +943,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.











    -

    Different kernels and Mercer's theorem

    +

    Different kernels and Mercer's theorem

    There are several popular kernels being used. These are @@ -960,7 +981,7 @@ in practice.











    -

    The moons example

    +

    The moons example

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











    -

    Mathematical optimization of convex functions

    +

    Mathematical optimization of convex functions

    A mathematical (quadratic) optimization problem, or just optimization problem, has the form @@ -1181,7 +1202,7 @@ Convex optimization problems play a central role in applied mathematics and we r











    -

    How do we solve these problems?

    +

    How do we solve these problems?

    If we use Python as programming language and wish to venture beyond @@ -1207,7 +1228,7 @@ This will make our life much easier. You don't need t write your own optimizer.











    -

    A simple example

    +

    A simple example

    We remind ourselves about the general problem we want to solve @@ -1275,7 +1296,7 @@ sol[primal obj











    -

    Back to the more realistic cases

    +

    Back to the more realistic cases

    We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the slack parameter \( C \) we have diff --git a/doc/pub/week46/html/week46.html b/doc/pub/week46/html/week46.html index 44693ef4c..6ed02043a 100644 --- a/doc/pub/week46/html/week46.html +++ b/doc/pub/week46/html/week46.html @@ -41,39 +41,41 @@ div { text-align: justify; text-justify: inter-word; } @@ -115,7 +117,7 @@ MathJax.Hub.Config({

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

    -

    Nov 8, 2020

    +

    Nov 9, 2020












    @@ -123,8 +125,8 @@ MathJax.Hub.Config({

    Overview of week 46

      -
    • Thursday: Summary of Gradient Boosting and further examples of applications.
    • -
    • Friday: Support Vector Machines, classification and regression
    • +
    • Thursday: Summary of Gradient Boosting and further examples of applications, from the physical sciences to the social sciences.
    • +
    • Friday: Support Vector Machines, classification and regression.
    Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) of Hastie et al contains also a good discussion. @@ -135,7 +137,26 @@ Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) o











    -

    Support Vector Machines, overarching aims

    +

    Thursday

    + +

    +The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie et al. chapter 10.1-10.10. + +

    +The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education. + +

    +









    + +

    Friday

    + +

    +We start with a new topic and second-last topic covered in this course, support vector machines (SVM). + +

    +









    + +

    Support Vector Machines, overarching aims

    A Support Vector Machine (SVM) is a very powerful and versatile @@ -168,7 +189,7 @@ unlikely that we can separate classes easily by say straight lines.











    -

    Hyperplanes and all that

    +

    Hyperplanes and all that

    The theory behind support vector machines (SVM hereafter) is based on @@ -255,7 +276,7 @@ plt.show()











    -

    What is a hyperplane?

    +

    What is a hyperplane?

    The aim of the SVM algorithm is to find a hyperplane in a @@ -286,7 +307,7 @@ $$











    -

    A \( p \)-dimensional space of features

    +

    A \( p \)-dimensional space of features

    We limit ourselves to two classes of outputs \( y_i \) and assign these classes the values \( y_i = \pm 1 \). @@ -328,7 +349,7 @@ When we try to separate hyperplanes, if it exists, we can use it to construct a

    -

    The two-dimensional case

    +

    The two-dimensional case

    Let us try to develop our intuition about SVMs by limiting ourselves to a two-dimensional @@ -355,7 +376,7 @@ for our data sample.











    -

    Getting into the details

    +

    Getting into the details

    Let us define the function @@ -377,7 +398,7 @@ $$











    -

    First attempt at a minimization approach

    +

    First attempt at a minimization approach

    How do we find the parameter \( b \) and the vector \( \boldsymbol{w} \)? What we could @@ -402,7 +423,7 @@ $$











    -

    Solving the equations

    +

    Solving the equations

    We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations @@ -420,7 +441,7 @@ where \( \eta \) is our by now well-known learning rate.











    -

    Code Example

    +

    Code Example

    The equations we discussed above can be coded rather easily (the @@ -434,7 +455,7 @@ regression). We are going to set up a simple case with two classes only and we w











    -

    Problems with the Simpler Approach

    +

    Problems with the Simpler Approach

    There are however problems with this approach, although it looks @@ -450,7 +471,7 @@ at all.











    -

    A better approach

    +

    A better approach

    A better approach is rather to try to define a large margin between @@ -492,7 +513,7 @@ about Lagrangian multipliers.











    -

    A quick Reminder on Lagrangian Multipliers

    +

    A quick Reminder on Lagrangian Multipliers

    Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an @@ -542,7 +563,7 @@ Then \( dz \) is no longer arbitrary.











    -

    Adding the Multiplier

    +

    Adding the Multiplier

    However, we can add to @@ -585,7 +606,7 @@ $$











    -

    Setting up the Problem

    +

    Setting up the Problem

    In order to solve the above problem, we define the following Lagrangian function to be minimized $$ {\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right], @@ -626,7 +647,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support











    -

    The problem to solve

    +

    The problem to solve

    We can rewrite @@ -650,7 +671,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec











    -

    The last steps

    +

    The last steps

    Solving the above problem, yields the values of \( \lambda_i \). @@ -684,7 +705,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr











    -

    A soft classifier

    +

    A soft classifier

    Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined. @@ -719,7 +740,7 @@ misclassifications.











    -

    Soft optmization problem

    +

    Soft optmization problem

    This has in turn the consequences that we change our optmization problem to finding the minimum of @@ -773,7 +794,7 @@ $$











    -

    Kernels and non-linearity

    +

    Kernels and non-linearity

    The cases we have studied till now, were all characterized by two classes @@ -846,7 +867,7 @@ plt.show()











    -

    The equations

    +

    The equations

    Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables) @@ -891,7 +912,7 @@ the trouble of performing the transformation











    -

    The problem to solve

    +

    The problem to solve

    Using our definition of the kernel We can rewrite again the Lagrangian $$ {\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j, @@ -927,7 +948,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.











    -

    Different kernels and Mercer's theorem

    +

    Different kernels and Mercer's theorem

    There are several popular kernels being used. These are @@ -965,7 +986,7 @@ in practice.











    -

    The moons example

    +

    The moons example

    @@ -1161,7 +1182,7 @@ plt.show()











    -

    Mathematical optimization of convex functions

    +

    Mathematical optimization of convex functions

    A mathematical (quadratic) optimization problem, or just optimization problem, has the form @@ -1186,7 +1207,7 @@ Convex optimization problems play a central role in applied mathematics and we r











    -

    How do we solve these problems?

    +

    How do we solve these problems?

    If we use Python as programming language and wish to venture beyond @@ -1212,7 +1233,7 @@ This will make our life much easier. You don't need t write your own optimizer.











    -

    A simple example

    +

    A simple example

    We remind ourselves about the general problem we want to solve @@ -1280,7 +1301,7 @@ sol[’primal objective’]











    -

    Back to the more realistic cases

    +

    Back to the more realistic cases

    We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the slack parameter \( C \) we have diff --git a/doc/pub/week46/ipynb/ipynb-week46-src.tar.gz b/doc/pub/week46/ipynb/ipynb-week46-src.tar.gz index 214bd697939bd2fb9a87724a3301b00fcb65f4f4..8c8096a1226ce8bd8eb7c9613ea43ccd952eb17f 100644 GIT binary patch literal 191 zcmV;w06_mAiwFSYz^PvV1MSbv3c@f92k@Qu6nTQtuHAMP+`)q&;tO;vb9LIzwnO*! z?gR9sco`z}cli?%LbA_S>unyny9*XW2uV4IF|#z8lEia8p)>)?vLt4lFwTHb#*#Py zvfN28opr(tr_?7@7S-FiVXUe??3rJIXa0#pEiG(zy{okXr5zSpH*iC2cpAyJR|S+B t?brf?*G?J)p?VNyDV@|xoWs`WqY)Pyg};8r^E}V{+5>??4?_S5008faSLFZz literal 191 zcmV;w06_mAiwFRXXQ*EQ1MSaC3c@fD2H>uHia9|^nl@bvcHu%0@d7EG+E`6$lA^u6 zeSoeMH${Yen}0%vVP>DN*4sRCcNZ*%5W{v+C5h+vgwhl!%hH%|#)q0v#!?;t zS#G75&N^X+Rq7Kei|TFP&{kC+=FF$SGylY)mKHX<-ql)x(hdu)8@M4h36Esks{%@m tcKiZ^*G?J)p?VNyDV@|xoWs`WqY)Pyg}*+=^E}V{+5_xEL`nb%001ypS5yE1 diff --git a/doc/pub/week46/ipynb/week46.ipynb b/doc/pub/week46/ipynb/week46.ipynb index 29c980264..f226dd798 100644 --- a/doc/pub/week46/ipynb/week46.ipynb +++ b/doc/pub/week46/ipynb/week46.ipynb @@ -10,7 +10,7 @@ " \n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", "\n", - "Date: **Nov 8, 2020**\n", + "Date: **Nov 9, 2020**\n", "\n", "Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -18,15 +18,24 @@ "\n", "## Overview of week 46\n", "\n", - "* **Thursday**: Summary of Gradient Boosting and further examples of applications.\n", + "* **Thursday**: Summary of Gradient Boosting and further examples of applications, from the physical sciences to the social sciences.\n", "\n", - "* **Friday**: Support Vector Machines, classification and regression\n", + "* **Friday**: Support Vector Machines, classification and regression.\n", "\n", "Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) of Hastie et al contains also a good discussion.\n", "\n", "[Overview of Support Vector Machines](https://www.youtube.com/watch?v=efR1C6CvhmE&ab_channel=StatQuestwithJoshStarmer). see also [this video](https://www.youtube.com/watch?v=N1vOgolbjSc&ab_channel=AliceZhao).\n", "\n", "\n", + "## Thursday\n", + "\n", + "The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie *et al.* chapter 10.1-10.10.\n", + "\n", + "The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education.\n", + "\n", + "## Friday\n", + "\n", + "We start with a new topic and second-last topic covered in this course, support vector machines (SVM). \n", "\n", "## Support Vector Machines, overarching aims\n", "\n", diff --git a/doc/src/week46/week46.do.txt b/doc/src/week46/week46.do.txt index 703650cbd..7a77bf35f 100644 --- a/doc/src/week46/week46.do.txt +++ b/doc/src/week46/week46.do.txt @@ -5,8 +5,8 @@ DATE: today !split ===== Overview of week 46 ===== -* _Thursday_: Summary of Gradient Boosting and further examples of applications. -* _Friday_: Support Vector Machines, classification and regression +* _Thursday_: Summary of Gradient Boosting and further examples of applications, from the physical sciences to the social sciences. +* _Friday_: Support Vector Machines, classification and regression. Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) of Hastie et al contains also a good discussion. @@ -14,6 +14,17 @@ Geron's chapter 5. Chapter 12 (sections 12.1-12.3 are the most relevant ones) o "Overview of Support Vector Machines":"https://www.youtube.com/watch?v=efR1C6CvhmE&ab_channel=StatQuestwithJoshStarmer". see also "this video":"https://www.youtube.com/watch?v=N1vOgolbjSc&ab_channel=AliceZhao". +!split +===== Thursday ===== + +The first lecture on Thursday is devoted to a summary from last week, with additional examples. This material is included in the lectures from week 45, see also Hastie *et al.* chapter 10.1-10.10. + +The second letter will be given by John M. Aiken, who recently defended his thesis on machine learning, and in particular using boosting methods, to data from the social sciences and science education. + +!split +===== Friday ===== + +We start with a new topic and second-last topic covered in this course, support vector machines (SVM). !split ===== Support Vector Machines, overarching aims =====