2015-04-20 02:31:20 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # scikit-learn-linear-reg "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:46:22 +08:00
" Credits: Forked from [PyCon 2015 Scikit-learn Tutorial](https://github.com/jakevdp/sklearn_pycon2015) by Jake VanderPlas \n " ,
" \n " ,
2015-04-20 02:31:20 +08:00
" * 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 \n AAALEgAACxIB0t1+/AAAFLVJREFUeJzt3W2MZNl5F/B/r3fda5zyumQqHoLDNCTZw4ioTQuD4x3H \n 8SovQGQD9liiCcSxSUIsD8wqTJNgJ9kPJFYsecZSNh4Sxy8xfICRyDpvSgAJx3HIgIyAMRuYzUGb \n qFcoykJjNfZgrSext/jQPePxzna9NH1PdVX9ftJIc6ur6z46avW/n3PPPXdlOBwGAGjjrlkXAADL \n RPACQEOCFwAaErwA0JDgBYCGBC8ANHT3qC+WUu5K8sEk9yd5Jsn31lpri8IAYBGN63i/LckLa62v \n TvKPkryr+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bu0n2nou6tbkx44pgueh4YYlc \n uHw117Z3M0wyTHJtezfnL13Jk095gg20InhhidzsdG+3e/1GHnn0sRlUA8tJ8AJAQ4IXlsiptf4d \n r/V7qzl3Zn0G1cByErywRLY2N9Lvrd467vdWc/Hs6Zw80ZthVbBcBC8smXNn1tPvrep0YUbcTgRL \n 5uSJXi6ePT3rMmBp6XgBoCHBCwANCV4AaEjwAkBDghcAGhK8ANCQ4AWAhsbex1tKeV6SDyS5P3sP \n NHlbrfW/dV0YACyiSTre1yV5ptb66iQ/nORd3ZYEAItrbPDWWn8xyfftH64lufO5YgDARCbaMrLW \n +sVSykeSvCHJmzqtCAAW2MpwOJz4zaWUlyb5ZJJTtdann+Mtk38YACyGlWnePMniqu9M8rJa648n \n eTrJM/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 \n AAALEgAACxIB0t1+/AAAIABJREFUeJzt3WlwG2l+3/EveF8QCZJoUDdHI6lFHYAIzK09y0fKqbXj \n XbnKihNfsR07nkRbzih2dh3vi9iuuGo1rvLYEx9rrzd5kahij484Z1Xs9drW2rtlgAIokXpGx1DS \n jEZskAIp3gfQeQGKqxmNSIJDNAjg96maKoJsoP/zDAY/PE8//Tw+13URERERb9SUugAREZFqouAV \n ERHxkIJXRETEQwpeERERDyl4RUREPKTgFRER8VDdWn+0bbsG+F3gMJADfsIYY7woTEREpBKt1+P9 \n TqDVGPMR4N8Dv1z8kkRERCrXesE7B7Tbtu0D2oHF4pckIiJSudYcagYuAk3AVaAL+O6iVyQiIlLB \n fGstGWnb9ufJDzX/vG3be4C/AI4bYz6w5+u6ruvz+YpTqYiIyPZUUPCt1+NtBR6s/JwB6oHaJ57Z \n 5yOdnirk/FKgYNCvNvaA2rn41MbFpzb2RjDoL+j49YL3i8Dv27b91+RD93PGmLlN1iYiIlL11gxe \n Y8wE8GmPahEREal4WkBDRETEQwpeERERDyl4RUREPKTgFRER8ZCCV0RExEMKXhEREQ8peEVERDyk \n 4BUREfGQgldERMRDCl4REREPKXhFREQ8pOAVERHxkIJXRETEQwpeERERDyl4RUREPKTgFRER8ZCC \n V0RExEMKXhEREQ8peEVERDyk4BUREfGQgldERMRDCl4REREPKXhFREQ8pOAVERHxkIJXRETEQ3Wl \n LkBERMRr5y8MMDySAaCvN8C5M/2enVs9XhERqSrnLwwwNJLBBVxgaCTDK69f5Na9KU/Or+AVEZGq \n 8rCn+6jM1AKvvZHy5PwKXhEREQ8peEVEpKr09QYe+13A38jZ02FPzq/gFRGRqnLuTD8Bf+Pq44C/ \n kVdfPsX+Hr8n5193VrNt2z8M/MjKw2YgAoSMMQ+KWJeIiEjRnD0dXr2m61VP9yGf67obPti27d8A \n LhljfvcJh7jptDezwqpVMOhHbVx8aufiUxsXn9rYG8Gg31fI8RsearZt+xng2BqhKyIiIuvYcI/X \n tu0/An7NGPO1NQ7bePdZRESkMhTU493QylW2bXcAh9cJXQANaxSZho68oXYuPrVx8amNvREMFjYp \n a6NDzR8D/rzgakREROQ9Nhq8h4EbxSxERESkGmxoqNkYc77YhYiIiFQDLaAhIiLiIQWviIiIhxS8 \n IiIiHlLwioiIeEjBKyIi4iEFr4iIiIcUvCIiIh5S8IqIiHhIwSsiIuIhBa+IiIiHFLwiIiIeUvCK \n i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CwiIhvhui63p94m7iRJjKbILEwA0FzXzIs7nyVqhbED \n B1fDtlqsGbzGmGXgBz2qRUREypzrurw9fZf4aJKEk2J8/j4ATbVNPN8TI2qFOdJ5iLqa6l1Gonr/ \n zUVEZEu4rsvdmXskVsLWmRsDoLG2gWdD/UStMH1dNvVVHLaPUiuIiMimvDszuhq292YdABpq6ola \n YWJWhKNdR2iorS9xlduPgldERDZsdDa9GrZ3Z+4BUF9Tx8ngCaJWmOPdffz6H1zh10fSQPoDd7eq \n dgpeERFZU3p2nISTJO4keWc6f2NLna+WcPcxYlaY491HaaprBB7f3WpoJMMrr198bHeraqbgFRGR \n x4zP3SfhpEg4SW5P5ZduqPXVcryrj6gVJhw8SnNd82PP2+juVtVMwSsiIgBk5icYcFLEnRQjD24D \n UOOr4WinTTQUIdJ9lJb6lhJXWf4UvCIiVWxiYZIBZ5CEk+Lm5AiQD9sjgUNEQ2EiweO01bdu+PW0 \n u9X6FLwiIlXmweIUl5x
" 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
}