In [297]:
import pandas as pd
import numpy as np
import scipy
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score, train_test_split, KFold
from sklearn import neighbors
from sklearn import metrics
import matplotlib.pyplot as plt
%matplotlib inline
music = pd.DataFrame()
music['duration'] = [184, 134, 243, 186, 122, 197, 294, 382, 102, 264,
205, 110, 307, 110, 397, 153, 190, 192, 210, 403,
164, 198, 204, 253, 234, 190, 182, 401, 376, 102]
music['loudness'] = [18, 34, 43, 36, 22, 9, 29, 22, 10, 24,
20, 10, 17, 51, 7, 13, 19, 12, 21, 22,
16, 18, 4, 23, 34, 19, 14, 11, 37, 42]
music['bpm'] = [ 105, 90, 78, 75, 120, 110, 80, 100, 105, 60,
70, 105, 95, 70, 90, 105, 70, 75, 102, 100,
100, 95, 90, 80, 90, 80, 100, 105, 70, 65]
In [299]:
#Initiating the cross validation generator, N splits = 5
kf = KFold(5)
# Convert data
X = np.array(music.iloc[:, 0:2])
Y = np.array(music['bpm'])
# instantiate learning model
knn = neighbors.KNeighborsRegressor(n_neighbors=5)
# fitting the model
knn.fit(X, Y)
#Scores for the cross validation
scores = cross_val_score(knn, X,Y, cv=kf, scoring = 'r2')
scores = cross_val_score(knn, X, Y, cv=5)
print("Weighted Accuracy: %0.2f (+/- %0.2f)" % (score.mean(), score.std() * 2))
In [300]:
# Same model with weights.
knn_w = neighbors.KNeighborsRegressor(n_neighbors=5, weights='uniform', metric='minkowski')
X = pd.DataFrame(music,columns= ['duration','loudness'])
Y = music.bpm
knn_w.fit(X, Y)
score_w = cross_val_score(knn_w, X, Y, cv=5)
print("Weighted Accuracy: %0.2f (+/- %0.2f)" % (score_w.mean(), score_w.std() * 2))