Skip to content

features

get_hour_distances(x)

This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object. It returns a new dictionary where the keys are the names of the hours of the day and the values are the result of applying a Gaussian function to the difference between the hour of the input datetime object and each hour of the day. Args: x (Dict[str, datetime]): A dictionary with a single key-value pair where the key is a string and the value is a datetime object. Returns: (Dict[str, float]): A dictionary where the keys are the names of the hours of the day and the values are the result of applying a Gaussian function to the difference between the hour of the input datetime object and each hour of the day. Examples: >>> from spotriver.utils.features import get_hour_distances >>> from datetime import datetime >>> get_hour_distances({“date”: datetime(2020, 1, 1)}) {‘0’: 0.6065306597126334, ‘1’: 0.36787944117144233, ‘2’: 0.1353352832366127, ‘3’: 0.01831563888873418, ‘4’: 0.00033546262790251185, ‘5’: 3.354626279025118e-06, ‘6’: 2.0611536224385582e-08, ‘7’: 8.315287191035679e-11, ‘8’: 2.0611536224385582e-13, ‘9’: 3.354626279025118e-16, ‘10’: 3.354626279025118e-19, ‘11’: 2.0611536224385582e-22, ‘12’: 8.315287191035679e-26, ‘13’: 2.0611536224385582e-29, ‘14’: 3.354626279025118e-33, ‘15’: 3.354626279025118e-37, ‘16’: 2.0611536224385582e-41, ‘17’: 8.315287191035679e-46, ‘18’: 2.0611536224385582e-50, ‘19’: 3.354626279025118e-55, ‘20’: 3.354626279025118e-60, ‘21’: 2.0611536224385582e-65, ‘22’: 8.315287191035679e-71, ‘23’: 2.0611536224385582e-76}

Source code in spotriver/utils/features.py
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
def get_hour_distances(x):
    """This function takes in a dictionary with a single key-value pair where the key is a string and the value
        is a datetime object.
        It returns a new dictionary where the keys are the names of the hours of the day and the values are
        the result of applying a Gaussian function to the difference between the hour of the input datetime
        object and each hour of the day.
    Args:
        x (Dict[str, datetime]):
            A dictionary with a single key-value pair where the key is a string and the value is a datetime object.
    Returns:
        (Dict[str, float]):
            A dictionary where the keys are the names of the hours of the day and the values are
            the result of applying a Gaussian function to the difference between the hour of the
            input datetime object and each hour of the day.
    Examples:
        >>> from spotriver.utils.features import get_hour_distances
        >>> from datetime import datetime
        >>> get_hour_distances({"date": datetime(2020, 1, 1)})
        {'0': 0.6065306597126334, '1': 0.36787944117144233, '2': 0.1353352832366127, '3': 0.01831563888873418,
        '4': 0.00033546262790251185, '5': 3.354626279025118e-06, '6': 2.0611536224385582e-08, '7': 8.315287191035679e-11,
        '8': 2.0611536224385582e-13, '9': 3.354626279025118e-16, '10': 3.354626279025118e-19,
        '11': 2.0611536224385582e-22, '12': 8.315287191035679e-26, '13': 2.0611536224385582e-29,
        '14': 3.354626279025118e-33, '15': 3.354626279025118e-37, '16': 2.0611536224385582e-41,
        '17': 8.315287191035679e-46, '18': 2.0611536224385582e-50, '19': 3.354626279025118e-55,
        '20': 3.354626279025118e-60, '21': 2.0611536224385582e-65, '22': 8.315287191035679e-71, '23': 2.0611536224385582e-76}
    """
    k = list(x.keys())[0]
    return {str(hour): math.exp(-((x[k].hour - hour) ** 2)) for hour in range(0, 24)}

get_month_distances(x)

This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object. It returns a new dictionary where the keys are the names of the months and the values are the result of applying a Gaussian function to the difference between the month of the input datetime object and each month.

Parameters:

Name Type Description Default
x Dict[str, datetime]

A dictionary with a single key-value pair where the key is a string and the value is a datetime object.

required

Returns:

Type Description
Dict[str, float]

A dictionary where the keys are the names of the months and the values are the result of applying a Gaussian function to the difference between the month of the input datetime object and each month.

Examples: >>> from spotriver.utils.features import get_month_distances >>> from datetime import datetime >>> get_month_distances({“date”: datetime(2020, 1, 1)}) {‘January’: 0.6065306597126334, ‘February’: 0.36787944117144233, ‘March’: 0.1353352832366127, ‘April’: 0.01831563888873418, ‘May’: 0.00033546262790251185, ‘June’: 3.354626279025118e-06, ‘July’: 2.0611536224385582e-08, ‘August’: 8.315287191035679e-11, ‘September’: 2.0611536224385582e-13, ‘October’: 3.354626279025118e-16, ‘November’: 3.354626279025118e-19, ‘December’: 2.0611536224385582e-22}

Source code in spotriver/utils/features.py
 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
