Free course · Lesson 3 of 5
Key ML Algorithms & When to Use Them
In this lesson you will
- Describe how common algorithms make predictions
- Choose a reasonable first algorithm for a problem
Different problems call for different algorithms. Here are the most common ones you'll encounter as a beginner. The key is knowing which algorithm to apply based on your data and your goal.
- Linear Regression: Predicts a continuous number (e.g., house price, temperature). Simple and interpretable — you can see exactly which features matter most.
- Decision Trees: Works for both classification (spam/not spam) and regression (price prediction). Easy to understand — like a flowchart of if/else questions.
- K-Nearest Neighbors (KNN): Classifies based on the "votes" of the K closest data points. Good for small datasets with clear clusters.
- K-Means Clustering: Unsupervised — groups data into K clusters based on similarity. Used for customer segmentation and pattern discovery.
💡 Practical Skill: Start with Decision Trees — they are easy to understand, require little data preprocessing, and work on both classification and regression problems. Use scikit-learn's DecisionTreeClassifier or DecisionTreeRegressor.
# Classic Iris flower classification using a Decision Tree# Install: pip install scikit-learn from sklearn.datasets import load_irisfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_scoreimport numpy as np # Load the famous Iris datasetiris = load_iris()X = iris.data # Features: sepal length, sepal width, petal length, petal widthy = iris.target # Labels: 0=setosa, 1=versicolor, 2=virginica # Split the dataX_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42) # Train a Decision Treetree = DecisionTreeClassifier(max_depth=3, random_state=42)tree.fit(X_train, y_train) # Evaluatepredictions = tree.predict(X_test)accuracy = accuracy_score(y_test, predictions)print(f"Model Accuracy: {accuracy * 100:.1f}%")print(f"Tested on {len(X_test)} flowers") # Predict a new flower# Example: [5.1, 3.5, 1.4, 0.2] -> should be setosa (0)new_flower = np.array([[5.1, 3.5, 1.4, 0.2]])pred = tree.predict(new_flower)[0]species = iris.target_names[pred]print(f"\nNew flower prediction: {species}") # List feature importancefor name, importance in zip(iris.feature_names, tree.feature_importances_): if importance > 0: print(f" {name}: importance = {importance:.3f}")
Practice exercise
For three problems, choose a first algorithm (linear regression, logistic regression, k-NN or a decision tree) and give one reason for each choice.
Key takeaways
- Start simple and compare with a baseline
- Decision trees are easy to explain; k-NN needs scaled features
- No algorithm is best for every problem
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.