In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
import itertools
import pickle
import sklearn

%matplotlib inline

In [2]:
f = open('/Users/Raafe/Desktop/DataDriven/Project/Stever.csv')
data = pd.read_csv(f,sep=',', header='infer', parse_dates=[1])

In [3]:
data.columns=['Time','Power','Temperature']
data1=data

In [4]:
data1['Temperature'] = pd.to_numeric(data1['Temperature'], errors='coerce')
data1['Power'] = pd.to_numeric(data1['Power'], errors='coerce')
data1['Time']=pd.to_datetime(data1['Time'],errors='coerce')

In [5]:
data1=data.set_index('Time',drop=False)
data1.dtypes
data2=data1.drop(data1.columns[0],axis=1)
data3=data2.resample('H').mean()
data4=data3.interpolate()
df=data4

In [6]:
fig=plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(df.Power)
ax1.set_ylabel('Load in W')

ax2 = ax1.twinx()
ax2.plot(df.Temperature, 'r-')
ax2.set_ylabel('Temperature in F', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')



In [7]:
plt.scatter(df.Temperature,df.Power)
plt.xlabel('Temperature in Fahrenheit')
plt.ylabel('Power in Watts')


Out[7]:
<matplotlib.text.Text at 0x11c95dd68>

In [8]:
df['Weekday']=df.index.dayofweek
df['Hour']=df.index.hour

In [9]:
Value=[]

for row in df['Power']:
    if row>98790.240626:
        Value.append(1)
    else:
        Value.append(-1)
        
df['Value']=Value
$$Mishra Ji's Code$$

In [10]:
d5 = df.set_index('Hour',append=True)
d6 = d5.copy()
d7 = d6.drop(d6.columns[1],axis =1)
d8 = d7.unstack('Hour')
d8['Time'] = d8.index
d8['day'] = d8['Time'].dt.dayofyear
d8 = d8.drop(d8.columns[24], axis = 1)
d9 = d8['Power'].groupby(d8['day']).mean()
d9


Out[10]:
Hour 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
day
1 71214.677399 70328.358986 70833.823829 69860.230991 71888.767840 71254.649480 70010.581186 70142.350652 71578.297918 67630.607814 ... 67751.813413 68072.907813 68871.170508 70257.633530 70978.553582 70416.924025 70295.226238 71172.590561 70811.005925 71051.585483
2 71597.109701 71369.165300 71386.316733 70331.883791 70700.402150 71882.197853 71226.337696 72834.453973 72884.980861 68726.851889 ... 66742.535223 66967.380795 66947.958986 69863.836069 70230.442775 69955.883277 69377.614606 69853.748243 70378.456837 70336.782814
3 69907.296095 70968.215040 69665.550392 70532.677866 70233.050392 70485.543165 70448.915952 69790.700131 68904.195444 67094.231121 ... 70925.393231 69259.836720 72132.218361 73075.061589 72503.731252 71123.424936 71470.354232 70814.761850 71596.750522 72673.724155
4 72291.731252 74557.426694 72340.636981 72592.158335 72337.121225 74473.451564 73448.570314 73184.046225 74636.132284 72153.707748 ... 71570.651824 70164.377344 71693.130470 72706.077215 75277.333986 73972.448764 75166.888023 75175.664975 76995.439194 73955.852866
5 77517.882944 75537.326825 76494.012110 76986.974610 76606.411330 76284.357684 76454.691407 76639.663543 73777.661590 73383.217059 ... 72463.353581 71333.075327 72383.495119 77618.872137 74446.113674 73738.890105 74929.440496 74080.265887 72767.805600 74598.545183
6 73571.332814 73265.958725 73320.174741 74771.225262 72935.984115 74359.246355 74492.352085 73971.633399 72231.538998 73648.862371 ... 71621.095444 72571.148699 70530.022267 76133.240105 78461.604949 76774.598308 76581.801043 78422.948829 75849.613933 75242.499220
7 75595.998829 75640.156772 76367.264715 78014.139844 75265.381642 74796.529298 74986.224350 74791.587501 72123.548793 74973.154689 ... 71577.704364 72972.953257 72949.520574 76047.781511 77892.999480 77959.021616 80077.558725 78865.868491 77603.019533 78888.396055
8 79484.797424 76818.609767 74942.274741 73401.015105 73734.755275 73315.793751 73318.049351 75876.028907 74258.508985 74013.390887 ... 71356.722853 71054.671747 76511.814844 75492.389845 78921.284637 78511.404689 77702.347527 76108.577866 78550.869403 75850.025262
9 75361.525131 74488.178256 71304.080600 72575.195053 70339.986460 69878.951238 69783.929820 69626.453842 73062.827605 71124.208270 ... 81275.646358 72230.730600 74066.317710 78371.017709 80398.559767 84749.977086 83338.622918 83734.917580 83108.740756 80933.590757
10 82606.600486 81145.652735 77889.379689 73024.314975 70715.519272 70747.567189 71003.230340 73513.651954 76454.091799 74556.078127 ... 88643.864715 90975.253125 93626.382424 93615.295971 95315.750004 97532.931515 101116.219930 96651.128778 97696.130474 95576.450394
11 92322.724998 86910.286592 82969.235158 80007.940235 79744.935157 78713.628777 82226.456121 87283.353127 92945.723046 93656.674482 ... 87772.432684 90477.053647 91340.257946 100432.048708 103655.931648 99681.861467 101020.997401 101768.575005 100766.034643 98032.782168
12 91553.638674 90869.451564 86387.148699 81823.415105 79354.627996 81670.588934 78522.351042 83280.387371 91501.836457 86581.561460 ... 89887.860159 90203.399742 93574.453660 97435.792710 98088.793752 100024.141021 101381.972661 101316.074612 101295.375129 97447.391156
13 96529.784641 91060.397399 87503.590106 83653.881251 81577.418100 84282.396355 84600.688543 89406.676825 86924.678389 87191.065237 ... 91358.559506 94621.027479 98803.211593 102846.737120 101481.441536 101750.711462 103050.575878 101770.650200 100110.594533 97660.349091
14 95906.294798 90847.403910 85249.822266 80632.412371 79605.200392 79103.088673 78957.808725 82429.806252 88387.966277 86132.711460 ... 87580.181165 88704.930990 92009.693364 95079.759897 93959.548571 97517.170971 96460.222918 97731.815495 98056.152996 95210.215971
15 94623.189716 90757.267971 84249.289194 83531.893751 79315.183204 77974.075392 80238.310418 82404.096226 86338.180860 87229.279168 ... 85211.256250 85892.539325 90318.704037 88594.630340 87938.788283 85902.692189 89196.926693 92791.865104 92999.024221 93016.416928
16 89841.239716 87222.303125 85460.915496 81185.256252 79171.463673 75924.413673 75980.819793 77968.077735 80437.634377 84664.297527 ... 93304.784769 93102.043625 95575.816797 97231.303646 95894.398960 94615.147137 95222.083859 96090.141277 94580.848052 92921.220707
17 90134.059508 87830.250911 86585.273178 83237.291407 82108.370053 79544.781903 79869.669532 78281.612111 80569.478908 81641.317060 ... 94075.172399 96098.353649 98003.595058 98636.554434 101230.299871 97488.629822 98632.819017 99186.601305 98546.815759 98769.415237
18 97013.168755 93883.026305 92118.964453 90079.411980 84957.877605 82398.123179 83891.849871 83010.570314 85630.826694 88388.565888 ... 99485.927536 96950.306643 98996.529818 106159.047134 108197.661335 107469.474223 108069.510038 104586.654824 108492.476566 105303.624095
19 102375.254038 94982.115108 90933.831123 88190.974090 85412.442188 87160.724741 83733.652345 90141.910809 96088.079173 94522.399742 ... 93388.221360 96052.607814 99804.690240 103105.902739 104262.075128 103669.047535 103938.002999 103643.376175 102852.806777 101778.883857
20 99489.599739 98477.194273 90520.858205 89429.939066 84297.270834 85027.857553 84194.618645 85711.943230 91810.286591 95190.121356 ... 89824.233075 93766.340755 99088.501309 97880.452871 97725.222011 101667.752125 101337.144273 99795.706379 101576.007164 100942.703520
21 100092.246226 93798.048572 89065.558987 84562.108985 83017.100261 80994.325652 81032.765756 83964.756382 89468.864585 92038.963673 ... 90389.213545 93290.680473 93250.069791 100630.914587 100933.574876 97771.203908 103935.764854 98716.851435 100868.914460 102295.676645
22 100845.787369 96280.765498 92311.207033 87596.229038 84879.805079 84065.846615 84428.990366 87242.737631 89430.679429 93441.645057 ... 91768.257425 96326.910160 101348.755996 105000.979170 99933.902868 100312.635421 96591.564070 97564.273438 99351.464721 98280.664327
23 97614.552604 94168.498809 91192.080642 87876.942840 85290.487241 83778.740626 81860.153257 82943.954559 81592.505861 81624.642579 ... 93945.531643 92969.052997 93454.763805 95676.807948 100547.873443 100821.606384 100188.874874 98887.700132 100339.252869 96572.342314
24 94697.728388 95710.505473 92514.466408 87701.169402 88017.051173 86303.924611 82462.717449 82404.592970 82883.252085 86314.721877 ... 94840.053649 92361.323806 94998.892189 96977.139065 97885.102091 99563.183862 97268.566274 101852.710683 101044.132687 100425.020445
25 95710.941279 93651.903520 86191.402475 82800.036329 80504.242710 81351.003647 81320.568230 84550.682033 87344.072005 87772.868267 ... 86233.805730 89709.759768 90219.872918 93446.137893 97191.613415 96961.614198 96188.350135 97334.068753 96708.539326 97346.883078
26 96373.228908 92875.669012 86936.369534 81578.615756 79940.935938 78096.222527 76251.530730 79956.130600 85358.581642 81315.192579 ... 83813.372267 85131.835808 89629.667189 92685.904035 93926.673962 95948.669798 94875.006384 98412.863289 98191.595444 99041.261331
27 93207.819402 89070.781642 85843.188413 85026.721486 81008.699741 77762.370444 79307.170314 81386.844012 85865.116016 86432.291797 ... 86697.615237 90098.930343 92583.984638 96160.315635 97461.238674 97410.064586 98707.153131 98941.678649 96883.175001 94173.823180
28 93171.067710 87307.705991 85084.277084 82448.075392 76899.925913 77254.176434 72976.265236 75741.630991 82360.223385 86027.296096 ... 86687.011980 88967.285940 87821.133337 88099.474350 91654.122918 93761.146619 95394.960289 95861.805735 95528.406901 99659.153557
29 97676.559376 89136.482033 86011.417058 83448.206642 80018.777084 78805.551954 77582.112241 80244.549351 86907.310157 87036.454038 ... 88029.276956 94412.749091 95038.975782 98050.991279 97469.437114 98729.730214 98559.143676 99659.701830 97694.703913 94794.616931
30 94427.934253 93424.387112 89479.972397 86371.256904 83050.324741 80018.810548 78464.330992 79164.896486 81381.980340 81975.389454 ... 83595.360679 85160.795055 86191.311591 90052.134507 90807.697789 92156.990365 94128.386853 92749.624613 94937.019274 91760.030862
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
336 89908.794011 90671.896356 85879.077085 83633.463283 80274.125001 78052.168752 78178.926694 82966.594662 85582.406779 85521.708074 ... 83487.018751 85925.714456 87434.706381 89054.473830 91269.678389 92234.200262 93588.829043 92259.375392 93688.569401 94549.257152
337 95653.432687 89619.337631 84162.147788 83236.239194 82345.203256 80300.980731 81019.726303 79850.278386 84043.424611 83845.213413 ... 88372.256123 89513.566147 92238.472138 95362.131490 93216.774350 94936.899482 95487.069926 96093.293099 96762.382421 99702.240499
338 94215.396452 92603.801696 88417.497138 86652.053257 83238.952476 81394.338933 81605.941017 84217.050653 85747.710289 84939.320054 ... 80917.592579 87667.631632 90006.845576 92975.157162 96659.250781 94104.413675 95187.506771 95718.529952 93904.059640 94316.145055
339 92768.063415 92096.053646 93262.564715 86061.695705 82949.382814 79703.825392 79801.926563 79756.979428 82286.200262 84548.325132 ... 86783.133464 89396.448829 93991.273312 96648.210028 96528.620447 96236.837504 95276.645311 93922.129953 95011.040501 97997.413806
340 94602.016538 93258.626696 92016.616151 86933.704950 83836.623309 81795.878256 80097.504038 79695.526825 80290.785548 81383.383465 ... 86814.433597 88728.359246 92455.216407 94748.034642 95707.164324 96868.565239 97003.334376 96791.391280 98843.414063 97065.538026
341 96717.544924 93066.940886 89133.874872 87513.413934 86240.627345 81646.223308 81110.539194 84477.753777 88620.006889 88513.613414 ... 84104.177084 88422.184768 92548.638547 95459.299873 92894.464066 96865.687244 98677.991929 97978.595966 98688.200778 96309.576565
342 94895.802347 93068.370709 87886.079429 84234.258205 81745.181251 79079.270705 77725.547528 81797.632423 84167.886721 85039.028777 ... 85615.723048 83020.156512 89233.651172 93413.135157 92771.568105 90237.873178 92937.566409 94617.631640 94848.921099 94718.443884
343 93615.984243 87226.643100 87645.535157 84097.859505 80121.748830 78952.803386 78369.501563 80362.164845 82272.847397 82302.186200 ... 82597.438282 129612.556651 87583.256513 87618.416800 87809.025001 91167.651565 88614.047917 91143.094922 94393.957555 92675.801302
344 90043.650392 87614.475132 84960.432163 79289.628777 81451.638673 78744.616537 78355.536069 79796.611199 83231.405991 82787.162762 ... 84736.186460 138054.156393 86261.770444 87025.065756 86970.152215 86524.874741 87900.883335 88945.377347 89078.245053 90859.036200
345 90343.549868 86423.770526 84747.849220 84827.693361 81965.733205 78622.745184 75122.561590 76727.897137 80037.373698 78597.991407 ... 128223.913292 129648.702227 128565.957558 99005.640108 87519.603907 86600.347267 88761.071747 86794.152606 88890.161850 87957.799480
346 89064.693100 86163.602346 81658.779949 78969.694532 77280.213283 74479.230210 73168.517059 75946.485157 71607.316798 94142.488154 ... 129390.459772 127760.187109 132384.519287 131479.417205 129722.263035 125083.404698 127039.002612 123066.782040 123162.742847 121834.906512
347 121959.900138 120814.523178 115362.065892 110618.231129 75821.780600 75080.536980 73089.971876 72468.019272 99532.030862 115967.039340 ... 117951.964978 120114.889205 118425.609510 119421.458210 116590.853644 86362.887112 84620.624481 87713.619533 86941.960939 86543.054821
348 85788.264434 82716.652475 79792.256642 77526.733205 75714.600261 74533.179688 71344.850392 78381.684116 80181.686851 77314.824949 ... 102289.356519 83257.069794 121438.669019 117635.010812 115084.012637 85261.383465 87602.897139 90264.716147 118889.993628 84589.567798
349 82186.637371 80718.371485 79588.931511 78690.515887 73711.313934 75275.775522 77181.062631 82694.167189 80281.961208 79792.895444 ... 87937.100260 87529.986330 92584.783859 92968.051042 90755.696227 91284.626565 94434.941800 93225.290108 92985.796098 90463.863542
350 92985.358204 90325.080469 87308.355730 83843.548862 82196.349350 79478.418882 79549.537241 77937.669792 77751.959116 81892.511590 ... 87180.251042 149685.872012 102651.953518 90633.391670 91946.099742 92063.697659 91082.051174 92804.059637 91068.898829 90050.320183
351 87644.857422 81845.582553 79974.251043 76346.927735 72934.611329 73201.818881 73282.572267 79942.194272 78808.023048 77954.957161 ... 88988.983726 87602.480730 92999.091018 93281.876436 90600.229948 94441.652735 94307.251175 95072.223962 95070.845186 93223.148168
352 92194.374352 87122.178907 86915.891930 85450.600652 83002.367579 80346.969663 79400.132032 83490.662762 86563.211981 82163.951043 ... 90650.217060 91492.770056 91987.073440 94383.675913 91918.051956 91529.892974 98387.260553 92632.820965 94649.469142 94039.201174
353 93388.578780 91190.802606 88628.218360 87436.609636 84037.639845 84083.997528 82451.427086 83146.293101 80214.936590 81671.562240 ... 84970.830601 86345.040886 85017.807553 87902.682553 87430.586722 88434.587109 89558.984505 90091.099741 91103.433597 89885.682294
354 87819.482684 86945.194402 84858.125001 82716.006121 84053.534897 82197.848960 83129.081251 84256.147267 85011.313543 83327.451564 ... 83455.097006 85301.736461 85406.242970 85086.770053 89015.467578 86536.802475 87426.533988 85540.190495 84283.616668 86335.099740
355 83721.336069 82739.217970 81382.953386 81660.950263 78725.207163 76744.548178 76973.223308 76964.322527 76401.297918 73950.604609 ... 73270.259116 74829.681902 76112.210939 75674.765496 76154.263022 74369.377215 76670.784033 75866.170444 75012.638673 79292.740757
356 77929.235287 76902.496095 73914.358855 76249.380340 70964.113804 71796.078524 70684.055209 70548.205080 68072.734832 69746.867709 ... 98790.240626 96462.264194 86375.629038 65695.707813 66504.308269 66777.251042 67057.119793 65539.536133 66662.969727 66808.279298
357 66845.032878 66169.435613 68292.265431 70791.199480 70440.559376 69148.555861 70136.258335 69153.557488 68138.382488 65760.143620 ... 107614.021617 104467.065633 100875.614844 100353.950263 100759.869666 100678.157168 100551.230082 99505.002606 97951.546747 98063.254242
358 81760.042769 66355.813738 65954.082358 66159.078581 66585.289064 66487.824871 66636.462827 65598.627149 62951.821681 63193.130665 ... 96233.752866 97409.303389 95156.481902 66075.424090 65162.639323 66896.577931 67234.694402 71322.196226 69970.114454 70455.884668
359 70705.651695 69645.819793 69455.233465 70628.292319 70557.671485 70402.513543 70311.987371 70502.245053 68469.969271 64994.617709 ... 63170.945964 64068.963933 66968.693751 65109.651172 66963.620965 65923.349350 67008.507553 66740.451368 65581.371225 67215.416863
360 69783.242254 68982.702215 69098.164585 69289.199610 68307.604103 67796.516863 68604.693490 67662.435418 66978.948763 64647.216082 ... 108501.435616 88000.757169 62586.548438 64858.452735 64259.612436 65656.016668 64436.285743 64925.355014 65284.070248 64353.492448
361 65349.505144 64379.260157 64460.242188 63795.179818 81584.538469 118232.093368 103664.506910 103486.538284 101458.646226 106086.180084 ... 64688.383139 67242.365951 69033.453647 67988.089780 69143.064714 68009.431576 68992.068165 69170.168687 70031.189324 69686.643490
362 70062.046355 70783.656642 68946.439780 70671.585158 69627.551823 70243.627410 70129.625847 70196.381837 68298.399480 66938.633529 ... 69698.932748 68843.170327 73100.756642 72105.826303 72602.791538 73298.032293 72912.505731 72271.196486 70453.072722 72019.894012
363 71769.656316 71216.432943 71456.399415 69727.377149 70732.054493 69569.351564 70309.226108 69482.808530 70227.430861 64045.906316 ... 66079.644206 69177.518425 64756.361915 64862.787566 65354.989910 68082.787306 67920.722006 68218.176629 67913.137697 69064.227475
364 68878.614324 68760.178777 69655.468425 68777.554949 67898.847918 70072.151303 68510.081701 70526.021030 69726.237176 67881.364845 ... 65684.317383 65884.644077 65590.387957 68143.441082 68723.335158 69183.479038 69242.762696 67893.015626 69100.275652 69143.631837
365 68511.565756 68915.876368 69231.581446 68860.836394 68144.966146 69194.399741 69798.937176 67959.598048 68742.610093 69858.711003 ... 67318.574871 67485.071160 69987.204298 70353.540236 70144.194337 70657.021355 70572.566668 70057.533464 70049.757684 70120.832827

365 rows × 24 columns


In [11]:
plt.imshow(d9, aspect='auto',cmap='summer')
plt.ylabel('Day of Year')
plt.xlabel('Hour of the Day')
plt.colorbar()


Out[11]:
<matplotlib.colorbar.Colorbar at 0x11c972ac8>

In [12]:
d10 = df.copy()
d10['Time'] = d10.index
d10['Weekday'] = d10['Time'].dt.dayofweek
d10.boxplot(by="Hour",column=['Power'])
plt.ylabel('Power')

d10.groupby('Weekday').boxplot(by="Hour",column=['Power'],figsize=(20,30),layout=(4,2))


Out[12]:
0         Axes(0.1,0.736957;0.363636x0.163043)
1    Axes(0.536364,0.736957;0.363636x0.163043)
2         Axes(0.1,0.541304;0.363636x0.163043)
3    Axes(0.536364,0.541304;0.363636x0.163043)
4         Axes(0.1,0.345652;0.363636x0.163043)
5    Axes(0.536364,0.345652;0.363636x0.163043)
6             Axes(0.1,0.15;0.363636x0.163043)
dtype: object
$$End of Mishra Ji's Code$$

In [13]:
df1=df.resample('D').mean()
df1['Day']=np.arange(367)
# df1
Y=df1['Power']
X=df1[['Temperature','Day']]

Y_mat=Y.as_matrix()
X_mat=X.as_matrix()

In [14]:
from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor(min_samples_split=20,random_state=99,min_samples_leaf=2)
model.fit(X_mat,Y_mat)
print(model)
expected=Y_mat
predicted=model.predict(X_mat)
# print(metrics.classification_report(expected, predicted))
# print(metrics.confusion_matrix(expected, predicted))

model.score(X_mat,Y_mat)


DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
           max_leaf_nodes=None, min_impurity_split=1e-07,
           min_samples_leaf=2, min_samples_split=20,
           min_weight_fraction_leaf=0.0, presort=False, random_state=99,
           splitter='best')
