KzO9K




Pay Notebook Creator: Rocky Singh0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [1]:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
In [2]:
import pandas as pd
dataset = pd.DataFrame([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
], columns=['x1', 'x2', 'y'])
dataset
Out[2]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
x1 x2 y
0 1 2 3
1 4 5 6
2 7 8 9
In [3]:
X = dataset[['x1', 'x2']].values
X
Out[3]:
array([[1, 2],
       [4, 5],
       [7, 8]])
In [4]:
y = dataset['y'].values
y
Out[4]:
array([3, 6, 9])
In [5]:
model.fit(X, y)
Out[5]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
In [6]:
model.predict([[8, 9]])
Out[6]:
array([10.])
In [7]:
model.predict([
    [0, 1],
    [8, 9],
])
Out[7]:
array([ 2., 10.])
In [8]:
# Save using pickle
from pickle import dump
dump(model, open('dummy-model.pkl', 'wb'))
In [9]:
# Save using joblib which is another option
import subprocess
subprocess.call('pip install joblib'.split())
from joblib import dump
dump(model, '/tmp/dummy-model.joblib')
Out[9]:
['/tmp/dummy-model.joblib']
In [10]:
from pickle import load
model = load(open('dummy-model.pkl', 'rb'))
model
Out[10]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
In [11]:
# Load using joblib which is another option
# import subprocess
# subprocess.call('pip install joblib'.split())
from joblib import load
model = load('/tmp/dummy-model.joblib')
model
Out[11]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
In [12]:
ls
'1. First Training Set - used as a training model.ipynb'
'2. Train model using dataset.ipynb'
 dummy-model.pkl
'First Training Set - used as a training model.ipynb'
 model.pkl
'predicts air pollution from tree count.ipynb'
'TOOL TEMPLATE - Predict Metrics by Zipcode 20190201.ipynb'
 Untitled1.ipynb
 Untitled.ipynb
 zipCodeAndTreeCountDataset.csv
 zipCodeAndTreeCountDataset.proj4
 zipcode-Copy1.csv
 zipcode.csv
In [19]:
import pandas as pd
t = pd.read_csv('zipCodeAndTreeCountDataset.csv')
t
Out[19]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
ZIPCODE BLDGZIP PO_NAME POPULATION AREA STATE COUNTY ST_FIPS CTY_FIPS URL SHAPE_AREA SHAPE_LEN Area in Sq Km Tree Count pollutant_sum Tree Count Per Sq Ft Tree Count Per Sq km wkt
0 11436 0 Jamaica 18681.0 2.269930e+07 NY Queens 36 81 http://www.usps.com/ 0.0 0.0 2.107694 347 545.928101 1.528682e-05 164.634885 POLYGON ((-73.80584847647393 40.68290932644246...
1 11213 0 Brooklyn 62426.0 2.963100e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 2.751168 570 762.125000 1.923661e-05 207.184697 POLYGON ((-73.9373976313981 40.6797295892508, ...
2 11212 0 Brooklyn 83866.0 4.197210e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 3.897041 596 988.006592 1.419991e-05 152.936545 POLYGON ((-73.90294132545436 40.67083977590008...
3 11225 0 Brooklyn 56527.0 2.369863e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 2.200345 393 587.316772 1.658324e-05 178.608324 POLYGON ((-73.9579731604348 40.67065695897565,...
4 11218 0 Brooklyn 72280.0 3.686880e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 3.423119 702 919.425720 1.904049e-05 205.076101 POLYGON ((-73.97208109564255 40.65059658727607...
5 11226 0 Brooklyn 106132.0 3.940860e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 3.658957 578 960.029358 1.466685e-05 157.968525 POLYGON ((-73.96190027968613 40.65487064531367...
6 11219 0 Brooklyn 92561.0 4.200274e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 3.899752 651 1029.860352 1.549899e-05 166.933702 POLYGON ((-73.98905871487798 40.64411892870269...
7 11210 0 Brooklyn 67067.0 4.788702e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 4.446157 802 1120.218262 1.674775e-05 180.380495 POLYGON ((-73.95839960027368 40.63632662546082...
8 11230 0 Brooklyn 80857.0 4.992670e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 4.635499 1119 1157.793823 2.241286e-05 241.397965 POLYGON ((-73.96451268730159 40.63668725312468...
9 11204 0 Brooklyn 77354.0 4.355518e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 4.043899 692 1035.828003 1.588789e-05 171.121992 POLYGON ((-73.98108387453883 40.63528985591707...
10 10471 0 Bronx 23477.0 8.965141e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 8.324106 257 1637.815796 2.866659e-06 30.874187 POLYGON ((-73.88191900199823 40.90666565537614...
11 10470 0 Bronx 14740.0 2.154346e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 2.000330 213 384.988373 9.886991e-06 106.482450 POLYGON ((-73.87790261636154 40.90555190505731...
12 10466 0 Bronx 68942.0 5.526249e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 5.131210 760 1169.651855 1.375255e-05 148.113218 POLYGON ((-73.84463568238043 40.90475077819391...
13 10467 0 Bronx 97932.0 6.933617e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 6.437914 665 1399.311523 9.590954e-06 103.294332 POLYGON ((-73.88010836796937 40.89519521108614...
14 10463 0 Bronx 70641.0 3.670338e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 3.407886 521 779.620972 1.419488e-05 152.880681 POLYGON ((-73.92064636495626 40.88723726005072...
15 10475 0 Bronx 40931.0 3.863330e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 3.587193 280 736.172729 7.247634e-06 78.055454 POLYGON ((-73.82722404306942 40.89092964709067...
16 10464 0 Bronx 4438.0 7.625748e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 7.080754 0 1035.359863 0.000000e+00 0.000000 POLYGON ((-73.81538709355007 40.88939419910433...
17 10469 0 Bronx 65101.0 6.804089e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 6.317708 1008 1498.477539 1.481462e-05 159.551535 POLYGON ((-73.85588386083408 40.88386140903516...
18 10468 0 Bronx 72877.0 3.444760e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 3.198451 447 921.837952 1.297623e-05 139.755172 POLYGON ((-73.88701149926719 40.88247162427221...
19 10463 0 Bronx 70641.0 3.119702e+06 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.289662 46 65.406052 1.474500e-05 158.805709 POLYGON ((-73.91544105058509 40.87559063041356...
20 10458 0 Bronx 79362.0 3.596881e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 3.339711 481 808.135498 1.337270e-05 144.024426 POLYGON ((-73.88480469568044 40.87845532589476...
21 10034 0 New York 39149.0 2.450389e+07 NY New York 36 61 http://www.usps.com/ 0.0 0.0 2.275160 237 546.599487 9.671933e-06 104.168505 POLYGON ((-73.92062094886884 40.87300145746758...
22 10033 0 New York 54284.0 1.615605e+07 NY New York 36 61 http://www.usps.com/ 0.0 0.0 1.500064 262 489.539307 1.621683e-05 174.659185 POLYGON ((-73.93213126543029 40.86945031866858...
23 10462 0 Bronx 75674.0 5.302251e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 4.923200 605 1186.329346 1.141025e-05 122.887564 POLYGON ((-73.87051512140579 40.8569744976658,...
24 10040 0 New York 41033.0 1.634074e+07 NY New York 36 61 http://www.usps.com/ 0.0 0.0 1.517216 226 403.592285 1.383046e-05 148.957014 POLYGON ((-73.93378898733546 40.86307153891337...
25 10453 0 Bronx 77576.0 2.574851e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 2.390729 461 656.619141 1.790395e-05 192.828195 POLYGON ((-73.89994956018222 40.85741964281875...
26 10465 0 Bronx 42012.0 1.084237e+08 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 10.067423 720 2197.093262 6.640612e-06 71.517805 POLYGON ((-73.8391534647799 40.83567473125792,...
27 10464 0 Bronx 4438.0 4.512531e+06 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 0.419009 0 75.528351 0.000000e+00 0.000000 POLYGON ((-73.77200435714323 40.85711605937528...
28 10464 0 Bronx 4438.0 1.158795e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 1.075987 146 166.049622 1.259929e-05 135.689310 POLYGON ((-73.79234936875058 40.85607420876183...
29 10461 0 Bronx 50549.0 6.282406e+07 NY Bronx 36 5 http://www.usps.com/ 0.0 0.0 5.833332 830 1337.014160 1.321150e-05 142.285751 POLYGON ((-73.83739648053641 40.8567843873281,...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
233 10120 1 New York 0.0 3.517927e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.003266 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.98938483295653 40.75000537600457...
234 10278 1 New York 0.0 2.067060e+05 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.019192 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.0047581751106 40.71507032820959,...
235 10155 1 New York 0.0 2.478469e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.002301 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.96843628441711 40.76110640977814...
236 10043 1 New York 0.0 3.826236e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.003552 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.00708359659971 40.70386978270482...
237 10081 1 New York 0.0 3.024051e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.002808 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.008714773259 40.7074893755098, -...
238 10096 1 New York 0.0 4.210611e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.003909 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.98301659173394 40.75485102653172...
239 10097 1 New York 0.0 6.582618e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.006112 0 41.044369 0.000000e+00 0.000000 POLYGON ((-73.98521646115499 40.76265216013989...
240 10196 1 New York 0.0 3.250244e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.003018 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.9824301517118 40.75680011816393,...
241 10196 1 New York 0.0 3.154825e+03 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.000293 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.98133175627783 40.75673242462563...
242 10275 1 New York 0.0 4.828042e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.004483 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.01450964009967 40.70555208010413...
243 10265 1 New York 0.0 1.722915e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.001600 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.00806000177114 40.70489246189538...
244 10045 1 New York 0.0 4.780899e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.004439 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.00910380250482 40.7087678695075,...
245 10047 1 New York 0.0 1.014978e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.000942 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.98633231048143 40.73957889013666...
246 10047 1 New York 0.0 1.014978e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.000942 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.98633231048143 40.73957889013666...
247 10080 1 New York 0.0 7.711145e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.007159 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.01287666105986 40.71051873297913...
248 10203 1 New York 0.0 3.722688e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.003456 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.01193550921882 40.70719828992588...
249 10259 1 New York 0.0 2.106431e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.001956 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.01036216523759 40.70876776311058...
250 10260 1 New York 0.0 5.251474e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.004876 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.00908463330282 40.70626028948341...
251 10285 1 New York 0.0 6.735039e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.006253 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.01476100812657 40.7130865913403,...
252 10286 1 New York 0.0 1.126441e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.001046 0 0.000000 0.000000e+00 0.000000 POLYGON ((-74.00936106898759 40.7064177757752,...
253 10035 0 New York 34884.0 2.349487e+07 NY New York 36 61 http://www.usps.com/ 0.0 0.0 2.181470 0 477.783386 0.000000e+00 0.000000 POLYGON ((-73.92741771645892 40.79839394486886...
254 11371 0 Flushing 0.0 3.055847e+07 NY Queens 36 81 http://www.usps.com/ 0.0 0.0 2.837371 17 644.740417 5.563105e-07 5.991461 POLYGON ((-73.88509634398015 40.77846404500434...
255 11361 0 Bayside 28496.0 5.016352e+07 NY Queens 36 81 http://www.usps.com/ 0.0 0.0 4.657903 932 1064.254639 1.857924e-05 200.090043 POLYGON ((-73.76641719803655 40.77595272856865...
256 10036 0 New York 23543.0 1.139511e+07 NY New York 36 61 http://www.usps.com/ 0.0 0.0 1.057995 127 441.509674 1.114513e-05 120.038330 POLYGON ((-73.98134106854107 40.75864499066453...
257 11414 0 Howard Beach 26148.0 6.392882e+07 NY Queens 36 81 http://www.usps.com/ 0.0 0.0 5.935843 683 1256.953125 1.068376e-05 115.063688 POLYGON ((-73.85068172809338 40.67164854067394...
258 10310 0 Staten Island 25003.0 5.346328e+07 NY Richmond 36 85 http://www.usps.com/ 0.0 0.0 4.963611 507 1160.368408 9.483145e-06 102.143369 POLYGON ((-74.12064635323901 40.64104377738636...
259 11693 0 Far Rockaway 11052.0 3.497516e+06 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 0.324747 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.8407585999925 40.62536368150901,...
260 11249 0 Brooklyn 28481.0 1.777221e+07 NY Kings 36 47 http://www.usps.com/ 0.0 0.0 1.650097 262 452.354218 1.474212e-05 158.778526 POLYGON ((-73.95805126567375 40.72442275986043...
261 10162 1 New York 0.0 2.103489e+04 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.001953 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.95132807302133 40.76931006893747...
262 10119 1 New York 0.0 1.263930e+05 NY New York 36 61 http://www.usps.com/ 0.0 0.0 0.011735 0 0.000000 0.000000e+00 0.000000 POLYGON ((-73.99354429600449 40.75145227487895...
<p>263 rows × 18 columns</p>
In [20]:
X1 = t[[
    'Tree Count Per Sq km']].values
X1
Out[20]:
array([[1.64634885e+02],
       [2.07184697e+02],
       [1.52936545e+02],
       [1.78608324e+02],
       [2.05076101e+02],
       [1.57968525e+02],
       [1.66933702e+02],
       [1.80380495e+02],
       [2.41397965e+02],
       [1.71121992e+02],
       [3.08741867e+01],
       [1.06482450e+02],
       [1.48113218e+02],
       [1.03294332e+02],
       [1.52880681e+02],
       [7.80554541e+01],
       [0.00000000e+00],
       [1.59551535e+02],
       [1.39755172e+02],
       [1.58805709e+02],
       [1.44024426e+02],
       [1.04168505e+02],
       [1.74659185e+02],
       [1.22887564e+02],
       [1.48957014e+02],
       [1.92828195e+02],
       [7.15178049e+01],
       [0.00000000e+00],
       [1.35689310e+02],
       [1.42285751e+02],
       [1.69102508e+02],
       [1.42147312e+02],
       [1.44164253e+02],
       [1.91163294e+02],
       [2.13722581e+02],
       [1.64176086e+02],
       [2.37681009e+02],
       [1.47116555e+02],
       [2.16337904e+02],
       [1.40654971e+02],
       [1.22676687e+02],
       [2.76279583e+02],
       [1.87971297e+02],
       [1.00237889e+02],
       [1.72565835e+02],
       [1.66295809e+02],
       [2.23624804e+02],
       [9.36125569e+01],
       [2.69920926e+02],
       [1.95941108e+02],
       [3.32156234e+01],
       [3.05043653e+02],
       [1.95739380e+02],
       [1.64156536e+02],
       [4.10556938e+01],
       [1.16844323e+02],
       [0.00000000e+00],
       [9.70612364e+01],
       [1.17643413e+02],
       [3.53087009e+02],
       [2.17352715e+02],
       [1.98127476e+02],
       [2.77956574e+02],
       [1.44384601e+02],
       [1.57753716e+02],
       [1.99883317e+02],
       [3.04788249e+02],
       [1.94207683e+02],
       [1.23094182e+02],
       [0.00000000e+00],
       [1.72331431e+02],
       [1.98122036e+02],
       [1.36737124e+02],
       [9.55316746e+01],
       [1.20397296e+02],
       [1.68798901e+02],
       [6.83354458e+01],
       [1.36376615e+02],
       [8.55136010e+01],
       [2.47135620e+02],
       [3.44122054e+01],
       [1.37456482e+02],
       [8.95779715e+01],
       [1.84874469e+02],
       [1.91173340e+02],
       [2.13007919e+02],
       [1.66770435e+02],
       [1.30104683e+02],
       [1.06647722e+02],
       [2.79328514e+02],
       [1.84378930e+02],
       [1.38321997e+02],
       [1.34835327e+02],
       [9.24867970e+01],
       [1.74727158e+02],
       [1.72211451e+02],
       [1.18900303e+02],
       [1.15228491e+02],
       [1.77894755e+02],
       [1.38093153e+02],
       [8.58851961e+01],
       [1.54010318e+02],
       [5.64680520e+01],
       [1.55754178e+02],
       [9.30771324e+01],
       [1.15630919e+01],
       [5.11281214e+01],
       [1.49435298e+02],
       [1.70357861e+02],
       [0.00000000e+00],
       [1.82360070e+02],
       [1.55771647e+02],
       [1.91767224e+02],
       [0.00000000e+00],
       [0.00000000e+00],
       [1.37041797e+02],
       [2.47269773e+02],
       [2.00188372e+02],
       [1.54531046e+02],
       [1.63008965e+02],
       [1.40976393e+02],
       [1.87354032e+02],
       [1.50279371e+02],
       [0.00000000e+00],
       [5.64752926e+01],
       [3.23230959e-01],
       [1.30533925e+02],
       [3.81382232e+01],
       [1.11866267e+02],
       [1.66597476e+02],
       [8.93975044e+01],
       [6.61079702e+01],
       [8.55561908e+01],
       [1.33020344e+02],
       [1.96989459e+01],
       [1.80436518e+02],
       [9.71540130e+01],
       [5.56531773e+01],
       [1.33735011e+02],
       [0.00000000e+00],
       [9.52012223e+01],
       [1.63565204e+02],
       [1.16174657e+02],
       [1.10355702e+02],
       [9.61795036e+01],
       [1.52652085e+02],
       [0.00000000e+00],
       [1.35114481e+02],
       [1.13391998e+02],
       [6.84589043e+01],
       [1.15072025e+02],
       [1.17773203e+02],
       [6.89570440e+01],
       [1.68733137e+02],
       [1.26096614e+00],
       [2.03850043e+02],
       [9.27257051e+01],
       [1.91430140e+02],
       [3.03721527e+02],
       [2.72412233e+02],
       [3.00118683e+02],
       [2.52511143e+02],
       [1.62378555e+02],
       [1.66234323e+02],
       [2.45847092e+02],
       [1.98395933e+02],
       [1.69315099e+02],
       [2.02549449e+02],
       [2.40165293e+02],
       [1.22991922e+02],
       [2.12662392e+02],
       [1.71269448e+02],
       [1.40366467e+02],
       [1.06443608e+02],
       [1.74184849e+02],
       [1.73704244e+02],
       [1.74200420e+02],
       [1.21368830e+02],
       [6.62616579e+01],
       [2.13281258e+02],
       [1.90909691e+02],
       [1.36693753e+02],
       [1.47654626e+02],
       [2.08262897e+02],
       [1.62792992e+02],
       [2.09025267e+02],
       [0.00000000e+00],
       [1.37287914e+02],
       [5.22499578e+02],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [1.50171769e+02],
       [1.84465974e+02],
       [0.00000000e+00],
       [2.27783297e+02],
       [2.91110841e+02],
       [6.80995023e+01],
       [7.88759661e+01],
       [2.06749856e+02],
       [1.64465537e+02],
       [1.47960650e+02],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [1.31284154e+02],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [0.00000000e+00],
       [5.99146103e+00],
       [2.00090043e+02],
       [1.20038330e+02],
       [1.15063688e+02],
       [1.02143369e+02],
       [0.00000000e+00],
       [1.58778526e+02],
       [0.00000000e+00],
       [0.00000000e+00]])
In [21]:
y = t['pollutant_sum']
y
Out[21]:
0       545.928101
1       762.125000
2       988.006592
3       587.316772
4       919.425720
5       960.029358
6      1029.860352
7      1120.218262
8      1157.793823
9      1035.828003
10     1637.815796
11      384.988373
12     1169.651855
13     1399.311523
14      779.620972
15      736.172729
16     1035.359863
17     1498.477539
18      921.837952
19       65.406052
20      808.135498
21      546.599487
22      489.539307
23     1186.329346
24      403.592285
25      656.619141
26     2197.093262
27       75.528351
28      166.049622
29     1337.014160
          ...     
233       0.000000
234       0.000000
235       0.000000
236       0.000000
237       0.000000
238       0.000000
239      41.044369
240       0.000000
241       0.000000
242       0.000000
243       0.000000
244       0.000000
245       0.000000
246       0.000000
247       0.000000
248       0.000000
249       0.000000
250       0.000000
251       0.000000
252       0.000000
253     477.783386
254     644.740417
255    1064.254639
256     441.509674
257    1256.953125
258    1160.368408
259       0.000000
260     452.354218
261       0.000000
262       0.000000
Name: pollutant_sum, Length: 263, dtype: float64
In [22]:
from sklearn.model_selection import cross_val_score
models = []
scores = []

def train(model, X):
    model.fit(X, y)
    models.append(model)
    score = cross_val_score(
        model, X, y, cv=3,
        scoring='neg_mean_absolute_error',
    ).mean()
    scores.append(score)
    return score
In [23]:
from sklearn.linear_model import LinearRegression
train(LinearRegression(), X1)
Out[23]:
-677.2937511233264
In [ ]:
 
In [24]:
import numpy as np
best_index = np.argmax(scores)
best_index
Out[24]:
0
In [25]:
best_model = models[best_index]
best_model
Out[25]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
In [26]:
import pickle
pickle.dump(best_model, open('model.pkl', 'wb'))
In [ ]:
 
In [ ]: