SKLearner Home | About | Contact | Examples

Configure KNeighborsRegressor "p" Parameter

The p parameter in scikit-learn’s KNeighborsRegressor controls the distance metric used to find the nearest neighbors.

KNeighborsRegressor is a regression algorithm that predicts the target value based on the average of the target values of the k-nearest neighbors in the feature space. The p parameter determines the power parameter for the Minkowski distance metric.

Generally, p=2 corresponds to the Euclidean distance, while p=1 corresponds to the Manhattan distance. Different values of p can lead to different results in terms of prediction accuracy and computational efficiency.

The default value for p is 2.

In practice, values of 1 and 2 are most commonly used, but other values can be explored depending on the specific dataset and problem.

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=5, 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 p values
p_values = [1, 2, 3]
mse_scores = []

for p in p_values:
    knn = KNeighborsRegressor(p=p)
    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"p={p}, MSE: {mse:.3f}")

Running the example gives an output like:

p=1, MSE: 337.369
p=2, MSE: 261.960
p=3, MSE: 253.865

The key steps in this example are:

  1. Generate a synthetic regression dataset with noise
  2. Split the data into train and test sets
  3. Train KNeighborsRegressor models with different p values
  4. Evaluate the mean squared error (MSE) for each model on the test set

Some tips and heuristics for setting p:

Issues to consider:



See Also