src.fairreckitlib.data.ratings.range_converter

This module contains the range converting functionality.

Classes:

RangeConverter: can convert ratings to be within in a specified range.

Functions:

create_range_converter: create an instance of the class (factory creation compatible).
create_range_converter_params: create range converter config parameters.

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

 1"""This module contains the range converting functionality.
 2
 3Classes:
 4
 5    RangeConverter: can convert ratings to be within in a specified range.
 6
 7Functions:
 8
 9    create_range_converter: create an instance of the class (factory creation compatible).
10    create_range_converter_params: create range converter config parameters.
11
12This program has been developed by students from the bachelor Computer Science at
13Utrecht University within the Software Project course.
14© Copyright Utrecht University (Department of Information and Computing Sciences)
15"""
16
17from typing import Any, Dict
18
19import pandas as pd
20
21from ...core.config.config_parameters import ConfigParameters
22from .base_converter import RatingConverter
23
24
25class RangeConverter(RatingConverter):
26    """Range Converter on data ratings.
27
28    Converts the rating column of the dataframe to a specified range.
29    """
30
31    def run(self, dataframe: pd.DataFrame) -> pd.DataFrame:
32        """Convert ratings in the dataframe.
33
34        Takes the max value and divides all values so that
35        they all fall within a range of [0,1], unless another upper
36        bound is given by the parameters on creation. The rating can then
37        also be multiplied by a scalar, e.g. when an implicit rating is needed.
38
39        Args:
40            dataframe: a df that should contain a 'rating' column.
41
42        Returns:
43            the converted dataframe.
44        """
45        upper_bound = self.params['upper_bound']
46        max_rating = dataframe.max()['rating']
47        dataframe['rating'] = dataframe['rating'].apply(lambda x : x / max_rating * upper_bound)
48
49        return dataframe
50
51
52def create_range_converter(name: str, params: Dict[str, Any], **_) -> RangeConverter:
53    """Create the Range Converter.
54
55    Args:
56        name: the name of the converter.
57        params: containing the following name-value pairs:
58            upper_bound(float): the upper bound of the range restriction.
59
60    Returns:
61        the data range converter.
62    """
63    return RangeConverter(name, params)
64
65
66def create_range_converter_params(**kwargs) -> ConfigParameters:
67    """Create the parameters of the range converter.
68
69    Returns:
70        the configuration parameters of the converter.
71    """
72    max_rating = kwargs['dataset'].get_matrix_config(kwargs['matrix_name']).ratings.rating_max
73
74    params = ConfigParameters()
75    params.add_number('upper_bound', float, max_rating, (1.0, max_rating))
76    return params
26class RangeConverter(RatingConverter):
27    """Range Converter on data ratings.
28
29    Converts the rating column of the dataframe to a specified range.
30    """
31
32    def run(self, dataframe: pd.DataFrame) -> pd.DataFrame:
33        """Convert ratings in the dataframe.
34
35        Takes the max value and divides all values so that
36        they all fall within a range of [0,1], unless another upper
37        bound is given by the parameters on creation. The rating can then
38        also be multiplied by a scalar, e.g. when an implicit rating is needed.
39
40        Args:
41            dataframe: a df that should contain a 'rating' column.
42
43        Returns:
44            the converted dataframe.
45        """
46        upper_bound = self.params['upper_bound']
47        max_rating = dataframe.max()['rating']
48        dataframe['rating'] = dataframe['rating'].apply(lambda x : x / max_rating * upper_bound)
49
50        return dataframe

Range Converter on data ratings.

Converts the rating column of the dataframe to a specified range.

def run( self, dataframe: pandas.core.frame.DataFrame) -> pandas.core.frame.DataFrame:
32    def run(self, dataframe: pd.DataFrame) -> pd.DataFrame:
33        """Convert ratings in the dataframe.
34
35        Takes the max value and divides all values so that
36        they all fall within a range of [0,1], unless another upper
37        bound is given by the parameters on creation. The rating can then
38        also be multiplied by a scalar, e.g. when an implicit rating is needed.
39
40        Args:
41            dataframe: a df that should contain a 'rating' column.
42
43        Returns:
44            the converted dataframe.
45        """
46        upper_bound = self.params['upper_bound']
47        max_rating = dataframe.max()['rating']
48        dataframe['rating'] = dataframe['rating'].apply(lambda x : x / max_rating * upper_bound)
49
50        return dataframe

Convert ratings in the dataframe.

Takes the max value and divides all values so that they all fall within a range of [0,1], unless another upper bound is given by the parameters on creation. The rating can then also be multiplied by a scalar, e.g. when an implicit rating is needed.

Args: dataframe: a df that should contain a 'rating' column.

Returns: the converted dataframe.

def create_range_converter( name: str, params: Dict[str, Any], **_) -> src.fairreckitlib.data.ratings.range_converter.RangeConverter:
53def create_range_converter(name: str, params: Dict[str, Any], **_) -> RangeConverter:
54    """Create the Range Converter.
55
56    Args:
57        name: the name of the converter.
58        params: containing the following name-value pairs:
59            upper_bound(float): the upper bound of the range restriction.
60
61    Returns:
62        the data range converter.
63    """
64    return RangeConverter(name, params)

Create the Range Converter.

Args: name: the name of the converter. params: containing the following name-value pairs: upper_bound(float): the upper bound of the range restriction.

Returns: the data range converter.

def create_range_converter_params( **kwargs) -> src.fairreckitlib.core.config.config_parameters.ConfigParameters:
67def create_range_converter_params(**kwargs) -> ConfigParameters:
68    """Create the parameters of the range converter.
69
70    Returns:
71        the configuration parameters of the converter.
72    """
73    max_rating = kwargs['dataset'].get_matrix_config(kwargs['matrix_name']).ratings.rating_max
74
75    params = ConfigParameters()
76    params.add_number('upper_bound', float, max_rating, (1.0, max_rating))
77    return params

Create the parameters of the range converter.

Returns: the configuration parameters of the converter.