Regression in Python


In [1]:
# Numerical arrays
import numpy as np

# Plotting.
import matplotlib.pyplot as plt
In [2]:
# Plots styles.
plt.style.use('ggplot')

# Plot size.
plt.rcParams['figure.figsize'] = (14, 10)
In [3]:
# Create two points.
x = np.array([4.0, 16.0])
y = np.array([6.0, 12.0])
x, y
Out[3]:
(array([ 4., 16.]), array([ 6., 12.]))
In [4]:
# Plot the points.
plt.plot(x, y, 'ro')

# Give ourselves some space.
plt.xlim([-5.0, 25.0])
plt.ylim([-5.0, 25.0]);
In [5]:
# Plot a straight line.
l = np.linspace(-5.0, 25.0, 100)
plt.plot(l, 0.5 * l + 4.0, 'g--')

# Plot a straight line segment.
l = np.linspace(4.0, 16.0, 10)
plt.plot(l, 0.5 * l + 4.0, 'b-')

# Plot the points.
plt.plot(x, y, 'ro')

# Give ourselves some space.
plt.xlim([-5.0, 25.0])
plt.ylim([-5.0, 25.0]);
In [6]:
# Plot a straight line.
l = np.linspace(-3.0, 20.0, 100)
plt.plot(l, 0.5 * l + 4.0, 'g-')

# Plot a parabola.
plt.plot(l, 10.0 * (l**2) - 199.5 * l + 644.0, 'b-')

# Plot a cubic.
plt.plot(l, (l**3)  - 20.0625 * l**2  + 65.75 * l, 'y-')

# Plot the points.
plt.plot(x, y, 'ro');


Lines


In [7]:
# Set up some x values.
x = np.linspace(0.0, 10.0, 1000)
x
Out[7]:
array([ 0.        ,  0.01001001,  0.02002002,  0.03003003,  0.04004004,
        0.05005005,  0.06006006,  0.07007007,  0.08008008,  0.09009009,
        0.1001001 ,  0.11011011,  0.12012012,  0.13013013,  0.14014014,
        0.15015015,  0.16016016,  0.17017017,  0.18018018,  0.19019019,
        0.2002002 ,  0.21021021,  0.22022022,  0.23023023,  0.24024024,
        0.25025025,  0.26026026,  0.27027027,  0.28028028,  0.29029029,
        0.3003003 ,  0.31031031,  0.32032032,  0.33033033,  0.34034034,
        0.35035035,  0.36036036,  0.37037037,  0.38038038,  0.39039039,
        0.4004004 ,  0.41041041,  0.42042042,  0.43043043,  0.44044044,
        0.45045045,  0.46046046,  0.47047047,  0.48048048,  0.49049049,
        0.5005005 ,  0.51051051,  0.52052052,  0.53053053,  0.54054054,
        0.55055055,  0.56056056,  0.57057057,  0.58058058,  0.59059059,
        0.6006006 ,  0.61061061,  0.62062062,  0.63063063,  0.64064064,
        0.65065065,  0.66066066,  0.67067067,  0.68068068,  0.69069069,
        0.7007007 ,  0.71071071,  0.72072072,  0.73073073,  0.74074074,
        0.75075075,  0.76076076,  0.77077077,  0.78078078,  0.79079079,
        0.8008008 ,  0.81081081,  0.82082082,  0.83083083,  0.84084084,
        0.85085085,  0.86086086,  0.87087087,  0.88088088,  0.89089089,
        0.9009009 ,  0.91091091,  0.92092092,  0.93093093,  0.94094094,
        0.95095095,  0.96096096,  0.97097097,  0.98098098,  0.99099099,
        1.001001  ,  1.01101101,  1.02102102,  1.03103103,  1.04104104,
        1.05105105,  1.06106106,  1.07107107,  1.08108108,  1.09109109,
        1.1011011 ,  1.11111111,  1.12112112,  1.13113113,  1.14114114,
        1.15115115,  1.16116116,  1.17117117,  1.18118118,  1.19119119,
        1.2012012 ,  1.21121121,  1.22122122,  1.23123123,  1.24124124,
        1.25125125,  1.26126126,  1.27127127,  1.28128128,  1.29129129,
        1.3013013 ,  1.31131131,  1.32132132,  1.33133133,  1.34134134,
        1.35135135,  1.36136136,  1.37137137,  1.38138138,  1.39139139,
        1.4014014 ,  1.41141141,  1.42142142,  1.43143143,  1.44144144,
        1.45145145,  1.46146146,  1.47147147,  1.48148148,  1.49149149,
        1.5015015 ,  1.51151151,  1.52152152,  1.53153153,  1.54154154,
        1.55155155,  1.56156156,  1.57157157,  1.58158158,  1.59159159,
        1.6016016 ,  1.61161161,  1.62162162,  1.63163163,  1.64164164,
        1.65165165,  1.66166166,  1.67167167,  1.68168168,  1.69169169,
        1.7017017 ,  1.71171171,  1.72172172,  1.73173173,  1.74174174,
        1.75175175,  1.76176176,  1.77177177,  1.78178178,  1.79179179,
        1.8018018 ,  1.81181181,  1.82182182,  1.83183183,  1.84184184,
        1.85185185,  1.86186186,  1.87187187,  1.88188188,  1.89189189,
        1.9019019 ,  1.91191191,  1.92192192,  1.93193193,  1.94194194,
        1.95195195,  1.96196196,  1.97197197,  1.98198198,  1.99199199,
        2.002002  ,  2.01201201,  2.02202202,  2.03203203,  2.04204204,
        2.05205205,  2.06206206,  2.07207207,  2.08208208,  2.09209209,
        2.1021021 ,  2.11211211,  2.12212212,  2.13213213,  2.14214214,
        2.15215215,  2.16216216,  2.17217217,  2.18218218,  2.19219219,
        2.2022022 ,  2.21221221,  2.22222222,  2.23223223,  2.24224224,
        2.25225225,  2.26226226,  2.27227227,  2.28228228,  2.29229229,
        2.3023023 ,  2.31231231,  2.32232232,  2.33233233,  2.34234234,
        2.35235235,  2.36236236,  2.37237237,  2.38238238,  2.39239239,
        2.4024024 ,  2.41241241,  2.42242242,  2.43243243,  2.44244244,
        2.45245245,  2.46246246,  2.47247247,  2.48248248,  2.49249249,
        2.5025025 ,  2.51251251,  2.52252252,  2.53253253,  2.54254254,
        2.55255255,  2.56256256,  2.57257257,  2.58258258,  2.59259259,
        2.6026026 ,  2.61261261,  2.62262262,  2.63263263,  2.64264264,
        2.65265265,  2.66266266,  2.67267267,  2.68268268,  2.69269269,
        2.7027027 ,  2.71271271,  2.72272272,  2.73273273,  2.74274274,
        2.75275275,  2.76276276,  2.77277277,  2.78278278,  2.79279279,
        2.8028028 ,  2.81281281,  2.82282282,  2.83283283,  2.84284284,
        2.85285285,  2.86286286,  2.87287287,  2.88288288,  2.89289289,
        2.9029029 ,  2.91291291,  2.92292292,  2.93293293,  2.94294294,
        2.95295295,  2.96296296,  2.97297297,  2.98298298,  2.99299299,
        3.003003  ,  3.01301301,  3.02302302,  3.03303303,  3.04304304,
        3.05305305,  3.06306306,  3.07307307,  3.08308308,  3.09309309,
        3.1031031 ,  3.11311311,  3.12312312,  3.13313313,  3.14314314,
        3.15315315,  3.16316316,  3.17317317,  3.18318318,  3.19319319,
        3.2032032 ,  3.21321321,  3.22322322,  3.23323323,  3.24324324,
        3.25325325,  3.26326326,  3.27327327,  3.28328328,  3.29329329,
        3.3033033 ,  3.31331331,  3.32332332,  3.33333333,  3.34334334,
        3.35335335,  3.36336336,  3.37337337,  3.38338338,  3.39339339,
        3.4034034 ,  3.41341341,  3.42342342,  3.43343343,  3.44344344,
        3.45345345,  3.46346346,  3.47347347,  3.48348348,  3.49349349,
        3.5035035 ,  3.51351351,  3.52352352,  3.53353353,  3.54354354,
        3.55355355,  3.56356356,  3.57357357,  3.58358358,  3.59359359,
        3.6036036 ,  3.61361361,  3.62362362,  3.63363363,  3.64364364,
        3.65365365,  3.66366366,  3.67367367,  3.68368368,  3.69369369,
        3.7037037 ,  3.71371371,  3.72372372,  3.73373373,  3.74374374,
        3.75375375,  3.76376376,  3.77377377,  3.78378378,  3.79379379,
        3.8038038 ,  3.81381381,  3.82382382,  3.83383383,  3.84384384,
        3.85385385,  3.86386386,  3.87387387,  3.88388388,  3.89389389,
        3.9039039 ,  3.91391391,  3.92392392,  3.93393393,  3.94394394,
        3.95395395,  3.96396396,  3.97397397,  3.98398398,  3.99399399,
        4.004004  ,  4.01401401,  4.02402402,  4.03403403,  4.04404404,
        4.05405405,  4.06406406,  4.07407407,  4.08408408,  4.09409409,
        4.1041041 ,  4.11411411,  4.12412412,  4.13413413,  4.14414414,
        4.15415415,  4.16416416,  4.17417417,  4.18418418,  4.19419419,
        4.2042042 ,  4.21421421,  4.22422422,  4.23423423,  4.24424424,
        4.25425425,  4.26426426,  4.27427427,  4.28428428,  4.29429429,
        4.3043043 ,  4.31431431,  4.32432432,  4.33433433,  4.34434434,
        4.35435435,  4.36436436,  4.37437437,  4.38438438,  4.39439439,
        4.4044044 ,  4.41441441,  4.42442442,  4.43443443,  4.44444444,
        4.45445445,  4.46446446,  4.47447447,  4.48448448,  4.49449449,
        4.5045045 ,  4.51451451,  4.52452452,  4.53453453,  4.54454454,
        4.55455455,  4.56456456,  4.57457457,  4.58458458,  4.59459459,
        4.6046046 ,  4.61461461,  4.62462462,  4.63463463,  4.64464464,
        4.65465465,  4.66466466,  4.67467467,  4.68468468,  4.69469469,
        4.7047047 ,  4.71471471,  4.72472472,  4.73473473,  4.74474474,
        4.75475475,  4.76476476,  4.77477477,  4.78478478,  4.79479479,
        4.8048048 ,  4.81481481,  4.82482482,  4.83483483,  4.84484484,
        4.85485485,  4.86486486,  4.87487487,  4.88488488,  4.89489489,
        4.9049049 ,  4.91491491,  4.92492492,  4.93493493,  4.94494494,
        4.95495495,  4.96496496,  4.97497497,  4.98498498,  4.99499499,
        5.00500501,  5.01501502,  5.02502503,  5.03503504,  5.04504505,
        5.05505506,  5.06506507,  5.07507508,  5.08508509,  5.0950951 ,
        5.10510511,  5.11511512,  5.12512513,  5.13513514,  5.14514515,
        5.15515516,  5.16516517,  5.17517518,  5.18518519,  5.1951952 ,
        5.20520521,  5.21521522,  5.22522523,  5.23523524,  5.24524525,
        5.25525526,  5.26526527,  5.27527528,  5.28528529,  5.2952953 ,
        5.30530531,  5.31531532,  5.32532533,  5.33533534,  5.34534535,
        5.35535536,  5.36536537,  5.37537538,  5.38538539,  5.3953954 ,
        5.40540541,  5.41541542,  5.42542543,  5.43543544,  5.44544545,
        5.45545546,  5.46546547,  5.47547548,  5.48548549,  5.4954955 ,
        5.50550551,  5.51551552,  5.52552553,  5.53553554,  5.54554555,
        5.55555556,  5.56556557,  5.57557558,  5.58558559,  5.5955956 ,
        5.60560561,  5.61561562,  5.62562563,  5.63563564,  5.64564565,
        5.65565566,  5.66566567,  5.67567568,  5.68568569,  5.6956957 ,
        5.70570571,  5.71571572,  5.72572573,  5.73573574,  5.74574575,
        5.75575576,  5.76576577,  5.77577578,  5.78578579,  5.7957958 ,
        5.80580581,  5.81581582,  5.82582583,  5.83583584,  5.84584585,
        5.85585586,  5.86586587,  5.87587588,  5.88588589,  5.8958959 ,
        5.90590591,  5.91591592,  5.92592593,  5.93593594,  5.94594595,
        5.95595596,  5.96596597,  5.97597598,  5.98598599,  5.995996  ,
        6.00600601,  6.01601602,  6.02602603,  6.03603604,  6.04604605,
        6.05605606,  6.06606607,  6.07607608,  6.08608609,  6.0960961 ,
        6.10610611,  6.11611612,  6.12612613,  6.13613614,  6.14614615,
        6.15615616,  6.16616617,  6.17617618,  6.18618619,  6.1961962 ,
        6.20620621,  6.21621622,  6.22622623,  6.23623624,  6.24624625,
        6.25625626,  6.26626627,  6.27627628,  6.28628629,  6.2962963 ,
        6.30630631,  6.31631632,  6.32632633,  6.33633634,  6.34634635,
        6.35635636,  6.36636637,  6.37637638,  6.38638639,  6.3963964 ,
        6.40640641,  6.41641642,  6.42642643,  6.43643644,  6.44644645,
        6.45645646,  6.46646647,  6.47647648,  6.48648649,  6.4964965 ,
        6.50650651,  6.51651652,  6.52652653,  6.53653654,  6.54654655,
        6.55655656,  6.56656657,  6.57657658,  6.58658659,  6.5965966 ,
        6.60660661,  6.61661662,  6.62662663,  6.63663664,  6.64664665,
        6.65665666,  6.66666667,  6.67667668,  6.68668669,  6.6966967 ,
        6.70670671,  6.71671672,  6.72672673,  6.73673674,  6.74674675,
        6.75675676,  6.76676677,  6.77677678,  6.78678679,  6.7967968 ,
        6.80680681,  6.81681682,  6.82682683,  6.83683684,  6.84684685,
        6.85685686,  6.86686687,  6.87687688,  6.88688689,  6.8968969 ,
        6.90690691,  6.91691692,  6.92692693,  6.93693694,  6.94694695,
        6.95695696,  6.96696697,  6.97697698,  6.98698699,  6.996997  ,
        7.00700701,  7.01701702,  7.02702703,  7.03703704,  7.04704705,
        7.05705706,  7.06706707,  7.07707708,  7.08708709,  7.0970971 ,
        7.10710711,  7.11711712,  7.12712713,  7.13713714,  7.14714715,
        7.15715716,  7.16716717,  7.17717718,  7.18718719,  7.1971972 ,
        7.20720721,  7.21721722,  7.22722723,  7.23723724,  7.24724725,
        7.25725726,  7.26726727,  7.27727728,  7.28728729,  7.2972973 ,
        7.30730731,  7.31731732,  7.32732733,  7.33733734,  7.34734735,
        7.35735736,  7.36736737,  7.37737738,  7.38738739,  7.3973974 ,
        7.40740741,  7.41741742,  7.42742743,  7.43743744,  7.44744745,
        7.45745746,  7.46746747,  7.47747748,  7.48748749,  7.4974975 ,
        7.50750751,  7.51751752,  7.52752753,  7.53753754,  7.54754755,
        7.55755756,  7.56756757,  7.57757758,  7.58758759,  7.5975976 ,
        7.60760761,  7.61761762,  7.62762763,  7.63763764,  7.64764765,
        7.65765766,  7.66766767,  7.67767768,  7.68768769,  7.6976977 ,
        7.70770771,  7.71771772,  7.72772773,  7.73773774,  7.74774775,
        7.75775776,  7.76776777,  7.77777778,  7.78778779,  7.7977978 ,
        7.80780781,  7.81781782,  7.82782783,  7.83783784,  7.84784785,
        7.85785786,  7.86786787,  7.87787788,  7.88788789,  7.8978979 ,
        7.90790791,  7.91791792,  7.92792793,  7.93793794,  7.94794795,
        7.95795796,  7.96796797,  7.97797798,  7.98798799,  7.997998  ,
        8.00800801,  8.01801802,  8.02802803,  8.03803804,  8.04804805,
        8.05805806,  8.06806807,  8.07807808,  8.08808809,  8.0980981 ,
        8.10810811,  8.11811812,  8.12812813,  8.13813814,  8.14814815,
        8.15815816,  8.16816817,  8.17817818,  8.18818819,  8.1981982 ,
        8.20820821,  8.21821822,  8.22822823,  8.23823824,  8.24824825,
        8.25825826,  8.26826827,  8.27827828,  8.28828829,  8.2982983 ,
        8.30830831,  8.31831832,  8.32832833,  8.33833834,  8.34834835,
        8.35835836,  8.36836837,  8.37837838,  8.38838839,  8.3983984 ,
        8.40840841,  8.41841842,  8.42842843,  8.43843844,  8.44844845,
        8.45845846,  8.46846847,  8.47847848,  8.48848849,  8.4984985 ,
        8.50850851,  8.51851852,  8.52852853,  8.53853854,  8.54854855,
        8.55855856,  8.56856857,  8.57857858,  8.58858859,  8.5985986 ,
        8.60860861,  8.61861862,  8.62862863,  8.63863864,  8.64864865,
        8.65865866,  8.66866867,  8.67867868,  8.68868869,  8.6986987 ,
        8.70870871,  8.71871872,  8.72872873,  8.73873874,  8.74874875,
        8.75875876,  8.76876877,  8.77877878,  8.78878879,  8.7987988 ,
        8.80880881,  8.81881882,  8.82882883,  8.83883884,  8.84884885,
        8.85885886,  8.86886887,  8.87887888,  8.88888889,  8.8988989 ,
        8.90890891,  8.91891892,  8.92892893,  8.93893894,  8.94894895,
        8.95895896,  8.96896897,  8.97897898,  8.98898899,  8.998999  ,
        9.00900901,  9.01901902,  9.02902903,  9.03903904,  9.04904905,
        9.05905906,  9.06906907,  9.07907908,  9.08908909,  9.0990991 ,
        9.10910911,  9.11911912,  9.12912913,  9.13913914,  9.14914915,
        9.15915916,  9.16916917,  9.17917918,  9.18918919,  9.1991992 ,
        9.20920921,  9.21921922,  9.22922923,  9.23923924,  9.24924925,
        9.25925926,  9.26926927,  9.27927928,  9.28928929,  9.2992993 ,
        9.30930931,  9.31931932,  9.32932933,  9.33933934,  9.34934935,
        9.35935936,  9.36936937,  9.37937938,  9.38938939,  9.3993994 ,
        9.40940941,  9.41941942,  9.42942943,  9.43943944,  9.44944945,
        9.45945946,  9.46946947,  9.47947948,  9.48948949,  9.4994995 ,
        9.50950951,  9.51951952,  9.52952953,  9.53953954,  9.54954955,
        9.55955956,  9.56956957,  9.57957958,  9.58958959,  9.5995996 ,
        9.60960961,  9.61961962,  9.62962963,  9.63963964,  9.64964965,
        9.65965966,  9.66966967,  9.67967968,  9.68968969,  9.6996997 ,
        9.70970971,  9.71971972,  9.72972973,  9.73973974,  9.74974975,
        9.75975976,  9.76976977,  9.77977978,  9.78978979,  9.7997998 ,
        9.80980981,  9.81981982,  9.82982983,  9.83983984,  9.84984985,
        9.85985986,  9.86986987,  9.87987988,  9.88988989,  9.8998999 ,
        9.90990991,  9.91991992,  9.92992993,  9.93993994,  9.94994995,
        9.95995996,  9.96996997,  9.97997998,  9.98998999, 10.        ])

$$ y = 5 x + 2 $$
In [8]:
# Create y - note numpy's element-wise operations.
y = 5.0 * x + 2.0
In [9]:
# Look at y.
y
Out[9]:
array([ 2.        ,  2.05005005,  2.1001001 ,  2.15015015,  2.2002002 ,
        2.25025025,  2.3003003 ,  2.35035035,  2.4004004 ,  2.45045045,
        2.5005005 ,  2.55055055,  2.6006006 ,  2.65065065,  2.7007007 ,
        2.75075075,  2.8008008 ,  2.85085085,  2.9009009 ,  2.95095095,
        3.001001  ,  3.05105105,  3.1011011 ,  3.15115115,  3.2012012 ,
        3.25125125,  3.3013013 ,  3.35135135,  3.4014014 ,  3.45145145,
        3.5015015 ,  3.55155155,  3.6016016 ,  3.65165165,  3.7017017 ,
        3.75175175,  3.8018018 ,  3.85185185,  3.9019019 ,  3.95195195,
        4.002002  ,  4.05205205,  4.1021021 ,  4.15215215,  4.2022022 ,
        4.25225225,  4.3023023 ,  4.35235235,  4.4024024 ,  4.45245245,
        4.5025025 ,  4.55255255,  4.6026026 ,  4.65265265,  4.7027027 ,
        4.75275275,  4.8028028 ,  4.85285285,  4.9029029 ,  4.95295295,
        5.003003  ,  5.05305305,  5.1031031 ,  5.15315315,  5.2032032 ,
        5.25325325,  5.3033033 ,  5.35335335,  5.4034034 ,  5.45345345,
        5.5035035 ,  5.55355355,  5.6036036 ,  5.65365365,  5.7037037 ,
        5.75375375,  5.8038038 ,  5.85385385,  5.9039039 ,  5.95395395,
        6.004004  ,  6.05405405,  6.1041041 ,  6.15415415,  6.2042042 ,
        6.25425425,  6.3043043 ,  6.35435435,  6.4044044 ,  6.45445445,
        6.5045045 ,  6.55455455,  6.6046046 ,  6.65465465,  6.7047047 ,
        6.75475475,  6.8048048 ,  6.85485485,  6.9049049 ,  6.95495495,
        7.00500501,  7.05505506,  7.10510511,  7.15515516,  7.20520521,
        7.25525526,  7.30530531,  7.35535536,  7.40540541,  7.45545546,
        7.50550551,  7.55555556,  7.60560561,  7.65565566,  7.70570571,
        7.75575576,  7.80580581,  7.85585586,  7.90590591,  7.95595596,
        8.00600601,  8.05605606,  8.10610611,  8.15615616,  8.20620621,
        8.25625626,  8.30630631,  8.35635636,  8.40640641,  8.45645646,
        8.50650651,  8.55655656,  8.60660661,  8.65665666,  8.70670671,
        8.75675676,  8.80680681,  8.85685686,  8.90690691,  8.95695696,
        9.00700701,  9.05705706,  9.10710711,  9.15715716,  9.20720721,
        9.25725726,  9.30730731,  9.35735736,  9.40740741,  9.45745746,
        9.50750751,  9.55755756,  9.60760761,  9.65765766,  9.70770771,
        9.75775776,  9.80780781,  9.85785786,  9.90790791,  9.95795796,
       10.00800801, 10.05805806, 10.10810811, 10.15815816, 10.20820821,
       10.25825826, 10.30830831, 10.35835836, 10.40840841, 10.45845846,
       10.50850851, 10.55855856, 10.60860861, 10.65865866, 10.70870871,
       10.75875876, 10.80880881, 10.85885886, 10.90890891, 10.95895896,
       11.00900901, 11.05905906, 11.10910911, 11.15915916, 11.20920921,
       11.25925926, 11.30930931, 11.35935936, 11.40940941, 11.45945946,
       11.50950951, 11.55955956, 11.60960961, 11.65965966, 11.70970971,
       11.75975976, 11.80980981, 11.85985986, 11.90990991, 11.95995996,
       12.01001001, 12.06006006, 12.11011011, 12.16016016, 12.21021021,
       12.26026026, 12.31031031, 12.36036036, 12.41041041, 12.46046046,
       12.51051051, 12.56056056, 12.61061061, 12.66066066, 12.71071071,
       12.76076076, 12.81081081, 12.86086086, 12.91091091, 12.96096096,
       13.01101101, 13.06106106, 13.11111111, 13.16116116, 13.21121121,
       13.26126126, 13.31131131, 13.36136136, 13.41141141, 13.46146146,
       13.51151151, 13.56156156, 13.61161161, 13.66166166, 13.71171171,
       13.76176176, 13.81181181, 13.86186186, 13.91191191, 13.96196196,
       14.01201201, 14.06206206, 14.11211211, 14.16216216, 14.21221221,
       14.26226226, 14.31231231, 14.36236236, 14.41241241, 14.46246246,
       14.51251251, 14.56256256, 14.61261261, 14.66266266, 14.71271271,
       14.76276276, 14.81281281, 14.86286286, 14.91291291, 14.96296296,
       15.01301301, 15.06306306, 15.11311311, 15.16316316, 15.21321321,
       15.26326326, 15.31331331, 15.36336336, 15.41341341, 15.46346346,
       15.51351351, 15.56356356, 15.61361361, 15.66366366, 15.71371371,
       15.76376376, 15.81381381, 15.86386386, 15.91391391, 15.96396396,
       16.01401401, 16.06406406, 16.11411411, 16.16416416, 16.21421421,
       16.26426426, 16.31431431, 16.36436436, 16.41441441, 16.46446446,
       16.51451451, 16.56456456, 16.61461461, 16.66466466, 16.71471471,
       16.76476476, 16.81481481, 16.86486486, 16.91491491, 16.96496496,
       17.01501502, 17.06506507, 17.11511512, 17.16516517, 17.21521522,
       17.26526527, 17.31531532, 17.36536537, 17.41541542, 17.46546547,
       17.51551552, 17.56556557, 17.61561562, 17.66566567, 17.71571572,
       17.76576577, 17.81581582, 17.86586587, 17.91591592, 17.96596597,
       18.01601602, 18.06606607, 18.11611612, 18.16616617, 18.21621622,
       18.26626627, 18.31631632, 18.36636637, 18.41641642, 18.46646647,
       18.51651652, 18.56656657, 18.61661662, 18.66666667, 18.71671672,
       18.76676677, 18.81681682, 18.86686687, 18.91691692, 18.96696697,
       19.01701702, 19.06706707, 19.11711712, 19.16716717, 19.21721722,
       19.26726727, 19.31731732, 19.36736737, 19.41741742, 19.46746747,
       19.51751752, 19.56756757, 19.61761762, 19.66766767, 19.71771772,
       19.76776777, 19.81781782, 19.86786787, 19.91791792, 19.96796797,
       20.01801802, 20.06806807, 20.11811812, 20.16816817, 20.21821822,
       20.26826827, 20.31831832, 20.36836837, 20.41841842, 20.46846847,
       20.51851852, 20.56856857, 20.61861862, 20.66866867, 20.71871872,
       20.76876877, 20.81881882, 20.86886887, 20.91891892, 20.96896897,
       21.01901902, 21.06906907, 21.11911912, 21.16916917, 21.21921922,
       21.26926927, 21.31931932, 21.36936937, 21.41941942, 21.46946947,
       21.51951952, 21.56956957, 21.61961962, 21.66966967, 21.71971972,
       21.76976977, 21.81981982, 21.86986987, 21.91991992, 21.96996997,
       22.02002002, 22.07007007, 22.12012012, 22.17017017, 22.22022022,
       22.27027027, 22.32032032, 22.37037037, 22.42042042, 22.47047047,
       22.52052052, 22.57057057, 22.62062062, 22.67067067, 22.72072072,
       22.77077077, 22.82082082, 22.87087087, 22.92092092, 22.97097097,
       23.02102102, 23.07107107, 23.12112112, 23.17117117, 23.22122122,
       23.27127127, 23.32132132, 23.37137137, 23.42142142, 23.47147147,
       23.52152152, 23.57157157, 23.62162162, 23.67167167, 23.72172172,
       23.77177177, 23.82182182, 23.87187187, 23.92192192, 23.97197197,
       24.02202202, 24.07207207, 24.12212212, 24.17217217, 24.22222222,
       24.27227227, 24.32232232, 24.37237237, 24.42242242, 24.47247247,
       24.52252252, 24.57257257, 24.62262262, 24.67267267, 24.72272272,
       24.77277277, 24.82282282, 24.87287287, 24.92292292, 24.97297297,
       25.02302302, 25.07307307, 25.12312312, 25.17317317, 25.22322322,
       25.27327327, 25.32332332, 25.37337337, 25.42342342, 25.47347347,
       25.52352352, 25.57357357, 25.62362362, 25.67367367, 25.72372372,
       25.77377377, 25.82382382, 25.87387387, 25.92392392, 25.97397397,
       26.02402402, 26.07407407, 26.12412412, 26.17417417, 26.22422422,
       26.27427427, 26.32432432, 26.37437437, 26.42442442, 26.47447447,
       26.52452452, 26.57457457, 26.62462462, 26.67467467, 26.72472472,
       26.77477477, 26.82482482, 26.87487487, 26.92492492, 26.97497497,
       27.02502503, 27.07507508, 27.12512513, 27.17517518, 27.22522523,
       27.27527528, 27.32532533, 27.37537538, 27.42542543, 27.47547548,
       27.52552553, 27.57557558, 27.62562563, 27.67567568, 27.72572573,
       27.77577578, 27.82582583, 27.87587588, 27.92592593, 27.97597598,
       28.02602603, 28.07607608, 28.12612613, 28.17617618, 28.22622623,
       28.27627628, 28.32632633, 28.37637638, 28.42642643, 28.47647648,
       28.52652653, 28.57657658, 28.62662663, 28.67667668, 28.72672673,
       28.77677678, 28.82682683, 28.87687688, 28.92692693, 28.97697698,
       29.02702703, 29.07707708, 29.12712713, 29.17717718, 29.22722723,
       29.27727728, 29.32732733, 29.37737738, 29.42742743, 29.47747748,
       29.52752753, 29.57757758, 29.62762763, 29.67767768, 29.72772773,
       29.77777778, 29.82782783, 29.87787788, 29.92792793, 29.97797798,
       30.02802803, 30.07807808, 30.12812813, 30.17817818, 30.22822823,
       30.27827828, 30.32832833, 30.37837838, 30.42842843, 30.47847848,
       30.52852853, 30.57857858, 30.62862863, 30.67867868, 30.72872873,
       30.77877878, 30.82882883, 30.87887888, 30.92892893, 30.97897898,
       31.02902903, 31.07907908, 31.12912913, 31.17917918, 31.22922923,
       31.27927928, 31.32932933, 31.37937938, 31.42942943, 31.47947948,
       31.52952953, 31.57957958, 31.62962963, 31.67967968, 31.72972973,
       31.77977978, 31.82982983, 31.87987988, 31.92992993, 31.97997998,
       32.03003003, 32.08008008, 32.13013013, 32.18018018, 32.23023023,
       32.28028028, 32.33033033, 32.38038038, 32.43043043, 32.48048048,
       32.53053053, 32.58058058, 32.63063063, 32.68068068, 32.73073073,
       32.78078078, 32.83083083, 32.88088088, 32.93093093, 32.98098098,
       33.03103103, 33.08108108, 33.13113113, 33.18118118, 33.23123123,
       33.28128128, 33.33133133, 33.38138138, 33.43143143, 33.48148148,
       33.53153153, 33.58158158, 33.63163163, 33.68168168, 33.73173173,
       33.78178178, 33.83183183, 33.88188188, 33.93193193, 33.98198198,
       34.03203203, 34.08208208, 34.13213213, 34.18218218, 34.23223223,
       34.28228228, 34.33233233, 34.38238238, 34.43243243, 34.48248248,
       34.53253253, 34.58258258, 34.63263263, 34.68268268, 34.73273273,
       34.78278278, 34.83283283, 34.88288288, 34.93293293, 34.98298298,
       35.03303303, 35.08308308, 35.13313313, 35.18318318, 35.23323323,
       35.28328328, 35.33333333, 35.38338338, 35.43343343, 35.48348348,
       35.53353353, 35.58358358, 35.63363363, 35.68368368, 35.73373373,
       35.78378378, 35.83383383, 35.88388388, 35.93393393, 35.98398398,
       36.03403403, 36.08408408, 36.13413413, 36.18418418, 36.23423423,
       36.28428428, 36.33433433, 36.38438438, 36.43443443, 36.48448448,
       36.53453453, 36.58458458, 36.63463463, 36.68468468, 36.73473473,
       36.78478478, 36.83483483, 36.88488488, 36.93493493, 36.98498498,
       37.03503504, 37.08508509, 37.13513514, 37.18518519, 37.23523524,
       37.28528529, 37.33533534, 37.38538539, 37.43543544, 37.48548549,
       37.53553554, 37.58558559, 37.63563564, 37.68568569, 37.73573574,
       37.78578579, 37.83583584, 37.88588589, 37.93593594, 37.98598599,
       38.03603604, 38.08608609, 38.13613614, 38.18618619, 38.23623624,
       38.28628629, 38.33633634, 38.38638639, 38.43643644, 38.48648649,
       38.53653654, 38.58658659, 38.63663664, 38.68668669, 38.73673674,
       38.78678679, 38.83683684, 38.88688689, 38.93693694, 38.98698699,
       39.03703704, 39.08708709, 39.13713714, 39.18718719, 39.23723724,
       39.28728729, 39.33733734, 39.38738739, 39.43743744, 39.48748749,
       39.53753754, 39.58758759, 39.63763764, 39.68768769, 39.73773774,
       39.78778779, 39.83783784, 39.88788789, 39.93793794, 39.98798799,
       40.03803804, 40.08808809, 40.13813814, 40.18818819, 40.23823824,
       40.28828829, 40.33833834, 40.38838839, 40.43843844, 40.48848849,
       40.53853854, 40.58858859, 40.63863864, 40.68868869, 40.73873874,
       40.78878879, 40.83883884, 40.88888889, 40.93893894, 40.98898899,
       41.03903904, 41.08908909, 41.13913914, 41.18918919, 41.23923924,
       41.28928929, 41.33933934, 41.38938939, 41.43943944, 41.48948949,
       41.53953954, 41.58958959, 41.63963964, 41.68968969, 41.73973974,
       41.78978979, 41.83983984, 41.88988989, 41.93993994, 41.98998999,
       42.04004004, 42.09009009, 42.14014014, 42.19019019, 42.24024024,
       42.29029029, 42.34034034, 42.39039039, 42.44044044, 42.49049049,
       42.54054054, 42.59059059, 42.64064064, 42.69069069, 42.74074074,
       42.79079079, 42.84084084, 42.89089089, 42.94094094, 42.99099099,
       43.04104104, 43.09109109, 43.14114114, 43.19119119, 43.24124124,
       43.29129129, 43.34134134, 43.39139139, 43.44144144, 43.49149149,
       43.54154154, 43.59159159, 43.64164164, 43.69169169, 43.74174174,
       43.79179179, 43.84184184, 43.89189189, 43.94194194, 43.99199199,
       44.04204204, 44.09209209, 44.14214214, 44.19219219, 44.24224224,
       44.29229229, 44.34234234, 44.39239239, 44.44244244, 44.49249249,
       44.54254254, 44.59259259, 44.64264264, 44.69269269, 44.74274274,
       44.79279279, 44.84284284, 44.89289289, 44.94294294, 44.99299299,
       45.04304304, 45.09309309, 45.14314314, 45.19319319, 45.24324324,
       45.29329329, 45.34334334, 45.39339339, 45.44344344, 45.49349349,
       45.54354354, 45.59359359, 45.64364364, 45.69369369, 45.74374374,
       45.79379379, 45.84384384, 45.89389389, 45.94394394, 45.99399399,
       46.04404404, 46.09409409, 46.14414414, 46.19419419, 46.24424424,
       46.29429429, 46.34434434, 46.39439439, 46.44444444, 46.49449449,
       46.54454454, 46.59459459, 46.64464464, 46.69469469, 46.74474474,
       46.79479479, 46.84484484, 46.89489489, 46.94494494, 46.99499499,
       47.04504505, 47.0950951 , 47.14514515, 47.1951952 , 47.24524525,
       47.2952953 , 47.34534535, 47.3953954 , 47.44544545, 47.4954955 ,
       47.54554555, 47.5955956 , 47.64564565, 47.6956957 , 47.74574575,
       47.7957958 , 47.84584585, 47.8958959 , 47.94594595, 47.995996  ,
       48.04604605, 48.0960961 , 48.14614615, 48.1961962 , 48.24624625,
       48.2962963 , 48.34634635, 48.3963964 , 48.44644645, 48.4964965 ,
       48.54654655, 48.5965966 , 48.64664665, 48.6966967 , 48.74674675,
       48.7967968 , 48.84684685, 48.8968969 , 48.94694695, 48.996997  ,
       49.04704705, 49.0970971 , 49.14714715, 49.1971972 , 49.24724725,
       49.2972973 , 49.34734735, 49.3973974 , 49.44744745, 49.4974975 ,
       49.54754755, 49.5975976 , 49.64764765, 49.6976977 , 49.74774775,
       49.7977978 , 49.84784785, 49.8978979 , 49.94794795, 49.997998  ,
       50.04804805, 50.0980981 , 50.14814815, 50.1981982 , 50.24824825,
       50.2982983 , 50.34834835, 50.3983984 , 50.44844845, 50.4984985 ,
       50.54854855, 50.5985986 , 50.64864865, 50.6986987 , 50.74874875,
       50.7987988 , 50.84884885, 50.8988989 , 50.94894895, 50.998999  ,
       51.04904905, 51.0990991 , 51.14914915, 51.1991992 , 51.24924925,
       51.2992993 , 51.34934935, 51.3993994 , 51.44944945, 51.4994995 ,
       51.54954955, 51.5995996 , 51.64964965, 51.6996997 , 51.74974975,
       51.7997998 , 51.84984985, 51.8998999 , 51.94994995, 52.        ])
In [10]:
# Plot x versus y.
plt.plot(x, y, 'k.')
Out[10]:
[<matplotlib.lines.Line2D at 0x1f868bc2310>]
In [11]:
# Do regression on the x and y arrays using numpy.
np.polyfit(x, y, 1)
Out[11]:
array([5., 2.])

$$ y = 3 x - 1 + \epsilon $$
In [12]:
# Create a y with noise.
y = 3.0 * x - 1.0 + np.random.normal(0.0, 1.0, len(x))
In [13]:
# Look at y.
y
Out[13]:
array([ 1.70899268e-01, -2.88868075e-01,  8.07175042e-01, -1.88970672e+00,
       -1.41383459e+00,  4.22800227e-01, -8.26629046e-01, -8.62045579e-01,
       -5.87970313e-01, -2.98702689e+00,  6.86742496e-01, -9.04646905e-01,
       -8.81728727e-01,  1.06025259e+00, -3.82122210e-01,  2.22657478e-01,
        4.24511903e-01, -1.52662449e+00, -2.90721272e-02, -8.28545635e-02,
       -3.14778319e-01, -1.23413447e+00, -7.48496927e-01, -1.24442846e-01,
       -2.88453276e-01, -1.02455914e+00, -1.22466749e+00, -1.04112025e+00,
       -1.09702099e+00,  9.82529225e-01, -2.91735503e+00,  6.41177656e-01,
        6.56887006e-01, -4.52312137e-01,  9.67684664e-01,  3.22360721e-01,
        1.40766013e+00,  4.87036358e-01, -3.63794265e-02,  7.87225477e-01,
       -9.02170371e-02,  3.64389096e-01,  4.83783322e-01,  2.82561563e-01,
       -1.39259186e+00, -5.69579052e-01,  3.05637994e+00,  1.36674914e+00,
        7.12383775e-01,  1.87453860e+00,  5.26120630e-01, -4.10874867e-01,
        3.69304150e-01, -9.38094524e-01,  3.40316852e-01, -5.89005517e-01,
        2.15101541e+00,  1.21432490e+00, -9.00954694e-02,  2.02689674e+00,
       -9.64393720e-01, -6.92789797e-01,  1.71117627e+00,  7.00622083e-01,
        1.43504363e+00,  1.31668013e+00,  1.13256847e+00,  2.58132031e-01,
        1.73943548e+00,  1.67817092e+00,  7.59491483e-01,  7.63583300e-02,
        1.32592653e+00,  1.26453155e+00,  7.92354623e-01,  1.58257496e+00,
        1.73207506e+00,  1.41634369e+00,  4.92529463e-01,  7.22109947e-01,
        6.92276560e-01,  2.77993838e+00,  1.73606741e+00,  1.75882392e+00,
        1.40051311e+00,  5.62770275e-01,  1.96561686e+00,  1.26839214e+00,
        1.15793109e-01,  2.86850639e+00,  7.15301460e-01,  8.59527055e-01,
        9.40370398e-01,  3.30854733e+00,  2.77302018e-01,  1.52816252e+00,
        3.17681659e-01,  2.93927678e+00,  2.53826073e+00,  2.18544388e+00,
        4.28276960e+00,  1.78925314e+00,  5.23329711e-01,  1.91901991e+00,
        1.29265079e+00,  1.46790839e+00,  1.92736150e+00,  2.22751211e+00,
        1.93832793e+00,  1.84820336e+00,  2.56445458e+00,  2.02594573e+00,
        2.35623087e+00,  1.83042174e+00,  2.98449137e+00,  2.81281924e+00,
        2.52717320e+00,  2.76194985e+00,  2.11128966e+00,  4.14441138e+00,
        2.11113270e+00,  2.23433748e+00,  4.25306983e+00,  1.54062190e+00,
        2.17500478e+00,  3.48755238e+00,  2.96320082e+00,  3.23521078e+00,
        1.82570274e+00,  2.59821539e+00,  2.75026798e+00,  5.04146193e+00,
        3.97066473e+00,  1.91858360e+00,  2.03160759e+00,  3.79849932e+00,
        4.38210511e+00,  3.44975744e+00,  4.79697291e+00,  3.28666474e+00,
        2.50329634e+00,  3.47689900e+00,  2.63820964e+00,  2.85431515e+00,
        4.40069220e+00,  3.80136815e+00,  3.57969797e+00,  3.79379987e+00,
        2.93932963e+00,  1.61967890e+00,  2.62755163e+00,  1.53251317e+00,
        4.38694401e+00,  3.32985084e+00,  4.57282125e+00,  3.81959260e+00,
        5.08713820e+00,  3.36740233e+00,  3.10838983e+00,  3.73461241e+00,
        3.56250265e+00,  4.53290821e+00,  3.71691941e+00,  4.88895278e+00,
        3.06514709e+00,  2.62264473e+00,  2.60677668e+00,  5.09391221e+00,
        5.50366463e+00,  4.86978425e+00,  5.58809645e+00,  3.73699851e+00,
        4.20259219e+00,  4.29169181e+00,  5.34289501e+00,  3.74718569e+00,
        4.34348949e+00,  4.70126737e+00,  6.34727690e+00,  5.14055994e+00,
        4.49654080e+00,  3.45227898e+00,  4.23904053e+00,  4.39191515e+00,
        3.66539805e+00,  2.89503845e+00,  4.89722378e+00,  4.30102386e+00,
        4.41322571e+00,  3.53413959e+00,  3.32646300e+00,  5.35581423e+00,
        4.58296405e+00,  5.77889695e+00,  4.87348404e+00,  3.70784188e+00,
        6.29856675e+00,  5.52579217e+00,  5.26098881e+00,  5.01102546e+00,
        4.92716115e+00,  5.19735707e+00,  3.63956822e+00,  4.34712227e+00,
        4.64576987e+00,  5.39554616e+00,  3.31245040e+00,  5.30675337e+00,
        6.18797032e+00,  3.60525623e+00,  4.17814749e+00,  7.28721192e+00,
        6.39702933e+00,  5.48877224e+00,  6.11148076e+00,  4.86925576e+00,
        6.23914666e+00,  5.26520908e+00,  5.78814648e+00,  5.85713402e+00,
        5.62515425e+00,  6.04459396e+00,  4.62874941e+00,  3.93510395e+00,
        4.60047755e+00,  5.87529748e+00,  5.46385044e+00,  6.33031245e+00,
        5.91446618e+00,  6.41982508e+00,  6.58859281e+00,  5.78869932e+00,
        4.37355215e+00,  4.76537390e+00,  6.31237460e+00,  4.76623411e+00,
        5.35740687e+00,  5.04275055e+00,  5.49310009e+00,  5.20536124e+00,
        6.57579360e+00,  6.82304276e+00,  3.49513711e+00,  6.30185511e+00,
        4.94470184e+00,  6.29307742e+00,  5.37497228e+00,  4.41284037e+00,
        6.32895282e+00,  8.38681060e+00,  4.88010554e+00,  7.28817677e+00,
        6.18407381e+00,  6.11304324e+00,  7.00585820e+00,  6.80088672e+00,
        5.74751542e+00,  7.11296766e+00,  6.41715844e+00,  6.23054854e+00,
        7.06585556e+00,  6.04126818e+00,  5.82299494e+00,  7.73949632e+00,
        5.68629939e+00,  8.47277382e+00,  6.91287872e+00,  8.44267263e+00,
        6.41623734e+00,  7.50544803e+00,  6.91820297e+00,  6.97508152e+00,
        8.14181132e+00,  6.74363820e+00,  7.64795396e+00,  6.92493394e+00,
        8.33132482e+00,  8.06933233e+00,  6.57047292e+00,  7.71366682e+00,
        6.75830228e+00,  9.93864501e+00,  7.27765497e+00,  7.59745006e+00,
        7.10411470e+00,  6.10542891e+00,  6.60528018e+00,  7.74994604e+00,
        7.04440747e+00,  8.34677727e+00,  8.61302182e+00,  7.57472460e+00,
        6.98174861e+00,  7.78522952e+00,  8.30509276e+00,  5.95418900e+00,
        7.28451937e+00,  8.83319276e+00,  7.90985700e+00,  8.69646338e+00,
        9.47779684e+00,  7.95643067e+00,  8.82116800e+00,  8.14350458e+00,
        7.99809281e+00,  8.93088390e+00,  7.35800980e+00,  8.64173999e+00,
        8.19615027e+00,  7.76873681e+00,  7.38092509e+00,  7.03759140e+00,
        8.34240807e+00,  6.78703858e+00,  7.84899228e+00,  9.15621217e+00,
        8.09287545e+00,  7.74146631e+00,  9.62948948e+00,  7.98560820e+00,
        9.27533649e+00,  1.08647363e+01,  7.65708032e+00,  8.60119433e+00,
        8.33995673e+00,  9.07955677e+00,  8.12980713e+00,  7.20226411e+00,
        9.76357575e+00,  7.97330286e+00,  8.21871368e+00,  1.00503499e+01,
        1.02853556e+01,  8.90127106e+00,  8.53583854e+00,  7.03081732e+00,
        7.55089863e+00,  9.77404750e+00,  8.13408155e+00,  8.91003221e+00,
        9.96927781e+00,  8.45359801e+00,  9.71070759e+00,  1.04904430e+01,
        8.72650745e+00,  9.45608216e+00,  9.97486314e+00,  1.01253064e+01,
        8.91245712e+00,  8.42709269e+00,  9.61063552e+00,  9.48554665e+00,
        9.67399173e+00,  1.02102249e+01,  1.15286687e+01,  9.76249871e+00,
        8.96975997e+00,  8.02303092e+00,  1.04645555e+01,  8.22275736e+00,
        1.10914215e+01,  8.12511697e+00,  9.86812857e+00,  7.82160285e+00,
        7.20315240e+00,  1.01699000e+01,  1.10215995e+01,  7.86177755e+00,
        1.09719084e+01,  9.88062227e+00,  8.43569053e+00,  9.83730101e+00,
        9.59528479e+00,  9.24640980e+00,  9.87006763e+00,  1.06273591e+01,
        9.22635699e+00,  1.07437182e+01,  1.22246597e+01,  9.32395645e+00,
        1.07075318e+01,  1.10696365e+01,  1.24865084e+01,  1.23500541e+01,
        1.05751283e+01,  1.13038616e+01,  1.00406398e+01,  1.02411187e+01,
        9.95356933e+00,  1.02720963e+01,  1.12154982e+01,  1.20422909e+01,
        9.93943125e+00,  8.68239751e+00,  1.23919212e+01,  1.03644391e+01,
        1.01629133e+01,  9.27991406e+00,  1.06341305e+01,  1.12126173e+01,
        9.36523226e+00,  1.04926130e+01,  1.08666256e+01,  1.27834558e+01,
        1.16368265e+01,  1.06966475e+01,  1.01011876e+01,  1.19528002e+01,
        1.21772258e+01,  1.16825731e+01,  1.09274965e+01,  1.19793176e+01,
        1.10532805e+01,  1.09425496e+01,  1.17697898e+01,  1.14947761e+01,
        1.42740320e+01,  1.16632566e+01,  1.11799424e+01,  1.08706875e+01,
        1.26918241e+01,  1.11962866e+01,  1.19119772e+01,  1.23324312e+01,
        1.20139091e+01,  1.16254152e+01,  1.25101381e+01,  1.07013750e+01,
        1.11653201e+01,  1.10884980e+01,  1.29899101e+01,  1.19247285e+01,
        1.13132281e+01,  1.10277908e+01,  1.12613743e+01,  1.14767488e+01,
        1.30672177e+01,  1.24386241e+01,  1.28423281e+01,  1.26354833e+01,
        1.14394408e+01,  1.31844066e+01,  1.12538859e+01,  1.32690120e+01,
        1.38918833e+01,  1.12241516e+01,  1.30896153e+01,  1.27427272e+01,
        1.25201800e+01,  1.24778287e+01,  1.26183244e+01,  1.37878414e+01,
        1.14359235e+01,  1.20208056e+01,  1.27647603e+01,  1.20018395e+01,
        1.28531210e+01,  1.40217945e+01,  1.09637938e+01,  1.22836486e+01,
        1.19785849e+01,  1.35716646e+01,  1.31133197e+01,  1.48279814e+01,
        1.38088373e+01,  1.52733739e+01,  1.19727537e+01,  1.34718916e+01,
        1.38858117e+01,  1.28522222e+01,  1.38916395e+01,  1.32065938e+01,
        1.29066907e+01,  1.40151492e+01,  1.29318447e+01,  1.34279671e+01,
        1.28663589e+01,  1.20648781e+01,  1.18471159e+01,  1.45826714e+01,
        1.56962240e+01,  1.25840661e+01,  1.36711444e+01,  1.45154510e+01,
        1.22683760e+01,  1.26044472e+01,  1.40222294e+01,  1.37970614e+01,
        1.28109822e+01,  1.47615673e+01,  1.35749631e+01,  1.37420045e+01,
        1.39301556e+01,  1.51873161e+01,  1.30865733e+01,  1.33463223e+01,
        1.18944364e+01,  1.25530000e+01,  1.32966132e+01,  1.40932977e+01,
        1.52382986e+01,  1.35627691e+01,  1.45176252e+01,  1.48579831e+01,
        1.65892099e+01,  1.44830937e+01,  1.39571385e+01,  1.40892418e+01,
        1.62624511e+01,  1.43453824e+01,  1.43916231e+01,  1.45948272e+01,
        1.46831035e+01,  1.67398052e+01,  1.48393526e+01,  1.29772181e+01,
        1.45288201e+01,  1.32553216e+01,  1.36029648e+01,  1.32093436e+01,
        1.63003642e+01,  1.47509220e+01,  1.35458919e+01,  1.44446283e+01,
        1.35826827e+01,  1.37016409e+01,  1.42399217e+01,  1.37761465e+01,
        1.60794456e+01,  1.39221925e+01,  1.39850812e+01,  1.49904662e+01,
        1.52393290e+01,  1.50831259e+01,  1.55105236e+01,  1.55128158e+01,
        1.48025826e+01,  1.49326865e+01,  1.53516965e+01,  1.56355989e+01,
        1.61816055e+01,  1.47352664e+01,  1.27397575e+01,  1.66769719e+01,
        1.57850040e+01,  1.60113848e+01,  1.40526483e+01,  1.64853403e+01,
        1.65139523e+01,  1.42865874e+01,  1.49494626e+01,  1.51097036e+01,
        1.63399466e+01,  1.51106581e+01,  1.46913362e+01,  1.55332062e+01,
        1.56264439e+01,  1.68572987e+01,  1.57477015e+01,  1.64297542e+01,
        1.65029638e+01,  1.70448406e+01,  1.51658101e+01,  1.49486817e+01,
        1.65990252e+01,  1.54402392e+01,  1.73976181e+01,  1.66233347e+01,
        1.59520669e+01,  1.58927931e+01,  1.68592342e+01,  1.68130677e+01,
        1.65943562e+01,  1.52691832e+01,  1.65937403e+01,  1.60053815e+01,
        1.70508191e+01,  1.52084880e+01,  1.64950718e+01,  1.52783516e+01,
        1.71568125e+01,  1.56028933e+01,  1.68854001e+01,  1.60035197e+01,
        1.71237044e+01,  1.74182850e+01,  1.64198546e+01,  1.56881902e+01,
        1.74216141e+01,  1.65598836e+01,  1.68562585e+01,  1.56117643e+01,
        1.80931188e+01,  1.69970472e+01,  1.60042355e+01,  1.64441065e+01,
        1.51122176e+01,  1.59193489e+01,  1.63051922e+01,  1.83032580e+01,
        1.68259116e+01,  1.68116797e+01,  1.62312909e+01,  1.81653635e+01,
        1.72605464e+01,  1.72876461e+01,  1.75145473e+01,  1.46539211e+01,
        1.83831221e+01,  1.93137395e+01,  1.77651591e+01,  1.79197080e+01,
        1.76213079e+01,  1.85356204e+01,  1.79358748e+01,  1.79039731e+01,
        1.71158945e+01,  1.70256192e+01,  1.80170629e+01,  1.76993294e+01,
        1.95658995e+01,  1.57664934e+01,  1.71472565e+01,  1.90394734e+01,
        1.73778350e+01,  1.81918451e+01,  1.80299640e+01,  1.77422956e+01,
        1.65037879e+01,  1.67311153e+01,  1.69505795e+01,  1.74390174e+01,
        1.79819462e+01,  1.84556540e+01,  1.68369773e+01,  1.87274891e+01,
        1.88286292e+01,  1.77853186e+01,  1.72258191e+01,  1.75855714e+01,
        1.61459666e+01,  1.84176029e+01,  1.78307238e+01,  1.85672670e+01,
        1.89011384e+01,  1.77332587e+01,  1.77943446e+01,  1.73249587e+01,
        1.81241328e+01,  1.78842342e+01,  1.85492271e+01,  1.81036831e+01,
        1.86320219e+01,  1.80849826e+01,  1.88156665e+01,  1.91012832e+01,
        2.14708672e+01,  2.02482236e+01,  1.84023563e+01,  1.75435221e+01,
        1.92438573e+01,  1.81244663e+01,  1.97537212e+01,  1.90061801e+01,
        1.69867857e+01,  1.96082577e+01,  1.97080300e+01,  1.82116035e+01,
        1.96735270e+01,  1.85080282e+01,  1.95380169e+01,  1.94522595e+01,
        1.80939537e+01,  2.10872608e+01,  1.83991308e+01,  1.98047768e+01,
        1.91433257e+01,  2.07258017e+01,  1.92982800e+01,  1.88209326e+01,
        1.94638186e+01,  1.81262154e+01,  1.81152426e+01,  1.84695875e+01,
        1.87150415e+01,  2.09134449e+01,  1.79450509e+01,  1.84170474e+01,
        1.88488917e+01,  1.97647798e+01,  1.93214539e+01,  1.84084990e+01,
        2.07860521e+01,  2.05310515e+01,  1.84152247e+01,  1.88807539e+01,
        2.05983501e+01,  2.03425818e+01,  1.98387051e+01,  2.05569557e+01,
        2.05949970e+01,  2.03004115e+01,  2.11944740e+01,  1.89989412e+01,
        2.03175443e+01,  2.18339059e+01,  1.88548780e+01,  1.96367632e+01,
        1.96882706e+01,  1.86481103e+01,  2.12017279e+01,  2.02053109e+01,
        1.95443263e+01,  1.90301340e+01,  2.17561802e+01,  2.01272816e+01,
        1.93315156e+01,  2.26112014e+01,  2.01154643e+01,  1.87779666e+01,
        1.95011424e+01,  1.97030801e+01,  2.09603869e+01,  2.00345918e+01,
        2.16231803e+01,  2.07061534e+01,  2.09725157e+01,  2.16226485e+01,
        2.09473993e+01,  2.04449724e+01,  2.19243982e+01,  2.16212790e+01,
        2.08903717e+01,  2.32545113e+01,  2.05603533e+01,  2.23609847e+01,
        2.16496375e+01,  2.33637063e+01,  1.97927846e+01,  2.09229662e+01,
        2.17848845e+01,  2.19759403e+01,  2.19789469e+01,  2.18759604e+01,
        2.28746716e+01,  2.06222049e+01,  2.10929738e+01,  2.21701179e+01,
        2.19419890e+01,  2.17892346e+01,  2.29800426e+01,  2.16078518e+01,
        2.22742682e+01,  2.27349802e+01,  2.10777965e+01,  2.24276436e+01,
        2.25838775e+01,  2.19627937e+01,  2.12336135e+01,  2.11946454e+01,
        2.26935467e+01,  2.26644842e+01,  2.14845625e+01,  2.19526010e+01,
        2.15583055e+01,  2.22132133e+01,  2.27925094e+01,  2.06140906e+01,
        2.24055526e+01,  2.32378925e+01,  2.28288460e+01,  2.16086546e+01,
        2.18559763e+01,  2.26001669e+01,  2.34625281e+01,  2.12593215e+01,
        2.25702108e+01,  2.23734827e+01,  2.24730058e+01,  2.25243255e+01,
        2.18792655e+01,  2.14545709e+01,  2.23466231e+01,  2.29256194e+01,
        2.20788342e+01,  2.31180552e+01,  2.17313637e+01,  2.27145019e+01,
        2.40936311e+01,  2.17629684e+01,  2.22751588e+01,  2.35072250e+01,
        2.19294810e+01,  2.18300725e+01,  2.25620781e+01,  2.32418870e+01,
        2.33807001e+01,  2.27743638e+01,  2.38122198e+01,  2.24375248e+01,
        2.29644156e+01,  2.12903603e+01,  2.52673265e+01,  2.21906476e+01,
        2.21123522e+01,  2.23166911e+01,  2.35691995e+01,  2.27931269e+01,
        2.32092267e+01,  2.26283773e+01,  2.32265842e+01,  2.40786003e+01,
        2.53704247e+01,  2.31338526e+01,  2.19057560e+01,  2.44038744e+01,
        2.17624052e+01,  2.39269246e+01,  2.39883171e+01,  2.42728854e+01,
        2.34144846e+01,  2.31429236e+01,  2.39414186e+01,  2.23963806e+01,
        2.28148032e+01,  2.34669060e+01,  2.48115036e+01,  2.34173203e+01,
        2.42641061e+01,  2.43591720e+01,  2.36929411e+01,  2.46280160e+01,
        2.46903870e+01,  2.42976404e+01,  2.45177553e+01,  2.25884668e+01,
        2.44022868e+01,  2.46445143e+01,  2.39462898e+01,  2.45659208e+01,
        2.39161536e+01,  2.38922745e+01,  2.46538999e+01,  2.49751083e+01,
        2.43525877e+01,  2.34915410e+01,  2.32342472e+01,  2.50317031e+01,
        2.38485388e+01,  2.47718442e+01,  2.36627913e+01,  2.45126599e+01,
        2.40122087e+01,  2.51282438e+01,  2.24315815e+01,  2.56208572e+01,
        2.37784207e+01,  2.41268502e+01,  2.42494528e+01,  2.42436033e+01,
        2.26420770e+01,  2.56863985e+01,  2.50839497e+01,  2.49663974e+01,
        2.36068714e+01,  2.40258889e+01,  2.34264730e+01,  2.41559340e+01,
        2.60270023e+01,  2.59592881e+01,  2.71433370e+01,  2.33609185e+01,
        2.59867325e+01,  2.49607462e+01,  2.56743343e+01,  2.45345095e+01,
        2.45999482e+01,  2.52409706e+01,  2.48582300e+01,  2.49403488e+01,
        2.49393097e+01,  2.75307634e+01,  2.61586443e+01,  2.56414269e+01,
        2.54057409e+01,  2.81999729e+01,  2.57713613e+01,  2.55349669e+01,
        2.43767476e+01,  2.64227203e+01,  2.51004969e+01,  2.59569472e+01,
        2.57955975e+01,  2.59380149e+01,  2.60591572e+01,  2.44930724e+01,
        2.49440701e+01,  2.58558646e+01,  2.62819337e+01,  2.66791488e+01,
        2.43942382e+01,  2.60042892e+01,  2.58224634e+01,  2.51417077e+01,
        2.57081442e+01,  2.75567488e+01,  2.67994716e+01,  2.62386605e+01,
        2.70298353e+01,  2.55227809e+01,  2.68301372e+01,  2.66228015e+01,
        2.74225508e+01,  2.66481964e+01,  2.59015569e+01,  2.59972837e+01,
        2.63027686e+01,  2.52345340e+01,  2.73038748e+01,  2.58525173e+01,
        2.86564044e+01,  2.65556864e+01,  2.73050955e+01,  2.68762476e+01,
        2.57194800e+01,  2.54158987e+01,  2.62295231e+01,  2.63326152e+01,
        2.53896351e+01,  2.75669296e+01,  2.59505423e+01,  2.78116181e+01,
        2.84485882e+01,  2.59660293e+01,  2.77173125e+01,  2.83302302e+01,
        2.79474649e+01,  2.75089282e+01,  2.74512280e+01,  2.95505424e+01,
        2.65223029e+01,  2.75652774e+01,  2.72313578e+01,  2.84152498e+01,
        2.88307922e+01,  2.70415022e+01,  2.69154227e+01,  2.84971135e+01,
        2.66811148e+01,  2.69151381e+01,  2.79876153e+01,  2.64252979e+01,
        2.74775014e+01,  2.82633769e+01,  2.66403244e+01,  2.47053233e+01,
        2.70954104e+01,  2.86284095e+01,  2.73372814e+01,  2.82268946e+01,
        2.87514225e+01,  2.91657044e+01,  2.79329901e+01,  2.84437821e+01,
        2.73113047e+01,  2.67576462e+01,  2.85346602e+01,  2.72198269e+01,
        2.85760795e+01,  2.76391874e+01,  3.01481060e+01,  2.79636931e+01,
        2.73119438e+01,  2.81914434e+01,  2.92192838e+01,  2.65624140e+01,
        2.80688819e+01,  2.83034711e+01,  2.75242346e+01,  2.74431850e+01,
        2.77080516e+01,  2.82280144e+01,  3.09864235e+01,  2.83993828e+01,
        2.84068092e+01,  2.77154579e+01,  2.89739827e+01,  2.92829677e+01,
        2.79889915e+01,  2.91662343e+01,  2.78858874e+01,  2.68098343e+01,
        2.72628459e+01,  2.88550212e+01,  2.68112941e+01,  2.72218629e+01,
        2.82664272e+01,  2.99839763e+01,  3.03652118e+01,  2.86002459e+01])
In [14]:
# Do regression on the x and y arrays using numpy.
np.polyfit(x, y, 1)
Out[14]:
array([ 2.9990233 , -1.03591347])
In [15]:
# Create variables with those values.
m, c = np.polyfit(x, y, 1)
# Have a look at m and c.
m, c
Out[15]:
(2.999023302695792, -1.0359134656300835)
In [16]:
# Plot x and y and the regression line in red.
plt.plot(x, y, 'k.')
plt.plot(x, m * x + c, 'r-')
Out[16]:
[<matplotlib.lines.Line2D at 0x1f868c0c9a0>]

Note that we can easily calculate the best m and c ourselves.

In [17]:
# Calculate mean x and mean y.
x_avg = np.mean(x)
y_avg = np.mean(y)

# Subtract means from x and y.
x_zero = x - x_avg
y_zero = y - y_avg

# Dot product of mean-adjusted x and y divided by dot product of mean adjusted x with itself.
m = np.sum(x_zero * y_zero) / np.sum(x_zero * x_zero)
# Subtract m times average x from average y.
c = y_avg - m * x_avg

# Let's have a look - same values as above.
m, c
Out[17]:
(2.999023302695789, -1.0359134656300757)

$$ y = 2 x^2 + 5x + 1 + \epsilon $$
In [18]:
# Create y from a polynomial in x.
y =  2.0 * x * x + 5.0 * x + 1.0 + np.random.normal(0.0, 1.0, len(x))
In [19]:
# Look at y.
y
Out[19]:
array([1.64593802e+00, 1.16505284e+00, 1.18978209e+00, 7.12664202e-01,
       1.78363788e-01, 6.54203277e-01, 1.58964173e+00, 1.57847440e-01,
       8.74635378e-01, 5.75742137e-02, 1.26695460e+00, 1.53578011e+00,
       2.16780258e+00, 4.79228086e-01, 1.78360951e+00, 5.66081825e-01,
       2.22581559e+00, 1.91084180e+00, 4.04399802e-01, 1.38568792e+00,
       1.94773735e+00, 2.10991526e+00, 2.03644280e+00, 1.54758713e+00,
       1.96540252e+00, 2.66951637e+00, 2.13405313e+00, 1.25225436e+00,
       3.13276525e+00, 2.13550346e+00, 2.04246053e+00, 2.39314273e+00,
       2.77240286e+00, 2.18306678e+00, 4.76218094e+00, 2.90428183e+00,
       3.54787935e+00, 3.19706942e+00, 4.30767540e+00, 2.40639200e+00,
       3.49749395e+00, 1.49410103e+00, 4.39918724e+00, 3.57999368e+00,
       5.07189784e+00, 2.66565925e+00, 2.24131144e+00, 5.20744626e+00,
       4.57792477e+00, 4.95516567e+00, 1.69227509e+00, 3.07791086e+00,
       5.69168983e+00, 3.92154226e+00, 4.54638397e+00, 4.49833905e+00,
       3.22553062e+00, 4.82818939e+00, 4.06634049e+00, 5.17242099e+00,
       4.97395738e+00, 5.24110945e+00, 4.93572718e+00, 3.93934765e+00,
       5.04256106e+00, 5.67122701e+00, 5.14204578e+00, 4.76866849e+00,
       5.37137735e+00, 6.14476504e+00, 4.76710620e+00, 5.37596965e+00,
       6.54447315e+00, 5.01499783e+00, 7.05098313e+00, 6.14112699e+00,
       6.94737020e+00, 3.69700732e+00, 4.86834869e+00, 8.92445801e+00,
       5.86304870e+00, 4.73925925e+00, 7.15091157e+00, 5.86245526e+00,
       5.84651147e+00, 5.67047897e+00, 6.02639491e+00, 7.11651556e+00,
       5.73321748e+00, 6.73476633e+00, 7.06835197e+00, 6.21070773e+00,
       5.97585958e+00, 7.26723702e+00, 6.95647018e+00, 7.64053524e+00,
       7.96500186e+00, 6.82364547e+00, 7.98317559e+00, 8.45015024e+00,
       8.33038876e+00, 8.58292004e+00, 8.70972028e+00, 9.10778681e+00,
       6.87658602e+00, 8.48514320e+00, 9.75355427e+00, 8.96160192e+00,
       8.68075036e+00, 8.41621285e+00, 9.83935780e+00, 9.89688534e+00,
       9.40662035e+00, 9.73780035e+00, 8.78102027e+00, 1.09668683e+01,
       9.92161568e+00, 9.74970956e+00, 1.13998040e+01, 9.93140179e+00,
       8.96257446e+00, 9.88794732e+00, 9.19103566e+00, 1.10814884e+01,
       1.22562538e+01, 1.04331563e+01, 9.37437138e+00, 1.16129752e+01,
       1.09838383e+01, 1.04442807e+01, 1.26090002e+01, 1.11436152e+01,
       1.00906106e+01, 9.36225639e+00, 1.07228677e+01, 1.29667789e+01,
       1.19807286e+01, 1.30283415e+01, 1.14685774e+01, 1.24298337e+01,
       1.29604813e+01, 1.20757225e+01, 1.26008483e+01, 1.36528337e+01,
       1.19831676e+01, 1.28371346e+01, 1.32311119e+01, 1.36132194e+01,
       1.18108194e+01, 1.37907496e+01, 1.29519245e+01, 1.07098543e+01,
       1.48727263e+01, 1.46219188e+01, 1.26114835e+01, 1.37122979e+01,
       1.35429146e+01, 1.47092616e+01, 1.30508627e+01, 1.38694929e+01,
       1.31526115e+01, 1.32655317e+01, 1.42982563e+01, 1.42632208e+01,
       1.42327265e+01, 1.55997333e+01, 1.49497321e+01, 1.44873277e+01,
       1.40086339e+01, 1.43375889e+01, 1.50873677e+01, 1.48444508e+01,
       1.54318512e+01, 1.47118538e+01, 1.47695007e+01, 1.50177179e+01,
       1.55434165e+01, 1.47303774e+01, 1.62290201e+01, 1.75608229e+01,
       1.50867066e+01, 1.73217039e+01, 1.68643124e+01, 1.59517898e+01,
       1.64680002e+01, 1.64574237e+01, 1.80142314e+01, 1.67986892e+01,
       1.75984473e+01, 1.78427299e+01, 1.87997715e+01, 1.85064612e+01,
       1.91560155e+01, 1.76731118e+01, 1.67878997e+01, 1.80052690e+01,
       1.83012693e+01, 1.88192463e+01, 2.03867682e+01, 1.95839058e+01,
       1.89210935e+01, 2.03339277e+01, 2.12303976e+01, 1.96398629e+01,
       1.93657728e+01, 1.97996008e+01, 2.02409640e+01, 1.90801567e+01,
       2.05297212e+01, 2.13489118e+01, 2.00690131e+01, 1.85343620e+01,
       2.18621792e+01, 2.01732923e+01, 2.02041374e+01, 2.05910406e+01,
       2.29578511e+01, 2.09812907e+01, 2.20021026e+01, 2.11414691e+01,
       2.13550427e+01, 2.16585385e+01, 2.11694741e+01, 2.14508293e+01,
       2.14645259e+01, 2.11488848e+01, 2.27089096e+01, 2.34621413e+01,
       2.22942586e+01, 2.20541480e+01, 2.26213191e+01, 2.37839232e+01,
       2.34823633e+01, 2.36596217e+01, 2.21419837e+01, 2.52707729e+01,
       2.45763198e+01, 2.51982840e+01, 2.28033814e+01, 2.63318460e+01,
       2.43662349e+01, 2.44751485e+01, 2.64640994e+01, 2.53692654e+01,
       2.54642146e+01, 2.56479051e+01, 2.55349847e+01, 2.69431629e+01,
       2.41850678e+01, 2.47437574e+01, 2.49488655e+01, 2.54166999e+01,
       2.50660388e+01, 2.55707445e+01, 2.61150350e+01, 2.83557451e+01,
       2.57655132e+01, 2.70582486e+01, 2.80514650e+01, 2.80615716e+01,
       2.57910213e+01, 2.73014619e+01, 2.70264451e+01, 2.64738452e+01,
       2.77836593e+01, 2.81838586e+01, 2.80869593e+01, 2.86061818e+01,
       2.97010023e+01, 2.77162266e+01, 2.96283363e+01, 2.96550333e+01,
       3.03534249e+01, 2.72216784e+01, 2.94814693e+01, 2.87539844e+01,
       2.99934196e+01, 3.02754161e+01, 3.16152769e+01, 3.01945357e+01,
       3.06075935e+01, 3.10767892e+01, 3.13019683e+01, 3.05161069e+01,
       3.16856975e+01, 3.00700448e+01, 3.01779040e+01, 3.17757215e+01,
       3.21803740e+01, 3.19992300e+01, 3.21278145e+01, 3.24110970e+01,
       3.42720060e+01, 3.37160600e+01, 3.17647955e+01, 3.59477897e+01,
       3.32017825e+01, 3.54896193e+01, 3.41781682e+01, 3.11018654e+01,
       3.34424726e+01, 3.55030276e+01, 3.44748492e+01, 3.41363023e+01,
       3.59638326e+01, 3.48367134e+01, 3.44159347e+01, 3.56621474e+01,
       3.45090891e+01, 3.43776379e+01, 3.61854131e+01, 3.60064923e+01,
       3.57630701e+01, 3.58543694e+01, 3.76705635e+01, 3.64598049e+01,
       3.77182366e+01, 3.55316168e+01, 3.86051229e+01, 3.53530478e+01,
       3.70455513e+01, 3.68850820e+01, 3.84835920e+01, 3.79299555e+01,
       3.82088395e+01, 3.79823430e+01, 3.90243262e+01, 3.78423697e+01,
       3.85951164e+01, 3.95578086e+01, 3.93182367e+01, 3.82302915e+01,
       3.86569588e+01, 3.88739090e+01, 3.82562850e+01, 3.97120118e+01,
       4.25842033e+01, 4.16678410e+01, 4.07429928e+01, 4.22401857e+01,
       4.22869536e+01, 4.08096058e+01, 4.20401598e+01, 4.10692221e+01,
       4.09503632e+01, 4.49662112e+01, 4.12951584e+01, 4.18025837e+01,
       4.32227680e+01, 4.44846039e+01, 4.20186412e+01, 4.24694369e+01,
       4.36809748e+01, 4.34128123e+01, 4.54057114e+01, 4.58221838e+01,
       4.51561880e+01, 4.30270769e+01, 4.55158112e+01, 4.42597493e+01,
       4.58919542e+01, 4.65689552e+01, 4.48298228e+01, 4.74079802e+01,
       4.34535830e+01, 4.45986245e+01, 4.75509864e+01, 4.62165819e+01,
       4.68814974e+01, 4.57273782e+01, 4.59500136e+01, 4.79414556e+01,
       4.75458097e+01, 4.60767963e+01, 4.88533191e+01, 4.75387875e+01,
       4.84894000e+01, 4.81215395e+01, 4.96482902e+01, 4.77780158e+01,
       4.95862260e+01, 5.04132626e+01, 4.79127620e+01, 4.80196831e+01,
       5.23074739e+01, 4.84017074e+01, 5.05144882e+01, 5.16215803e+01,
       5.17188236e+01, 4.94503200e+01, 4.97133515e+01, 5.19536446e+01,
       4.94679565e+01, 5.14917011e+01, 5.14796978e+01, 5.30979679e+01,
       5.27033874e+01, 5.35529696e+01, 5.09406956e+01, 5.38351355e+01,
       5.15094951e+01, 5.45324691e+01, 5.50028493e+01, 5.47894054e+01,
       5.36427091e+01, 5.26830072e+01, 5.38222787e+01, 5.59090800e+01,
       5.52115689e+01, 5.46820344e+01, 5.66487338e+01, 5.71745695e+01,
       5.81693902e+01, 5.66189904e+01, 5.80835744e+01, 5.70282601e+01,
       5.51042205e+01, 5.77603775e+01, 5.79158480e+01, 5.78369584e+01,
       5.81325315e+01, 5.77924570e+01, 5.66527305e+01, 5.59034182e+01,
       5.98850053e+01, 5.98181408e+01, 5.82537043e+01, 5.89564329e+01,
       5.84440480e+01, 5.77239621e+01, 5.95929425e+01, 5.93950743e+01,
       6.01563972e+01, 6.07325140e+01, 5.95500723e+01, 6.08392032e+01,
       6.14760421e+01, 6.23372694e+01, 6.07737641e+01, 6.17967023e+01,
       6.20898192e+01, 6.25811423e+01, 6.08117438e+01, 6.18138132e+01,
       6.38127005e+01, 6.16515445e+01, 6.28886439e+01, 6.52579458e+01,
       6.21954823e+01, 6.43507071e+01, 6.56590062e+01, 6.34256745e+01,
       6.47985008e+01, 6.43843139e+01, 6.66125693e+01, 6.42913614e+01,
       6.48951500e+01, 6.76709058e+01, 6.69764812e+01, 6.62184488e+01,
       6.65618775e+01, 6.61541851e+01, 6.74921867e+01, 6.55802125e+01,
       6.93919179e+01, 6.51929410e+01, 6.80690438e+01, 6.60942398e+01,
       6.61151978e+01, 6.83503502e+01, 6.93237404e+01, 6.73511974e+01,
       6.99641135e+01, 7.14608108e+01, 6.90631501e+01, 7.11726909e+01,
       7.01570652e+01, 6.90654624e+01, 7.01127468e+01, 7.08454234e+01,
       7.07570765e+01, 7.20770002e+01, 7.10342494e+01, 7.29212656e+01,
       7.35658495e+01, 7.31041428e+01, 7.36302832e+01, 7.23524193e+01,
       7.29589980e+01, 7.37958182e+01, 7.29695090e+01, 7.50332849e+01,
       7.47240690e+01, 7.21935687e+01, 7.30587133e+01, 7.53445690e+01,
       7.60237701e+01, 7.65731468e+01, 7.47054144e+01, 7.44992804e+01,
       7.63186510e+01, 7.58385715e+01, 7.49749874e+01, 7.69636598e+01,
       7.66564028e+01, 7.90959703e+01, 7.90867314e+01, 7.80508219e+01,
       7.88037887e+01, 7.78306941e+01, 8.03715000e+01, 8.00069816e+01,
       7.97480493e+01, 7.96797739e+01, 8.01128734e+01, 8.22090180e+01,
       7.92765922e+01, 7.98768268e+01, 7.91514995e+01, 8.21035035e+01,
       8.12348869e+01, 7.92268340e+01, 8.03574997e+01, 8.24527679e+01,
       8.21805708e+01, 8.21087894e+01, 8.35152280e+01, 8.25198803e+01,
       8.24886155e+01, 8.33638592e+01, 8.36938728e+01, 8.41151166e+01,
       8.42764223e+01, 8.44531501e+01, 8.46108147e+01, 8.40978891e+01,
       8.51580245e+01, 8.57452381e+01, 8.80714427e+01, 8.65725465e+01,
       8.85201452e+01, 8.71741315e+01, 8.76464749e+01, 8.65212523e+01,
       8.83425466e+01, 8.89856976e+01, 8.77307483e+01, 9.03734948e+01,
       8.90593672e+01, 8.78746526e+01, 8.74584070e+01, 8.85639307e+01,
       8.83242552e+01, 9.08165355e+01, 9.08945680e+01, 9.02684057e+01,
       9.19642822e+01, 9.16245888e+01, 9.24872513e+01, 9.08359908e+01,
       9.05954124e+01, 9.26489224e+01, 9.34592572e+01, 9.36124016e+01,
       9.31495305e+01, 9.44006751e+01, 9.47354013e+01, 9.35964647e+01,
       9.26787652e+01, 9.30214937e+01, 9.50433770e+01, 9.52571861e+01,
       9.68709793e+01, 9.65776888e+01, 9.47685317e+01, 9.75495776e+01,
       9.59893188e+01, 9.76099099e+01, 9.76093983e+01, 9.76540100e+01,
       9.60051273e+01, 9.94404096e+01, 9.77678130e+01, 9.66799008e+01,
       9.86134311e+01, 9.75430857e+01, 1.00377284e+02, 9.91467865e+01,
       9.99209364e+01, 9.97464893e+01, 9.86555511e+01, 1.01510455e+02,
       1.03086633e+02, 9.95156597e+01, 1.00465308e+02, 1.02155259e+02,
       1.02250766e+02, 1.01882641e+02, 1.03241450e+02, 1.04073145e+02,
       1.01244063e+02, 1.02734803e+02, 1.01317214e+02, 1.03754728e+02,
       1.05277926e+02, 1.04578299e+02, 1.05696778e+02, 1.05179427e+02,
       1.06700753e+02, 1.04877290e+02, 1.06870212e+02, 1.08298785e+02,
       1.06235738e+02, 1.05778951e+02, 1.07265073e+02, 1.09222645e+02,
       1.08027970e+02, 1.08167857e+02, 1.06975971e+02, 1.10998320e+02,
       1.09522712e+02, 1.10266413e+02, 1.10959364e+02, 1.08559917e+02,
       1.08499563e+02, 1.11639858e+02, 1.12712298e+02, 1.09559581e+02,
       1.12370977e+02, 1.10392014e+02, 1.11368307e+02, 1.12228032e+02,
       1.12856552e+02, 1.12742775e+02, 1.13443085e+02, 1.13300498e+02,
       1.12860287e+02, 1.15000382e+02, 1.14594011e+02, 1.16272254e+02,
       1.16194520e+02, 1.16051396e+02, 1.16314542e+02, 1.16841632e+02,
       1.16018291e+02, 1.16981173e+02, 1.17114205e+02, 1.15544566e+02,
       1.15583796e+02, 1.19196080e+02, 1.17702472e+02, 1.19715136e+02,
       1.17319018e+02, 1.18954952e+02, 1.19046495e+02, 1.18727925e+02,
       1.21281240e+02, 1.20794657e+02, 1.19524742e+02, 1.18974610e+02,
       1.22012678e+02, 1.22960212e+02, 1.20714801e+02, 1.22732458e+02,
       1.22862109e+02, 1.23118287e+02, 1.22153174e+02, 1.24022836e+02,
       1.22812299e+02, 1.23788700e+02, 1.24196507e+02, 1.23278329e+02,
       1.25717861e+02, 1.24116687e+02, 1.26955845e+02, 1.27169494e+02,
       1.26547001e+02, 1.24694758e+02, 1.25919303e+02, 1.26187757e+02,
       1.25752528e+02, 1.25990106e+02, 1.28670883e+02, 1.30006941e+02,
       1.28976574e+02, 1.30262460e+02, 1.30319298e+02, 1.31337570e+02,
       1.30517957e+02, 1.31339175e+02, 1.32028352e+02, 1.31268344e+02,
       1.33968601e+02, 1.32675551e+02, 1.32086957e+02, 1.31293627e+02,
       1.33140301e+02, 1.34028534e+02, 1.34292435e+02, 1.33238859e+02,
       1.34434943e+02, 1.34869587e+02, 1.34444280e+02, 1.36335585e+02,
       1.35762196e+02, 1.34896057e+02, 1.35355684e+02, 1.38016242e+02,
       1.36898167e+02, 1.36200360e+02, 1.37636529e+02, 1.39516616e+02,
       1.38277221e+02, 1.36789437e+02, 1.37590068e+02, 1.39797033e+02,
       1.41733365e+02, 1.38722933e+02, 1.41538381e+02, 1.39901135e+02,
       1.39350838e+02, 1.41239564e+02, 1.41325310e+02, 1.42161852e+02,
       1.42274461e+02, 1.43173700e+02, 1.42049106e+02, 1.44198377e+02,
       1.44026808e+02, 1.45334527e+02, 1.45491018e+02, 1.45110732e+02,
       1.43744942e+02, 1.44628313e+02, 1.43925580e+02, 1.46826582e+02,
       1.45771670e+02, 1.46011378e+02, 1.48743988e+02, 1.46780343e+02,
       1.49429350e+02, 1.49124573e+02, 1.48927919e+02, 1.48816824e+02,
       1.49507482e+02, 1.49962100e+02, 1.48783116e+02, 1.49791336e+02,
       1.51570972e+02, 1.49545352e+02, 1.52271343e+02, 1.51694250e+02,
       1.51142443e+02, 1.52688460e+02, 1.52795830e+02, 1.53904115e+02,
       1.54062651e+02, 1.52040304e+02, 1.53401165e+02, 1.54611942e+02,
       1.54866492e+02, 1.54560368e+02, 1.56086081e+02, 1.55618018e+02,
       1.56451732e+02, 1.55563629e+02, 1.56772726e+02, 1.58149150e+02,
       1.58460110e+02, 1.57583552e+02, 1.58940829e+02, 1.58665908e+02,
       1.59153658e+02, 1.59549606e+02, 1.59950995e+02, 1.58628546e+02,
       1.59061762e+02, 1.59357680e+02, 1.60883173e+02, 1.62906419e+02,
       1.61684746e+02, 1.62307671e+02, 1.63400918e+02, 1.61870250e+02,
       1.64350098e+02, 1.62292228e+02, 1.62421208e+02, 1.65349131e+02,
       1.63334935e+02, 1.66199443e+02, 1.68292950e+02, 1.66489670e+02,
       1.67020139e+02, 1.66696261e+02, 1.67198289e+02, 1.68025090e+02,
       1.67511095e+02, 1.66871630e+02, 1.69895230e+02, 1.68924333e+02,
       1.67687543e+02, 1.71780167e+02, 1.70813492e+02, 1.69310750e+02,
       1.69943271e+02, 1.70015108e+02, 1.72482942e+02, 1.71415907e+02,
       1.72819547e+02, 1.72950440e+02, 1.73322792e+02, 1.71751651e+02,
       1.73117196e+02, 1.73644345e+02, 1.73716392e+02, 1.75110320e+02,
       1.74614086e+02, 1.76202037e+02, 1.76697191e+02, 1.77414305e+02,
       1.78383944e+02, 1.76275054e+02, 1.78071535e+02, 1.78398309e+02,
       1.78123404e+02, 1.78490923e+02, 1.77995617e+02, 1.79068998e+02,
       1.78893981e+02, 1.82024392e+02, 1.80563947e+02, 1.82787908e+02,
       1.80849473e+02, 1.80988262e+02, 1.83370992e+02, 1.81435145e+02,
       1.81172981e+02, 1.83997461e+02, 1.83133467e+02, 1.85118184e+02,
       1.84245909e+02, 1.85951559e+02, 1.84289922e+02, 1.85672549e+02,
       1.87153957e+02, 1.86473943e+02, 1.86364966e+02, 1.87727764e+02,
       1.87938444e+02, 1.88550419e+02, 1.89989120e+02, 1.87670001e+02,
       1.88829534e+02, 1.90140197e+02, 1.89972097e+02, 1.90419280e+02,
       1.90744638e+02, 1.91091624e+02, 1.91311652e+02, 1.91413860e+02,
       1.92333824e+02, 1.92307368e+02, 1.91374385e+02, 1.93288766e+02,
       1.94327695e+02, 1.94786431e+02, 1.94715911e+02, 1.95106672e+02,
       1.93837845e+02, 1.95440097e+02, 1.95108008e+02, 1.96753861e+02,
       1.98272559e+02, 1.97459916e+02, 1.98103472e+02, 1.98755763e+02,
       1.97663091e+02, 2.00956262e+02, 1.99109398e+02, 1.98874631e+02,
       2.01553242e+02, 2.00438983e+02, 2.00999416e+02, 2.01060545e+02,
       2.03034488e+02, 2.03789347e+02, 2.03856820e+02, 2.03808875e+02,
       2.04178274e+02, 2.04904582e+02, 2.04090833e+02, 2.04401765e+02,
       2.04564789e+02, 2.04966765e+02, 2.05453020e+02, 2.06753256e+02,
       2.06068870e+02, 2.08286001e+02, 2.06365205e+02, 2.07606203e+02,
       2.07383706e+02, 2.07495038e+02, 2.07839152e+02, 2.09050217e+02,
       2.09414077e+02, 2.11340490e+02, 2.10531582e+02, 2.12395882e+02,
       2.12429614e+02, 2.12868350e+02, 2.11361251e+02, 2.13770787e+02,
       2.13593502e+02, 2.12624435e+02, 2.12796799e+02, 2.14251523e+02,
       2.16070234e+02, 2.12990504e+02, 2.15302431e+02, 2.17666670e+02,
       2.15292855e+02, 2.17009281e+02, 2.18653458e+02, 2.15809341e+02,
       2.19168599e+02, 2.18115795e+02, 2.19862497e+02, 2.18787021e+02,
       2.19474854e+02, 2.21322773e+02, 2.20351910e+02, 2.22188437e+02,
       2.22029997e+02, 2.22932070e+02, 2.22706145e+02, 2.22860607e+02,
       2.25236078e+02, 2.25946301e+02, 2.23550833e+02, 2.25057222e+02,
       2.24053996e+02, 2.25702956e+02, 2.25835740e+02, 2.26127525e+02,
       2.26840740e+02, 2.27656209e+02, 2.27432696e+02, 2.27960124e+02,
       2.27506844e+02, 2.28263269e+02, 2.28905870e+02, 2.30235337e+02,
       2.29979456e+02, 2.31787024e+02, 2.31947678e+02, 2.31233668e+02,
       2.31662591e+02, 2.33792010e+02, 2.33743150e+02, 2.32047222e+02,
       2.35442827e+02, 2.32566382e+02, 2.33752025e+02, 2.35984940e+02,
       2.35862780e+02, 2.35951261e+02, 2.36120293e+02, 2.35371087e+02,
       2.36140940e+02, 2.38712518e+02, 2.38747117e+02, 2.36450465e+02,
       2.39683182e+02, 2.39458450e+02, 2.40368909e+02, 2.39814540e+02,
       2.38820200e+02, 2.39231077e+02, 2.41237298e+02, 2.41230894e+02,
       2.43129333e+02, 2.42762515e+02, 2.44162025e+02, 2.44505736e+02,
       2.44589419e+02, 2.46558913e+02, 2.46503444e+02, 2.46424440e+02,
       2.46615983e+02, 2.46360025e+02, 2.46860506e+02, 2.48225763e+02,
       2.47647761e+02, 2.49762315e+02, 2.48666325e+02, 2.48032559e+02,
       2.47331770e+02, 2.49591064e+02, 2.51800222e+02, 2.50921227e+02])
In [20]:
# Blindly try the regression - we get answers.
# Create variables with those values.
m, c = np.polyfit(x, y, 1)
# Have a look at m and c.
m, c
Out[20]:
(25.008475244304485, -32.342910013748686)
In [21]:
# Plot the line and the points.
plt.plot(x, y, 'k.')
plt.plot(x, m * x + c, 'r-')
Out[21]:
[<matplotlib.lines.Line2D at 0x1f868c3d3a0>]
In [22]:
# Create variables with those values.
a, b, c = np.polyfit(x, y, 2)

# Plot the line and the points.
plt.plot(x, y, 'k.')
plt.plot(x, a * x * x + b * x + c, 'r-')
Out[22]:
[<matplotlib.lines.Line2D at 0x1f8690c1e80>]

Note how the points below the line are bunched in a specific $x$ range.


Multiple linear regression

Let's try multiple linear regression using sklearn. https://scikit-learn.org/stable/

In [23]:
# Import linear_model from sklearn.
import sklearn.linear_model as lm
In [24]:
# Create a linear regression model instance.
m = lm.LinearRegression()
In [25]:
# Let's use pandas to read a csv file and organise our data.
import pandas as pd
In [26]:
# Read the iris csv from online.
df = pd.read_csv('https://datahub.io/machine-learning/iris/r/iris.csv')
In [27]:
df
Out[27]:
sepallength sepalwidth petallength petalwidth class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
... ... ... ... ... ...
145 6.7 3.0 5.2 2.3 Iris-virginica
146 6.3 2.5 5.0 1.9 Iris-virginica
147 6.5 3.0 5.2 2.0 Iris-virginica
148 6.2 3.4 5.4 2.3 Iris-virginica
149 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 5 columns

$$ petalwidth = t (sepallength) + u (sepalwidth) + v (petallength) + c $$
In [28]:
# Let's pretend we want to do linear regression on these variables to predict petal width.
x = df[['sepallength', 'sepalwidth', 'petallength']]
In [29]:
# Here's petal width.
y = df['petalwidth']
In [30]:
# Ask our model to fit the data.
m.fit(x, y)
Out[30]:
LinearRegression()
In [31]:
# Here's our intercept.
m.intercept_
Out[31]:
-0.248723586024453
In [32]:
# Here's our coefficients, in order.
m.coef_
Out[32]:
array([-0.21027133,  0.22877721,  0.52608818])
In [33]:
# See how good our fit is.
m.score(x, y)
Out[33]:
0.9380481344518986
In [34]:
# Calculating the score by hand.
t, u, v = m.coef_
c = m.intercept_

y_avg = y.mean()

u = ((y - (t * x['sepallength'] + u * x['sepalwidth'] + v * x['petallength'] + c))**2).sum()
v = ((y - y.mean())**2).sum()

1 - (u/v)
Out[34]:
0.9380481344518986

Using statsmodels

In [35]:
# Using statsmodels.
import statsmodels.api as sm

# Tell statmodels to include an intercept.
xwithc = sm.add_constant(x)

# Create a model.
msm = sm.OLS(y, xwithc)
# Fit the data.
rsm = msm.fit()
# Print a summary.
print(rsm.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:             petalwidth   R-squared:                       0.938
Model:                            OLS   Adj. R-squared:                  0.937
Method:                 Least Squares   F-statistic:                     736.9
Date:                Wed, 18 Nov 2020   Prob (F-statistic):           6.20e-88
Time:                        11:25:32   Log-Likelihood:                 36.809
No. Observations:                 150   AIC:                            -65.62
Df Residuals:                     146   BIC:                            -53.57
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
===============================================================================
                  coef    std err          t      P>|t|      [0.025      0.975]
-------------------------------------------------------------------------------
const          -0.2487      0.178     -1.396      0.165      -0.601       0.103
sepallength    -0.2103      0.048     -4.426      0.000      -0.304      -0.116
sepalwidth      0.2288      0.049      4.669      0.000       0.132       0.326
petallength     0.5261      0.024     21.536      0.000       0.478       0.574
==============================================================================
Omnibus:                        5.603   Durbin-Watson:                   1.577
Prob(Omnibus):                  0.061   Jarque-Bera (JB):                6.817
Skew:                           0.222   Prob(JB):                       0.0331
Kurtosis:                       3.945   Cond. No.                         90.0
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

End