added more scikit-learn functionality
This commit is contained in:
@@ -156,7 +156,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>May 27, 2018</h4></center> <!-- date -->
|
||||
<center><h4>May 28, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -253,9 +253,9 @@ into the hassle of exploring how to set up dependencies and paths, we
|
||||
recommend two widely used distrubutions which set up all relevant
|
||||
dependencies for Python, namely
|
||||
|
||||
<ol>
|
||||
<ul>
|
||||
<li> <a href="https://docs.anaconda.com/" target="_blank">Anaconda</a>,</li>
|
||||
</ol>
|
||||
</ul>
|
||||
|
||||
which is an open source
|
||||
distribution of the Python and R programming languages for large-scale
|
||||
@@ -263,9 +263,9 @@ data processing, predictive analytics, and scientific computing, that
|
||||
aims to simplify package management and deployment. Package versions
|
||||
are managed by the package management system <b>conda</b>.
|
||||
|
||||
<ol>
|
||||
<ul>
|
||||
<li> <a href="https://www.enthought.com/product/canopy/" target="_blank">Enthought canopy</a></li>
|
||||
</ol>
|
||||
</ul>
|
||||
|
||||
is a Python
|
||||
distribution for scientific and analytic computing distribution and
|
||||
@@ -492,16 +492,18 @@ relative error.
|
||||
As mentioned above, <b>scikit-learn</b> has an impressive functionality.
|
||||
We can for example extract the values of \( \alpha \) and \( \beta \) and
|
||||
their error estimates, or the variance and standard deviation and many
|
||||
other properties from the statistical data analysis. Here we show an
|
||||
example of the functionality of scikit-learn.
|
||||
other properties from the statistical data analysis.
|
||||
|
||||
<p>
|
||||
Here we show an
|
||||
example of the functionality of scikit-learn.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error, r2_score
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
|
||||
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">2.0+</span> <span style="color: #666666">5*</span>x<span style="color: #666666">+0.5</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
|
||||
@@ -514,6 +516,10 @@ ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> mean_squared_error(y, ypredict))
|
||||
<span style="color: #408080; font-style: italic"># Explained variance score: 1 is perfect prediction </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> r2_score(y, ypredict))
|
||||
<span style="color: #408080; font-style: italic"># Mean squared log error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Mean squared log error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_squared_log_error(y, ypredict) )
|
||||
<span style="color: #408080; font-style: italic"># Mean absolute error </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">'Mean absolute error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">'</span> <span style="color: #666666">%</span> mean_absolute_error(y, ypredict))
|
||||
plt<span style="color: #666666">.</span>plot(x, ypredict, <span style="color: #BA2121">"r-"</span>)
|
||||
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">'ro'</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0.0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">1.5</span>, <span style="color: #666666">7.0</span>])
|
||||
@@ -543,17 +549,38 @@ constant model that always predicts the expected value of \( \hat{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{\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
|
||||
$$
|
||||
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},
|
||||
$$
|
||||
|
||||
where the mean value
|
||||
where we have defined the mean value of \( \hat{y} \) as
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
We will discuss in more detail these and more function in the various lectures.
|
||||
Another quantity will meet again in our discussions of regression analysis is
|
||||
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|.
|
||||
$$
|
||||
|
||||
Finally 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,
|
||||
$$
|
||||
|
||||
<p>
|
||||
where \( \log_e (x) \) stands for the natural logarithm of \( x \). This error
|
||||
estimate is best to use when targets having exponential growth, such
|
||||
as population counts, average sales of a commodity over a span of
|
||||
years etc.
|
||||
|
||||
<p>
|
||||
We will discuss in more
|
||||
detail these and more function in the various lectures.
|
||||
|
||||
<p>
|
||||
Another useful Python package is
|
||||
|
||||
Reference in New Issue
Block a user