def get_month_distances(x: Dict[str, datetime]) -> Dict[str, float]:
    """
    This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object.
    It returns a new dictionary where the keys are the names of the months and the values are the result of applying a Gaussian
    function to the difference between the month of the input datetime object and each month.

    Args:
        x (Dict[str, datetime]):
            A dictionary with a single key-value pair where the key is a string and the value is a datetime object.

    Returns:
        (Dict[str, float]):
            A dictionary where the keys are the names of the months and the values are the result of
            applying a Gaussian function to the difference between the month of the input datetime object and each month.
    Examples:
        >>> from spotriver.utils.features import get_month_distances
        >>> from datetime import datetime
        >>> get_month_distances({"date": datetime(2020, 1, 1)})
        {'January': 0.6065306597126334, 'February': 0.36787944117144233,
        'March': 0.1353352832366127, 'April': 0.01831563888873418, 'May': 0.00033546262790251185,
        'June': 3.354626279025118e-06, 'July': 2.0611536224385582e-08,
        'August': 8.315287191035679e-11,
        'September': 2.0611536224385582e-13, 'October': 3.354626279025118e-16,
        'November': 3.354626279025118e-19, 'December': 2.0611536224385582e-22}
    """
    k = list(x.keys())[0]
    return {calendar.month_name[month]: math.exp(-((x[k].month - month) ** 2)) for month in range(1, 13)}

get_ordinal_date(x)

This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object. It returns a new dictionary where the keys are the string “ordinal_date” and the value is the ordinal date of the input datetime object. Args: x (Dict[str, datetime]): A dictionary with a single key-value pair where the key is a string and the value is a datetime object. Returns: (Dict[str, float]): A dictionary where the keys are the string “ordinal_date” and the value is the ordinal date of the input datetime object. Examples: >>> from datetime import datetime >>> from spotriver.utils.features import get_ordinal_date >>> get_ordinal_date({“date”: datetime(2020, 1, 1)}) {‘ordinal_date’: 737424}

Source code in spotriver/utils/features.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def get_ordinal_date(x):
    """This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object.
        It returns a new dictionary where the keys are the string "ordinal_date" and the value is the ordinal date of the input datetime object.
    Args:
        x (Dict[str, datetime]): A dictionary with a single key-value pair where the key is a string and the value is a datetime object.
    Returns:
        (Dict[str, float]):
            A dictionary where the keys are the string "ordinal_date" and the value is the ordinal date of the input datetime object.
    Examples:
        >>> from datetime import datetime
        >>> from spotriver.utils.features import get_ordinal_date
        >>> get_ordinal_date({"date": datetime(2020, 1, 1)})
        {'ordinal_date': 737424}
    """
    k = list(x.keys())[0]
    return {"ordinal_date": x[k].toordinal()}

get_weekday_distances(x)

This function takes in a dictionary with a single key-value pair where the key is a string and the value is a datetime object. It returns a new dictionary where the keys are the names of the days of the week and the values are the result of applying a Gaussian function to the difference between the weekday of the input datetime object and each day of the week. Args: x (Dict[str, datetime]): A dictionary with a single key-value pair where the key is a string and the value is a datetime object. Returns: (Dict[str, float]): A dictionary where the keys are the names of the days of the week and the values are the result of applying a Gaussian function to the difference between the weekday of the input datetime object and each day of the week. Examples: >>> from spotriver.utils.features import get_weekday_distances >>> from datetime import datetime >>> get_weekday_distances({“date”: datetime(2020, 1, 1)}) {‘Monday’: 0.6065306597126334, ‘Tuesday’: 0.36787944117144233, ‘Wednesday’: 0.1353352832366127, ‘Thursday’: 0.01831563888873418, ‘Friday’: 0.00033546262790251185, ‘Saturday’: 3.354626279025118e-06, ‘Sunday’: 2.0611536224385582e-08}

Source code in spotriver/utils/features.py
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
def get_weekday_distances(x):
    """This function takes in a dictionary with a single key-value pair where the key is a string and the value is
        a datetime object.
        It returns a new dictionary where the keys are the names of the days of the week and the values are the
        result of applying a Gaussian function to the difference between the weekday of the input datetime object
        and each day of the week.
    Args:
        x (Dict[str, datetime]):
            A dictionary with a single key-value pair where the key is a string and the value is a datetime object.
    Returns:
        (Dict[str, float]):
            A dictionary where the keys are the names of the days of the week and the values
            are the result of applying a Gaussian function to the difference between the weekday of the input
            datetime object and each day of the week.
    Examples:
        >>> from spotriver.utils.features import get_weekday_distances
        >>> from datetime import datetime
        >>> get_weekday_distances({"date": datetime(2020, 1, 1)})
        {'Monday': 0.6065306597126334, 'Tuesday': 0.36787944117144233, 'Wednesday': 0.1353352832366127,
        'Thursday': 0.01831563888873418, 'Friday': 0.00033546262790251185, 'Saturday': 3.354626279025118e-06,
        'Sunday': 2.0611536224385582e-08}
    """
    # Monday is the first day, i.e., 0:
    k = list(x.keys())[0]
    return {calendar.day_name[weekday]: math.exp(-((x[k].weekday() - weekday) ** 2)) for weekday in range(0, 7)}