Skip to content

Instantly share code, notes, and snippets.

@amirziai
Created June 12, 2018 04:59
Show Gist options
  • Select an option

  • Save amirziai/ab7d6e5f9cfc8ad832cadaf10b112c1e to your computer and use it in GitHub Desktop.

Select an option

Save amirziai/ab7d6e5f9cfc8ad832cadaf10b112c1e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"x = range(1, 10 + 1)\n",
"x = np.array(x)[:, np.newaxis]\n",
"y = np.exp(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr = LinearRegression()\n",
"lr.fit(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5139031732249719"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.score(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-3962.96101439],\n",
" [-2307.99692574],\n",
" [ -653.03283709],\n",
" [ 1001.93125156],\n",
" [ 2656.89534021],\n",
" [ 4311.85942886],\n",
" [ 5966.82351751],\n",
" [ 7621.78760616],\n",
" [ 9276.75169481],\n",
" [10931.71578346]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict(x)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"ss_tot = np.sum((y - np.mean(y)) ** 2)\n",
"ss_res = np.sum((lr.predict(x) - y) ** 2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"439693249.40453446"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss_tot"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"213733493.2899452"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss_res"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5139031732249719"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r2 = 1 - ss_res / ss_tot\n",
"r2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Resources"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.wikiwand.com/en/Coefficient_of_determinationm"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment