0Pricing
Data Science Academy · Lección

k-Nearest Neighbors

Clasificar según los ejemplos más cercanos.

k-Nearest Neighbors es una lección gratuita de Data Science Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Data Science Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Data Science Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Judged by Your Neighbors

k-Nearest Neighbors classifies a point by looking at the examples closest to it. Birds of a feather end up in the same class. 🐦

No Real Training

kNN is a lazy learner: it just memorizes the data. The real work happens later, when a new point asks for a label.

Measure the Distance

To find who is near, the model computes a distance to every training point, most often plain straight-line distance.

Take a Vote

The closest k neighbors each cast a vote. Whichever class wins the majority becomes the prediction for the new point.

k Is the Key Choice

The letter k is how many neighbors you consult. It is the one knob that most shapes how kNN behaves.

Small k, Jumpy Borders

A tiny k, like 1, follows every wiggle in the data and reacts to noise, giving jagged, overfit boundaries.

Large k, Smoother Calls

A big k averages over many points for smoother decisions, but too large and it blurs the real differences between classes.

Scaling Is Not Optional

Distance is unfair if one feature spans thousands and another spans tenths. Always scale features so each counts equally.

Build the Classifier

Create it like any estimator and pick your n_neighbors right away. Here we consult the five closest points.

from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=5)

Fit, Then Predict

The familiar contract still applies: fit stores the data, predict finds neighbors and votes on the answer.

model.fit(X_train, y_train)
model.predict(X_test)

Watch the Cost

kNN is simple but can be slow at prediction time, since every guess compares the new point to all stored examples.

Quick Check

Let's pin down the parameter that defines kNN.

Recap

kNN predicts by letting the closest k neighbors vote. Choose k with care and scale your features, and it works well. 🎯

Preguntas frecuentes

¿La lección «k-Nearest Neighbors» es gratis?

Sí — el texto completo de «k-Nearest Neighbors» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Data Science Academy, actualiza a CoddyKit PRO. El curso de Data Science Academy incluye 4 lecciones en total.

¿Qué aprenderé en «k-Nearest Neighbors»?

Clasificar según los ejemplos más cercanos. Practicas Data Science Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Data Science Academy?

No se requiere experiencia previa. Data Science Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «k-Nearest Neighbors»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Data Science Academy?

Sí. Cada lección de Data Science Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Regresión logística para respuestas sí/no
  2. k-Nearest Neighbors
  3. Árboles de decisión y Random Forests
  4. Fundamentos de Gradient Boosting
← Volver a Data Science Academy