Out[14]:
0.9127184030261648

In [15]:
plt.plot(expected,'b')
plt.ylabel('Power')
plt.xlabel('Days')
plt.plot(predicted,'--g')


Out[15]:
[<matplotlib.lines.Line2D at 0x10425ae48>]

In [16]:
# plt.plot(df[df.Value>0].Temperature,df[df.Value<0].Temperature, 'b+')
# plt.plot(df[df.Value<=0].x1,df[df.Value<=0].x2, 'ro')
# plt.show()
plt.plot(df[df.Value==1].Temperature,'b+')
plt.plot(df[df.Value==-1].Temperature,'r')
plt.ylabel('Temperature in F')


Out[16]:
<matplotlib.text.Text at 0x11adb3dd8>

In [17]:
# plt.plot(df[df.Value==1].Power,'b+')
# plt.plot(df[df.Value==-1].Power,'r')

In [18]:
from sklearn.cluster import KMeans

# Clean it up
d9 = d9.replace(np.inf,np.nan).fillna(0)


# Make it compatible with sklearn:
X = d9.as_matrix().astype(np.float32)
## remove days with weird consumption pattern, as shown in stem plot above
X = np.concatenate([X[:297,:],X[314:,:]])
print(X.shape)

#since we are interested in weekdays/weekends, lets subtract the seasonal effects
#here I compute a naive low-pass over 10 days
lp = 10
seasonal = []
for i in range(int(len(X))):
    seasonal.append(np.mean(X[np.max([i-lp,0]):i+lp,:]))
    

plt.plot(seasonal, label='Seasonal Effect')
plt.plot(np.mean(X,axis=1), label='Daily Average')

X = (X.T - seasonal).T
plt.plot(np.mean(X,axis=1), label='Normalized Days')
plt.ylabel('Power')
plt.xlabel('Days')
plt.legend()

# Find the clusters
clusters = KMeans(n_clusters=3).fit(X)


(348, 24)

In [32]:
num_clust = 3

cluster_assignments = clusters.predict(X)
plt.subplot(num_clust+1,1,1)
plt.plot(cluster_assignments[:150])
plt.ylim([0.2,1.1])

for cluster_id in range(len(clusters.cluster_centers_)):
    plt.subplot(num_clust+1,1,cluster_id+2)
    cluster_members = X[cluster_assignments==cluster_id,:]
    print(len(cluster_members))
    for i in range(len(cluster_members)):
        plt.plot(cluster_members[i,:], color='grey', lw='0.1')
    plt.plot(clusters.cluster_centers_[cluster_id,:], color='k', lw='1')
#     plt.ylim([-4000,4000])


55
107
186

In [20]:
plot_step = 1

X1 = df1[['Temperature','Day']]
y1 = df1['Power']

# Let's pre-compute the range for our features
x_min, x_max = X1['Temperature'].min() - 1, X1['Temperature'].max() + 1
y_min, y_max = X1['Day'].min() - 1, X1['Day'].max() + 1
# And create a meshgrid so that we can create a countour plot on it
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),np.arange(y_min, y_max, plot_step))

# Now we predict the values for all of the cells in the meshgrid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) # If you're curious what this is, read here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html and https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html
# And we reshape those results to have the same shape as the mesh
Z = Z.reshape(xx.shape)
# Now we can finally contour-plot, using a specific colormap
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)

# Let's add labels to the axes
plt.xlabel('Temperature')
plt.ylabel('Day')
plt.axis("Tight")

# Plot the training points
plt.plot(df1[df1.Value>0].Temperature,df1[df1.Value>0].Day, 'b+')
plt.plot(df1[df1.Value<=0].Temperature,df1[df1.Value<=0].Day, 'ro')
plt.axis("Tight")

# And add a few more beautification items
plt.suptitle("Decision Surface of our Decision Tree")
plt.legend(['Positives','Negatives'])
plt.show()



In [21]:
plt.plot(df1[df1.Value>0].Temperature,df1[df1.Value>0].Day, 'b+')
plt.plot(df1[df1.Value<=0].Temperature,df1[df1.Value<=0].Day, 'ro')
plt.show()



In [22]:
df_P=df[df['Value']==1]
df_N=df[df['Value']==-1]

In [23]:
import scipy.stats  as stats
a=df_P['Power'].corr(df_P['Temperature'])
b=df_N['Power'].corr(df_N['Temperature'])
print(a)
print(b)


0.567309338511
-0.315344780001

In [24]:
Temp=[]

for row in df['Temperature']:
    if row>58.482674:
        Temp.append(1)
    else:
        Temp.append(-1)
        
df['Temp']=Temp

In [25]:
df_P1=df[df['Temp']==1]
df_N1=df[df['Temp']==-1]

In [26]:
a1=df_P1['Power'].corr(df_P1['Temperature'])
b1=df_N1['Power'].corr(df_N1['Temperature'])

In [27]:
print(a1)
print(b1)


0.627149153633
-0.18839087237

In [28]:
plt.hist2d(df['Power'],df['Temperature'])
plt.colorbar()


Out[28]:
<matplotlib.colorbar.Colorbar at 0x11de6b400>

In [29]:
plt.violinplot(df['Power'])
plt.ylabel('Power')
plt.xlabel('Probability')


Out[29]:
<matplotlib.text.Text at 0x104288b38>

In [30]:
plt.psd(df['Power'])


Out[30]:
(array([  1.05691800e+12,   5.28480135e+11,   5.29576178e+09,
          3.77489296e+09,   1.50215064e+09,   1.36685692e+09,
          1.22709785e+09,   1.01239621e+09,   8.04080303e+08,
          1.02257381e+09,   8.49455717e+09,   1.19688033e+10,
          1.90810698e+09,   3.60170457e+08,   4.09507292e+08,
          3.26449928e+08,   2.58594916e+08,   1.82970867e+08,
          1.63770248e+08,   2.49259459e+08,   4.05453880e+08,
          9.79576781e+08,   7.33405217e+08,   2.50275529e+08,
          2.20648512e+08,   1.80579128e+08,   1.40911441e+08,
          1.17910267e+08,   1.28631762e+08,   1.40648622e+08,
          1.38167092e+08,   2.13441793e+08,   1.99597552e+08,
          1.51611046e+08,   1.10110903e+08,   8.34122427e+07,
          8.82487753e+07,   8.85542977e+07,   1.00817974e+08,
          1.07165899e+08,   1.01312345e+08,   5.98397030e+07,
          9.12606438e+07,   1.16676071e+08,   9.04423091e+07,
          6.30884820e+07,   6.35042485e+07,   6.33960684e+07,
          6.76229073e+07,   6.63208817e+07,   7.09443205e+07,
          5.65505096e+07,   4.97329206e+07,   7.35644646e+07,
          9.93188318e+07,   1.01842486e+08,   6.31508138e+07,
          5.79951252e+07,   6.22876496e+07,   5.27204592e+07,
          4.55963171e+07,   4.52969994e+07,   4.83398968e+07,
          4.95487853e+07,   3.93049075e+07,   5.21053405e+07,
          5.25821334e+07,   3.97079688e+07,   4.36589311e+07,
          3.76988296e+07,   3.41842755e+07,   2.91647140e+07,
          3.32213060e+07,   3.44522244e+07,   3.59875204e+07,
          2.88502507e+07,   3.75692838e+07,   3.13408958e+07,
          2.60660311e+07,   2.04987871e+07,   2.53469046e+07,
          2.46668683e+07,   2.31848169e+07,   2.59392898e+07,
          2.76954803e+07,   2.34203207e+07,   3.22535767e+07,
          3.36319911e+07,   2.31740276e+07,   2.56117579e+07,
          2.22804361e+07,   2.17482733e+07,   2.42682135e+07,
          2.07322672e+07,   2.27523529e+07,   2.40051301e+07,
          2.51180885e+07,   2.63208539e+07,   2.00711515e+07,
          2.10370843e+07,   1.85238039e+07,   1.39422095e+07,
          2.26150873e+07,   2.02049726e+07,   1.94137711e+07,
          1.42127895e+07,   1.78736646e+07,   1.90563096e+07,
          1.66964337e+07,   1.84285854e+07,   2.12307058e+07,
          1.60850658e+07,   2.32288701e+07,   2.17028323e+07,
          1.79571133e+07,   2.13988952e+07,   1.88992434e+07,
          2.26831100e+07,   2.01770062e+07,   1.72170340e+07,
          1.50727578e+07,   1.42239551e+07,   1.53293566e+07,
          2.37930072e+07,   1.70699840e+07,   1.38307830e+07,
          1.43712379e+07,   1.69635016e+07,   1.32215169e+07]),
 array([ 0.       ,  0.0078125,  0.015625 ,  0.0234375,  0.03125  ,
         0.0390625,  0.046875 ,  0.0546875,  0.0625   ,  0.0703125,
         0.078125 ,  0.0859375,  0.09375  ,  0.1015625,  0.109375 ,
         0.1171875,  0.125    ,  0.1328125,  0.140625 ,  0.1484375,
         0.15625  ,  0.1640625,  0.171875 ,  0.1796875,  0.1875   ,
         0.1953125,  0.203125 ,  0.2109375,  0.21875  ,  0.2265625,
         0.234375 ,  0.2421875,  0.25     ,  0.2578125,  0.265625 ,
         0.2734375,  0.28125  ,  0.2890625,  0.296875 ,  0.3046875,
         0.3125   ,  0.3203125,  0.328125 ,  0.3359375,  0.34375  ,
         0.3515625,  0.359375 ,  0.3671875,  0.375    ,  0.3828125,
         0.390625 ,  0.3984375,  0.40625  ,  0.4140625,  0.421875 ,
         0.4296875,  0.4375   ,  0.4453125,  0.453125 ,  0.4609375,
         0.46875  ,  0.4765625,  0.484375 ,  0.4921875,  0.5      ,
         0.5078125,  0.515625 ,  0.5234375,  0.53125  ,  0.5390625,
         0.546875 ,  0.5546875,  0.5625   ,  0.5703125,  0.578125 ,
         0.5859375,  0.59375  ,  0.6015625,  0.609375 ,  0.6171875,
         0.625    ,  0.6328125,  0.640625 ,  0.6484375,  0.65625  ,
         0.6640625,  0.671875 ,  0.6796875,  0.6875   ,  0.6953125,
         0.703125 ,  0.7109375,  0.71875  ,  0.7265625,  0.734375 ,
         0.7421875,  0.75     ,  0.7578125,  0.765625 ,  0.7734375,
         0.78125  ,  0.7890625,  0.796875 ,  0.8046875,  0.8125   ,
         0.8203125,  0.828125 ,  0.8359375,  0.84375  ,  0.8515625,
         0.859375 ,  0.8671875,  0.875    ,  0.8828125,  0.890625 ,
         0.8984375,  0.90625  ,  0.9140625,  0.921875 ,  0.9296875,
         0.9375   ,  0.9453125,  0.953125 ,  0.9609375,  0.96875  ,
         0.9765625,  0.984375 ,  0.9921875,  1.       ]))

In [ ]: