100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HomeBlogScikit-Learn for Beginners: Machine Learning in Python
Data Science

Scikit-Learn for Beginners: Machine Learning in Python

SV

SkillVeris Team

Data Science Team

May 8, 2026 11 min read
Share:
Scikit-Learn for Beginners: Machine Learning in Python
Key Takeaway

Scikit-learn's API is elegantly consistent: every model has fit(X, y), predict(X), and score(X, y).

In this guide, you'll learn:

  • Train on training data, evaluate on test data, and use cross-validation for reliable performance estimates.
  • Switching algorithms is just changing the class name — the rest of the code stays the same.
  • Always split train/test before any preprocessing, and fit scalers on the training data only to avoid data leakage.
  • For imbalanced classes use precision, recall, F1, and ROC-AUC instead of accuracy; for regression use RMSE, MAE, and R2.

1What Is Scikit-Learn?

Scikit-learn (sklearn) is the standard Python library for classical machine learning: linear regression, logistic regression, decision trees, random forests, support vector machines, k-means clustering, and more.

It provides a clean, consistent API that makes switching between algorithms easy, plus excellent tools for model evaluation, preprocessing, and pipeline construction.

Common Imports

A typical set of imports for a sklearn project.

code
import sklearn
print(sklearn.__version__)  # 1.4.x or later
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score, classification_report
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder

2The Consistent API

Every scikit-learn model follows the same three-method API: fit to train, predict on new data, and score to evaluate. Switching algorithms is just changing the class name — the rest of the code stays the same. This uniformity is sklearn's greatest strength.

The four main ML task types and when to use each.
The four main ML task types and when to use each.

fit, predict, score

The same three methods work across every estimator.

code
# Create model
model = RandomForestClassifier(n_estimators=100, random_state=42)
# Train on labelled data
model.fit(X_train, y_train)
# Predict on new data
predictions = model.predict(X_test)
# Evaluate
score = model.score(X_test, y_test)  # accuracy for classifiers, R2 for regressors

3Loading and Preparing Data

Scikit-learn ships with built-in datasets for learning, and works seamlessly with pandas DataFrames loaded from CSV. The library expects X with shape (n_samples, n_features) and y with shape (n_samples,).

Loading Built-in and CSV Data

Separate the feature matrix X from the target y.

code
from sklearn.datasets import load_iris, load_diabetes
import pandas as pd
# Built-in datasets for learning
iris = load_iris(as_frame=True)
df = iris.frame
X = iris.data  # feature matrix: (150, 4)
y = iris.target  # labels: 0, 1, 2
# From a CSV
df = pd.read_csv("data.csv")
X = df.drop("target", axis=1)  # all columns except target
y = df["target"]  # target column
# sklearn expects: X shape (n_samples, n_features), y shape (n_samples,)
print(X.shape, y.shape)  # e.g. (150, 4) (150,)

4Train/Test Split

Never evaluate a model on data it was trained on — that gives optimistically biased results. Always hold out a test set and use it only for final evaluation. The stratify argument preserves class proportions across both splits.

⚠️Watch Out

Data leakage happens when information from the test set influences training. Common causes: fitting preprocessing (StandardScaler, PCA) on all data instead of training data only; including future data in features; target leakage (a feature only known after the target is known). Leakage produces models that look great in evaluation but fail in production.

Splitting the Data

An 80/20 split with a fixed random_state for reproducibility.

code
from sklearn.model_selection import train_test_split
# Split 80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(
    X, y,
    test_size=0.2,    # 20% for testing
    random_state=42,  # reproducible split
    stratify=y        # preserve class proportions in both splits
)
print(f"Train: {X_train.shape[0]} samples")
print(f"Test: {X_test.shape[0]} samples")

5Classification: Predicting Categories

Classification predicts discrete categories. The example below trains a Random Forest on the breast cancer dataset and measures accuracy, which typically lands around 0.96.

A Random Forest Classifier

Fit, predict, and score in a few lines.

code
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
preds = model.predict(X_test)
acc = accuracy_score(y_test, preds)
print(f"Accuracy: {acc:.3f}")  # typically ~0.96

6Model Evaluation for Classification

Accuracy alone can mislead. For imbalanced datasets (90% class 0, 10% class 1), a model that always predicts "class 0" gets 90% accuracy. Use precision, recall, F1-score, and ROC-AUC instead, alongside a confusion matrix.

Matching the evaluation metric to the problem type.
Matching the evaluation metric to the problem type.

Classification Metrics

A full report, confusion matrix, and ROC-AUC.

code
from sklearn.metrics import (classification_report, confusion_matrix,
                             ConfusionMatrixDisplay, roc_auc_score)
# Full classification report
print(classification_report(y_test, preds, target_names=data.target_names))
# Confusion matrix
cm = confusion_matrix(y_test, preds)
disp = ConfusionMatrixDisplay(cm, display_labels=data.target_names)
disp.plot()
# ROC-AUC (for binary classification with probability scores)
proba = model.predict_proba(X_test)[:, 1]  # probability of positive class
auc = roc_auc_score(y_test, proba)
print(f"ROC-AUC: {auc:.3f}")

7Regression: Predicting Numbers

Regression predicts continuous numbers. Ridge adds regularisation to linear regression, while Gradient Boosting is usually the best performer on tabular data.

Ridge and Gradient Boosting

Two regressors on the diabetes dataset.

code
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.ensemble import GradientBoostingRegressor
data = load_diabetes()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42)
# Linear regression with regularisation
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
# Gradient boosting (usually best for tabular data)
gb = GradientBoostingRegressor(n_estimators=200, learning_rate=0.1, random_state=42)
gb.fit(X_train, y_train)
preds = gb.predict(X_test)

8Model Evaluation for Regression

Regression metrics quantify error in the target's units. RMSE and MAE measure average error, while R2 ranges from 1.0 (perfect) down through 0 (baseline) to negative (worse than predicting the mean).

RMSE, MAE, and R2

Three complementary regression metrics.

code
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import numpy as np
preds = gb.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, preds))
mae = mean_absolute_error(y_test, preds)
r2 = r2_score(y_test, preds)
print(f"RMSE: {rmse:.2f}")  # same units as target
print(f"MAE: {mae:.2f}")   # average absolute error
print(f"R2: {r2:.3f}")     # 1.0 = perfect, 0 = baseline, negative = worse than mean

9Feature Engineering and Preprocessing

Scaling is required for SVM, KNN, and linear models, and categorical features must be encoded. Crucially, fit the scaler on training data and only transform the test data. ColumnTransformer applies different transforms to different columns.

Scaling and Encoding

Fit on train, transform on test, and combine with ColumnTransformer.

code
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
# Scale numeric features (required for SVM, KNN, linear models)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)  # fit + transform on train
X_test_scaled = scaler.transform(X_test)        # transform only on test
# One-hot encode categorical features
encoder = OneHotEncoder(sparse_output=False, handle_unknown="ignore")
# ColumnTransformer: apply different transforms to different columns
numeric_cols = ["age", "salary"]
categorical_cols = ["dept", "city"]
preprocessor = ColumnTransformer([
    ("num", StandardScaler(), numeric_cols),
    ("cat", OneHotEncoder(handle_unknown="ignore"), categorical_cols),
])

10Cross-Validation

Cross-validation trains and evaluates the model multiple times on different splits, giving a reliable estimate of real-world performance. The mean and standard deviation tell you both how good the model is and how stable it is across data splits.

Stratified K-Fold Cross-Validation

Five folds give a mean and a spread.

code
from sklearn.model_selection import cross_val_score, StratifiedKFold
model = RandomForestClassifier(n_estimators=100, random_state=42)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=cv, scoring="accuracy")
print(f"CV scores: {scores}")
print(f"Mean: {scores.mean():.3f} +/- {scores.std():.3f}")

11Pipelines

Pipelines are the correct way to handle preprocessing: by including the scaler inside the pipeline, cross-validation fits the scaler on training folds only, preventing leakage. Always use pipelines in production code.

Building and Saving a Pipeline

fit, predict, and score work exactly the same on a pipeline.

code
from sklearn.pipeline import Pipeline
# Chain preprocessing + model into one object
pipe = Pipeline([
    ("scaler", StandardScaler()),
    ("model", RandomForestClassifier(n_estimators=100, random_state=42)),
])
# fit/predict/score work exactly the same
pipe.fit(X_train, y_train)
preds = pipe.predict(X_test)
print(f"Pipeline accuracy: {pipe.score(X_test, y_test):.3f}")
# Cross-validate the whole pipeline (no leakage)
scores = cross_val_score(pipe, X, y, cv=5)
print(f"CV mean: {scores.mean():.3f}")
# Save and load
import joblib
joblib.dump(pipe, "model.joblib")
pipe_loaded = joblib.load("model.joblib")

12Key Takeaways

The sklearn workflow is consistent and predictable once these habits are in place.

  • Every sklearn model: fit(X_train, y_train) -> predict(X_test) -> score(X_test, y_test).
  • Always split train/test before any preprocessing; fit scalers on training data only.
  • For imbalanced classes, use F1/AUC not accuracy; for regression, use RMSE/MAE/R2.
  • Cross-validation gives more reliable estimates than a single train/test split.
  • Use Pipelines to chain preprocessing and modelling into one leak-proof, reusable object.

13What to Learn Next

Go deeper with machine learning using these next steps.

  • Machine Learning for Beginners — the conceptual foundation behind these algorithms.
  • NumPy for Data Science — the array operations sklearn operates on.
  • Pandas for Beginners — load and prepare real-world data for sklearn.

14Frequently Asked Questions

What is the difference between fit() and fit_transform()? fit() learns parameters from data (e.g. the mean and standard deviation for StandardScaler). transform() applies the learned transformation. fit_transform() does both in one step. Use fit_transform() on training data, then transform() only on test data — never refit on test data.

When should I use Random Forest vs Gradient Boosting vs Linear models? Linear models (Ridge, Logistic Regression) are fast, interpretable, and a good baseline that works well with many features. Random Forest is robust, handles missing values, has low hyperparameter sensitivity, and is a good default for most tabular problems. Gradient Boosting (XGBoost, LightGBM) is usually the best performer on tabular data but slower to train and more sensitive to hyperparameters. Start with a linear baseline, then try Random Forest, then Gradient Boosting.

What is overfitting and how do I detect it? Overfitting occurs when a model memorises training data but fails to generalise. Signs: training accuracy much higher than test accuracy (e.g. 99% train, 72% test). Solutions: more data, regularisation (Ridge, Lasso, max_depth on trees), cross-validation to detect it, and simpler models.

Does scikit-learn support deep learning? No. Scikit-learn covers classical machine learning. For deep learning (neural networks, computer vision, NLP), use PyTorch or TensorFlow/Keras. The two ecosystems complement each other: sklearn for classical ML, PyTorch for deep learning, and sklearn's preprocessing and evaluation tools can be used alongside PyTorch.

📄

Get The Print Version

Download a PDF of this article for offline reading.

About the Publisher

SV

SkillVeris Team

Data Science Team

Our data team shares real-world analytics, ML, and SQL insights grounded in industry practice.

View all posts

Never miss an update

Get the latest tutorials and guides delivered to your inbox.

No spam. Unsubscribe anytime.

Frequently Asked Questions

21 categories · pick one to explore

