CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 7 min · free

Machine Learning in Python with Scikit-learn

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn what scikit-learn gives you, why data is split into train and test sets, how fit, predict and score work, and how to read the result. A score of 0.9 means 27 of 30 test flowers were right, and the mistakes matter as much as the number.

Follows the syllabus of: CBSE Class 11 Artificial Intelligence (843)

On screen in this lesson

What is machine learning?

The computer learns patterns from examples
We do not write the rules ourselves
Example: learn a fruit's type from its size and colour
The learned result is called a model

What scikit-learn provides

Ready datasets to practise with
Ready models: classification, regression, clustering
train_test_split() to divide the data
Metrics to measure how good a model is
Install: pip install scikit-learn; import sklearn

Features and labels

ColumnRoleExample
Petal lengthFeature1.4 cm
Petal widthFeature0.2 cm
SpeciesLabelsetosa

Why split the data?

A model can memorise the examples it saw
Training set: the examples it learns from
Test set: new examples kept hidden
Like a school exam with unseen questions

Reading the result

0.9 means 27 of 30 test flowers were right
Random guessing among 3 species: about 33%
Only 30 test flowers: 1 mistake = 3.3%
Which flowers went wrong matters too

Where the mistakes were

Real speciesCorrectMistakes
setosa7 of 7none
versicolor11 of 121 as virginica
virginica9 of 112 as versicolor

Quick answers

Is 90 percent accuracy good?

It depends. Random guessing among 3 species gives about 33 percent, but a 30-flower test set means one mistake moves the score by 3.3 percent.

What does a very large gap between training and test score mean?

The model memorised instead of learning.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Your first model scores ninety percent. Is that good or bad? By the end of this lesson, you will know how to answer. We will see what scikit learn gives us, split data for training and testing, and build a model with fit, predict and score. Then we will read the result properly.

First, a quick reminder of machine learning. The computer learns patterns from many examples. We do not write the rules by hand. For example, after seeing many fruits, it can guess a new fruit's type from its size and colour. What it learns is called a model.

Scikit learn is a free Python library for machine learning. It gives ready datasets, so you can practise without collecting data. It gives ready models for classification, regression and clustering. Classification predicts a category, regression predicts a number, and clustering makes groups. It has a function to split data, and metrics, which are ways to measure a model. You install it as scikit dash learn, but import it as S K learn.

We will use the famous iris flower dataset that comes with scikit learn. It has one hundred fifty flowers of three species. Petal length is a feature, which means an input the model looks at. Petal width is another feature, and there are four features in all. The species is the label, which means the answer we want the model to predict.

Load iris gives us the data. With return ex why equals True, it returns two parts. By custom, capital ex holds the features, and small why holds the labels. ex dot shape is one hundred fifty comma four, so there are one hundred fifty rows and four feature columns. Why dot shape is one hundred fifty, one label for each flower. The species are stored as numbers zero, one and two.

Why do we split the data? A model can simply memorise the examples it has already seen. So we keep two sets. The training set is what the model learns from. The test set is kept hidden until the end. It is like a board exam. If the questions were the same as the practice sheet, full marks would prove nothing.

Train test split divides the data for us. Test size equals zero point two keeps twenty percent for testing. So one hundred twenty flowers are for training, and thirty are for testing. The rows are shuffled first, and random state fixes that shuffle, so you get the same split every time. It returns four parts, in this order: ex train, ex test, why train and why test.

Every scikit learn project follows the same steps. First, load the features and labels. Second, split them into training and test sets. Third, fit the model on the training data, which means let it learn. Fourth, predict the answers for the test data. Fifth, score the model, and read what that score really means.

We will use a simple model called kay nearest neighbours. To guess a new flower, it finds the three most similar flowers it has seen, and takes a majority vote. It is like asking your three nearest neighbours for directions. The three inside the brackets is the number of neighbours. Model dot fit, with ex train and why train, is the learning step.

Model dot predict takes features only, with no answers, and returns its guesses. Here we ask about the first five test flowers. The model guesses two, one, zero, one, one. The real labels are two, one, zero, one, two. Pause and predict. How many did it get right? Four of five, because the last flower was wrong.

Model dot score compares the predictions with the real test labels. For a classifier, it returns the accuracy, which means the fraction of correct answers. Here the score is zero point nine. So twenty seven of the thirty test flowers were right, and three were wrong. Every scikit learn model uses the same three words: fit, predict and score.

Now let us read the number, not just say it. Zero point nine means twenty seven of thirty test flowers were right. Compare it with random guessing, which gets about thirty three percent with three species, so the model has clearly learned. But the test set is small, so a single mistake changes the score by three point three percent. And we must also ask which flowers went wrong.

In one typical run, here is how the thirty test flowers did, species by species. All seven setosa flowers were right. Eleven of twelve versicolor flowers were right, and one was called virginica. Nine of eleven virginica flowers were right, and two were called versicolor. So the model only mixes up the two species that look alike. That is far more useful than the single number.

A few more checks. On the training data, the score is zero point nine seven five, and on the test data it is zero point nine. A small gap is normal, but a very large gap means the model memorised instead of learning. Change the random state, and the score may become one point zero or drop lower, so one split is not the full story. And in serious uses like health, missing a sick patient matters more than overall accuracy.

Let us revise what we learned today. Scikit learn gives datasets, models, splitting and metrics. Capital ex holds the features, and small why holds the labels. Train test split keeps a hidden test set. Fit learns, predict answers, and score measures. And always read the result, by comparing with guessing, checking the mistakes, and minding the test size. So is ninety percent good? Now you know it depends.

Courses that teach this

CourseUnit
CBSE Class 11 Artificial Intelligence (843)Python Programming

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →