Menu

Building a K-Nearest Neighbours Model

Building a K-Nearest Neighbours Model

With the data split and ready, it is time to build the actual model. This project uses K Nearest Neighbors, since it works especially well on small, clean datasets like this one.

from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)

Code Explanation

  • KNeighborsClassifier(n_neighbors=3) creates the model, checking the 3 closest data points whenever it makes a prediction.
  • model.fit(X_train, y_train) trains the model, allowing it to learn the relationship between the flower measurements and their species.

Once training is complete, the model is ready to make predictions on new data.

predictions = model.predict(X_test)
print(predictions)

model.predict(X_test) uses the trained model to predict species for the test data, which it has never seen before. After generating these predictions, the next lesson checks exactly how accurate they actually are.


Machine Learning Tutorial

AP

Abhishek Pati



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details