Does SkillVeris have a tech blog, and what does it cover?
Yes, the SkillVeris blog has over 500 articles covering AI and machine learning, programming, web development, DevOps, cloud, security, databases and career guidance. Articles are practical and answer-first, and many use the Learn Through Hobbies approach, teaching technical concepts through cricket, music, gaming or cooking analogies. Everything is free to read.
What is the SkillVeris tech glossary and how big is it?
The SkillVeris glossary is a free reference of roughly 2,000-plus technology terms, each with a clear plain-language definition. It spans AI, programming, web, DevOps, cloud, security and database vocabulary, so whenever a lesson, article or job description uses jargon you do not recognise, the glossary gives you a fast, reliable answer.
Are the developer cheat sheets on SkillVeris free to download?
The cheat sheets are completely free to use, like everything else on SkillVeris. Each sheet condenses a language or tool into its essential syntax, commands and patterns for quick reference while coding. They are designed for rapid lookup during real work, complementing the deeper explanations found in study notes and courses.
Which programming references and cheat sheets are available?
Cheat sheets cover the platform's main domains, including programming languages, AI and ML tooling, web development, DevOps, cloud, security and databases, matching the topics of the 37 live courses. Each sheet lists related reading links and hashtags, so you can jump from a quick reference into fuller study notes or blog articles.
How do I find the meaning of a technical term quickly?
Search the SkillVeris glossary, which holds around 2,000-plus terms with concise, plain-language definitions. Each entry gets to the point in its first sentence, then links to related reading like blog posts or study notes for deeper context. It is faster and more consistent than sifting through scattered search results.
Is the SkillVeris blog good for beginners learning to code?
Yes, many blog articles are written specifically for beginners, and the Learn Through Hobbies style makes them unusually approachable: you might learn Python concepts through cricket or understand APIs through cooking. With 500-plus articles across skill levels, beginners can start with fundamentals and keep reading as they advance, entirely free.
Can cheat sheets replace full courses for learning a language?
No, cheat sheets are references, not teaching tools; they assume you already understand the concepts and just need syntax or commands fast. To actually learn a language, take a structured SkillVeris course with its 24–40 lessons and assessments, then keep the cheat sheet beside you while practising in Code Lab.
How often are new blog articles published on SkillVeris?
The blog grows regularly and already exceeds 500 articles, with new posts added as courses launch and technologies evolve. Topics track the platform's catalogue across AI, programming, web development, DevOps, cloud and security, so checking the Blog section periodically surfaces fresh tutorials, explainers and career-focused pieces, all free to read.
Does the glossary cover AI and machine learning terms?
Yes, AI and machine learning vocabulary is a major part of the roughly 2,000-plus term glossary, covering everything from foundational terms to modern concepts around LLMs, RAG and MLOps. Definitions are plain-language and answer-first, which helps when dense AI papers or course lessons throw unfamiliar jargon at you.
Are there cheat sheets for interview preparation?
Cheat sheets work well as interview-day refreshers because they compress syntax, commands and key concepts into scannable references. For dedicated preparation, combine them with the SkillVeris interview questions feature, which includes readiness scoring, plus study notes for depth. Reviewing a relevant cheat sheet just before an interview steadies recall under pressure.
Can I read the tech blog without signing up?
Yes, the blog is freely readable, and SkillVeris never charges for content. All 500-plus articles are open, covering tutorials, concept explainers and career advice. Creating a free account adds value elsewhere on the platform, like course progress tracking and certificates, but reading the blog requires no commitment at all.
How is the SkillVeris glossary different from Wikipedia?
The glossary is purpose-built for learners: definitions are short, plain-language and answer-first, sized for a quick lookup mid-lesson rather than a deep encyclopedic read. Entries also cross-link to related SkillVeris study notes, blog posts and courses, so a definition becomes a doorway into structured learning instead of a dead end.
Do blog articles use the Learn Through Hobbies method?
Many blog articles teach technical topics through hobby analogies, a hallmark of the SkillVeris blog, so you will find articles explaining programming through cricket, machine learning through music, or system design through cooking. The analogy is the teaching device; the article still delivers the real technical concept underneath.
Where can I find quick programming references while coding?
Open the SkillVeris cheat sheets, which are built exactly for that moment: compact, scannable references for syntax, commands and common patterns across languages and tools. Keep the relevant sheet in a browser tab while you work in Code Lab or your own editor, and dip into the glossary for terminology.
Is there a glossary entry for terms I meet in job descriptions?
Very likely yes, with roughly 2,000-plus terms across AI, programming, web, DevOps, cloud, security and databases, the glossary covers most jargon that appears in tech job descriptions. Decoding a listing this way helps you judge role fit honestly and prepares you to discuss those terms in interviews.
Are the blog articles written for the Indian tech audience?
The blog serves Indian learners plus a worldwide audience. Content stays globally relevant while acknowledging realities that matter in India, such as free access being essential for students and freshers, and career guidance that connects naturally to the SkillVeris jobs portal, which aggregates roles across India, UK, USA, Germany and Remote.
Can I suggest a topic for the blog or glossary?
SkillVeris content grows in response to what learners need, so feedback is welcome through the platform's support channels. If a term is missing from the glossary or a topic deserves an article, telling the team helps prioritise it. Meanwhile, the AI Mentor can answer the question immediately, 24/7, at any depth.
Do cheat sheets and glossary entries link to deeper learning?
Yes, every cheat sheet and glossary entry carries related reading links into study notes, blog articles and courses, plus concept hashtags for discovering similar content. This cross-linking means a thirty-second lookup can smoothly become a structured learning session whenever you decide you want more than a quick answer.
What makes SkillVeris programming references trustworthy?
The references are written to strict internal quality standards, kept consistent with the platform's 37 live courses, and never padded with invented statistics or hype. Definitions and cheat sheets are reviewed against the same content contracts that govern courses, and the answer-first style makes any inaccuracy easy to spot and correct.
How do the blog, glossary and cheat sheets fit into my learning routine?
Use them as satellites around your main course: read blog articles for context and motivation, hit the glossary the instant jargon appears, and keep cheat sheets open while coding. Together with study notes, Code Lab and the 24/7 AI Mentor, they turn passive reading into a complete, free learning system.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse