Guides › AI & Machine Learning
Machine Learning Explained with Python
Understand machine learning concepts through Python examples. Learn regression, classification, clustering, preprocessing, model evaluation, overfitting, and how to build your first predictive model.
Machine Learning Explained with Python
Introduction
Machine Learning (ML) enables computers to learn patterns from examples and use those patterns to make predictions or decisions. Instead of writing every rule manually, developers train a model using data.
This guide introduces the core concepts and demonstrates a complete basic workflow using Python and scikit-learn.
1. How Machine Learning Works
A typical ML workflow includes:
- Define a problem.
- Collect relevant data.
- Explore and clean the data.
- Select features and a target.
- Split data into training and testing sets.
- Train a model.
- Evaluate its predictions.
- Improve and deploy the model if appropriate.
2. Types of Machine Learning
Supervised Learning
Uses examples with known target values.
- Regression predicts numerical values.
- Classification predicts categories.
Unsupervised Learning
Finds structure in data without target labels.
- Clustering groups similar examples.
- Dimensionality reduction summarizes features.
Reinforcement Learning
An agent learns behavior through actions and feedback from an environment.
3. Install the Tools
Install Python, then run:
python -m pip install numpy pandas matplotlib scikit-learn
Use a virtual environment for each project when possible.
4. Features and Targets
Features are the input variables used by a model. The target is what the model learns to predict.
For a house-price model:
- Features could include floor area, location, and number of bedrooms.
- Target could be the sale price.
A feature should be available at the time a real prediction is made.
5. Your First Regression Model
This example learns a simple relationship between hours studied and a practice score.
from sklearn.linear_model import LinearRegression
X = [[1], [2], [3], [4], [5]]
y = [20, 35, 50, 65, 80]
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([[6]])
print("Predicted score:", prediction[0])
The dataset is synthetic and intentionally simple. A real educational outcome cannot reliably be predicted from study hours alone.
6. Training and Testing
Training data is used to fit a model. Test data estimates performance on examples the model did not train on.
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, random_state=42
)
For time-series forecasting, use a time-aware split instead of randomly mixing past and future observations.
7. Regression Algorithms
Common algorithms include:
- Linear Regression
- Decision Tree Regressor
- Random Forest Regressor
- Gradient Boosting Regressor
Evaluate regression using MAE, MSE, RMSE, and R-squared where appropriate.
8. Classification Algorithms
Classification predicts a category.
Common algorithms:
- Logistic Regression
- K-Nearest Neighbors
- Decision Trees
- Random Forest
- Support Vector Machines
- Naive Bayes
Example: classify messages as spam or not spam using labeled examples.
9. Build a Classification Model
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
data = load_iris()
X = data.data
y = 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(random_state=42)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
This example uses a built-in educational dataset. The reported test result can vary with the split and model settings.
10. Unsupervised Learning with Clustering
Clustering groups similar observations.
K-Means is a common introductory algorithm. Feature scaling can be important because distance-based algorithms are sensitive to feature magnitudes.
Before interpreting clusters, check whether the groups are meaningful for the problem rather than assuming every cluster represents a real-world category.
11. Data Preprocessing
Common preprocessing steps include:
- Imputing missing values.
- Encoding categorical features.
- Scaling numerical features.
- Removing invalid or duplicate records where appropriate.
- Handling imbalanced classes.
- Creating useful features.
Use pipelines to apply preprocessing consistently. Fit transformations only on training data to avoid leakage.
12. Overfitting and Underfitting
Overfitting occurs when a model captures noise in training data and generalizes poorly. Underfitting occurs when a model cannot capture important patterns.
Possible solutions include:
- Using a simpler model.
- Collecting more representative data.
- Applying regularization.
- Using cross-validation.
- Tuning hyperparameters carefully.
13. Model Evaluation
For regression:
- MAE measures average absolute error.
- MSE penalizes larger errors more strongly.
- RMSE expresses error in the target's units.
- R-squared compares performance against a baseline based on the target mean.
For classification:
- Precision measures how many positive predictions are correct.
- Recall measures how many actual positives are found.
- F1-score balances precision and recall.
- A confusion matrix shows prediction counts by class.
Choose metrics based on the consequences of different errors.
14. Hyperparameters and Cross-Validation
Parameters are learned during training. Hyperparameters are configuration choices set before or around training, such as tree depth or regularization strength.
Cross-validation helps compare configurations using multiple data splits. Keep a final test set separate until the model-selection process is complete.
15. Feature Importance and Explainability
Some models provide feature-importance estimates. Other approaches include permutation importance and SHAP-style explanations.
Importance scores do not automatically prove that a feature causes an outcome. Consider correlated features, data quality, and the context of the model.
16. Save and Load a Model
For a trusted local model, scikit-learn supports serialization workflows. Only load model files from trusted sources because some serialization formats can execute code when loaded.
import joblib
joblib.dump(model, "model.joblib")
loaded_model = joblib.load("model.joblib")
Save the preprocessing pipeline and relevant dependency versions along with the model.
17. Beginner Practice Projects
- House Price Prediction.
- Student Score Prediction.
- Iris Flower Classification.
- Spam Message Detection.
- Customer Churn Prediction.
- Customer Segmentation.
- Sales Forecasting.
- Movie Recommendation Prototype.
18. Common Beginner Mistakes
- Training and testing on the same data.
- Using accuracy alone for imbalanced classes.
- Ignoring missing values.
- Fitting preprocessing on the entire dataset.
- Tuning repeatedly against the test set.
- Treating correlation as causation.
- Claiming a model is accurate without evaluating it.
19. Learning Checklist
- Understand features and targets.
- Train a regression model.
- Train a classification model.
- Learn clustering.
- Clean and transform data.
- Evaluate models with suitable metrics.
- Use cross-validation.
- Build and document at least two projects.
Conclusion
Machine learning becomes easier when you build small models and inspect their predictions. Start with simple baselines, evaluate on unseen data, and improve your approach through experiments.
More guides
- How to Use Codify: A 7-Day Start Plan — New here? Pick a roadmap, make a study plan, start the DSA sheet, revise with notes, practise for placements and test yourself — one step a day, then a daily routine.
- Infosys Preparation Guide: Online Test, Pseudocode and Interview — Roles (SE, DSE, SP), what each section of the Infosys online test checks, how to crack pseudocode and reasoning, a 4-week plan and the interview.
- Wipro Elite NLTH Preparation: Aptitude, Essay and Coding — The Wipro fresher test round by round — quant, logical and verbal, the written essay, two coding problems — plus a 3-week plan and interview tips.
- Accenture Preparation Guide: Cognitive, Technical, Coding and Communication — Every Accenture round explained — cognitive, MS Office/networking/cloud technical MCQs, pseudocode, coding and the spoken-English test — with a 4-week plan.