added more scikit-learn functionality

This commit is contained in:
mhjensen
2018-05-28 08:41:15 -04:00
parent b4246a0ebd
commit eb8d0529b0
10 changed files with 247 additions and 61 deletions
+55 -8
View File
@@ -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: **May 27, 2018**\n",
"Date: **May 28, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -96,7 +96,7 @@
"recommend two widely used distrubutions which set up all relevant\n",
"dependencies for Python, namely \n",
"\n",
"1. [Anaconda](https://docs.anaconda.com/), \n",
"* [Anaconda](https://docs.anaconda.com/), \n",
"\n",
"which is an open source\n",
"distribution of the Python and R programming languages for large-scale\n",
@@ -104,7 +104,7 @@
"aims to simplify package management and deployment. Package versions\n",
"are managed by the package management system **conda**. \n",
"\n",
"1. [Enthought canopy](https://www.enthought.com/product/canopy/) \n",
"* [Enthought canopy](https://www.enthought.com/product/canopy/) \n",
"\n",
"is a Python\n",
"distribution for scientific and analytic computing distribution and\n",
@@ -382,7 +382,9 @@
"As mentioned above, **scikit-learn** has an impressive functionality.\n",
"We can for example extract the values of $\\alpha$ and $\\beta$ and\n",
"their error estimates, or the variance and standard deviation and many\n",
"other properties from the statistical data analysis. Here we show an\n",
"other properties from the statistical data analysis. \n",
"\n",
"Here we show an\n",
"example of the functionality of scikit-learn."
]
},
@@ -397,7 +399,7 @@
"import numpy as np \n",
"import matplotlib.pyplot as plt \n",
"from sklearn.linear_model import LinearRegression \n",
"from sklearn.metrics import mean_squared_error, r2_score\n",
"from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error\n",
"\n",
"x = np.random.rand(100,1)\n",
"y = 2.0+ 5*x+0.5np.random.randn(100,1)\n",
@@ -410,6 +412,10 @@
"print(\"Mean squared error: %.2f\" % mean_squared_error(y, ypredict))\n",
"# Explained variance score: 1 is perfect prediction \n",
"print('Variance score: %.2f' % r2_score(y, ypredict))\n",
"# Mean squared log error \n",
"print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )\n",
"# Mean absolute error \n",
"print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))\n",
"plt.plot(x, ypredict, \"r-\")\n",
"plt.plot(x, y ,'ro')\n",
"plt.axis([0.0,1.0,1.5, 7.0])\n",
@@ -452,7 +458,7 @@
"constant model that always predicts the expected value of $\\hat{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{\\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"
]
},
{
@@ -468,7 +474,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"where the mean value"
"where we have defined the mean value of $\\hat{y}$ as"
]
},
{
@@ -484,7 +490,48 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We will discuss in more detail these and more function in the various lectures.\n",
"Another quantity will meet again in our discussions of regression analysis is \n",
" 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.\n",
"The MAE is defined as follows"
]
},
{
"cell_type": "markdown",
"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",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally we present the \n",
"squared logarithmic (quadratic) error"
]
},
{
"cell_type": "markdown",
"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",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where $\\log_e (x)$ stands for the natural logarithm of $x$. This error\n",
"estimate is best to use when targets having exponential growth, such\n",
"as population counts, average sales of a commodity over a span of\n",
"years etc. \n",
"\n",
"We will discuss in more\n",
"detail these and more function in the various lectures.\n",
"\n",
"Another useful Python package is\n",
"[pandas](https://pandas.pydata.org/), which is an open source library\n",