removing hat symbols
This commit is contained in:
@@ -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