core

Stores the main class to boost a estimator

KNNSpatialBooster

 KNNSpatialBooster (n_neighbors:int=5, temperature:float=0.2,
                    n_loops:int=50, verbose:bool=False,
                    estimator:Any=RandomForestRegressor(),
                    estimator_output_1d_array:bool=True,
                    spatial_features:Union[Literal['*'],List[int]]='*',
                    remove_target_spatial_cols:bool=False,
                    remove_neighbor_spatial_cols:bool=True)

A KNN Spatial Booster.

You can use it with any model. It uses the training dataset to improve the spatial perception of the model adding more features. Remember to use warm_start=True if your model is a scikit-learn model.

Read more in the KNNSpatialBooster docs.

Type Default Details
n_neighbors int 5 The number of neighbors to use as feature. Also known as “k”.
temperature float 0.2 If temperature=0, exactly “k” nearest neighbors are selected. If
temperature is higher, other neighbors can be selected.
n_loops int 50 Quantity of .fit() calls. If temperature>0, will create a new
dataset foreach loop.
verbose bool False If set to True, will print the current loop.
estimator typing.Any RandomForestRegressor() Any Estimator as defined by scikit-learn with warm_start=True.
estimator_output_1d_array bool True Used to be compatible with estimators that needs 1d arrays in .fit().
spatial_features typing.Union[typing.Literal[’*’], typing.List[int]] * Define which columns should be used as coordinates for KNN.
remove_target_spatial_cols bool False Set to True if you believe the model should not use the original
spatial features in the training process.
remove_neighbor_spatial_cols bool True Set to True if you believe the model should not use the neighbors
spatial features in the training process.

KNNSpatialBooster.fit

 KNNSpatialBooster.fit (X:numpy.ndarray, Y:numpy.ndarray)

Fit model for X and Y.

Type Details
X ndarray Shape must be (number of samples,) or (number of samples, number_of_features).
Y ndarray Shape must be (number of samples,) or (number of samples, number of outputs).
Returns None

KNNSpatialBooster.predict

 KNNSpatialBooster.predict (X:numpy.ndarray)

Predict for X.

Type Details
X ndarray Shape must be (number of samples,) or (number of samples, number_of_features).
Returns ndarray Predicted value for given X.

KNNSpatialBooster.score

 KNNSpatialBooster.score (X:numpy.ndarray, Y:numpy.ndarray)

Calculate score for X and Y. It uses the estimator score function.

Type Details
X ndarray Shape must be (number of samples,) or (number of samples, number_of_features).
Y ndarray Shape must be (number of samples,) or (number of samples, number of outputs).
Returns float Estimator score.
KNNSpatialBooster.random_first_k(10, 3, 0.5)
array([[0, 2, 3],
       [0, 2, 3],
       [0, 1, 2],
       [0, 3, 4],
       [1, 2, 3],
       [0, 1, 2],
       [1, 3, 4],
       [0, 1, 3],
       [0, 2, 3],
       [0, 1, 2]])
KNNSpatialBooster.get_neighbors(
    np.array([
        [0, 1, 10],
        [0, 1, 15],
        [0, 1, 17],
        [0, 2, 20],
        [0, 2, 25],
        [0, 2, 27],
        [2, 0, 30],
        [2, 0, 35],
        [2, 0, 37],
    ]),
    np.array([
        [20],
        [30],
        [35],
        [40],
        [50],
        [55],
        [60],
        [70],
        [75],
    ]),
    np.array([
        [1, 1, 40],
        [2, 1, 50],
    ]),
    [0, 1],
    3,
    remove_first_neighbor=True
)
array([[[10.        , 15.        , 27.        , 35.        ,
         30.        , 25.        , 20.        ],
        [30.        , 35.        , 17.        , 10.        ,
         15.        , 27.        , 25.        ]],

       [[20.        , 30.        , 55.        , 70.        ,
         60.        , 50.        , 40.        ],
        [60.        , 70.        , 35.        , 20.        ,
         30.        , 55.        , 50.        ]],

       [[ 0.5       ,  0.5       ,  0.41421356,  0.41421356,
          0.41421356,  0.41421356,  0.41421356],
        [ 0.5       ,  0.5       ,  0.33333333,  0.33333333,
          0.33333333,  0.30901699,  0.30901699]]])