removing hat symbols
This commit is contained in:
@@ -384,7 +384,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Aug 26, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Aug 27, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -369,7 +369,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<p>
|
||||
Having defined vectors, we are now ready to try out matrices. We can
|
||||
define a \( 3 \times 3 \) real matrix \( \hat{A} \) as (recall that we user
|
||||
define a \( 3 \times 3 \) real matrix \( \boldsymbol{A} \) as (recall that we user
|
||||
lowercase letters for vectors and uppercase letters for matrices)
|
||||
|
||||
<p>
|
||||
@@ -435,9 +435,9 @@ A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>r
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
||||
\( \hat{x}, \hat{y}, \hat{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
\( \boldsymbol{x}, \boldsymbol{y}, \boldsymbol{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
$$
|
||||
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\boldsymbol{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
||||
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
||||
\end{bmatrix},
|
||||
@@ -449,9 +449,9 @@ $$
|
||||
$$
|
||||
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \hat{W} \)
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \boldsymbol{W} \)
|
||||
$$
|
||||
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
x_1 & y_1 & z_1 \\
|
||||
x_2 & y_2 & z_2 \\
|
||||
\dots & \dots & \dots \\
|
||||
@@ -462,8 +462,8 @@ $$
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 3\times 3 \) covariance matrix
|
||||
\( \hat{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \hat{x} \) etc using the Numpy
|
||||
\( \boldsymbol{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
|
||||
@@ -373,7 +373,7 @@ We start with perhaps our simplest possible example, using <b>Scikit-Learn</b> t
|
||||
<p>
|
||||
What follows is a simple Python code where we have defined a function
|
||||
\( y \) in terms of the variable \( x \). Both are defined as vectors with \( 100 \) entries.
|
||||
The numbers in the vector \( \hat{x} \) are given
|
||||
The numbers in the vector \( \boldsymbol{x} \) are given
|
||||
by random numbers generated with a uniform distribution with entries
|
||||
\( x_i \in [0,1] \) (more about probability distribution functions
|
||||
later). These values are then used to define a function \( y(x) \)
|
||||
@@ -400,7 +400,7 @@ where \( N(0,1) \) represents random numbers generated by the normal
|
||||
distribution. From <b>Scikit-Learn</b> we import then the
|
||||
<b>LinearRegression</b> functionality and make a prediction \( \tilde{y} =
|
||||
\alpha + \beta x \) using the function <b>fit(x,y)</b>. We call the set of
|
||||
data \( (\hat{x},\hat{y}) \) for our training data. The Python package
|
||||
data \( (\boldsymbol{x},\boldsymbol{y}) \) for our training data. The Python package
|
||||
<b>scikit-learn</b> has also a functionality which extracts the above
|
||||
fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will
|
||||
distinguish between training data and test data.
|
||||
@@ -499,7 +499,7 @@ There are many ways to define the cost function. A simpler approach is to look a
|
||||
the relative error (why would we prefer the MSE instead of the relative error?) as
|
||||
|
||||
$$
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \boldsymbol{y} -\boldsymbol{\tilde{y}}\vert}{\vert \boldsymbol{y}\vert}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -581,7 +581,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
The function <b>coef</b> gives us the parameter \( \beta \) of our fit while <b>intercept</b> yields
|
||||
\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( \alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function <b>meansquarederror</b> gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
@@ -595,16 +595,16 @@ The <b>r2score</b> function computes \( R^2 \), the coefficient of
|
||||
determination. It provides a measure of how well future samples are
|
||||
likely to be predicted by the model. Best possible score is 1.0 and it
|
||||
can be negative (because the model can be arbitrarily worse). A
|
||||
constant model that always predicts the expected value of \( \hat{y} \),
|
||||
constant model that always predicts the expected value of \( \boldsymbol{y} \),
|
||||
disregarding the input features, would get a \( R^2 \) score of \( 0.0 \).
|
||||
|
||||
<p>
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
@@ -613,13 +613,13 @@ Another quantity taht we will meet again in our discussions of regression analys
|
||||
the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error.
|
||||
The MAE is defined as follows
|
||||
$$
|
||||
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\text{MAE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
$$
|
||||
|
||||
We present the
|
||||
squared logarithmic (quadratic) error
|
||||
$$
|
||||
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\text{MSLE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
$$
|
||||
|
||||
<p>
|
||||
|
||||
@@ -473,17 +473,17 @@ y <span style="color: #666666">=</span> <span style="color: #666666">2.0+5*</spa
|
||||
<li> Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as</li>
|
||||
</ol>
|
||||
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
and the \( R^2 \) score function.
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
@@ -384,7 +384,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Aug 26, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Aug 27, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p> <br>
|
||||
<center><h4>Aug 26, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Aug 27, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -1170,7 +1170,7 @@ x = np.log(np.array([<span style="color: #B452CD">4.0</span>, <span style="color
|
||||
|
||||
<p>
|
||||
Having defined vectors, we are now ready to try out matrices. We can
|
||||
define a \( 3 \times 3 \) real matrix \( \hat{A} \) as (recall that we user
|
||||
define a \( 3 \times 3 \) real matrix \( \boldsymbol{A} \) as (recall that we user
|
||||
lowercase letters for vectors and uppercase letters for matrices)
|
||||
|
||||
<p>
|
||||
@@ -1236,10 +1236,10 @@ A = np.random.rand(n, n)
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
||||
\( \hat{x}, \hat{y}, \hat{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
\( \boldsymbol{x}, \boldsymbol{y}, \boldsymbol{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
<p> <br>
|
||||
$$
|
||||
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\boldsymbol{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
||||
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
||||
\end{bmatrix},
|
||||
@@ -1254,10 +1254,10 @@ $$
|
||||
<p> <br>
|
||||
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \hat{W} \)
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \boldsymbol{W} \)
|
||||
<p> <br>
|
||||
$$
|
||||
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
x_1 & y_1 & z_1 \\
|
||||
x_2 & y_2 & z_2 \\
|
||||
\dots & \dots & \dots \\
|
||||
@@ -1269,8 +1269,8 @@ $$
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 3\times 3 \) covariance matrix
|
||||
\( \hat{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \hat{x} \) etc using the Numpy
|
||||
\( \boldsymbol{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
@@ -1457,7 +1457,7 @@ We start with perhaps our simplest possible example, using <b>Scikit-Learn</b> t
|
||||
<p>
|
||||
What follows is a simple Python code where we have defined a function
|
||||
\( y \) in terms of the variable \( x \). Both are defined as vectors with \( 100 \) entries.
|
||||
The numbers in the vector \( \hat{x} \) are given
|
||||
The numbers in the vector \( \boldsymbol{x} \) are given
|
||||
by random numbers generated with a uniform distribution with entries
|
||||
\( x_i \in [0,1] \) (more about probability distribution functions
|
||||
later). These values are then used to define a function \( y(x) \)
|
||||
@@ -1486,7 +1486,7 @@ where \( N(0,1) \) represents random numbers generated by the normal
|
||||
distribution. From <b>Scikit-Learn</b> we import then the
|
||||
<b>LinearRegression</b> functionality and make a prediction \( \tilde{y} =
|
||||
\alpha + \beta x \) using the function <b>fit(x,y)</b>. We call the set of
|
||||
data \( (\hat{x},\hat{y}) \) for our training data. The Python package
|
||||
data \( (\boldsymbol{x},\boldsymbol{y}) \) for our training data. The Python package
|
||||
<b>scikit-learn</b> has also a functionality which extracts the above
|
||||
fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will
|
||||
distinguish between training data and test data.
|
||||
@@ -1590,7 +1590,7 @@ the relative error (why would we prefer the MSE instead of the relative error?)
|
||||
|
||||
<p> <br>
|
||||
$$
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \boldsymbol{y} -\boldsymbol{\tilde{y}}\vert}{\vert \boldsymbol{y}\vert}.
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
@@ -1674,7 +1674,7 @@ plt.show()
|
||||
The function <b>coef</b> gives us the parameter \( \beta \) of our fit while <b>intercept</b> yields
|
||||
\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( \alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function <b>meansquarederror</b> gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
|
||||
<p> <br>
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
<p> <br>
|
||||
@@ -1689,18 +1689,18 @@ The <b>r2score</b> function computes \( R^2 \), the coefficient of
|
||||
determination. It provides a measure of how well future samples are
|
||||
likely to be predicted by the model. Best possible score is 1.0 and it
|
||||
can be negative (because the model can be arbitrarily worse). A
|
||||
constant model that always predicts the expected value of \( \hat{y} \),
|
||||
constant model that always predicts the expected value of \( \boldsymbol{y} \),
|
||||
disregarding the input features, would get a \( R^2 \) score of \( 0.0 \).
|
||||
|
||||
<p>
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
<p> <br>
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
<p> <br>
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
@@ -1712,7 +1712,7 @@ Another quantity taht we will meet again in our discussions of regression analys
|
||||
The MAE is defined as follows
|
||||
<p> <br>
|
||||
$$
|
||||
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\text{MAE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
@@ -1720,7 +1720,7 @@ We present the
|
||||
squared logarithmic (quadratic) error
|
||||
<p> <br>
|
||||
$$
|
||||
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\text{MSLE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
@@ -3406,20 +3406,20 @@ y = <span style="color: #B452CD">2.0</span>+<span style="color: #B452CD">5</span
|
||||
<p><li> Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as</li>
|
||||
</ol>
|
||||
<p> <br>
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
and the \( R^2 \) score function.
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
<p> <br>
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
<p> <br>
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
|
||||
@@ -300,7 +300,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Aug 26, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Aug 27, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1280,7 +1280,7 @@ x = np.log(np.array([<span style="color: #B452CD">4.0</span>, <span style="color
|
||||
|
||||
<p>
|
||||
Having defined vectors, we are now ready to try out matrices. We can
|
||||
define a \( 3 \times 3 \) real matrix \( \hat{A} \) as (recall that we user
|
||||
define a \( 3 \times 3 \) real matrix \( \boldsymbol{A} \) as (recall that we user
|
||||
lowercase letters for vectors and uppercase letters for matrices)
|
||||
|
||||
<p>
|
||||
@@ -1346,9 +1346,9 @@ A = np.random.rand(n, n)
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
||||
\( \hat{x}, \hat{y}, \hat{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
\( \boldsymbol{x}, \boldsymbol{y}, \boldsymbol{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
$$
|
||||
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\boldsymbol{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
||||
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
||||
\end{bmatrix},
|
||||
@@ -1360,9 +1360,9 @@ $$
|
||||
$$
|
||||
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \hat{W} \)
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \boldsymbol{W} \)
|
||||
$$
|
||||
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
x_1 & y_1 & z_1 \\
|
||||
x_2 & y_2 & z_2 \\
|
||||
\dots & \dots & \dots \\
|
||||
@@ -1373,8 +1373,8 @@ $$
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 3\times 3 \) covariance matrix
|
||||
\( \hat{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \hat{x} \) etc using the Numpy
|
||||
\( \boldsymbol{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
@@ -1559,7 +1559,7 @@ We start with perhaps our simplest possible example, using <b>Scikit-Learn</b> t
|
||||
<p>
|
||||
What follows is a simple Python code where we have defined a function
|
||||
\( y \) in terms of the variable \( x \). Both are defined as vectors with \( 100 \) entries.
|
||||
The numbers in the vector \( \hat{x} \) are given
|
||||
The numbers in the vector \( \boldsymbol{x} \) are given
|
||||
by random numbers generated with a uniform distribution with entries
|
||||
\( x_i \in [0,1] \) (more about probability distribution functions
|
||||
later). These values are then used to define a function \( y(x) \)
|
||||
@@ -1586,7 +1586,7 @@ where \( N(0,1) \) represents random numbers generated by the normal
|
||||
distribution. From <b>Scikit-Learn</b> we import then the
|
||||
<b>LinearRegression</b> functionality and make a prediction \( \tilde{y} =
|
||||
\alpha + \beta x \) using the function <b>fit(x,y)</b>. We call the set of
|
||||
data \( (\hat{x},\hat{y}) \) for our training data. The Python package
|
||||
data \( (\boldsymbol{x},\boldsymbol{y}) \) for our training data. The Python package
|
||||
<b>scikit-learn</b> has also a functionality which extracts the above
|
||||
fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will
|
||||
distinguish between training data and test data.
|
||||
@@ -1685,7 +1685,7 @@ There are many ways to define the cost function. A simpler approach is to look a
|
||||
the relative error (why would we prefer the MSE instead of the relative error?) as
|
||||
|
||||
$$
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \boldsymbol{y} -\boldsymbol{\tilde{y}}\vert}{\vert \boldsymbol{y}\vert}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -1767,7 +1767,7 @@ plt.show()
|
||||
<p>
|
||||
The function <b>coef</b> gives us the parameter \( \beta \) of our fit while <b>intercept</b> yields
|
||||
\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( \alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function <b>meansquarederror</b> gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
@@ -1781,16 +1781,16 @@ The <b>r2score</b> function computes \( R^2 \), the coefficient of
|
||||
determination. It provides a measure of how well future samples are
|
||||
likely to be predicted by the model. Best possible score is 1.0 and it
|
||||
can be negative (because the model can be arbitrarily worse). A
|
||||
constant model that always predicts the expected value of \( \hat{y} \),
|
||||
constant model that always predicts the expected value of \( \boldsymbol{y} \),
|
||||
disregarding the input features, would get a \( R^2 \) score of \( 0.0 \).
|
||||
|
||||
<p>
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
@@ -1799,13 +1799,13 @@ Another quantity taht we will meet again in our discussions of regression analys
|
||||
the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error.
|
||||
The MAE is defined as follows
|
||||
$$
|
||||
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\text{MAE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
$$
|
||||
|
||||
We present the
|
||||
squared logarithmic (quadratic) error
|
||||
$$
|
||||
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\text{MSLE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -3394,17 +3394,17 @@ y = <span style="color: #B452CD">2.0</span>+<span style="color: #B452CD">5</span
|
||||
<li> Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as</li>
|
||||
</ol>
|
||||
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
and the \( R^2 \) score function.
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
@@ -305,7 +305,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Aug 26, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Aug 27, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1285,7 +1285,7 @@ x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>l
|
||||
|
||||
<p>
|
||||
Having defined vectors, we are now ready to try out matrices. We can
|
||||
define a \( 3 \times 3 \) real matrix \( \hat{A} \) as (recall that we user
|
||||
define a \( 3 \times 3 \) real matrix \( \boldsymbol{A} \) as (recall that we user
|
||||
lowercase letters for vectors and uppercase letters for matrices)
|
||||
|
||||
<p>
|
||||
@@ -1351,9 +1351,9 @@ A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>r
|
||||
<p>
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
||||
\( \hat{x}, \hat{y}, \hat{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
\( \boldsymbol{x}, \boldsymbol{y}, \boldsymbol{z} \) with \( n \) elements each. The covariance matrix is defined as
|
||||
$$
|
||||
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\boldsymbol{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
||||
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
||||
\end{bmatrix},
|
||||
@@ -1365,9 +1365,9 @@ $$
|
||||
$$
|
||||
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \hat{W} \)
|
||||
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \boldsymbol{W} \)
|
||||
$$
|
||||
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
x_1 & y_1 & z_1 \\
|
||||
x_2 & y_2 & z_2 \\
|
||||
\dots & \dots & \dots \\
|
||||
@@ -1378,8 +1378,8 @@ $$
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 3\times 3 \) covariance matrix
|
||||
\( \hat{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \hat{x} \) etc using the Numpy
|
||||
\( \boldsymbol{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
@@ -1564,7 +1564,7 @@ We start with perhaps our simplest possible example, using <b>Scikit-Learn</b> t
|
||||
<p>
|
||||
What follows is a simple Python code where we have defined a function
|
||||
\( y \) in terms of the variable \( x \). Both are defined as vectors with \( 100 \) entries.
|
||||
The numbers in the vector \( \hat{x} \) are given
|
||||
The numbers in the vector \( \boldsymbol{x} \) are given
|
||||
by random numbers generated with a uniform distribution with entries
|
||||
\( x_i \in [0,1] \) (more about probability distribution functions
|
||||
later). These values are then used to define a function \( y(x) \)
|
||||
@@ -1591,7 +1591,7 @@ where \( N(0,1) \) represents random numbers generated by the normal
|
||||
distribution. From <b>Scikit-Learn</b> we import then the
|
||||
<b>LinearRegression</b> functionality and make a prediction \( \tilde{y} =
|
||||
\alpha + \beta x \) using the function <b>fit(x,y)</b>. We call the set of
|
||||
data \( (\hat{x},\hat{y}) \) for our training data. The Python package
|
||||
data \( (\boldsymbol{x},\boldsymbol{y}) \) for our training data. The Python package
|
||||
<b>scikit-learn</b> has also a functionality which extracts the above
|
||||
fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will
|
||||
distinguish between training data and test data.
|
||||
@@ -1690,7 +1690,7 @@ There are many ways to define the cost function. A simpler approach is to look a
|
||||
the relative error (why would we prefer the MSE instead of the relative error?) as
|
||||
|
||||
$$
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \boldsymbol{y} -\boldsymbol{\tilde{y}}\vert}{\vert \boldsymbol{y}\vert}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -1772,7 +1772,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
The function <b>coef</b> gives us the parameter \( \beta \) of our fit while <b>intercept</b> yields
|
||||
\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( \alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function <b>meansquarederror</b> gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
@@ -1786,16 +1786,16 @@ The <b>r2score</b> function computes \( R^2 \), the coefficient of
|
||||
determination. It provides a measure of how well future samples are
|
||||
likely to be predicted by the model. Best possible score is 1.0 and it
|
||||
can be negative (because the model can be arbitrarily worse). A
|
||||
constant model that always predicts the expected value of \( \hat{y} \),
|
||||
constant model that always predicts the expected value of \( \boldsymbol{y} \),
|
||||
disregarding the input features, would get a \( R^2 \) score of \( 0.0 \).
|
||||
|
||||
<p>
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
@@ -1804,13 +1804,13 @@ Another quantity taht we will meet again in our discussions of regression analys
|
||||
the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error.
|
||||
The MAE is defined as follows
|
||||
$$
|
||||
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\text{MAE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
$$
|
||||
|
||||
We present the
|
||||
squared logarithmic (quadratic) error
|
||||
$$
|
||||
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\text{MSLE}(\boldsymbol{y}, \boldsymbol{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -3399,17 +3399,17 @@ y <span style="color: #666666">=</span> <span style="color: #666666">2.0+5*</spa
|
||||
<li> Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as</li>
|
||||
</ol>
|
||||
|
||||
$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
and the \( R^2 \) score function.
|
||||
If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as
|
||||
$$
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
where we have defined the mean value of \( \boldsymbol{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
Binary file not shown.
@@ -10,7 +10,7 @@
|
||||
"<!-- Author: --> \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: **Aug 26, 2021**\n",
|
||||
"Date: **Aug 27, 2021**\n",
|
||||
"\n",
|
||||
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||||
"\n",
|
||||
@@ -994,7 +994,7 @@
|
||||
"## Matrices in Python\n",
|
||||
"\n",
|
||||
"Having defined vectors, we are now ready to try out matrices. We can\n",
|
||||
"define a $3 \\times 3 $ real matrix $\\hat{A}$ as (recall that we user\n",
|
||||
"define a $3 \\times 3 $ real matrix $\\boldsymbol{A}$ as (recall that we user\n",
|
||||
"lowercase letters for vectors and uppercase letters for matrices)"
|
||||
]
|
||||
},
|
||||
@@ -1131,7 +1131,7 @@
|
||||
"source": [
|
||||
"As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.\n",
|
||||
"As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors\n",
|
||||
"$\\hat{x}, \\hat{y}, \\hat{z}$ with $n$ elements each. The covariance matrix is defined as"
|
||||
"$\\boldsymbol{x}, \\boldsymbol{y}, \\boldsymbol{z}$ with $n$ elements each. The covariance matrix is defined as"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1139,7 +1139,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\hat{\\Sigma} = \\begin{bmatrix} \\sigma_{xx} & \\sigma_{xy} & \\sigma_{xz} \\\\\n",
|
||||
"\\boldsymbol{\\Sigma} = \\begin{bmatrix} \\sigma_{xx} & \\sigma_{xy} & \\sigma_{xz} \\\\\n",
|
||||
" \\sigma_{yx} & \\sigma_{yy} & \\sigma_{yz} \\\\\n",
|
||||
" \\sigma_{zx} & \\sigma_{zy} & \\sigma_{zz} \n",
|
||||
" \\end{bmatrix},\n",
|
||||
@@ -1167,7 +1167,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The Numpy function **np.cov** calculates the covariance elements using the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have the exact mean values. \n",
|
||||
"The following simple function uses the **np.vstack** function which takes each vector of dimension $1\\times n$ and produces a $3\\times n$ matrix $\\hat{W}$"
|
||||
"The following simple function uses the **np.vstack** function which takes each vector of dimension $1\\times n$ and produces a $3\\times n$ matrix $\\boldsymbol{W}$"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1175,7 +1175,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\hat{W} = \\begin{bmatrix} x_0 & y_0 & z_0 \\\\\n",
|
||||
"\\boldsymbol{W} = \\begin{bmatrix} x_0 & y_0 & z_0 \\\\\n",
|
||||
" x_1 & y_1 & z_1 \\\\\n",
|
||||
" x_2 & y_2 & z_2 \\\\\n",
|
||||
" \\dots & \\dots & \\dots \\\\\n",
|
||||
@@ -1190,8 +1190,8 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"which in turn is converted into into the $3\\times 3$ covariance matrix\n",
|
||||
"$\\hat{\\Sigma}$ via the Numpy function **np.cov()**. We note that we can also calculate\n",
|
||||
"the mean value of each set of samples $\\hat{x}$ etc using the Numpy\n",
|
||||
"$\\boldsymbol{\\Sigma}$ via the Numpy function **np.cov()**. We note that we can also calculate\n",
|
||||
"the mean value of each set of samples $\\boldsymbol{x}$ etc using the Numpy\n",
|
||||
"function **np.mean(x)**. We can also extract the eigenvalues of the\n",
|
||||
"covariance matrix through the **np.linalg.eig()** function."
|
||||
]
|
||||
@@ -1472,7 +1472,7 @@
|
||||
"\n",
|
||||
"What follows is a simple Python code where we have defined a function\n",
|
||||
"$y$ in terms of the variable $x$. Both are defined as vectors with $100$ entries. \n",
|
||||
"The numbers in the vector $\\hat{x}$ are given\n",
|
||||
"The numbers in the vector $\\boldsymbol{x}$ are given\n",
|
||||
"by random numbers generated with a uniform distribution with entries\n",
|
||||
"$x_i \\in [0,1]$ (more about probability distribution functions\n",
|
||||
"later). These values are then used to define a function $y(x)$\n",
|
||||
@@ -1508,7 +1508,7 @@
|
||||
"distribution. From **Scikit-Learn** we import then the\n",
|
||||
"**LinearRegression** functionality and make a prediction $\\tilde{y} =\n",
|
||||
"\\alpha + \\beta x$ using the function **fit(x,y)**. We call the set of\n",
|
||||
"data $(\\hat{x},\\hat{y})$ for our training data. The Python package\n",
|
||||
"data $(\\boldsymbol{x},\\boldsymbol{y})$ for our training data. The Python package\n",
|
||||
"**scikit-learn** has also a functionality which extracts the above\n",
|
||||
"fitting parameters $\\alpha$ and $\\beta$ (see below). Later we will\n",
|
||||
"distinguish between training data and test data.\n",
|
||||
@@ -1638,7 +1638,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\epsilon_{\\mathrm{relative}}= \\frac{\\vert \\hat{y} -\\hat{\\tilde{y}}\\vert}{\\vert \\hat{y}\\vert}.\n",
|
||||
"\\epsilon_{\\mathrm{relative}}= \\frac{\\vert \\boldsymbol{y} -\\boldsymbol{\\tilde{y}}\\vert}{\\vert \\boldsymbol{y}\\vert}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -1752,7 +1752,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"MSE(\\hat{y},\\hat{\\tilde{y}}) = \\frac{1}{n}\n",
|
||||
"MSE(\\boldsymbol{y},\\boldsymbol{\\tilde{y}}) = \\frac{1}{n}\n",
|
||||
"\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2,\n",
|
||||
"$$"
|
||||
]
|
||||
@@ -1769,10 +1769,10 @@
|
||||
"determination. It provides a measure of how well future samples are\n",
|
||||
"likely to be predicted by the model. Best possible score is 1.0 and it\n",
|
||||
"can be negative (because the model can be arbitrarily worse). A\n",
|
||||
"constant model that always predicts the expected value of $\\hat{y}$,\n",
|
||||
"constant model that always predicts the expected value of $\\boldsymbol{y}$,\n",
|
||||
"disregarding the input features, would get a $R^2$ score of $0.0$.\n",
|
||||
"\n",
|
||||
"If $\\tilde{\\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as"
|
||||
"If $\\tilde{\\boldsymbol{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1780,7 +1780,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"R^2(\\hat{y}, \\tilde{\\hat{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -1788,7 +1788,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"where we have defined the mean value of $\\hat{y}$ as"
|
||||
"where we have defined the mean value of $\\boldsymbol{y}$ as"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1814,7 +1814,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\text{MAE}(\\hat{y}, \\hat{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n-1} \\left| y_i - \\tilde{y}_i \\right|.\n",
|
||||
"\\text{MAE}(\\boldsymbol{y}, \\boldsymbol{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n-1} \\left| y_i - \\tilde{y}_i \\right|.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -1831,7 +1831,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\text{MSLE}(\\hat{y}, \\hat{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n - 1} (\\log_e (1 + y_i) - \\log_e (1 + \\tilde{y}_i) )^2,\n",
|
||||
"\\text{MSLE}(\\boldsymbol{y}, \\boldsymbol{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n - 1} (\\log_e (1 + y_i) - \\log_e (1 + \\tilde{y}_i) )^2,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -4156,7 +4156,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"MSE(\\hat{y},\\hat{\\tilde{y}}) = \\frac{1}{n}\n",
|
||||
"MSE(\\boldsymbol{y},\\boldsymbol{\\tilde{y}}) = \\frac{1}{n}\n",
|
||||
"\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2,\n",
|
||||
"$$"
|
||||
]
|
||||
@@ -4166,7 +4166,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"and the $R^2$ score function.\n",
|
||||
"If $\\tilde{\\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as"
|
||||
"If $\\tilde{\\boldsymbol{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -4174,7 +4174,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"R^2(\\hat{y}, \\tilde{\\hat{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -4182,7 +4182,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"where we have defined the mean value of $\\hat{y}$ as"
|
||||
"where we have defined the mean value of $\\boldsymbol{y}$ as"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -786,7 +786,7 @@ print(x.itemsize)
|
||||
===== Matrices in Python =====
|
||||
|
||||
Having defined vectors, we are now ready to try out matrices. We can
|
||||
define a $3 \times 3 $ real matrix $\hat{A}$ as (recall that we user
|
||||
define a $3 \times 3 $ real matrix $\bm{A}$ as (recall that we user
|
||||
lowercase letters for vectors and uppercase letters for matrices)
|
||||
|
||||
!bc pycod
|
||||
@@ -835,10 +835,10 @@ print(A)
|
||||
|
||||
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
||||
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
||||
$\hat{x}, \hat{y}, \hat{z}$ with $n$ elements each. The covariance matrix is defined as
|
||||
$\bm{x}, \bm{y}, \bm{z}$ with $n$ elements each. The covariance matrix is defined as
|
||||
!bt
|
||||
\[
|
||||
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\bm{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
||||
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
||||
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
||||
\end{bmatrix},
|
||||
@@ -851,10 +851,10 @@ where for example
|
||||
\]
|
||||
!et
|
||||
The Numpy function _np.cov_ calculates the covariance elements using the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have the exact mean values.
|
||||
The following simple function uses the _np.vstack_ function which takes each vector of dimension $1\times n$ and produces a $3\times n$ matrix $\hat{W}$
|
||||
The following simple function uses the _np.vstack_ function which takes each vector of dimension $1\times n$ and produces a $3\times n$ matrix $\bm{W}$
|
||||
!bt
|
||||
\[
|
||||
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
\bm{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
||||
x_1 & y_1 & z_1 \\
|
||||
x_2 & y_2 & z_2 \\
|
||||
\dots & \dots & \dots \\
|
||||
@@ -865,8 +865,8 @@ The following simple function uses the _np.vstack_ function which takes each vec
|
||||
!et
|
||||
|
||||
which in turn is converted into into the $3\times 3$ covariance matrix
|
||||
$\hat{\Sigma}$ via the Numpy function _np.cov()_. We note that we can also calculate
|
||||
the mean value of each set of samples $\hat{x}$ etc using the Numpy
|
||||
$\bm{\Sigma}$ via the Numpy function _np.cov()_. We note that we can also calculate
|
||||
the mean value of each set of samples $\bm{x}$ etc using the Numpy
|
||||
function _np.mean(x)_. We can also extract the eigenvalues of the
|
||||
covariance matrix through the _np.linalg.eig()_ function.
|
||||
|
||||
@@ -1026,7 +1026,7 @@ We start with perhaps our simplest possible example, using _Scikit-Learn_ to per
|
||||
|
||||
What follows is a simple Python code where we have defined a function
|
||||
$y$ in terms of the variable $x$. Both are defined as vectors with $100$ entries.
|
||||
The numbers in the vector $\hat{x}$ are given
|
||||
The numbers in the vector $\bm{x}$ are given
|
||||
by random numbers generated with a uniform distribution with entries
|
||||
$x_i \in [0,1]$ (more about probability distribution functions
|
||||
later). These values are then used to define a function $y(x)$
|
||||
@@ -1054,7 +1054,7 @@ where $N(0,1)$ represents random numbers generated by the normal
|
||||
distribution. From _Scikit-Learn_ we import then the
|
||||
_LinearRegression_ functionality and make a prediction $\tilde{y} =
|
||||
\alpha + \beta x$ using the function _fit(x,y)_. We call the set of
|
||||
data $(\hat{x},\hat{y})$ for our training data. The Python package
|
||||
data $(\bm{x},\bm{y})$ for our training data. The Python package
|
||||
_scikit-learn_ has also a functionality which extracts the above
|
||||
fitting parameters $\alpha$ and $\beta$ (see below). Later we will
|
||||
distinguish between training data and test data.
|
||||
@@ -1149,7 +1149,7 @@ the relative error (why would we prefer the MSE instead of the relative error?)
|
||||
|
||||
!bt
|
||||
\[
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
|
||||
\epsilon_{\mathrm{relative}}= \frac{\vert \bm{y} -\bm{\tilde{y}}\vert}{\vert \bm{y}\vert}.
|
||||
\]
|
||||
!et
|
||||
|
||||
@@ -1225,7 +1225,7 @@ plt.show()
|
||||
The function _coef_ gives us the parameter $\beta$ of our fit while _intercept_ yields
|
||||
$\alpha$. Depending on the constant in front of the normal distribution, we get values near or far from $\alpha =2$ and $\beta =5$. Try to play around with different parameters in front of the normal distribution. The function _meansquarederror_ gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
|
||||
!bt
|
||||
\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
\[ MSE(\bm{y},\bm{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
\]
|
||||
!et
|
||||
@@ -1238,16 +1238,16 @@ The _r2score_ function computes $R^2$, the coefficient of
|
||||
determination. It provides a measure of how well future samples are
|
||||
likely to be predicted by the model. Best possible score is 1.0 and it
|
||||
can be negative (because the model can be arbitrarily worse). A
|
||||
constant model that always predicts the expected value of $\hat{y}$,
|
||||
constant model that always predicts the expected value of $\bm{y}$,
|
||||
disregarding the input features, would get a $R^2$ score of $0.0$.
|
||||
|
||||
If $\tilde{\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as
|
||||
If $\tilde{\bm{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as
|
||||
!bt
|
||||
\[
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\bm{y}, \tilde{\bm{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
\]
|
||||
!et
|
||||
where we have defined the mean value of $\hat{y}$ as
|
||||
where we have defined the mean value of $\bm{y}$ as
|
||||
!bt
|
||||
\[
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
@@ -1258,14 +1258,14 @@ Another quantity taht we will meet again in our discussions of regression analys
|
||||
The MAE is defined as follows
|
||||
!bt
|
||||
\[
|
||||
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\text{MAE}(\bm{y}, \bm{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
|
||||
\]
|
||||
!et
|
||||
We present the
|
||||
squared logarithmic (quadratic) error
|
||||
!bt
|
||||
\[
|
||||
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\text{MSLE}(\bm{y}, \bm{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
|
||||
\]
|
||||
!et
|
||||
|
||||
@@ -2723,18 +2723,18 @@ o Write your own code (following the examples under the "regression notes":"http
|
||||
o Use thereafter _scikit-learn_ (see again the examples in the regression slides) and compare with your own code.
|
||||
o Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as
|
||||
!bt
|
||||
\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||||
\[ MSE(\bm{y},\bm{\tilde{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
\]
|
||||
!et
|
||||
and the $R^2$ score function.
|
||||
If $\tilde{\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as
|
||||
If $\tilde{\bm{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as
|
||||
!bt
|
||||
\[
|
||||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
R^2(\bm{y}, \tilde{\bm{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
\]
|
||||
!et
|
||||
where we have defined the mean value of $\hat{y}$ as
|
||||
where we have defined the mean value of $\bm{y}$ as
|
||||
!bt
|
||||
\[
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
|
||||
Reference in New Issue
Block a user