Skip to content

misc

get_regressor(name)

Returns a scikit-learn regressor based on the given name.

Parameters:

Name Type Description Default
name str

The name of the regressor. Supported names are: “linear”, “polynomial”, “random_forest”, and “kriging”.

required

Returns:

Name Type Description
object object

A scikit-learn regressor object.

Raises:

Type Description
ValueError

If an unknown regressor name is provided.

Example

from spotpython.utils.misc import get_regressor regressor = get_regressor(“linear”) print(type(regressor))

Source code in spotpython/utils/misc.py
 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
def get_regressor(name) -> object:
    """
    Returns a scikit-learn regressor based on the given name.

    Args:
        name (str): The name of the regressor.
            Supported names are: "linear", "polynomial", "random_forest", and "kriging".

    Returns:
        object: A scikit-learn regressor object.

    Raises:
        ValueError: If an unknown regressor name is provided.

    Example:
        >>> from spotpython.utils.misc import get_regressor
        >>> regressor = get_regressor("linear")
        >>> print(type(regressor))
        <class 'sklearn.linear_model._base.LinearRegression'>
    """
    if name == "linear":
        mdl = LinearRegression()
    elif name == "polynomial":
        degree_polyn = 2
        mdl = Pipeline([("poly", PolynomialFeatures(degree=degree_polyn)), ("linear", LinearRegression())])
    elif name == "random_forest":
        mdl = RandomForestRegressor()
    # elif name == "xgboost":
    #     mdl = xgb.XGBRegressor(objective="reg:squarederror", n_estimators=100, random_state=42)
    elif name == "kriging":
        mdl = Kriging()
    else:
        raise ValueError(f"Unknown regressor {name}")
    return mdl