Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

2-ttu-predict-post-features.html 15 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>&lt;no title&gt; &#8212; California Renewable Curtailments documentation</title>
  6. <link rel="stylesheet" href="../_static/haiku.css" type="text/css" />
  7. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  8. <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
  9. <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
  10. <script src="../_static/jquery.js"></script>
  11. <script src="../_static/underscore.js"></script>
  12. <script src="../_static/doctools.js"></script>
  13. <script src="../_static/language_data.js"></script>
  14. <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
  15. <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  16. <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
  17. <link rel="index" title="Index" href="../genindex.html" />
  18. <link rel="search" title="Search" href="../search.html" />
  19. </head><body>
  20. <div class="header" role="banner"><h1 class="heading"><a href="../index.html">
  21. <span>California Renewable Curtailments documentation</span></a></h1>
  22. <h2 class="heading"><span>&lt;no title&gt;</span></h2>
  23. </div>
  24. <div class="topnav" role="navigation" aria-label="top navigation">
  25. <p>
  26. <a class="uplink" href="../index.html">Contents</a>
  27. </p>
  28. </div>
  29. <div class="content" role="main">
  30. <style>
  31. /* CSS for nbsphinx extension */
  32. /* remove conflicting styling from Sphinx themes */
  33. div.nbinput.container,
  34. div.nbinput.container div.prompt,
  35. div.nbinput.container div.input_area,
  36. div.nbinput.container div[class*=highlight],
  37. div.nbinput.container div[class*=highlight] pre,
  38. div.nboutput.container,
  39. div.nboutput.container div.prompt,
  40. div.nboutput.container div.output_area,
  41. div.nboutput.container div[class*=highlight],
  42. div.nboutput.container div[class*=highlight] pre {
  43. background: none;
  44. border: none;
  45. padding: 0 0;
  46. margin: 0;
  47. box-shadow: none;
  48. }
  49. /* avoid gaps between output lines */
  50. div.nboutput.container div[class*=highlight] pre {
  51. line-height: normal;
  52. }
  53. /* input/output containers */
  54. div.nbinput.container,
  55. div.nboutput.container {
  56. display: -webkit-flex;
  57. display: flex;
  58. align-items: flex-start;
  59. margin: 0;
  60. width: 100%;
  61. }
  62. @media (max-width: 540px) {
  63. div.nbinput.container,
  64. div.nboutput.container {
  65. flex-direction: column;
  66. }
  67. }
  68. /* input container */
  69. div.nbinput.container {
  70. padding-top: 5px;
  71. }
  72. /* last container */
  73. div.nblast.container {
  74. padding-bottom: 5px;
  75. }
  76. /* input prompt */
  77. div.nbinput.container div.prompt pre {
  78. color: #307FC1;
  79. }
  80. /* output prompt */
  81. div.nboutput.container div.prompt pre {
  82. color: #BF5B3D;
  83. }
  84. /* all prompts */
  85. div.nbinput.container div.prompt,
  86. div.nboutput.container div.prompt {
  87. width: 4.5ex;
  88. padding-top: 5px;
  89. position: relative;
  90. user-select: none;
  91. }
  92. div.nbinput.container div.prompt > div,
  93. div.nboutput.container div.prompt > div {
  94. position: absolute;
  95. right: 0;
  96. margin-right: 0.3ex;
  97. }
  98. @media (max-width: 540px) {
  99. div.nbinput.container div.prompt,
  100. div.nboutput.container div.prompt {
  101. width: unset;
  102. text-align: left;
  103. padding: 0.4em;
  104. }
  105. div.nboutput.container div.prompt.empty {
  106. padding: 0;
  107. }
  108. div.nbinput.container div.prompt > div,
  109. div.nboutput.container div.prompt > div {
  110. position: unset;
  111. }
  112. }
  113. /* disable scrollbars on prompts */
  114. div.nbinput.container div.prompt pre,
  115. div.nboutput.container div.prompt pre {
  116. overflow: hidden;
  117. }
  118. /* input/output area */
  119. div.nbinput.container div.input_area,
  120. div.nboutput.container div.output_area {
  121. -webkit-flex: 1;
  122. flex: 1;
  123. overflow: auto;
  124. }
  125. @media (max-width: 540px) {
  126. div.nbinput.container div.input_area,
  127. div.nboutput.container div.output_area {
  128. width: 100%;
  129. }
  130. }
  131. /* input area */
  132. div.nbinput.container div.input_area {
  133. border: 1px solid #e0e0e0;
  134. border-radius: 2px;
  135. background: #f5f5f5;
  136. }
  137. /* override MathJax center alignment in output cells */
  138. div.nboutput.container div[class*=MathJax] {
  139. text-align: left !important;
  140. }
  141. /* override sphinx.ext.imgmath center alignment in output cells */
  142. div.nboutput.container div.math p {
  143. text-align: left;
  144. }
  145. /* standard error */
  146. div.nboutput.container div.output_area.stderr {
  147. background: #fdd;
  148. }
  149. /* ANSI colors */
  150. .ansi-black-fg { color: #3E424D; }
  151. .ansi-black-bg { background-color: #3E424D; }
  152. .ansi-black-intense-fg { color: #282C36; }
  153. .ansi-black-intense-bg { background-color: #282C36; }
  154. .ansi-red-fg { color: #E75C58; }
  155. .ansi-red-bg { background-color: #E75C58; }
  156. .ansi-red-intense-fg { color: #B22B31; }
  157. .ansi-red-intense-bg { background-color: #B22B31; }
  158. .ansi-green-fg { color: #00A250; }
  159. .ansi-green-bg { background-color: #00A250; }
  160. .ansi-green-intense-fg { color: #007427; }
  161. .ansi-green-intense-bg { background-color: #007427; }
  162. .ansi-yellow-fg { color: #DDB62B; }
  163. .ansi-yellow-bg { background-color: #DDB62B; }
  164. .ansi-yellow-intense-fg { color: #B27D12; }
  165. .ansi-yellow-intense-bg { background-color: #B27D12; }
  166. .ansi-blue-fg { color: #208FFB; }
  167. .ansi-blue-bg { background-color: #208FFB; }
  168. .ansi-blue-intense-fg { color: #0065CA; }
  169. .ansi-blue-intense-bg { background-color: #0065CA; }
  170. .ansi-magenta-fg { color: #D160C4; }
  171. .ansi-magenta-bg { background-color: #D160C4; }
  172. .ansi-magenta-intense-fg { color: #A03196; }
  173. .ansi-magenta-intense-bg { background-color: #A03196; }
  174. .ansi-cyan-fg { color: #60C6C8; }
  175. .ansi-cyan-bg { background-color: #60C6C8; }
  176. .ansi-cyan-intense-fg { color: #258F8F; }
  177. .ansi-cyan-intense-bg { background-color: #258F8F; }
  178. .ansi-white-fg { color: #C5C1B4; }
  179. .ansi-white-bg { background-color: #C5C1B4; }
  180. .ansi-white-intense-fg { color: #A1A6B2; }
  181. .ansi-white-intense-bg { background-color: #A1A6B2; }
  182. .ansi-default-inverse-fg { color: #FFFFFF; }
  183. .ansi-default-inverse-bg { background-color: #000000; }
  184. .ansi-bold { font-weight: bold; }
  185. .ansi-underline { text-decoration: underline; }
  186. div.nbinput.container div.input_area div[class*=highlight] > pre,
  187. div.nboutput.container div.output_area div[class*=highlight] > pre,
  188. div.nboutput.container div.output_area div[class*=highlight].math,
  189. div.nboutput.container div.output_area.rendered_html,
  190. div.nboutput.container div.output_area > div.output_javascript,
  191. div.nboutput.container div.output_area:not(.rendered_html) > img{
  192. padding: 5px;
  193. }
  194. /* fix copybtn overflow problem in chromium (needed for 'sphinx_copybutton') */
  195. div.nbinput.container div.input_area > div[class^='highlight'],
  196. div.nboutput.container div.output_area > div[class^='highlight']{
  197. overflow-y: hidden;
  198. }
  199. /* hide copybtn icon on prompts (needed for 'sphinx_copybutton') */
  200. .prompt a.copybtn {
  201. display: none;
  202. }
  203. /* Some additional styling taken form the Jupyter notebook CSS */
  204. div.rendered_html table {
  205. border: none;
  206. border-collapse: collapse;
  207. border-spacing: 0;
  208. color: black;
  209. font-size: 12px;
  210. table-layout: fixed;
  211. }
  212. div.rendered_html thead {
  213. border-bottom: 1px solid black;
  214. vertical-align: bottom;
  215. }
  216. div.rendered_html tr,
  217. div.rendered_html th,
  218. div.rendered_html td {
  219. text-align: right;
  220. vertical-align: middle;
  221. padding: 0.5em 0.5em;
  222. line-height: normal;
  223. white-space: normal;
  224. max-width: none;
  225. border: none;
  226. }
  227. div.rendered_html th {
  228. font-weight: bold;
  229. }
  230. div.rendered_html tbody tr:nth-child(odd) {
  231. background: #f5f5f5;
  232. }
  233. div.rendered_html tbody tr:hover {
  234. background: rgba(66, 165, 245, 0.2);
  235. }
  236. </style>
  237. <p>The input of this notebook is a dataset that has already been</p>
  238. <div class="nbinput nblast docutils container">
  239. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[35]:
  240. </pre></div>
  241. </div>
  242. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  243. <span></span>import pandas as pd
  244. import statsmodels.formula.api as smf
  245. import statsmodels.api as sm
  246. from src.conf import settings
  247. TRAINING_DIR = settings.DATA_DIR / &quot;processed/training/&quot;
  248. data = pd.read_parquet(TRAINING_DIR / &quot;0_labeled_data.parquet&quot;)
  249. </pre></div>
  250. </div>
  251. </div>
  252. <div class="nbinput docutils container">
  253. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[30]:
  254. </pre></div>
  255. </div>
  256. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  257. <span></span>data.head()
  258. </pre></div>
  259. </div>
  260. </div>
  261. <div class="nboutput nblast docutils container">
  262. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[30]:
  263. </pre></div>
  264. </div>
  265. <div class="output_area rendered_html docutils container">
  266. <div>
  267. <style scoped>
  268. .dataframe tbody tr th:only-of-type {
  269. vertical-align: middle;
  270. }
  271. .dataframe tbody tr th {
  272. vertical-align: top;
  273. }
  274. .dataframe thead th {
  275. text-align: right;
  276. }
  277. </style>
  278. <table border="1" class="dataframe">
  279. <thead>
  280. <tr style="text-align: right;">
  281. <th></th>
  282. <th>timestamp</th>
  283. <th>is_weekday</th>
  284. <th>t</th>
  285. <th>dswrf</th>
  286. <th>SUNSD</th>
  287. <th>curtailment_event</th>
  288. <th>solar_curtailment</th>
  289. <th>load</th>
  290. </tr>
  291. <tr>
  292. <th>index</th>
  293. <th></th>
  294. <th></th>
  295. <th></th>
  296. <th></th>
  297. <th></th>
  298. <th></th>
  299. <th></th>
  300. <th></th>
  301. </tr>
  302. </thead>
  303. <tbody>
  304. <tr>
  305. <th>0</th>
  306. <td>2017-02-01 00:00:00-08:00</td>
  307. <td>False</td>
  308. <td>282.630219</td>
  309. <td>204.382217</td>
  310. <td>2130985.0</td>
  311. <td>False</td>
  312. <td>8480.663464</td>
  313. <td>7.404797e+06</td>
  314. </tr>
  315. <tr>
  316. <th>1</th>
  317. <td>2017-02-02 00:00:00-08:00</td>
  318. <td>False</td>
  319. <td>282.440430</td>
  320. <td>155.364441</td>
  321. <td>1721099.0</td>
  322. <td>False</td>
  323. <td>288.956667</td>
  324. <td>7.367050e+06</td>
  325. </tr>
  326. <tr>
  327. <th>2</th>
  328. <td>2017-02-03 00:00:00-08:00</td>
  329. <td>False</td>
  330. <td>283.654236</td>
  331. <td>162.995560</td>
  332. <td>1762346.0</td>
  333. <td>False</td>
  334. <td>869.203799</td>
  335. <td>7.272859e+06</td>
  336. </tr>
  337. <tr>
  338. <th>3</th>
  339. <td>2017-02-04 00:00:00-08:00</td>
  340. <td>True</td>
  341. <td>283.166656</td>
  342. <td>197.639999</td>
  343. <td>1947685.0</td>
  344. <td>True</td>
  345. <td>69858.576657</td>
  346. <td>6.611103e+06</td>
  347. </tr>
  348. <tr>
  349. <th>4</th>
  350. <td>2017-02-05 00:00:00-08:00</td>
  351. <td>True</td>
  352. <td>283.199097</td>
  353. <td>174.813339</td>
  354. <td>2019408.0</td>
  355. <td>False</td>
  356. <td>25.348000</td>
  357. <td>6.661248e+06</td>
  358. </tr>
  359. </tbody>
  360. </table>
  361. </div></div>
  362. </div>
  363. <div class="nbinput nblast docutils container">
  364. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[ ]:
  365. </pre></div>
  366. </div>
  367. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  368. <span></span>
  369. </pre></div>
  370. </div>
  371. </div>
  372. <div class="nbinput nblast docutils container">
  373. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[39]:
  374. </pre></div>
  375. </div>
  376. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  377. <span></span>class ModelRun:
  378. def __init__(self, formula, data, threshold=.8):
  379. data = data.copy()
  380. self.formula = formula
  381. self.threshold = threshold
  382. self.test = data.sample(int(len(data)*.2//1))
  383. self.training = data[~data.index.isin(self.test.index)]
  384. def compute_performance(self, predictions):
  385. cutoff = self.threshold
  386. positive_predictions = predictions.query(&quot;probability &gt; @cutoff&quot;)[&quot;curtailment_event&quot;].value_counts()
  387. negative_predictions = predictions.query(&quot;probability &lt;= @cutoff&quot;)[&quot;curtailment_event&quot;].value_counts()
  388. true_positives = positive_predictions.loc[True]
  389. false_positives = positive_predictions.loc[False]
  390. true_negatives = negative_predictions.loc[False]
  391. false_negatives = negative_predictions[True]
  392. accuracy = (true_positives+true_negatives)/len(predictions)
  393. precision = true_positives / (true_positives + false_positives)
  394. self.performance = {
  395. &quot;true_positives&quot;: true_positives,
  396. &quot;false_positives&quot;: false_positives,
  397. &quot;true_negatives&quot;: true_negatives,
  398. &quot;false_negatives&quot;: false_negatives,
  399. &quot;accuracy&quot;: accuracy,
  400. &quot;precision&quot;: precision
  401. }
  402. def run(self):
  403. &quot;&quot;&quot;
  404. &quot;&quot;&quot;
  405. result = smf.glm(
  406. self.formula,
  407. self.training,
  408. family=sm.families.Binomial()
  409. ).fit()
  410. self.result = result
  411. # De-label our test data
  412. predictions = result.predict(
  413. self.test.drop(columns=[&quot;curtailment_event&quot;])
  414. )
  415. predictions.name = &quot;probability&quot;
  416. # We are predicting probability of False because patsy reverses categorical designations
  417. predictions = 1 - predictions
  418. # Validate our test data back to our predictions
  419. predictions = self.test.merge(predictions, left_index=True, right_index=True)
  420. self.predictions = predictions
  421. self.compute_performance(predictions)
  422. </pre></div>
  423. </div>
  424. </div>
  425. <div class="nbinput docutils container">
  426. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[65]:
  427. </pre></div>
  428. </div>
  429. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  430. <span></span>model_run = ModelRun(
  431. &quot;C(curtailment_event) ~ C(timestamp.dt.month) + C(is_weekday) + load + t + dswrf&quot;,
  432. data,
  433. 0.8
  434. )
  435. model_run.run()
  436. model_run.performance
  437. </pre></div>
  438. </div>
  439. </div>
  440. <div class="nboutput nblast docutils container">
  441. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[65]:
  442. </pre></div>
  443. </div>
  444. <div class="output_area docutils container">
  445. <div class="highlight"><pre>
  446. {&#39;true_positives&#39;: 2,
  447. &#39;false_positives&#39;: 1,
  448. &#39;true_negatives&#39;: 51,
  449. &#39;false_negatives&#39;: 18,
  450. &#39;accuracy&#39;: 0.7361111111111112,
  451. &#39;precision&#39;: 0.6666666666666666}
  452. </pre></div></div>
  453. </div>
  454. <div class="nbinput nblast docutils container">
  455. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[57]:
  456. </pre></div>
  457. </div>
  458. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  459. <span></span>
  460. </pre></div>
  461. </div>
  462. </div>
  463. <div class="nbinput nblast docutils container">
  464. <div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[ ]:
  465. </pre></div>
  466. </div>
  467. <div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
  468. <span></span>
  469. </pre></div>
  470. </div>
  471. </div>
  472. </div>
  473. <div class="bottomnav" role="navigation" aria-label="bottom navigation">
  474. <p>
  475. <a class="uplink" href="../index.html">Contents</a>
  476. </p>
  477. </div>
  478. <div class="footer" role="contentinfo">
  479. &#169; Copyright Thomas Tu, 2020.
  480. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 2.4.4.
  481. </div>
  482. </body>
  483. </html>
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...