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

utils.py 2.1 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
  1. """
  2. Just utility functions
  3. """
  4. from dataclasses import dataclass
  5. from typing import *
  6. import numpy as np
  7. import pandas as pd
  8. from typing import *
  9. from enum import Enum, auto
  10. @dataclass
  11. class Table:
  12. @staticmethod
  13. def from_rows(rows: List[List[str]]):
  14. return Table(rows[0], rows[1:])
  15. headers: List[str]
  16. body: List[List[str]]
  17. caption: str = ''
  18. @property
  19. def rows(self) -> str:
  20. return "".join(['<tr><td>'+'</td><td>'.join(row)+'</td></tr>' for row in self.body])
  21. def _repr_html_(self):
  22. '''
  23. Nice JupyterLab table HTML representation
  24. :return:
  25. '''
  26. return f"<table border='2'> {'' if self.caption == '' else '<caption>' + self.caption + '</caption>'}" \
  27. f"<tr><th>{'</th><th>'.join(self.headers)}</th></tr>" \
  28. f"{self.rows}" \
  29. f"</table>"
  30. def show(df: pd.DataFrame, cols: int, rows: int = 3) -> pd.DataFrame:
  31. return df[df.columns[0:cols]].head(rows)
  32. def less_or_value(n: np.ndarray, max: float = 0.0, value: float = 0.0):
  33. k = n.copy()
  34. k[k >= max] = value
  35. return k
  36. def more_or_value(n: np.ndarray, min: float = 0.0, value: float = 0.0):
  37. k = n.copy()
  38. k[k <= min] = value
  39. return k
  40. def matplot_to_plotly_colors(cmap, pl_entries=11, rdigits=2):
  41. # cmap - colormap
  42. # pl_entries - int = number of Plotly colorscale entries
  43. # rdigits - int -=number of digits for rounding scale values
  44. scale = np.linspace(0, 1, pl_entries)
  45. colors = (cmap(scale)[:, :3]*255).astype(np.uint8)
  46. pl_colorscale = [[round(s, rdigits), f'rgb{tuple(color)}'] for s, color in zip(scale, colors)]
  47. return pl_colorscale
  48. import shap.plots.colors
  49. red_blue = matplot_to_plotly_colors(shap.plots.colors._colors.red_blue)
  50. red_blue_transparent = matplot_to_plotly_colors(shap.plots.colors._colors.red_blue_transparent)
  51. red_blue_no_bounds = matplot_to_plotly_colors(shap.plots.colors._colors.red_blue_no_bounds)
  52. red_blue_circle = matplot_to_plotly_colors(shap.plots.colors._colors.red_blue_circle)
  53. red_white_blue = matplot_to_plotly_colors(shap.plots.colors._colors.red_white_blue)
Tip!

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

Comments

Loading...