Machine learning can look like a wall of algorithms, but beginners learn it fastest by following the same workflow every real project uses: understand and clean the data, build features, split the data properly, start with simple models, evaluate honestly and keep watching the model once it is in use. This guide follows that order, with short Python examples using pandas and scikit-learn, and ends with the questions data science interviewers ask.
You will need basic Python first; our Python for Beginners guide covers it. For the statistics behind the models, see our statistics guide for data analysts.
Contents
- 1. Data science vs machine learning
- 2. The Python libraries to learn, in order
- 3. Jupyter notebooks
- 4. pandas and groupby
- 5. Reading a dataset before you analyse it
- 6. Data cleaning checklist
- 7. Feature engineering and leakage
- 8. Train-test split
- 9. Cross-validation
- 10. Overfitting and underfitting
- 11. Linear regression
- 12. Logistic regression
- 13. Classification models and class imbalance
- 14. Decision trees and random forests
- 15. Gradient boosting
- 16. K-nearest neighbours
- 17. K-means clustering
- 18. PCA
- 19. Neural networks
- 20. Model evaluation: why accuracy is not enough
- 21. Monitoring a model after deployment
- 22. Writing a data science case study
- 23. Data science interview questions
1. Data science vs machine learning
Data science is the full process: define the question, gather and clean data, explore it, build a model when one is needed, and explain the result to the people who will act on it. Machine learning is one set of methods inside that process, where a model learns patterns from data, such as predicting whether a customer will leave or grouping similar products. Many useful projects need only statistics and clear charts, so learn the whole process first, then the models. A sensible order is Python and pandas, then statistics, then simple models such as linear and logistic regression, then testing models on data they have not seen.
2. The Python libraries to learn, in order
- pandas: reads, cleans, filters, groups and joins tables. Almost every project starts here.
- NumPy: fast arrays of numbers, used underneath pandas.
- Matplotlib and Seaborn: charts from code; Seaborn makes statistical plots such as box plots and heatmaps with less code.
- scikit-learn: standard models plus tools to split data, build pipelines and evaluate results.
Learn them in that order, and move to scikit-learn only once you can explain what a model is doing.
3. Jupyter notebooks
A notebook mixes code cells, their output and charts, and Markdown notes on one page, so you can test one step at a time and explain your work as you go. Install Jupyter locally or start with a free online notebook service. Build good habits early: run all cells from top to bottom before sharing so results match the code, put a title and short summary at the top, and move repeated code into functions.
4. pandas and groupby
A DataFrame is a table with named columns, like an Excel sheet, usually created with read_csv or read_excel. The first commands to learn are head() and info() to inspect, df[df["city"] == "Bangalore"] to filter, merge() to join, and isna() and fillna() for missing values. pandas repeats the same steps on new data with one run and keeps a written record of every step.
Summaries such as “total sales by city” use groupby, which works like SQL’s GROUP BY: split rows into groups, apply a calculation, combine the results.
df.groupby("city")["amount"].sum()
summary = df.groupby("course").agg(
enquiries=("enquiry_id", "count"),
avg_age=("age", "mean"),
cities=("city", "nunique"),
).reset_index()
df["city_total"] = df.groupby("city")["amount"].transform("sum")count() counts non-empty values while size() counts rows; nunique() counts distinct values. Group by several columns and unstack(fill_value=0) (or pd.pivot_table) for a pivot-style table, and group by month with df["order_date"].dt.to_period("M") after pd.to_datetime. Watch for text numbers that will not sum (use pd.to_numeric), blank groups silently dropped (use dropna=False) and forgetting reset_index().
5. Reading a dataset before you analyse it
Most wrong results come from misreading the data, not from a wrong formula. Before analysing, ask what one row represents (an order, a customer, a day?), what period the data covers and whether days are missing, which columns are numbers, dates and text, and who created the data and how. Then count rows, look at the first and last few, check each number column’s minimum and maximum for impossible values, count blanks per column, and look for duplicates in the column that should be unique. Write short notes, such as “dates before March are missing”, and mention them when you share results.
6. Data cleaning checklist
Cleaning takes most of the time in a real project, and experienced analysts treat it as the core skill.
- Missing values: decide whether to remove, fill or flag them, and record the choice.
- Duplicates: check whether identical rows are real repeats or errors.
- Wrong types: numbers stored as text, dates in two formats.
- Outliers: a value that looks wrong may be a real extreme; check before deleting.
- Inconsistent labels: “Bangalore”, “bangalore” and “Bengaluru” may be the same city.
Log every step in order, then compare row counts, totals and sample rows with the original file. Cleaning that silently changes totals is a common cause of wrong results.
7. Feature engineering and leakage
Features are the inputs a model uses, and better features often help more than a more complex algorithm. Typical examples: extract month, weekday or holiday from a date; count orders in the last 90 days; turn categories such as city into numbers with one-hot encoding (which implies no order); or combine columns, such as price per square metre. Scale numbers where the model needs it and handle missing values in a planned way.
Avoid leakage: never use information that would not exist at prediction time, such as a column filled in after the outcome is known. It makes a model look excellent in testing and fail in real use. Documenting the prediction date helps reviewers spot such features.
8. Train-test split
A model must be tested on data it did not see during training, or it may simply have memorised the answers. Split the data into a training set to fit the model and a test set kept aside for the final check; about 80/20 is common, and with little data use cross-validation instead.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)Use stratify to keep class proportions, and a fixed random_state so results can be repeated. Do not shuffle time-ordered data (train on older records, test on newer), and do not tune again and again on the test set: use a separate validation set to compare models and choose thresholds.
9. Cross-validation
In k-fold cross-validation the data is split into k parts; the model trains on k minus one parts and is tested on the remaining one, k times, so each part is used once for testing, and the scores are averaged. It uses all the data for both training and testing, shows whether a score is stable or depends on one lucky split, and compares models fairly. For data in time order, such as monthly sales, use a time-based split instead of shuffling.
10. Overfitting and underfitting
- Underfitting: the model is too simple, like a straight line through a curved pattern, and scores badly on both training and test data. Fix it with a more flexible model or more useful features.
- Overfitting: the model learns the training data too closely, including its noise, and scores well on training data but badly on new data. Fix it with more data, a simpler model or regularisation, checked with cross-validation.
Compare training and test scores: both low means underfitting; a high training score with a much lower test score means overfitting. Every data science interview asks about this.
11. Linear regression
Linear regression draws the straight line that best fits the data: a starting value plus a slope times the input. Plot house size against price and the slope shows how much price tends to rise per extra square metre. Check that the relationship looks roughly straight on a scatter plot, look for outliers pulling the line, and test on unseen data. Remember that the slope shows an association, not proof that changing one value will change the other. Practice: fit a line to a public dataset with two numeric columns and write three sentences on what the slope means, how well it fits and one reason it could be wrong.
12. Logistic regression
Despite its name, logistic regression is a classification model for yes/no questions such as churn, spam or loan repayment. It passes a linear result through an S-shaped sigmoid curve to produce a probability between 0 and 1, then a threshold turns it into a decision.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
probs = model.predict_proba(X_test)[:, 1]Each feature’s coefficient shows whether it pushes the probability up or down, which is why business teams like the model. 0.5 is not always the right threshold: lower it when missing a positive case is costly, such as fraud, and raise it when false alarms are costly, comparing precision and recall at several thresholds. Scale numeric features and one-hot encode categories first. Use it as a baseline before trying more complex models.
13. Classification models and class imbalance
Classification predicts a category learned from past labelled examples. Beginners usually compare logistic regression (simple and explainable), a decision tree (decisions you can draw) and a random forest (many trees, often more accurate). Check how many examples each class has before training: when one class is rare, a model can look accurate by always predicting the common class. Practice: on a public churn dataset, train a logistic regression and a decision tree and compare which predicts the rare class better.
14. Decision trees and random forests
A decision tree works like a flowchart: “Attended more than 80% of classes?” then “Submitted the first project?” until a leaf gives the prediction, so every prediction can be explained. At each step the algorithm picks the question that best separates the classes, measured with Gini impurity or entropy (or error reduction for numbers).
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=4, min_samples_leaf=20, random_state=42)
model.fit(X_train, y_train)An unlimited tree keeps splitting until it memorises the training data, so limit it with max_depth, min_samples_leaf or pruning. Trees need no feature scaling. A random forest trains many trees on random samples of rows and features and combines their votes, which reduces overfitting and is usually more stable than one tree.
15. Gradient boosting
Gradient boosting models, including XGBoost, LightGBM and CatBoost, are widely used for tabular problems such as churn and credit scoring. Where a forest builds trees independently, boosting builds them one after another: start with a simple prediction, compute the errors, train a small tree to predict those errors, add it scaled by a learning rate, and repeat hundreds of times.
from sklearn.ensemble import HistGradientBoostingClassifier
model = HistGradientBoostingClassifier(learning_rate=0.1, max_depth=4,
early_stopping=True, random_state=42)
model.fit(X_train, y_train)The settings that matter most are the number of trees, the learning rate (smaller values such as 0.05 to 0.1 with more trees usually generalise better), shallow depth (often 3 to 6) and early stopping. Always use a validation set, check carefully for leakage (powerful models exploit it), and use feature importance or SHAP values when predictions must be explained.
16. K-nearest neighbours
KNN classifies a new example by finding the k most similar training examples and letting them vote (or averaging them for numbers). Small k follows the data closely and can overfit; large k is smoother but may blur real differences. Use an odd k for two classes and choose it with cross-validation. Because KNN relies on distance, always scale features first, ideally in a pipeline:
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=7))KNN is simple and easy to explain with examples, but slow on large datasets and sensitive to irrelevant features. A favourite interview question: KNN is supervised (it needs labels), while k-means is unsupervised (it groups unlabelled data).
17. K-means clustering
Clustering finds natural groups in data without labels. K-means chooses k centroids, assigns each point to its nearest centroid, moves each centroid to the average of its points, and repeats until they stop moving. A retailer might find frequent low-value buyers, occasional high-value buyers and inactive customers, and market to each group differently.
from sklearn.cluster import KMeans
labels = KMeans(n_clusters=3, n_init=10, random_state=42).fit_predict(X_scaled)Choose k with the elbow method, the silhouette score and business sense: three clear groups beat five nobody can act on. Scale features first, encode categories, and remember k-means finds round, similar-sized groups; long or uneven clusters need methods such as DBSCAN.
18. PCA
Principal component analysis combines correlated columns into fewer new features, called components, keeping as much information as possible. If the first three components explain 90% of the variation, three columns can often replace twenty.
from sklearn.decomposition import PCA
pca_pipeline = make_pipeline(StandardScaler(), PCA(n_components=0.90))
X_reduced = pca_pipeline.fit_transform(X_train)PCA helps with visualisation (squeeze many features into two to plot), speed and removing redundancy. Scale first, fit on training data only to avoid leakage, and remember that components are mixtures of columns, hard to explain to business users, and capture only linear patterns.
19. Neural networks
A neural network is made of layers of small units that multiply inputs by weights, add them up and pass the result on; stacking layers lets it learn complex patterns. Training means predicting, measuring the error and adjusting the weights slightly, many times over. Neural networks shine with large amounts of images, audio and text, and power modern AI tools, but for small tables of business data simpler models are often as good and easier to explain. Learn regression and classification first, then build a small network on a well-known dataset.
20. Model evaluation: why accuracy is not enough
A model that predicts “no fraud” for every transaction can be 99% accurate and useless. If 98 of 100 cases are negative, always predicting negative scores 98% while finding nothing.
- Confusion matrix: counts true positives, true negatives, false positives and false negatives.
- Precision: of the cases flagged, how many were right?
- Recall: of the real cases, how many were found?
- F1 score: the harmonic mean of precision and recall, low when either is low.
- Precision-recall curve: shows the trade-off as the threshold changes.
Focus on recall when missing a case is costly, such as disease, and precision when false alarms are costly, such as blocking a real customer. For regression, mean absolute error is the average size of errors in the target’s units, and root mean squared error gives large errors more weight. A calibrated model’s probabilities match reality: among cases scored around 70%, about 70% should be positive. Review false positives and negatives by group to find patterns worth fixing.
21. Monitoring a model after deployment
Once a model is used on new data, its accuracy can drop with no error message, because the world changes: customers buy differently, a sensor is replaced, a season ends. This is data drift. Track input data (averages, ranges and missing values compared with training data), the share of each predicted class over time, and outcomes compared with predictions once they are known. A simple habit: save the training statistics with the model, recompute them weekly on new data and compare side by side. A large change is a reason to look closer, not proof the model is broken.
22. Writing a data science case study
- The business problem: the decision someone needs to make, in one sentence.
- The data: where it came from, how many rows, and the cleaning steps that changed results.
- The approach: a simple baseline first, then a model, and why you chose it.
- The result and its limit: the score on unseen data and one honest limitation.
- The recommendation: what the business should do and what you would measure next.
For a worked example of checking whether a result is believable, read your first data science project: is your model score too good to be true?
23. Data science interview questions
- Classification vs regression? Classification predicts a category, such as whether an order will be returned; regression predicts a number, such as delivery time.
- Feature vs target? A feature is an input, such as purchases before the prediction date; the target is the outcome the model predicts.
- Why fit a scaler or imputer on the training set only? Fitting on all data lets the test set influence training; learn the transformation from training data and apply it everywhere else.
- What is a pipeline? One workflow combining preprocessing and the model, so the same learned transformations are applied consistently.
- Validation vs test set? The validation set compares models, tunes settings and picks thresholds; the test set is kept for one final evaluation.
- When should you use stratified sampling? When splits should preserve class proportions, unless group or time boundaries matter more.
- What is class imbalance? One class is much rarer than another; report metrics for the rare class.
- What does a classification threshold do? Turns a score into a decision; lowering it flags more cases, usually raising recall and lowering precision.
- How do decision trees and random forests predict? A tree routes a record through feature tests to a leaf; a forest combines many randomised trees for a more stable prediction.
- What is data drift? A change in the data a model receives over time; track key features and check whether performance changes.
For a longer list, see Data Science questions and answers: statistics, machine learning and model monitoring.
Learn data science with ScholarLogic
This workflow is the core of the Data Science course in Bangalore at ScholarLogic, BTM Layout, with Python, statistics, machine learning and projects reviewed by trainers. Book a free demo class and a counsellor will share the fee and the next batch dates. To book, call 78923 93639.
