Skip to content

Instantly share code, notes, and snippets.

@ijstokes
Created September 22, 2017 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijstokes/87cd5477cc76a63473eaeaaf8ff55047 to your computer and use it in GitHub Desktop.
Save ijstokes/87cd5477cc76a63473eaeaaf8ff55047 to your computer and use it in GitHub Desktop.
Data Science with Anaconda
name: mpg_prediction
description: Predict MPG using a linear regression model
commands:
default:
bokeh_app: .
packages:
- bokeh=0.12.5=py36_1
- pandas=0.20.1=np112py36_0
- scikit-learn=0.18.1=np112py36_1
import pandas as pd
from sklearn.externals.joblib import load
from bokeh.charts import Scatter
from bokeh.io import curdoc, show, output_notebook, output_file
mpg_model = load('mpg_linear_regression_model.pkl')
data = pd.read_csv('mpg_data.csv')
# augment the mpg DataFrame with the prediction
data['prediction'] = mpg_model.predict(data['cyl displ hp weight accel yr'.split()])
s1 = Scatter(data=data,
x='mpg', y='prediction', color='origin',
height=300, width=600,
title='Fuel efficiency predictions of selected vehicles from 1970-1982',
tools='hover, box_zoom, lasso_select, save, reset',
tooltips = [
('model','@name'),
('HP', '@hp'),
('actual MPG', '@mpg'),
('predicted MPG', '@prediction')
])
s2 = Scatter(data=data,
x='yr', y='mpg', color='origin',
height=300, width=600,
title='Fuel efficiency of selected vehicles from 1970-1982',
tools='hover, box_zoom, save, reset',
tooltips = [
('model','@name'),
('HP', '@hp'),
('cyl', '@cyl'),
('weight', '@weight')
])
curdoc().add_root(s1)
curdoc().add_root(s2)
curdoc().title = "MPG Prediction"
index mpg cyl displ hp weight accel yr origin name make L100 force mpg_lr lr mpg_rf rf
220 17.0 8 260.0 110 4060 19.0 77 American oldsmobile cutlass supreme oldsmobile 13.810741687979538 213.68421052631578 16.935134707947125 0.06486529205287539 15 2.0
245 39.4 4 85.0 70 2070 18.6 78 Asian datsun b210 gx datsun 5.958949459280512 111.29032258064515 31.443861657060292 7.956138342939706 31 8.399999999999999
134 16.0 8 302.0 140 4141 14.0 74 American ford gran torino ford 14.67391304347826 295.7857142857143 13.925139893665776 2.0748601063342242 14 2.0
147 24.0 4 120.0 97 2489 15.0 74 Asian honda civic honda 9.782608695652174 165.93333333333334 25.331002320874692 -1.331002320874692 23 1.0
390 28.0 4 120.0 79 2625 18.6 82 American ford ranger ford 8.385093167701863 141.1290322580645 30.865171729623757 -2.865171729623757 31 -3.0
33 16.0 6 225.0 105 3439 15.5 71 American plymouth satellite custom plymouth 14.67391304347826 221.8709677419355 16.129122971574677 -0.12912297157467734 18 -2.0
266 27.2 4 119.0 97 2300 14.7 78 Asian datsun 510 datsun 8.631713554987211 156.4625850340136 29.843168062398785 -2.643168062398786 23 4.199999999999999
226 18.5 6 250.0 98 3525 19.0 77 American ford granada ford 12.690951821386603 185.52631578947367 20.79424901021942 -2.2942490102194206 17 1.5
263 17.5 8 318.0 140 4080 13.7 78 American dodge magnum xe dodge 13.416149068322982 297.8102189781022 17.516826628363983 -0.016826628363983076 15 2.5
327 44.6 4 91.0 67 1850 13.8 80 Asian honda civic 1500 gl honda 5.264184051472021 134.05797101449275 33.59626779629903 11.00373220370097 36 8.600000000000001
188 14.5 8 351.0 152 4215 12.8 76 American ford gran torino ford 16.19190404797601 329.296875 15.164846888127908 -0.6648468881279079 16 -1.5
273 17.0 6 163.0 125 3140 13.6 78 European volvo 264gl volvo 13.810741687979538 230.88235294117646 24.056810033319497 -7.056810033319497 20 -3.0
9 15.0 8 390.0 190 3850 8.5 70 American amc ambassador dpl amc 15.652173913043477 452.94117647058823 13.438618526273604 1.5613814737263958 15 0.0
236 33.5 4 98.0 83 2075 15.9 77 American dodge colt m/m dodge 7.008436080467229 130.50314465408806 30.484989103008967 3.0150108969910328 30 3.5
211 13.0 8 350.0 145 4055 12.0 76 American chevy c10 chevrolet 18.06020066889632 337.9166666666667 15.961756215780934 -2.961756215780934 15 -2.0
172 18.0 6 171.0 97 2984 14.5 75 American ford pinto ford 13.043478260869565 205.79310344827587 22.14058281236544 -4.140582812365441 19 -1.0
217 25.5 4 122.0 96 2300 15.5 77 American plymouth arrow gs plymouth 9.207161125319693 148.38709677419354 29.182499589231302 -3.682499589231302 24 1.5
102 11.0 8 400.0 150 4997 14.0 73 American chevrolet impala chevrolet 21.34387351778656 356.92857142857144 7.264440015979051 3.7355599840209486 13 -2.0
72 13.0 8 307.0 130 4098 14.0 72 American chevrolet chevelle concours (sw) chevrolet 18.06020066889632 292.7142857142857 12.345921118319662 0.6540788816803378 14 -1.0
11 14.0 8 340.0 160 3609 8.0 70 American plymouth 'cuda 340 plymouth 16.770186335403725 451.125 14.11414099329033 -0.11414099329033078 15 -1.0
162 21.0 6 231.0 110 3039 15.0 75 American buick skyhawk buick 11.180124223602483 202.6 22.318887991098553 -1.3188879910985527 18 3.0
181 25.0 4 116.0 81 2220 16.9 76 European opel 1900 opel 9.391304347826088 131.36094674556213 28.785537862744423 -3.785537862744423 26 -1.0
336 25.8 4 156.0 92 2620 14.4 81 American dodge aries wagon (sw) dodge 9.10010111223458 181.94444444444443 29.705539830330995 -3.9055398303309943 26 -0.1999999999999993
341 35.1 4 81.0 60 1760 16.1 81 Asian honda civic 1300 honda 6.688963210702341 109.31677018633539 35.29035858954299 -0.19035858954298845 39 -3.8999999999999986
343 37.0 4 85.0 65 1975 19.4 81 Asian datsun 210 mpg datsun 6.345475910693302 101.80412371134021 34.537083729910776 2.4629162700892238 31 6.0
95 13.0 8 360.0 175 3821 11.0 73 American amc ambassador brougham amc 18.06020066889632 347.3636363636364 16.04079520729923 -3.0407952072992295 15 -2.0
240 21.5 4 121.0 110 2600 12.8 77 European bmw 320i bmw 10.920121334681497 203.125 26.860165191401745 -5.3601651914017445 24 -2.5
307 41.5 4 98.0 76 2144 14.7 80 European vw rabbit volkswagen 5.657412257726558 145.85034013605443 31.90293837216299 9.597061627837011 30 11.5
177 22.0 4 121.0 98 2945 14.5 75 European volvo 244dl volvo 10.67193675889328 203.10344827586206 22.700108580674314 -0.7001085806743141 23 -1.0
54 27.0 4 97.0 60 1834 19.0 71 European volkswagen model 111 volkswagen 8.695652173913043 96.52631578947368 27.39754987460965 -0.3975498746096484 26 1.0
261 17.7 6 231.0 165 3445 13.4 78 American buick regal sport coupe (turbo) buick 13.264554163596168 257.08955223880594 23.102371415203567 -5.402371415203568 18 -0.3000000000000007
362 28.0 4 112.0 88 2605 19.6 82 American chevrolet cavalier chevrolet 8.385093167701863 132.90816326530611 31.484719940644208 -3.484719940644208 27 1.0
36 18.0 6 232.0 100 3288 15.5 71 American amc matador amc 13.043478260869565 212.1290322580645 17.094522398922788 0.9054776010772123 19 -1.0
75 18.0 4 121.0 112 2933 14.5 72 European volvo 145e (sw) volvo 13.043478260869565 202.27586206896552 20.836821128227992 -2.8368211282279923 19 -1.0
381 38.0 6 262.0 85 3015 17.0 82 American oldsmobile cutlass ciera (diesel) oldsmobile 6.178489702517162 177.35294117647058 27.733491527273312 10.266508472726688 18 20.0
316 29.8 4 134.0 90 2711 15.5 80 Asian toyota corona liftback toyota 7.878611030055442 174.90322580645162 28.37221631268068 1.4277836873193195 27 2.8000000000000007
144 28.0 4 90.0 75 2125 14.5 74 American dodge colt dodge 8.385093167701863 146.55172413793105 27.18683664934372 0.8131633506562785 24 4.0
321 27.9 4 156.0 105 2800 14.4 80 American dodge colt dodge 8.41514726507714 194.44444444444443 27.98877591888282 -0.08877591888282055 26 1.8999999999999986
171 24.0 4 119.0 97 2545 17.0 75 Asian datsun 710 datsun 9.782608695652174 149.7058823529412 26.118575016519603 -2.1185750165196033 23 1.0
26 10.0 8 307.0 200 4376 15.0 70 American chevy c20 chevrolet 23.478260869565215 291.73333333333335 11.08719174637109 -1.08719174637109 11 -1.0
212 13.0 8 302.0 130 3870 15.0 76 American ford f108 ford 18.06020066889632 258.0 17.40366810231018 -4.403668102310181 17 -4.0
282 20.6 6 225.0 110 3360 16.6 79 American dodge aspen 6 dodge 11.397214014352047 202.40963855421685 23.454666148611498 -2.8546661486114964 20 0.6000000000000014
59 20.0 4 140.0 90 2408 19.5 72 American chevrolet vega chevrolet 11.739130434782608 123.48717948717949 25.056728697070692 -5.056728697070692 21 -1.0
295 25.4 5 183.0 77 3530 20.1 79 European mercedes benz 300d mercedes-benz 9.243409791167409 175.62189054726366 21.98506799748684 3.414932002513158 27 -1.6000000000000014
24 21.0 6 199.0 90 2648 15.0 70 American amc gremlin amc 11.180124223602483 176.53333333333333 20.556182068559576 0.4438179314404245 18 3.0
13 14.0 8 455.0 225 3086 10.0 70 American buick estate wagon (sw) buick 16.770186335403725 308.6 20.540657379863216 -6.540657379863216 15 -1.0
337 23.5 6 173.0 110 2725 12.6 81 American chevrolet citation chevrolet 9.990749306197964 216.26984126984127 28.84321132347065 -5.343211323470651 26 -2.5
335 26.6 4 151.0 84 2635 16.4 81 American buick skylark buick 8.826413860738802 160.6707317073171 29.745382671726002 -3.1453826717260007 27 -0.3999999999999986
79 22.0 4 122.0 86 2395 16.0 72 American ford pinto (sw) ford 10.67193675889328 149.6875 24.295088991612214 -2.2950889916122144 21 1.0
150 19.0 6 225.0 95 3264 16.0 75 American plymouth valiant custom plymouth 12.356979405034323 204.0 20.390744640426366 -1.390744640426366 20 -1.0
20 25.0 4 110.0 87 2672 17.5 70 European peugeot 504 peugeot 9.391304347826088 152.68571428571428 20.98390061559871 4.01609938440129 25 0.0
344 37.7 4 89.0 62 2050 17.3 81 Asian toyota tercel toyota 6.227655403067696 118.4971098265896 33.47092780200836 4.229072197991641 34 3.700000000000003
229 15.5 8 400.0 190 4325 12.2 77 American chrysler cordoba chrysler 15.147265077138849 354.5081967213115 16.2739926879153 -0.7739926879152996 16 -0.5
260 19.2 8 305.0 145 3425 13.2 78 American chevrolet monte carlo landau chevrolet 12.228260869565219 259.469696969697 22.376928823577135 -3.1769288235771356 17 2.1999999999999993
275 16.2 6 163.0 133 3410 15.8 78 European peugeot 604sl peugeot 14.492753623188406 215.82278481012656 22.763637081286735 -6.5636370812867355 20 -3.8000000000000007
210 16.5 8 350.0 180 4380 12.1 76 American cadillac seville cadillac 14.229249011857707 361.9834710743802 14.677417270238134 1.8225827297618657 16 0.5
42 13.0 8 400.0 170 4746 12.0 71 American ford country squire (sw) ford 18.06020066889632 395.5 7.73764472268374 5.26235527731626 13 0.0
48 23.0 4 122.0 86 2220 14.0 71 American mercury capri 2000 mercury 10.207939508506616 158.57142857142858 24.38242976989766 -1.3824297698976586 28 -5.0
15 22.0 6 198.0 95 2833 15.5 70 American plymouth duster plymouth 10.67193675889328 182.7741935483871 19.44958425801296 2.5504157419870417 18 4.0
202 32.0 4 85.0 70 1990 17.0 76 Asian datsun b-210 datsun 7.33695652173913 117.05882352941177 30.11714922048299 1.8828507795170104 31 1.0
227 16.0 8 400.0 180 4220 11.1 77 American pontiac grand prix lj pontiac 14.67391304347826 380.1801801801802 16.51310579996593 -0.5131057999659312 15 1.0
41 12.0 8 383.0 180 4955 11.5 71 American dodge monaco (sw) dodge 19.565217391304348 430.8695652173913 6.386348062004945 5.613651937995055 12 0.0
31 25.0 4 113.0 95 2228 14.0 71 Asian toyota corona toyota 9.391304347826088 159.14285714285714 24.59175118349445 0.40824881650555156 24 1.0
269 23.2 4 156.0 105 2745 16.7 78 American plymouth sapporo plymouth 10.119940029985006 164.37125748502996 27.268775530080053 -4.068775530080053 19 4.199999999999999
286 18.2 8 318.0 135 3830 15.2 79 American dodge st. regis dodge 12.900143334925945 251.97368421052633 20.301930673367554 -2.101930673367555 17 1.1999999999999993
329 29.8 4 89.0 62 1845 15.3 80 European vokswagen rabbit volkswagen 7.878611030055442 120.58823529411764 33.77916119322241 -3.9791611932224065 39 -9.2
300 34.5 4 105.0 70 2150 14.9 79 American plymouth horizon tc3 plymouth 6.805293005671078 144.29530201342283 30.926123099155607 3.573876900844393 34 0.5
252 20.2 6 200.0 85 2965 15.8 78 American ford fairmont (auto) ford 11.622901420576842 187.65822784810126 24.595438786828854 -4.395438786828855 20 0.1999999999999993
272 20.3 5 131.0 103 2830 15.9 78 European audi 5000 audi 11.565645748554294 177.98742138364778 26.233177544547065 -5.933177544547064 20 0.3000000000000007
191 24.0 6 200.0 81 3012 17.6 76 American ford maverick ford 9.782608695652174 171.13636363636363 22.898633112708804 1.1013668872911957 19 5.0
178 25.0 4 121.0 115 2671 13.5 75 European saab 99le saab 9.391304347826088 197.85185185185185 25.043473438690295 -0.043473438690295296 24 1.0
52 31.0 4 71.0 65 1773 19.0 71 Asian toyota corolla 1200 toyota 7.5736325385694245 93.3157894736842 27.96854098891463 3.0314590110853707 35 -4.0
82 28.0 4 98.0 80 2164 15.0 72 American dodge colt (sw) dodge 8.385093167701863 144.26666666666668 25.57556151564688 2.4244384843531215 27 1.0
91 13.0 8 351.0 158 4363 13.0 73 American ford ltd ford 18.06020066889632 335.61538461538464 11.91524445894694 1.0847555410530596 13 0.0
274 21.6 4 121.0 115 2795 15.7 78 European saab 99gle saab 10.869565217391303 178.0254777070064 26.963536451564988 -5.363536451564986 19 2.6000000000000014
318 37.0 4 119.0 92 2434 15.0 80 Asian datsun 510 hatchback datsun 6.345475910693302 162.26666666666668 30.353193428101957 6.646806571898043 35 2.0
380 25.0 6 181.0 110 2945 16.4 82 American buick century limited buick 9.391304347826088 179.57317073170734 28.799862393690777 -3.799862393690777 22 3.0
114 15.0 8 350.0 145 4082 13.0 73 American chevrolet monte carlo s chevrolet 15.652173913043477 314.0 13.578719748777939 1.4212802512220613 13 2.0
189 22.0 6 225.0 100 3233 15.4 76 American plymouth valiant plymouth 10.67193675889328 209.93506493506493 21.44849094663827 0.5515090533617304 20 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment