Coverage for contextualized/analysis/utils.py: 50%
6 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-18 16:32 -0400
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-18 16:32 -0400
1"""
2Miscellaneous utility functions.
3"""
5from typing import *
7import numpy as np
10def convert_to_one_hot(col: Collection[Any]) -> Tuple[np.ndarray, List[Any]]:
11 """
12 Converts a categorical variable to a one-hot vector.
14 Args:
15 col (Collection[Any]): The categorical variable.
17 Returns:
18 Tuple[np.ndarray, List[Any]]: The one-hot vector and the possible values.
19 """
20 vals = list(set(col))
21 one_hot_vars = np.array([vals.index(x) for x in col], dtype=np.float32)
22 return one_hot_vars, vals