text_plain_output_1.png
from scipy.interpolate import griddata

from sklearn.metrics import mean_squared_error

from sklearn.multioutput import MultiOutputRegressor

from sklearn.neighbors import KNeighborsRegressor

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

traindf = pd.read_csv('../input/train.csv')
testdf = pd.read_csv('../input/test.csv')
totalRows = traindf[' y'].max()
totalCols = traindf['x'].max()
trainData = traindf.iloc[:, 2:]
trainlabels = traindf.iloc[:, 0:2]
testData = testdf.iloc[:, 2:]
testlabels = testdf.iloc[:, 0:2]
dimOfdata = trainData.shape[1]
numberOfSamples = len(trainlabels)

train_X = trainData.values
train_Y = trainlabels.values
test_X = testData.values
test_Y = testlabels.values

from scipy.interpolate import griddata
room_y = np.zeros(((totalRows + 1) * (totalCols + 1), 2))
k = 0
for i in range(totalCols + 1):
    for j in range(totalRows + 1):
        room_y[k, :] = [i, j]
        k += 1
room_x = griddata(train_Y, train_X, room_y, method='cubic')
room_x = np.nan_to_num(room_x)

k = 4
model = MultiOutputRegressor(KNeighborsRegressor(k))
model.fit(train_x, train_y)

pred_results = model.predict(val_x)

resultdf = pd.read_csv('../input/sample_submission.csv')
realy, realx = (resultdf.iloc[:, 0], resultdf.iloc[:, 1])
testdf[' y'] = pred_results[:, 1]
testdf['x'] = pred_results[:, 0]
data = {'realy': realy, 'realx': realx, 'predicty': pred_results[:, 0], 'predictx': pred_results[:, 1]}
df = pd.DataFrame(data)

from sklearn.metrics import mean_squared_error
predicts = testdf.iloc[:, 0:2]
trues = resultdf.iloc[:, 0:2]
df['mse'] = df.apply(lambda x: mean_squared_error([x['realx'], x['realy']], [x['predictx'], x['predicty']]), axis=1)
df.to_csv('./submission.csv')

imageSize = (totalRows + 1, totalCols + 1)
testlabels = resultdf.iloc[:, 0:2]
def getRouteImage():
    image = np.full(imageSize, 128)
    for index, lables in testlabels.iterrows():
        image[lables[' y']][lables['x']] = 80
    return image
image = getRouteImage()
for predict_y, predict_x in predicts.iloc[:, 0:2].values:
    predict_y, predict_x = (to_int(predict_y), to_int(predict_x))
    image[predict_x][predict_y] = 255
plt.figure(figsize=(20, 20))
plt.title('images')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')