#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Apr 18 11:41:39 2021 @author: doug """ import numpy as np import scipy.io as sio from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsRegressor from matplotlib import pyplot import pylab as plt # from sklearn import metrics # For accuracy_score and confusion_matrix #Load dataset mat_contents=sio.loadmat("knnregressdata1.mat") X=mat_contents['age'] T=mat_contents['bmd'] X_train, X_test, t_train, t_test = train_test_split(X, T, test_size=0.3) knnR=KNeighborsRegressor(n_neighbors=4) knnR.fit(X_train,t_train) t_score=knnR.score(X_test,t_test) # New data to plot the full function: XX=np.linspace(np.max(X),np.min(X),200) temp=XX.reshape(1,-1) t_out=knnR.predict(temp.T) # This works, but is a bit awkward pyplot.plot(temp.T,t_out,color='r') plt.scatter(X,T,color='k') plt.show()