Sign in to save your place.
Master the key classification algorithms: Logistic Regression, Naive Bayes, SVM, and KNN. Learn when to use each one!
Despite its name, Logistic Regression is a classification algorithm, not regression! It predicts the probability of belonging to a class.
Logistic Regression uses the "sigmoid" function to squeeze any number into a probability between 0 and 1.
Think of it as a volume knob that only goes from 0% to 100% - it can't go beyond!
from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # Create and train model log_reg = LogisticRegression(random_state=42) log_reg.fit(X_train, y_train) # Predict y_pred = log_reg.predict(X_test) # Get probabilities y_proba = log_reg.predict_proba(X_test) print("Probability of class 0 and 1:", y_proba[0]) # Evaluate print("Accuracy:", accuracy_score(y_test, y_pred)) print(classification_report(y_test, y_pred))
Naive Bayes uses probability and Bayes' theorem to classify. It's "naive" because it assumes features are independent (which they usually aren't!).
A doctor sees symptoms: fever, cough, fatigue. Based on past patients:
Naive Bayes combines these probabilities to make a diagnosis!
from sklearn.naive_bayes import GaussianNB, MultinomialNB # Gaussian Naive Bayes (for continuous features) gnb = GaussianNB() gnb.fit(X_train, y_train) print("Gaussian NB Accuracy:", gnb.score(X_test, y_test)) # Multinomial Naive Bayes (for count data, like text) # Great for text classification! from sklearn.feature_extraction.text import CountVectorizer texts = ["I love this movie", "Terrible film", "Great acting", "Awful plot"] labels = [1, 0, 1, 0] vectorizer = CountVectorizer() X_text = vectorizer.fit_transform(texts) mnb = MultinomialNB() mnb.fit(X_text, labels) # Predict new text new_text = vectorizer.transform(["I enjoyed this"]) print("Prediction:", mnb.predict(new_text)) # Likely: [1] (positive)
Pros: Fast, works well with small data, great for text classification
Cons: "Naive" independence assumption rarely holds, may produce poor probability estimates
SVM finds the optimal boundary (hyperplane) that separates classes with maximum margin.
Imagine separating cats from dogs with a fence. SVM builds the fence:
from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler # SVM requires scaling! scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Linear SVM svm_linear = SVC(kernel='linear', C=1.0) svm_linear.fit(X_train_scaled, y_train) print("Linear SVM:", svm_linear.score(X_test_scaled, y_test)) # RBF Kernel SVM (for non-linear data) svm_rbf = SVC(kernel='rbf', C=1.0, gamma='scale') svm_rbf.fit(X_train_scaled, y_train) print("RBF SVM:", svm_rbf.score(X_test_scaled, y_test)) # Get probabilities (slower) svm_proba = SVC(kernel='rbf', probability=True) svm_proba.fit(X_train_scaled, y_train) probabilities = svm_proba.predict_proba(X_test_scaled)
| Kernel | Use Case | Description |
|---|---|---|
| linear | Linearly separable data | Straight line/plane boundary |
| rbf (default) | Most common, non-linear | Creates curved boundaries |
| poly | Polynomial relationships | Higher-degree curves |
KNN is the simplest algorithm: To classify a new point, look at its K nearest neighbors and vote!
For a full deep dive (how it works step-by-step, distance metrics, choosing K, scaling, and Python code), see the dedicated k-Nearest Neighbors (kNN) lesson.
"Tell me who your friends are, and I'll tell you who you are."
If most of your 5 nearest neighbors like pizza, you probably like pizza too!
from sklearn.neighbors import KNeighborsClassifier # KNN also needs scaling scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Create KNN with K=5 knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X_train_scaled, y_train) print("KNN Accuracy:", knn.score(X_test_scaled, y_test)) # Finding optimal K from sklearn.model_selection import cross_val_score k_range = range(1, 20) scores = [] for k in k_range: knn = KNeighborsClassifier(n_neighbors=k) cv_scores = cross_val_score(knn, X_train_scaled, y_train, cv=5) scores.append(cv_scores.mean()) # Plot to find best K plt.plot(k_range, scores) plt.xlabel('K') plt.ylabel('CV Accuracy') plt.title('Finding Optimal K') plt.show()
| Algorithm | Best For | Pros | Cons |
|---|---|---|---|
| Logistic Regression | Binary classification, need probabilities | Fast, interpretable, probabilistic | Assumes linear relationship |
| Naive Bayes | Text classification, small data | Very fast, works with small data | Independence assumption |
| SVM | High-dimensional data, clear margins | Works well in high dimensions | Slow on large data, needs scaling |
| KNN | Simple problems, no assumptions | No training, intuitive | Slow prediction, curse of dimensionality |
# Quick comparison of all algorithms from sklearn.linear_model import LogisticRegression from sklearn.naive_bayes import GaussianNB from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import cross_val_score models = { 'Logistic Regression': LogisticRegression(), 'Naive Bayes': GaussianNB(), 'SVM': SVC(), 'KNN': KNeighborsClassifier() } print("Model Comparison (5-fold CV):\n") for name, model in models.items(): scores = cross_val_score(model, X_scaled, y, cv=5) print(f"{name:20s}: {scores.mean():.3f} ± {scores.std():.3f}")
| Metric | Formula | When to Use |
|---|---|---|
| Accuracy | (TP + TN) / Total | Balanced classes |
| Precision | TP / (TP + FP) | Cost of false positives is high |
| Recall | TP / (TP + FN) | Cost of false negatives is high |
| F1 Score | 2 × (Prec × Rec) / (Prec + Rec) | Balance of precision and recall |
| AUC-ROC | Area under ROC curve | Comparing models, imbalanced data |
from sklearn.metrics import ( accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, classification_report ) # All metrics at once print(classification_report(y_test, y_pred)) # Individual metrics print("Accuracy:", accuracy_score(y_test, y_pred)) print("Precision:", precision_score(y_test, y_pred)) print("Recall:", recall_score(y_test, y_pred)) print("F1:", f1_score(y_test, y_pred)) # Confusion Matrix cm = confusion_matrix(y_test, y_pred) print("Confusion Matrix:\n", cm) # [[TN, FP], # [FN, TP]]
In one sentence: when would you care more about recall than precision? Give a real-world example (e.g. medical test, spam filter).
A probability of 0.9 should, on a calibrated model, mean what?