2
$\begingroup$

I am trying to use the K-Neighbouts for regression and I find to my surprise that not shuffling the training data has a huge effect on the quality of the prediction.

With shuffling. 98% training data: enter image description here

Without shuffling the data (sequential data). 98% training data: enter image description here

I am using the Sklearn library in python, and the shuffling function:

train, test = train_test_split(data, test_size=0.02)

Why am I getting this results?

Is the clustering "initial guess" the issue here?

The test suggests that depending on the order the data is fed to the algorithm the neighbours change. I would expect this not to happen.

PS: In the problem I want to predict both the sign of the values and their magnitude, hence the right plot and its sign prediction histogram.

Code without shuffling

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler

df = SOME_PANDAS_DATAFRAME

# split in training and test
train_proportion = 0.98
train_size = int(len(df) * train_proportion)
test_size = len(df) - train_size
train, test = df.iloc[0:train_size, :], df.iloc[train_size:len(df), :]

trainX = train[in_series_train].values
trainY = train[out_series_train].values.reshape(-1)
testX = test[in_series_test].values
testY = test[out_series_test].values.reshape(-1)

# fit model no training data
n_neighbors = 5
leaf_size = 6
p = 2

model = KNeighborsRegressor(n_neighbors=n_neighbors, weights='distance', algorithm='auto',
                            leaf_size=leaf_size, p=p, metric='minkowski', n_jobs=1)

scaler = RobustScaler()

print('Fitting...')
trainX_ = scaler.fit_transform(trainX)
model.fit(trainX_, trainY)

print('Predicting...')
testX_ = scaler.fit_transform(testX)
predY = model.predict(testX_)

Variation to shuffle the data

train, test = train_test_split(df, test_size=0.02)

This is the only code variation

$\endgroup$
2
  • $\begingroup$ Can you post more of your code? Or, can you verify that the steps you took follow this example? dataquest.io/blog/k-nearest-neighbors-in-python $\endgroup$
    – Jon
    Commented Dec 15, 2016 at 17:54
  • $\begingroup$ I added the relevant code. It follows the steps as far as I can tell. $\endgroup$ Commented Dec 16, 2016 at 8:37

1 Answer 1

2
$\begingroup$

After giving it a lot of thought, I found the problem.

What I am trying to forecast is a value of a magnitude at a certain hour of the day given a number of inputs. I turns out that when the data is shuffled, the neighbours are the values of the magnitude for at other hours of the same day. And those values are the ones I need to forecast.

I guess the lesson here is that if you want to forecast time series you cannot shuffle the data.

$\endgroup$
3
  • 1
    $\begingroup$ I'm not sure I 100% understand your answer, is it that in the "non-shuffling" case you're trying to predict the future, but when doing shuffling you're just trying to fill in missing points in the past? $\endgroup$
    – einar
    Commented Dec 16, 2016 at 12:03
  • $\begingroup$ Plus also, you could look into the block bootstrap and its variants if you want to do resampling/data perturbation/"shuffling" in a time series setting $\endgroup$
    – einar
    Commented Dec 16, 2016 at 12:05
  • $\begingroup$ Imagine you have 4 weeks data in hourly steps. To test the method you pick 3 weeks to train and the last week to forecast. If you shuffle the 4 weeks data into train and test sets, you'll have data from the fourth week in the train set, hence hours from the 4th week are used to predict other hours from the fourth week having those hours a great similarity. By not shuffling we won't have any info about how the hours in the fourth week look like. $\endgroup$ Commented Dec 16, 2016 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.