Skip to content

Instantly share code, notes, and snippets.

@jaganadhg
Created October 9, 2013 14:09
Show Gist options
  • Select an option

  • Save jaganadhg/6901929 to your computer and use it in GitHub Desktop.

Select an option

Save jaganadhg/6901929 to your computer and use it in GitHub Desktop.
Scipy_linier_regression
{
"metadata": {
"name": "Scipy_linier_regression"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"#http://scipy-central.org/item/16/0/basic-linear-regression\n",
"from scipy import stats\n",
"import numpy as np\n",
"import pylab\n",
"\n",
"# Fit the model\n",
"x = np.array([1, 2, 5, 7, 10, 15])\n",
"y = np.array([2, 6, 7, 9, 14, 19])\n",
"slope, intercept, r_value, p_value, slope_std_error = stats.linregress(x, y)\n",
"\n",
"# Calculate some additional outputs\n",
"predict_y = intercept + slope * x\n",
"pred_error = y - predict_y\n",
"degrees_of_freedom = len(x) - 2\n",
"residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom)\n",
"\n",
"# Plotting\n",
"pylab.plot(x, y, 'o')\n",
"pylab.plot(x, predict_y, 'k-')\n",
"pylab.show()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment