Machine learning is a branch of Artificial Intelligence that focuses on building systems that can learn from data and improve their performance over time without being explicitly programmed.
TYPES OF MACHINE LEARNING.
- Supervised learning.
- Unsupervised learning.
- Reinforcement learning.
Supervised learning.
Supervised learning is a type of machine learning where a model learns from labelled data.
Unsupervised learning.
Unsupervised learning is a type of machine learning where the model works with unlabelled data.
Instead of predicting a known output, the model tries to discover hidden patterns, structures, or relationships in the data on its own.
Reinforcement learning.
Reinforcement learning is a type of machine learning where a model learns by interacting with an environment and receiving rewards or penalties based on its actions.
Instead of learning from labelled data or finding patterns, it learns through trial and error.
Supervised learning.
- Regression - is a type of supervised learning used to predict continuous numerical values.
- Classification - Classification is a type of supervised learning where the goal is to predict categories (classes) instead of numbers.
Regression.
How models learn in regression.
- The model starts with random values for y = mx + c
### Example: Predicting house prices.
y = mx + cprice(y) = 100(m) * size(x) + 5000(c)5000 is the base price if x = 0 - The model compares predictions with actual prices using loss function.
error = (prediction price - actual price) - The model adjusts the formula to reduce the error using gradient descent.
price = 150 * size + 30000 - The model repeats until error is minimized.
price = 200 * size + 10000
Supervised learning workflow.
- Collect data.
- Prepare data - Cleaning, pre-processing, visualization.
- Split data into train and test sets.
- Choose the algorithm to use.
- Training the model - learning patterns from training data.
- Make predictions on the test data.
- Evaluate the model's performance.
- Tune and improve the model.
- Deploy - use on new data.
Linear regression workflow.
Collect data
Collecting data involves gathering relevant CSV's, databases, Excel, APIs etc
import pandas as pd
df = pd.read_csv("Kenya_Crops_Dataset.csv")
print(df)
Prepare data
Data pre-processing involves cleaning, handling missing values, removing duplicates etc
examples.
df.info() - gets a summary of the dataset structure in pandas.
df.drop(columns = ['Cabin'], inplace = True) - dropping columns with lots of missing values.
df['Age'] = df['Age'].fillna(df['Age'].median()) - filling missing values with median. Used median in case of outliers.
df.info()
Visualization.
# visualization
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(5,3))
sns.heatmap(df.corr(),annot=True, cmap='coolwarm', center=0)
plt.title('Feature correlation')
plt.show()
example heatmap.

Split data into train and test sets.
Splitting data helps avoid overfitting of the models i.e. models performing very well on training data but poorly on new data. It also ensures the model doesn't memorize data.
#separate features(X) from label(y)
X = df[['size', 'bedrooms', 'bathrooms','age']]
y = df['price']
#split data
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)
Training the model
Training involves adjusting model parameters to minimize error using training data.
#training model
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
print(f"price: {model.intercept_:.2f}")
Make predictions on the test data.
After training, the model uses learned patterns to predict outcomes on new data.
# evaluation of the model
y_pred = model.predict(X_test)
Evaluate the model's performance.
Model evaluation involves measuring how well predictions match real values using test data
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.metrics import root_mean_squared_error
mse = mean_squared_error(y_test, y_pred)
rmse = root_mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"the rmse is {rmse:.2f}")
print(f"the r2 is {r2:.2f}")
Regularization.
Regularization is a technique used to reduce overfitting by preventing the model from becoming too complex.
Ridge (l2) and Lasso (L1)
Ridge
Adds squared penalty on coefficients. Shrinks coefficients but doesn’t make them zero. Keeps all features but reduces magnitude of weights
#Ridge l2 regularization
from sklearn.linear_model import Ridge
# create a rigde model
ridge = Ridge(alpha=1.0)
# train linear model with reguralization
ridge.fit(X_train,y_train)
#make prediction
y_pred_ridge = ridge.predict(X_test)
#Evaluation
r2_ridge = r2_score(y_test, y_pred_ridge)
print(f"r2_ridge= {r2_ridge}")
print(f"R2 linear = {r2}")
print(f"Difference : {r2_ridge - r2}")
Lasso
Adds absolute value penalty. Can reduce some coefficients to zero. it removes useless features.
# create a lasso model
from sklearn.linear_model import Lasso
lasso = Lasso(alpha=1.0)
# train linear model with reguralization
lasso.fit(X_train,y_train)
#make predictions
y_pred_lasso = lasso.predict(X_test)
#Evaluation
r2_lasso = r2_score(y_test, y_pred_ridge)
print(r2_lasso)
print(r2)
print(f"Difference : {r2_lasso - r2}")
Classification.
Classification is a supervised machine learning technique used to predict labels or categories from input data. It assigns each data point to a predefined class based on learned patterns.
Predict categories: Determines the class of new data points.
Uses labeled data: Trained on datasets where the correct class is known.
Common examples: Spam vs non spam emails, diseased vs. healthy patients.
Types of Classification.
1. Binary Classification.
Binary classification is the simplest type of classification where data is divided into two possible categories. The model analyzes input features and decides which of the two classes the data belongs to.
Some important aspects of binary classification are:
- Two classes only: Each data point is assigned to one of two categories.
- Common examples: Spam vs not spam emails, diseased vs healthy patients.
- Decision based on features: The model uses input features to determine the correct class.
2. Multiclass Classification.
Multiclass classification is used when data needs to be divided into more than two categories. The model analyzes the input features and selects the class that best matches the data.
Some important aspects of multiclass classification are:
- Multiple classes: Each data point is assigned to one of several possible categories.
- Single final prediction: The model selects only one class for each input.
- Common examples: Image classification such as identifying animals like cat, dog or bird.
3. Multi Label Classification.
Multi label classification allows a single piece of data to belong to multiple categories at the same time. Unlike multiclass classification, where each data point is assigned only one class, this approach allows the model to assign multiple labels to the same input.
Key aspects include:
- Multiple labels per data point: One input can belong to more than one category.
- Labels can overlap: Classes are not mutually exclusive.
- Common example: A movie recommendation system may tag a movie as both action and comedy based on features like plot, actors or genre tags.
Logistic regression workflow.
# Hands on - Breast cancer diagnosis
import numpy as np
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
X = data.data
y = data.target
split data
Splitting data helps avoid overfitting of the models i.e. models performing very well on training data but poorly on new data. It also ensures the model doesn't memorize data.
# split data into train and test datasets.
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)
scaling data
Feature scaling is the process of putting all numerical features on a similar scale so that no feature dominates others just because of its size.
# Scale features.
# Logistic regression is sensitive to feature scale
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
X_train = scale.fit_transform(X_train) #Learn scale from training data only.
X_test = scale.transform(X_test)
Training the model.
Training involves adjusting model parameters to minimize error using training data.
# Train model.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
**making predictions**
Predict.
After training, the model uses learned patterns to predict outcomes on new data.
# predict
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
print(f"Sample predictions: {y_pred[:5]}")
print(f"Sample probabilities: {y_prob[:5].round(3)}")
Evaluating the model
Model evaluation involves measuring how well predictions match real values using test data
# Evaluate
from sklearn.metrics import accuracy_score, classification_report
accuracy = accuracy_score(y_test, y_pred)
classificationR = classification_report(y_test, y_pred, target_names=data.target_names)
print(f"Accuracy: {accuracy:.2%}")
print(classificationR)
Display the confusion matrix
The confusion matrix is used to evaluate classification models by showing correct and incorrect predictions.
from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test, display_labels=data.target_names, cmap='Blues')
plt.show()
# TP FN
# FP TN


Top comments (0)