Added more scikit-learn functions
This commit is contained in:
@@ -93,7 +93,7 @@ data processing, predictive analytics, and scientific computing, that
|
||||
aims to simplify package management and deployment. Package versions
|
||||
are managed by the package management system _conda_.
|
||||
|
||||
o "Enthoughtmcanopy":"https://www.enthought.com/product/canopy/"
|
||||
o "Enthought canopy":"https://www.enthought.com/product/canopy/"
|
||||
|
||||
is a Python
|
||||
distribution for scientific and analytic computing distribution and
|
||||
@@ -296,37 +296,74 @@ plt.title(r'Relative error')
|
||||
plt.show()
|
||||
!ec
|
||||
|
||||
Depending on the parameter in front of the normal distribution, we may have a small or larger relative error. Try to play around with different training data sets and study (graphically) the value of the relative error.
|
||||
Depending on the parameter in front of the normal distribution, we may
|
||||
have a small or larger relative error. Try to play around with
|
||||
different training data sets and study (graphically) the value of the
|
||||
relative error.
|
||||
|
||||
As mentioned above, _scikit-learn_ 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.
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression
|
||||
As mentioned above, _scikit-learn_ 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.
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.metrics import mean_squared_error, r2_score
|
||||
|
||||
x = np.random.rand(100,1)
|
||||
y = 2*x+np.random.randn(100,1)
|
||||
y = 2.0+ 5*x+0.5np.random.randn(100,1)
|
||||
linreg = LinearRegression()
|
||||
linreg.fit(x,y)
|
||||
ypredict = linreg.predict(x)
|
||||
print('Coefficients: \n', linreg.coef_)
|
||||
print('The intercept alpha: \n', linreg.intercept_)
|
||||
print('Coefficient beta : \n', linreg.coef_)
|
||||
# The mean squared error
|
||||
print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
|
||||
# Explained variance score: 1 is perfect prediction
|
||||
print('Variance score: %.2f' % r2_score(y, ypredict))
|
||||
plt.plot(x, ypredict, "r-")
|
||||
plt.plot(x, y ,'ro')
|
||||
plt.axis([0,1.0,0, 5.0])
|
||||
plt.axis([0.0,1.0,1.5, 7.0])
|
||||
plt.xlabel(r'$x$')
|
||||
plt.ylabel(r'$y$')
|
||||
plt.title(r'Linear Regression fit ')
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
We will come to the definition of these outputs later.
|
||||
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}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
\]
|
||||
!et
|
||||
|
||||
The smaller the value, the better the fit. Ideally we would like to
|
||||
have an MSE equal zero. The attentive reader has probably recognized
|
||||
this function as being similar to the $\chi^2$ function defined above.
|
||||
|
||||
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}$,
|
||||
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
|
||||
!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},
|
||||
\]
|
||||
!et
|
||||
where the mean value
|
||||
!bt
|
||||
\[
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
\]
|
||||
!et
|
||||
We will discuss in more detail these and more function in the various lectures.
|
||||
|
||||
Another useful Python package is
|
||||
"pandas":"https://pandas.pydata.org/", which is an open source library
|
||||
|
||||
Reference in New Issue
Block a user