Home > Mobile >  Computing MAE with scikit-learn
Computing MAE with scikit-learn

Time:09-05

I am stuck on this problem:

import numpy as np
from sklearn.neighbors import KNeighborsRegressor

def mae(prediction, target):

   return np.sum(np.abs(prediction - target)) 

knn_model = KNeighborsRegressor(n_neighbors=k)

prediction = knn_model.fit(X_train,y_train)
val_mae = mae(prediction, y_val)

CodePudding user response:

.fit returns the actual model object, you need to call .predict to get predictions

knn_model.fit(X_train,y_train)
prediction = knn_model.predict(X_val)
val_mae = mae(prediction, y_val)
  • Related