suhec¶
label_encoder ¶
LabelEncoder ¶
LabelEncoder(columns_to_encode=None)
Bases: object
Label Encode categorical values for multiple columns at once
NOTE:
Shamlessly copied from https://github.com/jrzaurin/pytorch-widedeep
NOTE:
LabelEncoder reserves 0 for
unseen
new categories. This is convenient
when defining the embedding layers, since we can just set padding idx to 0.
Parameters:
-
columns_to_encode
(
list, Optional, default = None
) –List of strings containing the names of the columns to encode. If
None
all columns of typeobject
in the dataframe will be label encoded.
Attributes:
-
encoding_dict
(
Dict
) –Dictionary containing the encoding mappings in the format, e.g. :
{'colname1': {'cat1': 1, 'cat2': 2, ...}, 'colname2': {'cat1': 1, 'cat2': 2, ...}, ...}
# noqa -
inverse_encoding_dict(Dict)
(
Dict
) –Dictionary containing the inverse encoding mappings in the format, e.g. :
{'colname1': {1: 'cat1', 2: 'cat2', ...}, 'colname2': {1: 'cat1', 2: 'cat2', ...}, ...}
# noqa
Source code in suhec/label_encoder.py
29 30 31 32 33 |
|
fit ¶
fit(df)
Creates encoding attributes
Returns:
-
LabelEncoder(
LabelEncoder
) –LabelEncoder
fitted object
Source code in suhec/label_encoder.py
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 |
|
transform ¶
transform(df)
Label Encoded the categories in columns_to_encode
Returns:
-
pd.DataFrame
–pd.DataFrame: label-encoded dataframe
Source code in suhec/label_encoder.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
fit_transform ¶
fit_transform(df)
Combines fit
and transform
Returns:
-
pd.DataFrame
–pd.DataFrame: label-encoded dataframe
Examples:
>>> import pandas as pd
>>> from data_preparation.label_encoder import LabelEncoder
>>> df = pd.DataFrame({'col1': [1,2,3], 'col2': ['me', 'you', 'him']})
>>> columns_to_encode = ['col2']
>>> encoder = LabelEncoder(columns_to_encode)
>>> encoder.fit_transform(df)
col1 col2
0 1 1
1 2 2
2 3 3
>>> encoder.encoding_dict
{'col2': {'me': 1, 'you': 2, 'him': 3}}
Source code in suhec/label_encoder.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
inverse_transform ¶
inverse_transform(df)
Returns the original categories
Returns:
-
pd.DataFrame
–pd.DataFrame: label-encoded dataframe
Examples:
>>> import pandas as pd
>>> from data_preparation.label_encoder import LabelEncoder
>>> df = pd.DataFrame({'col1': [1,2,3], 'col2': ['me', 'you', 'him']})
>>> columns_to_encode = ['col2']
>>> encoder = LabelEncoder(columns_to_encode)
>>> df_enc = encoder.fit_transform(df)
>>> encoder.inverse_transform(df_enc)
col1 col2
0 1 me
1 2 you
2 3 him
Source code in suhec/label_encoder.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
surname_classification ¶
create_optimizer ¶
create_optimizer(model, learning_rate=5e-05, eps=1e-08)
Creates an optimizer for the BERT model.
- model: The BERT model.
- learning_rate: Learning rate for the optimizer.
- eps: Epsilon for the AdamW optimizer.
- An AdamW optimizer.
Source code in suhec/surname_classification.py
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 |
|
train_model ¶
train_model(
model,
train_dataloader,
validation_dataloader,
optimizer,
scheduler,
device,
num_epochs=10,
specific_epoch_to_defreeze=5,
)
Trains and evaluates the BERT model.
- model: The BERT model for classification.
- train_dataloader: DataLoader for the training data.
- validation_dataloader: DataLoader for the validation data.
- optimizer: Optimizer for training.
- device: Device to train on (e.g., 'cuda', 'cpu').
- num_epochs: Number of training epochs.
- The trained model.
Source code in suhec/surname_classification.py
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 |
|
evaluate_model ¶
evaluate_model(model, dataloader, device)
Evaluates the BERT model.
- model: The trained BERT model.
- dataloader: DataLoader for the validation or test data.
- device: Device for evaluation (e.g., 'cuda', 'cpu').
- Average loss and accuracy of the model on the given data.
Source code in suhec/surname_classification.py
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 |
|
predict_nationality ¶
predict_nationality(
model, surnames, tokenizer, inverse_label_dict, device
)
Predicts and decodes the nationalities of given surnames.
- model: The trained BERT model.
- surnames: List of surnames to predict.
- tokenizer: The tokenizer used for BERT model.
- inverse_label_dict: Dictionary for converting numeric labels back to nationalities.
- device: Device for prediction (e.g., 'cuda', 'cpu').
- Decoded predictions for each surname.
Source code in suhec/surname_classification.py
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 |
|
utils ¶
get_kepler_pod_stats ¶
get_kepler_pod_stats(
to_timestamp,
from_timestamp,
prometheus_url="http://prometheus-kube-prometheus-prometheus.monitoring:9090",
container_namespace="jupyterhub",
pod_name="jupyter-5uperpalo",
)
Function to query Kepler power consumption data of specific pod in Kubernetes.
https://sustainable-computing.io/design/kepler-energy-sources/¶
https://github.com/sustainable-computing-io/kepler/blob/1c397ff00b72b5cb1585d0de2cd495c73d88f07a/grafana-dashboards/Kepler-Exporter.json#L299¶
https://prometheus.io/docs/prometheus/latest/querying/basics/#time-durations¶
[metric for metric in prom.all_metrics() if "kepler" in metric]¶
Parameters:
-
to_timestamp
(
list
) –'to' timestamp
-
from_timestamp
(
list
) –'from' timestamp
-
prometheus_url
(
str
) –Prometheus service url
-
container_namespace
(
str
) –Kubernetes pod namespace name
-
pod_name
(
str
) –Kubernetes namespace name
Returns:
-
metrics(
dict
) –Kepler metrics of the power consumption of pod in Kubernetes
Source code in suhec/utils.py
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 |
|
intsec ¶
intsec(list1, list2)
Simple intesection of two lists.
Parameters:
-
list1
(
list
) –list1
-
list2
(
list
) –list2
Returns:
-
list(
list
) –intersection of lists
Source code in suhec/utils.py
68 69 70 71 72 73 74 75 76 |
|
dill_load ¶
dill_load(file_loc)
Helper function to open/close dill file, otherwise the python outputs warning that the file remains opened
Parameters:
-
file_loc
(
str
) –location of the file
Returns:
-
content(
dict
) –content of dill file, usually dictionary
Source code in suhec/utils.py
79 80 81 82 83 84 85 86 87 88 89 90 |
|
dill_dump ¶
dill_dump(file_loc, content)
Helper function to open/close dill file and dump content into it, otherwise the python outputs warning that the file remains opened
Parameters:
-
file_loc
(
str
) –location of the file
-
content
(
object
) –data that will be saved to dill, usually dictionary
Source code in suhec/utils.py
93 94 95 96 97 98 99 100 101 102 |
|