mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
134 lines
24 KiB
Plaintext
134 lines
24 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# scikit-learn-linear-reg"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"* Linear Regression"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib inline\n",
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"import seaborn; \n",
|
||
|
"from sklearn.linear_model import LinearRegression\n",
|
||
|
"import pylab as pl\n",
|
||
|
"\n",
|
||
|
"seaborn.set()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Linear Regression\n",
|
||
|
"\n",
|
||
|
"Linear Regression is a supervised learning algorithm that models the relationship between a scalar dependent variable y and one or more explanatory variables (or independent variable) denoted X.\n",
|
||
|
"\n",
|
||
|
"Generate some data:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAd4AAAFVCAYAAABB6Y7YAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFLVJREFUeJzt3W2MZNl5F/B/r3fda5zyumQqHoLDNCTZw4ioTQuD4x3H\n8SovQGQD9liiCcSxSUIsD8wqTJNgJ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S7C7WqL0rkf1shrvIfgGm/Hjus1m0VjnLu3CGN83G+LOs5jfNzHbhpH\neo0XgIMtQvc5K8s8djreOXOc/4JdJMa5e8a4e8a4DR0vABxjghcAGhK8ANCQ4AWAhgQvADQkeAGg\nIcELAA0JXgBoSPACQEOCFwAaErwA0JDgBYCG7p51AUBbFy5fzePbu0n2nou6tbkx44pgueh4YYlc\nuHw117Z3M0wyTHJtezfnL13Jk095gg20InhhidzsdG+3e/1GHnn0sRlUA8tJ8AJAQ4IXlsiptf4d\nr/V7qzl3Zn0G1cByErywRLY2N9Lvrd467vdWc/Hs6Zw80ZthVbBcBC8smXNn1tPvrep0YUbcTgRL\n5uSJXi6ePT3rMmBp6XgBoCHBCwANCV4AaEjwAkBDghcAGhK8ANCQ4AWAhsbex1tKeV6SDyS5P3sP\nNHlbrfW/dV0YACyiSTre1yV5ptb66iQ/nORd3ZYEAItrbPDWWn8xyfftH64lufO5YgDARCbaMrLW\n+sVSykeSvCHJmzqtCAAW2MpwOJz4zaWUlyb5ZJJTtdann+Mtk38YACyGlWnePMniqu9M8rJa648n\neTrJM/v/ntPOzvVpzs+
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x11006e550>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Create some simple data\n",
|
||
|
"import numpy as np\n",
|
||
|
"np.random.seed(0)\n",
|
||
|
"X = np.random.random(size=(20, 1))\n",
|
||
|
"y = 3 * X.squeeze() + 2 + np.random.randn(20)\n",
|
||
|
"\n",
|
||
|
"plt.plot(X.squeeze(), y, 'o');"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Fit the model:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAd4AAAFVCAYAAABB6Y7YAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3WlwG2l+3/EveF8QCZJoUDdHI6lFHYAIzK09y0fKqbXj\nXbnKihNfsR07nkRbzih2dh3vi9iuuGo1rvLYEx9rrzd5kahij484Z1Xs9drW2rtlgAIokXpGx1DS\njEZskAIp3gfQeQGKqxmNSIJDNAjg96maKoJsoP/zDAY/PE8//Tw+13URERERb9SUugAREZFqouAV\nERHxkIJXRETEQwpeERERDyl4RUREPKTgFRER8VDdWn+0bbsG+F3gMJADfsIYY7woTEREpBKt1+P9\nTqDVGPMR4N8Dv1z8kkRERCrXesE7B7Tbtu0D2oHF4pckIiJSudYcagYuAk3AVaAL+O6iVyQiIlLB\nfGstGWnb9ufJDzX/vG3be4C/AI4bYz6w5+u6ruvz+YpTqYiIyPZUUPCt1+NtBR6s/JwB6oHaJ57Z\n5yOdnirk/FKgYNCvNvaA2rn41MbFpzb2RjDoL+j49YL3i8Dv27b91+RD93PGmLlN1iYiIlL11gxe\nY8wE8GmPahEREal4WkBDRETEQwpeERERDyl4RUREPKTgFRER8ZCCV0RExEMKXhEREQ8peEVERDyk\n4BUREfGQgldERMRDCl4REREPKXhFREQ8pOAVERHxkIJXRETEQwpeERERDyl4RUREPKTgFRER8ZCC\nV0RExEMKXhEREQ8peEVERDyk4BUREfGQgldERMRDCl4REREPKXhFREQ8pOAVERHxkIJXRETEQ3Wl\nLkBERMRr5y8MMDySAaCvN8C5M/2enVs9XhERqSrnLwwwNJLBBVxgaCTDK69f5Na9KU/Or+AVEZGq\n8rCn+6jM1AKvvZHy5PwKXhEREQ8peEVEpKr09QYe+13A38jZ02FPzq/gFRGRqnLuTD8Bf+Pq44C/\nkVdfPsX+Hr8n5193VrNt2z8M/MjKw2YgAoSMMQ+K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F7wzwBeNMb9n2/Yh4H/btn3YGJPzoDYRKRFdvxWv5Nwc1yfeIu4k\nueQMMr00A8COBj8f33OKqBXmQPt+anyVs338mrcT2bbdANQYY+ZXHn8D+Iwx5p0nPGVL700SEZHK\nk3NzvDl2k6/fjvN3byeYmH8AwI7GNl7YE+WlfTGOdB+kpqZswnZLbyf6USAMvGzb9i5gB/DuWk+o\nlnvGSqWa7ssrJbVz8amNi287tbHruow8uE3cSTLgDDKxkJ+z21rfwqldzxG1IhzqOEBtTS0A4+Mz\npSy3IMFgYeuTrxe8vwf8vm3bf7Xy+Ec1zCwiIhvhui63p94m7iRJjKbILEwA0FzXzIs7nyVqhbED\nB1fDtlqsGbzGmGXgBz2qRUREypzrurw9fZf4aJKEk2J8/j4ATbVNPN8TI2qFOdJ5iLqa6l1Gonr/\nzUVEZEu4rsvdmXskVsLWmRsDoLG2gWdD/UStMH1dNvVVHLaPUiuIiMimvDszuhq292YdABpq6ola\nYWJWhKNdR2iorS9xlduPgldERDZsdDa9GrZ3Z+4BUF9Tx8ngCaJWmOPdffz6H1zh10fSQPoDd7eq\ndgpeERFZU3p2nISTJO4keWc6f2NLna+WcPcxYlaY491HaaprBB7f3WpoJMMrr198bHeraqbgFRGR\nx4zP3SfhpEg4SW5P5ZduqPXVcryrj6gVJhw8SnNd82PP2+juVtVMwSsiIgBk5icYcFLEnRQjD24D\nUOOr4WinTTQUIdJ9lJb6lhJXWf4UvCIiVWxiYZIBZ5CEk+Lm5AiQD9sjgUNEQ2EiweO01bdu+PW0\nu9X6FLwiIlXmweIUl5x
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x103f774d0>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = LinearRegression()\n",
|
||
|
"model.fit(X, y)\n",
|
||
|
"\n",
|
||
|
"# Plot the data and the model prediction\n",
|
||
|
"X_fit = np.linspace(0, 1, 100)[:, np.newaxis]\n",
|
||
|
"y_fit = model.predict(X_fit)\n",
|
||
|
"\n",
|
||
|
"plt.plot(X.squeeze(), y, 'o')\n",
|
||
|
"plt.plot(X_fit.squeeze(), y_fit);"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 2",
|
||
|
"language": "python",
|
||
|
"name": "python2"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 2
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython2",
|
||
|
"version": "2.7.9"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|