SKLearner Home | About | Contact | Examples

Configure KNeighborsRegressor "leaf_size" Parameter

The leaf_size parameter in scikit-learn’s KNeighborsRegressor affects the efficiency of the tree-based data structures used in neighbor searches.

KNeighborsRegressor is a regression model that predicts the target value based on the average of the nearest neighbors’ target values.

The leaf_size parameter determines the size of the leaf in the KDTree or BallTree, affecting the speed and memory of the construction and query processes.

The default value for leaf_size is 30. Commonly used values range from 20 to 50, depending on the dataset’s size and dimensionality.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different leaf_size values
leaf_size_values = [20, 30, 40, 50]
mse_scores = []

for leaf_size in leaf_size_values:
    knn = KNeighborsRegressor(leaf_size=leaf_size)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mse_scores.append(mse)
    print(f"leaf_size={leaf_size}, Mean Squared Error: {mse:.3f}")

Running the example gives an output like:

leaf_size=20, Mean Squared Error: 3728.344
leaf_size=30, Mean Squared Error: 3728.344
leaf_size=40, Mean Squared Error: 3728.344
leaf_size=50, Mean Squared Error: 3728.344

The key steps in this example are:

  1. Generate a synthetic regression dataset with relevant features.
  2. Split the data into train and test sets.
  3. Train KNeighborsRegressor models with different leaf_size values.
  4. Evaluate the mean squared error of each model on the test set.

Some tips and heuristics for setting leaf_size:

Issues to consider:



See Also