Build Your First ML Model

    Yash Kabra4 min readUpdated

    Building your first machine learning model is more approachable than it sounds. With Python and scikit-learn, it comes down to five steps: load the data, split it into training and test sets, choose and train a model, evaluate it on data it has not seen, and use it to predict. This guide walks through all five with a complete, working example.

    The five steps

    Almost every supervised ML project follows the same skeleton:

    1. Load the data into a clean, numeric form.
    2. Split it into a training set (to learn from) and a test set (to check honestly).
    3. Choose and train a model on the training set.
    4. Evaluate it on the test set.
    5. Predict on new inputs.

    Keeping this skeleton in mind stops the process feeling like magic. Let us build one end to end.

    A complete example

    We will use the built-in Iris dataset — measurements of flowers, with the species as the label — and train a model to predict the species.

    # Build your first classifier end to end with scikit-learn.
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.metrics import accuracy_score
    
    # Step 1 — Load: X = measurements, y = species label.
    X, y = load_iris(return_X_y=True)
    
    # Step 2 — Split: keep 25% aside to test fairly.
    # random_state makes the split reproducible.
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.25, random_state=42
    )
    
    # Step 3 — Choose and train the model.
    model = LogisticRegression(max_iter=200)
    model.fit(X_train, y_train)
    
    # Step 4 — Evaluate on the unseen test set.
    predictions = model.predict(X_test)
    accuracy = accuracy_score(y_test, predictions)
    print("Test accuracy:", round(accuracy, 2))   # e.g. about 0.97
    
    # Step 5 — Predict on a new flower's measurements.
    new_flower = [[5.1, 3.5, 1.4, 0.2]]
    print("Predicted class:", model.predict(new_flower)[0])
    

    That is a full working model. The fit / predict / score pattern is the same for nearly every scikit-learn algorithm, so once you know it for one model you know it for most.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Why the train/test split matters

    The single most important idea here is the split. We never judge the model on the data it learned from. If a student memorises the exact answers to a practice test, scoring full marks on it proves nothing about a new exam. The test set is that new exam. A model that does well on training data but poorly on the test set is overfitting — it memorised noise instead of learning the real pattern. To prepare data for this step well, see data preprocessing.

    Reading the result

    An accuracy of, say, 0.97 means the model predicted correctly for 97% of the test flowers. Whether that is good depends on the problem and the baseline. For example, if 90% of emails were not spam, a lazy model guessing "not spam" every time would already score 0.90, so beating that baseline is what counts. Always compare against a sensible baseline rather than reading the number alone.

    Trying a different model

    Swapping models is easy because the interface is consistent. To try a decision tree instead, change two lines:

    from sklearn.tree import DecisionTreeClassifier
    
    model = DecisionTreeClassifier(random_state=42)
    model.fit(X_train, y_train)
    # everything else stays the same
    

    This makes experimenting straightforward: try a few models, evaluate each the same way, and keep the one that performs best on the test set.

    Common mistakes

    • Testing on training data. The most common beginner error. Always evaluate on the held-out test set.
    • Forgetting random_state. Without it, your split changes each run and results are not reproducible. Set it while learning.
    • Reading accuracy without a baseline. A high number can be meaningless if one class dominates. Compare against the simplest possible guess.
    • Skipping data preparation. Real data needs cleaning, encoding, and often scaling before this code will work.

    FAQ

    Do I need a big dataset for my first model? No. Built-in datasets like Iris are perfect for learning the workflow before you tackle real data.

    Which model should a beginner start with? Logistic regression for classification and linear regression for numbers are simple, fast, and easy to understand. See classification vs regression.

    How do I save a trained model? Use Python's joblib library: from joblib import dump; dump(model, "model.joblib"). Avoid the old, removed sklearn.externals.joblib path.

    Keep learning

    Explore the full AI & machine learning hub for more guides, or join the waitlist for our structured Artificial Intelligence course at Infoplanet, Jalgaon, to build real ML projects step by step with mentor support.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Yash Kabra

    Founder, Atlee Technologies

    Yash Kabra is the founder of Atlee Technologies, a product studio that ships SaaS products end-to-end. He owns products from strategy through launch and growth — including Infoplanet, TrackRise and Perqee — and teaches AI, Machine Learning and Data Science at Infoplanet with a focus on how these tools are used to build real products.

    Related guides