ComenzarEmpieza gratis

Hyperparameters of KNN

To apply the concepts learned in the prior exercise, it is good practice to try out learnings on a new algorithm. The k-nearest-neighbors algorithm is not as popular as it used to be but can still be an excellent choice for data that has groups of data that behave similarly. Could this be the case for our credit card users?

In this case you will try out several different values for one of the core hyperparameters for the knn algorithm and compare performance.

You will have available:

  • X_train, X_test, y_train, y_test DataFrames

Este ejercicio forma parte del curso

Hyperparameter Tuning in Python

Ver curso

Instrucciones del ejercicio

  • Build a knn estimator for the following values of n_neighbors [5,10,20].
  • Fit each to the training data and produce predictions.
  • Get an accuracy score for each model and print them out.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Build a knn estimator for each value of n_neighbours
knn_5 = KNeighborsClassifier(n_neighbors=____)
knn_10 = KNeighborsClassifier(n_neighbors=____)
knn_20 = KNeighborsClassifier(n_neighbors=____)

# Fit each to the training data & produce predictions
knn_5_predictions = knn_5.fit(_____, _____).predict(_____)
knn_10_predictions = knn_10.fit(_____, _____).predict(_____)
knn_20_predictions = knn_20.fit(_____, _____).predict(_____)

# Get an accuracy score for each of the models
knn_5_accuracy = accuracy_score(y_test, _____)
knn_10_accuracy = accuracy_score(y_test, _____)
knn_20_accuracy = accuracy_score(y_test, _____)
print("The accuracy of 5, 10, 20 neighbours was {}, {}, {}".format(_____, _____, _____))
Editar y ejecutar código