MulaiMulai sekarang secara gratis

Visualizing model complexity

Now you have calculated the accuracy of the KNN model on the training and test sets using various values of n_neighbors, you can create a model complexity curve to visualize how performance changes as the model becomes less complex!

The variables neighbors, train_accuracies, and test_accuracies, which you generated in the previous exercise, have all been preloaded for you. You will plot the results to aid in finding the optimal number of neighbors for your model.

Latihan ini adalah bagian dari kursus

Supervised Learning with scikit-learn

Lihat Kursus

Petunjuk latihan

  • Add a title "KNN: Varying Number of Neighbors".
  • Plot the .values() method of train_accuracies on the y-axis against neighbors on the x-axis, with a label of "Training Accuracy".
  • Plot the .values() method of test_accuracies on the y-axis against neighbors on the x-axis, with a label of "Testing Accuracy".
  • Display the plot.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Add a title
plt.title("____")

# Plot training accuracies
plt.plot(____, ____, label="____")

# Plot test accuracies
plt.plot(____, ____, label="____")

plt.legend()
plt.xlabel("Number of Neighbors")
plt.ylabel("Accuracy")

# Display the plot
____
Edit dan Jalankan Kode