Machine Learning With Python.
Machine Learning With Python.
Joseph T. Handy
Copyright © 2024 by Joseph T. Handy
So, what are you waiting for? Buckle up, grab your favourite coding buddy
(or just grab a cup of coffee!), and get ready to unleash the power of
machine learning!
In the first chapter, we'll unveil the fascinating world of ML, exploring its
various applications and why Python reigns supreme in this domain. We'll
also set up your learning environment, so you can start coding right away.
See you there!
PART 1
FOUNDATIONS
CHAPTER 1
INTRODUCTION TO MACHINE
LEARNING WITH PYTHON
Imagine a world where computers learn from data, just like humans do!
This isn't science fiction; it's the reality of machine learning (ML)! Buckle
up, because in this chapter, we'll embark on a thrilling voyage to understand
what ML is all about and how it's reshaping the world around us.
2. Fraud Detection:
4. Self-Driving Cars:
These are just a few glimpses into the vast and ever-expanding world of ML
applications. As you delve deeper into this field, you'll discover even more
innovative and impactful ways machine learning is shaping our future!
● Python:
Python
# Print a message
print("Hello, world!")
C++
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
As you can see, the Python code is much shorter and easier to read
compared to the C++ code, making it more approachable for newcomers.
4. Cross-Platform Compatibility:
Now, let's install the essential libraries we'll need for our ML journey:
pandas, scikit-learn, and TensorFlow. Open your command prompt or
terminal again and type the following command:
pip install pandas scikit-learn tensorflow
Pip is the package manager for Python, and this command instructs it to
install the listed libraries. Simply press enter and let pip handle the
download and installation process.
4. Verifying Library Installation:
Python
import pandas
import sklearn
import tensorflow
print(pandas.__version__) # Check pandas version
print(sklearn.__version__) # Check scikit-learn
version
print(tensorflow.__version__) # Check TensorFlow
version
If you see the version numbers for each library printed out, you're good to
go!
Remember:
● Make sure you have internet access for the installation process to
work.
● If you encounter any errors during installation, refer to the official
documentation of Python and the libraries for troubleshooting
assistance.
With Python and its essential libraries installed, you've unlocked the
gateway to the world of building intelligent systems!
This interactive and visual nature makes Jupyter Notebooks perfect for:
Python
# Print a message
print("Hello, world!")
Running this cell will display the message "Hello, world!" right below the
code.
2. Integrated Development Environments (IDEs): The Feature-Rich
Powerhouses
IDEs offer a more comprehensive development environment, packed with
features like:
● Code completion: Helping you write code faster and with fewer
errors.
● Debugging tools: Assisting you in identifying and fixing bugs in
your code.
● Version control: Enabling you to track changes and revert to
previous versions of your code.
Popular IDEs for Python and ML include PyCharm, Visual Studio Code,
and Spyder. While these tools provide robust features, the learning curve
might be steeper compared to Jupyter Notebooks.
● Numbers:
○ Integers (whole numbers): age = 25
○ Floats (decimal numbers): pi = 3.14159
● Text (strings): name = "Alice"
● Lists (collections of items): fruits = ["apple", "banana",
"orange"]
2. Operators:
These are the tools we use to manipulate and perform calculations on our
data stored in variables. They act like operators in maths:
● Arithmetic: +, -, *, /
● Comparison: ==, !=, <, >, <=, >=
● Logical: and, or, not
Code example:
Python
# Calculate the area of a rectangle
length = 10
width = 5
area = length * width
print(f"The area of the rectangle is: {area}")
3. Control Flow:
This determines how your code executes based on certain conditions. Think
of it like decision-making in real life:
● if-else: Executes code based on a condition.
● for loops: Repeats a block of code a specific number of times.
● while loops: Repeats a block of code as long as a condition is true.
Code example:
Python
age = 18
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
4. Functions:
These are reusable blocks of code that perform specific tasks. They help
you organise your code and avoid redundancy.
Code example:
Python
def greet(name):
"""Prints a greeting message."""
print(f"Hello, {name}!")
greet("Bob") # Call the function with an argument
These are just a few of the essential Python concepts that will form the
foundation of your ML journey. As we progress, we'll gradually introduce
more advanced concepts and build upon these building blocks to create
sophisticated ML models.
Creating a DataFrame:
Python
import pandas as pd
# Create a dictionary with data
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 22], 'City': ['New York', 'Los
Angeles', 'Chicago']}
# Create a DataFrame from the dictionary
df = pd.DataFrame(data)
# Print the DataFrame
print(df)
Explanation:
Python
# Access the 'Name' column
names = df['Name']
# Get the first row
first_row = df.iloc[0]
# Add a new column 'Country'
df['Country'] = ['USA', 'USA', 'USA']
# Print the modified DataFrame
print(df)
Explanation:
1. Access data:
○ names = df['Name'] assigns the values from the
Name column to the variable names.
○ first_row = df.iloc[0] assigns the data from the
first row (index 0) to the variable first_row.
This retrieves all values of different
columns in the first row.
2. Modify data:
○ df['Country'] = ['USA', 'USA', 'USA'] adds a new
column named Country with the specified
values for each row.
Beyond the Basics:
As you progress in your data science journey, you'll discover even more
powerful features of DataFrames:
Data often comes in inconsistent formats, like having ages in both years and
strings. Cleaning and transforming your data ensure consistency and
facilitates analysis:
Python
# Convert 'Age' column to integer
df['Age'] = df['Age'].astype(int)
# Remove leading and trailing spaces from the
'City' column
df['City'] = df['City'].str.strip()
# Create a new column 'Age_Group' based on age
ranges
df['Age_Group'] = ['Young' if age < 25 else
'Adult' for age in df['Age']]
# Print the DataFrame after formatting
print(df)
Here are code examples to illustrate the concepts in this section:
Python
# Convert 'Age' column to integer
df['Age'] = df['Age'].astype(int)
# Remove leading and trailing spaces from the
'City' column
df['City'] = df['City'].str.strip()
# Create a new column 'Age_Group' based on age
ranges
df['Age_Group'] = ['Young' if age < 25 else
'Adult' for age in df['Age']]
# Create a new column 'Score_Category' based on
score ranges
df['Score_Category'] = pd.cut(df['Score'], bins=
[0, 50, 75, 100], labels=['Low', 'Medium',
'High'])
# Print the DataFrame after formatting
print(df)
Remember:
Data cleaning and manipulation might seem tedious, but it's an essential
investment for building robust machine learning models. By diligently
cleaning and transforming your data, you lay the foundation for reliable and
insightful results in the next chapters!
Remember:
EDA is an iterative process. As you explore your data, you might uncover
new questions that lead you to further explore specific aspects. Embrace
this curiosity and keep digging deeper to unlock the full potential of your
data.
PART 2
SUPERVISED LEARNING
CHAPTER 4
FUNDAMENTALS OF
SUPERVISED LEARNING:
UNVEILING THE MAGIC OF
PREDICTIONS
Welcome back, data explorers! We've delved into the exciting world of data
preparation and exploration, and now, it's time to take the next step:
supervised learning. This is where the rubber meets the road, as you teach
algorithms to make predictions based on your data! Buckle up, because
we're about to witness some serious machine learning magic!
Regression:
Python
from sklearn.linear_model import LinearRegression
# Sample data: movie features (X) and user ratings
(y)
X = [[120, "Comedy"], [180, "Action"], [100,
"Drama"]] # Movie duration, genre
y = [4, 5, 3] # User ratings
# Create and train the linear regression model
model = LinearRegression()
model.fit(X, y)
# Predict the rating for a new movie (150 minutes,
Comedy genre)
new_movie = [[150, "Comedy"]]
predicted_rating = model.predict(new_movie)
print("Predicted rating for the new movie:",
predicted_rating[0])
Classification:
● What it is: A technique for predicting categorical values.
● Think of it as: Assigning data points to predefined categories, like
classifying emails as spam or not spam, or categorising images as
containing cats or dogs.
● Example: Predicting the genre of a movie (e.g., comedy, action,
drama) based on its plot description or audio features.
Regression:
Classification:
Python
from sklearn.metrics import accuracy_score
# Example: Predicted vs. actual email spam
classification
y_pred = [1, 0, 1, 0] # Predicted spam (1) or not
spam (0)
y_true = [1, 0, 0, 1] # Actual spam (1) or not
spam (0)
accuracy = accuracy_score(y_true, y_pred)
print("Accuracy:", accuracy)
# Interpretation: 75% of the predictions were
correct.
Python
from sklearn.linear_model import LinearRegression
# Sample data: features (X) and calories burned
(y)
X = [[30, 140, "Running"], [45, 160, "Cycling"],
[20, 120, "Swimming"]] # Duration (minutes),
Heart Rate, Exercise Type
y = [300, 450, 200] # Calories burned
# Create and train the linear regression model
model = LinearRegression()
model.fit(X, y)
# Predict the calories burned for a new workout
session (40 minutes, 150 heart rate, "Swimming")
new_workout = [[40, 150, "Swimming"]]
predicted_calories = model.predict(new_workout)
print("Predicted calories burned for the new
workout:", predicted_calories[0])
Python
from sklearn.tree import DecisionTreeClassifier
# Sample data: features (X) and churn status (y)
X = [[7 days, 10 workouts, Free], [30 days, 2
workouts, Premium], [14 days, 5 workouts, Basic]]
# Days since last activity, Workouts per week,
Subscription type
y = [1, 0, 1] # Churn status (1 = churned, 0 =
not churned)
# Create and train the decision tree classifier
model
model = DecisionTreeClassifier()
model.fit(X, y)
# Predict churn status for a new user (20 days
since last activity, 3 workouts per week, Basic
subscription)
new_user = [[20, 3, "Basic"]]
predicted_churn = model.predict(new_user)
print("Predicted churn status for the new user:",
predicted_churn[0])
# Interpret the output: 1 represents churn, 0
represents not churn
Imagine building a sandcastle on the beach. You have different tools at your
disposal: a giant shovel, a small trowel, and a delicate seashell. Each tool
has its strengths and weaknesses, and the best choice depends on the
specific task at hand:
Finding the sweet spot between bias and variance is crucial for optimal
performance.
There's no single "best" model for all situations. The optimal choice
depends on your specific data, task, and desired performance metrics. Here
are some tips for navigating the model selection process:
Explanation:
Python
import pandas as pd
from sklearn.linear_model import LinearRegression
# Load salary dataset
data = pd.read_csv("salary_data.csv")
# Define features (X) and target variable (y)
X = data[["years_experience", "education_level"]]
y = data["salary"]
# Create and train the model
model = LinearRegression()
model.fit(X, y)
# Predict salary for a person with 5 years
experience and an education level of 2
new_employee = [[5, 2]]
predicted_salary = model.predict(new_employee)
print("Predicted salary:", predicted_salary[0])
Python
# Import necessary libraries
from sklearn.linear_model import LinearRegression
import pandas as pd
# Load your data (replace with your data source)
data = pd.read_csv("movie_data.csv")
# Define features (X) and target variable (y)
X = data["subscription_duration"] # Feature:
Subscription duration (months)
y = data["movies_watched"] # Target variable:
Number of movies watched
# Create and train the linear regression model
model = LinearRegression()
model.fit(X.values.reshape(-1, 1), y) # Reshape X
for compatibility
# Make predictions!
new_duration = 12 # Example: Predict for a 12-
month subscription
predicted_movies = model.predict([[new_duration]])
# Print the predicted number of movies watched
print("Predicted number of movies watched:",
predicted_movies[0])
Explanation:
Let's say you want to predict the price of a used car based on its mileage
and age:
Python
import pandas as pd
from sklearn.linear_model import LinearRegression
# Load car price dataset
data = pd.read_csv("car_prices.csv")
# Define features and target variable
X = data[["mileage", "age"]] # Features: mileage
and age
y = data["price"]
# Create and train the model
model = LinearRegression()
model.fit(X, y)
# Predict the price of a car with 50,000 miles and
3 years old
new_car = [[50000, 3]]
predicted_price = model.predict(new_car)
print("Predicted price:", predicted_price[0])
Explanation:
Python
import pandas as pd
from sklearn.linear_model import LinearRegression
# Load the website traffic dataset
data = pd.read_csv("website_traffic.csv")
# One-hot encode the 'day' feature (if your
dataset contains categorical data)
data = pd.get_dummies(data, columns=['day'])
# Define features and target variable
X = data[["ad_spend", "day_Mon", "day_Tue",
"day_Wed", "day_Thu", "day_Fri"]]
y = data["traffic"]
# Create and train the model object
model = LinearRegression()
model.fit(X, y)
# Predict traffic for $100 ad spend on a Wednesday
new_datapoint = [[100, 0, 0, 1, 0, 0]] # '1' in
the 'day_Wed' column
predicted_traffic = model.predict(new_datapoint)
print("Predicted website traffic:",
predicted_traffic[0])
Explanation:
Key Points:
● Features: Identify and select the features that you believe are most
influential in predicting the target variable.
● Data Preprocessing: Ensure your data is in a suitable numerical
format for use with scikit-learn. This may involve handling
categorical features using techniques like one-hot encoding, as
shown in the website traffic example.
By analysing these metrics, you can gain valuable insights into how well
your model is performing. Remember, a perfect model doesn't exist, but
striving for a balance between low MSE and high R-squared is crucial for
reliable predictions.
Python
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
# Load the Iris flower dataset (classifying flower
types)
iris = load_iris()
# Separate features (X) and target variable (y)
X = iris.data
y = iris.target
# Create and train the KNN model with k=3
neighbors
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
# Predict the class of a new flower (based on its
features)
new_flower = X[-1] # Select the last data point
as the new flower
predicted_class = model.predict([new_flower])[0]
# Print the predicted flower type (e.g., 0 - Iris
Setosa, 1 - Iris Versicolor, 2 - Iris Virginica)
print("Predicted flower type:", predicted_class)
Python
from sklearn.linear_model import
LogisticRegression
# (Assuming you have loaded and preprocessed your
data)
# Split data into features (X) and target variable
(y)
# Create and train the logistic regression model
model = LogisticRegression()
model.fit(X, y)
# Predict the probability of a new data point
belonging to a specific class (e.g., spam or not
spam)
new_datapoint = X[0] # Select a data point
predicted_probabilities =
model.predict_proba([new_datapoint])[0]
# The first element in 'predicted_probabilities'
represents the probability of class 0
# The second element represents the probability of
class 1 (and so on)
print("Predicted probabilities:",
predicted_probabilities)
These are just a few examples, and the choice of algorithm depends on your
specific problem and data characteristics. As you progress in your data
science journey, you'll encounter a wider range of classification algorithms,
each with its own strengths and weaknesses.
Here are code example to illustrate the concepts in this section:
Let's say you have data about customers and want to predict whether they're
likely to make a purchase based on features like age, income, and past
purchase history.
Python
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
# Load customer data
data = pd.read_csv("customer_data.csv")
# Define features and target variable
X = data[["age", "income", "num_purchases"]]
y = data["will_purchase"] # 0: Not likely to
purchase, 1: Likely to purchase
# Create and train a KNN classifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
# Predict for a new customer
new_customer = [[35, 60000, 5]] # Age 35, income
60000, 5 past purchases
prediction = model.predict(new_customer)[0]
# Interpret the results
if prediction == 1:
print("The customer is likely to make a
purchase.")
else:
print("The customer is not likely to make a
purchase.")
Explanation:
1. Load data: We load the customer data from a
CSV file (remember to replace
"customer_data.csv" with your actual
filename).
2. Define features and target: We identify
relevant features (age, income, past
purchases) and the target we want to predict
(whether they'll make a purchase).
3. Create and train model: A KNeighborsClassifier is
set up with n_neighbors=3 (meaning it'll look at
the 3 closest neighbours). We train the model
using model.fit(X, y).
4. Prediction: We make a prediction for a new
customer profile and store it in the prediction
variable.
5. Interpretation: We print the outcome of the
prediction in a human-readable format.
Example 2: Logistic Regression – Predicting Email Spam
Imagine building a model to flag emails as spam or not spam based on their
content. Here's a simplified illustration using logistic regression:
Python
import pandas as pd
from sklearn.linear_model import
LogisticRegression
# Load email data (preprocessed text features)
data = pd.read_csv("email_data.csv")
# Define features and target
X = data[["word_count", "contains_offer",
"sender_suspicious"]]
y = data["is_spam"] # 0: Not spam, 1: Spam
# Create and train the model
model = LogisticRegression()
model.fit(X, y)
# Predict for a new email
new_email = [[200, 1, 0]] # 200 word count,
contains an offer, not from a suspicious sender
predicted_probability =
model.predict_proba(new_email)[0][1] #
Probability of being spam
if predicted_probability > 0.5:
print("This email is likely spam.")
else:
print("This email is likely not spam.")
Explanation:
Note that text data often requires preprocessing steps (like converting words
into numerical features) before being suitable for machine learning
algorithms. We've assumed this is already done for simplicity.
Let's consider a basic example where a decision tree predicts if a patient has
a certain disease based on symptoms.
Python
from sklearn.tree import DecisionTreeClassifier
# Load medical data (replace with your actual
data)
# Define features and target
X = data[["fever", "cough", "fatigue"]]
y = data["has_disease"] # 0: No disease, 1: Has
disease
# Create and train decision tree model
model = DecisionTreeClassifier()
model.fit(X, y)
# Predict for a new patient
new_patient = [[1, 0, 1]] # Fever, no cough,
fatigue
prediction = model.predict(new_patient)[0]
# ... (interpret the results)
Remember, choosing the right tool for the job is crucial for successful
classification! In the next section, we'll delve into the world of
implementing these algorithms in Python using scikit-learn, empowering
you to code up your own classification models.
Python
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
# Load the Iris flower dataset (classifying flower
types)
iris = load_iris()
# Separate features (X) and target variable (y)
X = iris.data
y = iris.target
# Create and train the KNN model with k=3
neighbors
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
# Predict the class of a new flower (based on its
features)
new_flower = [[5.1, 3.5, 1.4, 0.2]] # Sample
features of a new flower
predicted_class = model.predict([new_flower])[0]
# Print the predicted flower type (e.g., 0 - Iris
Setosa, 1 - Iris Versicolor, 2 - Iris Virginica)
print("Predicted flower type:", predicted_class)
Explanation:
1. Libraries: We import to load the
load_iris
dataset and KNeighborsClassifier from scikit-learn.
2. Load data: We load the Iris flower dataset,
which has features like sepal length and petal
width, and the corresponding flower types
(classes).
3. Separate features and target: We split the data
into features (X) and the target variable (y)
representing the flower types.
4. Create and train the model: We create a
KNeighborsClassifier object (model) with n_neighbors=3
and train it using the fit method.
5. Make predictions: We create a new flower
data point (new_flower) with its features and use
the predict method to get the predicted class
label.
6. Print the results: We print the predicted
flower type based on the model's prediction.
This is just a basic example using KNN. You can explore other algorithms
like LogisticRegression or DecisionTreeClassifier following a similar
structure, replacing the model creation step with the corresponding scikit-
learn class.
Here are code example to illustrate the concepts in this section:
Explanation:
Note that this example assumes your text data has undergone some basic
preprocessing to transform it into a suitable format for the model. Let's
break down the key steps:
● Load and preprocess data: We load the preprocessed movie
reviews and sentiment labels.
● Text vectorization: We use a CountVectorizer to
convert the text of the reviews into numerical
features (word counts).
● Create and train the model: Using
LogisticRegression, we train the model to associate
the word count patterns with the sentiment
labels.
● Predict on new data: We convert a new review
into its corresponding feature representation
and use the predict method to determine its
predicted sentiment.
Example 2: Decision Tree for Credit Card Eligibility
Let's say you want to predict if a customer is eligible for a credit card based
on their income, age, and existing debt.
Python
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
# Load credit card application data
data = pd.read_csv("credit_applications.csv")
# Define features and target variable
X = data[["income", "age", "debt"]]
y = data["approved"] # 0: Not approved, 1:
Approved
# Create and train a decision tree classifier
model = DecisionTreeClassifier()
model.fit(X, y)
# Predict for a new customer application
new_customer = [[50000, 30, 10000]]
prediction = model.predict(new_customer)[0]
if prediction == 1:
print("The customer is likely eligible for a
credit card.")
else:
print("The customer may not be eligible for a
credit card.")
Explanation:
This example is more straightforward, as the data likely doesn't require
complex text preprocessing. The decision tree learns rules based on income,
age, and debt to predict a customer's credit card eligibility.
Remember: These examples illustrate different classification algorithms and
potential applications. Each dataset and problem might necessitate different
preprocessing and feature engineering approaches.
Python
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
# (Assuming you have loaded and preprocessed your
data)
# Define features (X) and target variable (y)
# Create a KNN model with different hyperparameter
values to try
model = KNeighborsClassifier()
# Define a grid of hyperparameter values to search
through
param_grid = {"n_neighbors": [3, 5, 7]} # Try
different numbers of neighbors
# Use GridSearchCV to find the best hyperparameter
combination
grid_search = GridSearchCV(model, param_grid,
scoring="accuracy")
grid_search.fit(X, y)
# Print the best model found and its corresponding
hyperparameters
print("Best KNN model:",
grid_search.best_estimator_)
print("Best hyperparameters:",
grid_search.best_params_)
Explanation:
Explanation:
Explanation:
1. Load email data: Load your dataset
containing email text and spam labels.
2. Extract basic text features: Employ a
vectorizer like TfidfVectorizer to represent words
in emails as numerical features.
3. Create new features: Derive features from
email metadata (e.g., counting capital letters
in the subject or checking for exclamation
marks), hinting at spam-like behaviour.
4. Combine features: Combine the text-based
features with your newly created features to
form an enhanced feature set.
Example 2: Hyperparameter Tuning for Logistic Regression
We want to optimise the parameters of a logistic regression model for credit
approval prediction:
Python
from sklearn.linear_model import
LogisticRegression
from sklearn.model_selection import
RandomizedSearchCV
# (Assume you have loaded and preprocessed your
data)
# Define features (X) and target variable (y)
# Create a LogisticRegression model with different
hyperparameters
model = LogisticRegression(solver="liblinear")
# Define the parameter grid for randomised search
param_grid = {
"C": [0.01, 0.1, 1, 10], # Regularisation
strength
"penalty": ["l1", "l2"], # Type of
regularisation
}
# Perform randomised search for hyperparameter
tuning
random_search = RandomizedSearchCV(model,
param_grid, n_iter=20, scoring="accuracy")
random_search.fit(X, y)
# Print the best model and hyperparameters
print("Best Logistic Regression model:",
random_search.best_estimator_)
print("Best hyperparameters:",
random_search.best_params_)
Explanation:
Python
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
# Load the Iris flower dataset
iris = load_iris()
# Define features (X)
X = iris.data
# Choose the number of clusters (k) - Experiment
with different values!
k = 3 # Trying 3 clusters for this example
# Create and train the KMeans model
model = KMeans(n_clusters=k)
model.fit(X)
# Predict cluster labels for each data point
predicted_cluster_labels = model.predict(X)
# Print the cluster labels for the first 5 data
points
print("Predicted cluster labels for the first 5
data points:", predicted_cluster_labels[:5])
Explanation:
Python
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from PIL import Image # Needs installation: 'pip
install Pillow'
# Load and preprocess image
image = Image.open("my_image.jpg").convert('RGB')
# Convert to RGB format
image_array = np.asarray(image).reshape(-1, 3) #
Reshape for clustering
# Choose the number of clusters (k)
k = 5 # Experiment to find the best segmentation
# Perform K-Means clustering
model = KMeans(n_clusters=k)
model.fit(image_array)
cluster_assignments = model.labels_
# Reshape to reconstruct the image with segmented
colours
segmented_image =
cluster_assignments.reshape(image.size[1],
image.size[0])
# Display the segmented image
plt.imshow(segmented_image)
plt.show()
Explanation:
Hierarchical clustering can reveal nested clusters, which can be helpful for
complex datasets. Here's an example:
Python
import pandas as pd
from sklearn.cluster import
AgglomerativeClustering
import scipy.cluster.hierarchy as shc # For
dendrogram visualisation
# Load customer data (assuming preprocessed)
data = pd.read_csv("customer_data.csv")
# Define features and target variable (not
applicable here)
X = data[["annual_income", "spending_score"]]
# Create a dendrogram to visualise hierarchical
relationships
plt.figure(figsize=(10, 5)) # Adjust figure size
plt.title("Customer Dendrogram")
dendrogram = shc.dendrogram(shc.linkage(X,
method="ward")) # 'ward' minimises variance
within clusters
plt.show()
# Perform hierarchical clustering (example with 3
clusters)
model = AgglomerativeClustering(n_clusters=3)
model.fit(X)
# Add cluster labels to customer data
data["cluster"] = model.labels_
print(data.groupby("cluster").describe())
Explanation:
Explanation:
Explanation:
Explanation:
● Input Layer: Receives the raw data, the starting point for the
information flow. The number of neurons in this layer corresponds
to the number of features in your data (e.g., 784 neurons for a
28x28 pixel grayscale image).
● Hidden Layers: These layers are the heart of the learning process,
where the magic happens! Each hidden layer typically consists of
multiple interconnected neurons. The number of hidden layers and
neurons significantly impacts the network's capacity to learn
complex patterns. Choosing the right architecture (number of layers
and neurons) is crucial for optimal performance and can be an art
form in itself.
● Output Layer: Produces the final output, which can be:
○ Classification: Assigning data points to specific categories
(e.g., identifying handwritten digits).
○ Regression: Predicting continuous values (e.g., forecasting
stock prices).
○ Generation: Creating new data (e.g., generating realistic
images).
Python
import tensorflow as tf
# Define a simple neural network with one hidden
layer
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu',
input_shape=(784,)), # Input layer with 784
neurons (for MNIST handwritten digits)
tf.keras.layers.Dense(10, activation='softmax')
# Hidden layer with 10 neurons and softmax
activation for classification
])
# Compile the model, specifying the loss function
(measures error) and optimizer (algorithm for
weight adjustments)
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
# Train the model on your data (replace with your
actual training data)
model.fit(X_train, y_train, epochs=10)
# Use the trained model to make predictions on new
data
predictions = model.predict(X_test)
# Example: Training a model for handwritten digit
recognition (MNIST dataset)
model.fit(X_train, y_train, epochs=10) # Train
the model on training data for 10 epochs
# Evaluate the model's performance on unseen data
test_loss, test_acc = model.evaluate(X_test,
y_test)
print("Test Accuracy:", test_acc)
# Use the trained model to make predictions on a
new image
new_image = ... # Load your new image data
prediction = model.predict(np.array([new_image]))
# Wrap the image in a numpy array
print("Predicted digit:", np.argmax(prediction))
Python
import tensorflow as tf
# Input layer to accommodate 3 features
input_layer = tf.keras.layers.Dense(units=3,
input_shape=(3,))
# Output layer for binary classification (e.g.,
cat or dog)
output_layer = tf.keras.layers.Dense(units=1,
activation='sigmoid')
Explanation:
● input_layer: We create a dense (fully connected) layer with 3
neurons, designed to receive an input with three features.
● output_layer: A single neuron is used for binary classification. The
'sigmoid' activation function is suitable here because it squishes
outputs between 0 and 1, representing probabilities (e.g., the
probability of the image being a cat).
Explanation:
● hidden_layer_1: 16 neurons with 'ReLU' (Rectified Linear Unit)
activation. ReLU is a popular choice, introducing non-linearity and
often producing fast convergence in training.
● hidden_layer_2: 8 neurons and the 'tanh' (hyperbolic tangent)
activation. Tanh offers an output range between -1 and 1, which can
be helpful in certain scenarios.
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
# Flatten 2D images into 1D array
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10,
activation='softmax') # 10 classes for digit
classification
])
Explanation:
● Flatten: This layer takes a 2D image input (e.g., 28x28 pixels) and
flattens it into a 1D array, making it suitable for dense layers.
● Hidden Layer: Dense layer with 128 neurons and ReLU activation.
● Output Layer: Dense layer with 10 neurons (for 0-9-digit
classification) and 'softmax' activation. Softmax ensures outputs
sum to 1, representing class probabilities.
4. Compiling a Model:
Python
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Explanation:
Why TensorFlow?
Python
import tensorflow as tf
# Create a constant tensor
const_tensor = tf.constant([1, 2, 3, 4])
# Print the tensor
print(const_tensor)
Explanation:
1. We import TensorFlow.
2. We create a constant tensor named const_tensor
with the values [1, 2, 3, 4].
3. We print the tensor, revealing its contents.
Here are code example to illustrate the concepts in this section:
1. Creating Tensors:
Python
import tensorflow as tf
# Constant tensors
scalar_tensor = tf.constant(7) # Single value
vector_tensor = tf.constant([10, 20, 30]) # 1D
array
matrix_tensor = tf.constant([[1, 2], [3, 4]]) #
2D array
# Tensors with specific data types
int_tensor = tf.constant([1, 2, 3],
dtype=tf.int32)
float_tensor = tf.constant([3.14, 1.618, 2.718],
dtype=tf.float64)
# Tensors from Python lists using
tf.convert_to_tensor
python_list = [50, 60, 70]
list_tensor = tf.convert_to_tensor(python_list)
Explanation:
Python
tensor1 = tf.constant([2, 4, 6])
tensor2 = tf.constant([1, 3, 5])
addition = tf.add(tensor1, tensor2)
subtraction = tf.subtract(tensor1, tensor2)
multiplication = tf.multiply(tensor1, tensor2)
print(addition) # Output: tf.Tensor([3 7 11],
shape=(3,), dtype=int32)
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(4, activation='relu',
input_shape=(2,)), # Two inputs, 4 neurons in the
hidden layer
tf.keras.layers.Dense(1, activation='sigmoid')
# Output for binary classification
])
model.compile(optimizer='sgd',
loss='binary_crossentropy', metrics=['accuracy'])
Explanation:
Python
# Assuming you have your training data in
'X_train' and 'y_train'
model.fit(X_train, y_train, epochs=10,
batch_size=32)
Explanation:
Python
import tensorflow as tf
from tensorflow.keras.datasets import mnist
# Load the MNIST dataset (handwritten digits)
(train_images, train_labels), (test_images,
test_labels) = mnist.load_data()
# Preprocess the data:
# 1. Normalise pixel values:
train_images = train_images.astype('float32') /
255.0
test_images = test_images.astype('float32') /
255.0
# 2. Reshape images:
train_images =
train_images.reshape((train_images.shape[0], 28 *
28))
test_images =
test_images.reshape((test_images.shape[0], 28 *
28))
Explanation:
Explanation:
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Explanation:
The moment of truth! Let's train our network using the prepared data:
Python
# Train the model on the training data for 5
epochs
model.fit(train_images, train_labels, epochs=5)
# Evaluate the model's performance on unseen data
test_loss, test_acc = model.evaluate(test_images,
test_labels)
print("Test Accuracy:", test_acc)
Explanation:
Python
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model
# Load the pre-trained VGG16 model without the
final classification layers
base_model = VGG16(weights='imagenet',
include_top=False, input_shape=(img_height,
img_width, 3))
# Freeze the pre-trained model layers (optional,
can be helpful to prevent overfitting)
base_model.trainable = False
# Add your own custom layers for fine-tuning
x = base_model.output
x = Flatten()(x)
predictions = Dense(num_classes,
activation='softmax')(x)
# Create the final model
model = Model(inputs=base_model.input,
outputs=predictions)
# Compile the model for training
model.compile(optimizer='adam',
loss='categorical_crossentropy', metrics=
['accuracy'])
# Train the model on your image data and labels
model.fit(train_images, train_labels, epochs=10)
# Evaluate the model's performance on unseen data
test_loss, test_acc = model.evaluate(test_images,
test_labels)
print("Test Accuracy:", test_acc)
This is just a simplified example, and the specific steps can vary depending
on your chosen architecture and dataset. However, it provides a basic
understanding of the process.
Python
import tensorflow as tf
from tensorflow.keras.preprocessing.image import
ImageDataGenerator
# Assuming you have images organised into 'train'
and 'test' folders by class
train_dir = 'path/to/train'
test_dir = 'path/to/test'
img_height = 224
img_width = 224
batch_size = 32
# Create ImageDataGenerator objects for efficient
image loading and augmentation
train_datagen = ImageDataGenerator(
rescale=1./255, # Normalise pixel values
rotation_range=20, # Random rotations
width_shift_range=0.2,
height_shift_range=0.2
)
test_datagen = ImageDataGenerator(rescale=1./255)
# Only normalisation for testing
# Flow images from directories, resizing and
generating batches
train_data = train_datagen.flow_from_directory(
train_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical' # Multi-class
classification
)
test_data = test_datagen.flow_from_directory(
test_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical'
)
Explanation
Python
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Flatten, Dense
base_model = VGG16(weights='imagenet',
include_top=False, input_shape=(img_height,
img_width, 3))
base_model.trainable = False # Optionally freeze
the pre-trained layers
# Add custom classification layers
x = base_model.output
x = Flatten()(x) # Flatten the output of the base
model
predictions = Dense(10, activation='softmax')(x)
# 10 classes for output
model = tf.keras.Model(inputs=base_model.input,
outputs=predictions)
Explanation
Python
model.compile(optimizer='adam',
loss='categorical_crossentropy', metrics=
['accuracy'])
model.fit(train_data, epochs=5,
validation_data=test_data)
test_loss, test_acc = model.evaluate(test_data)
print("Test Accuracy:", test_acc)
Explanation
RNNs are specifically designed to handle sequential data like text, where
the order of words matters. Unlike traditional neural networks that process
each word independently, RNNs can consider the context of surrounding
words, allowing them to:
Explanation:
1. Text Preprocessing:
Python
import nltk # Natural Language Toolkit (NLTK) is
helpful for this task
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
def preprocess_text(text):
# Convert to lowercase and remove punctuation
text = text.lower().replace(".",
"").replace(",", "") # Simplify for this example
# Tokenize the text (split into words)
words = nltk.word_tokenize(text)
# Remove stop words (common words like 'the',
'and', etc.)
stop_words = set(stopwords.words('english'))
words = [w for w in words if w not in
stop_words]
# Stemming (reduce words to their root form)
stemmer = PorterStemmer()
words = [stemmer.stem(w) for w in words]
return words
# Example usage
text = "This is a sample sentence for
demonstrating text preprocessing!"
processed_text = preprocess_text(text)
print(processed_text)
# Output: ['sampl', 'sentenc', 'demonstr', 'text',
'preproces']
Explanation:
Python
from tensorflow.keras.datasets import imdb
from tensorflow.keras.layers import GRU
# Load the IMDB dataset (pre-packaged movie review
sentiment classification)
(train_data, train_labels), (test_data,
test_labels) = imdb.load_data(num_words=10000)
# Define the model architecture (using GRU,
another type of RNN cell)
model = tf.keras.Sequential([
Embedding(10000, 128, input_length=max_len),
GRU(64),
Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs=5,
validation_data=(test_data, test_labels))
Explanation:
Python
from gensim.models import Word2Vec
# Train a Word2Vec model on your corpus of text
(assuming you have a list of sentences)
model = Word2Vec(sentences, size=100, window=5,
min_count=2)
# Access word vectors
word = "word_of_interest"
if word in model.wv.key_to_index: # Check if the
word exists in the model's vocabulary
word_vector = model.wv[word]
Explanation:
While delving into the intricacies of each architecture is beyond the scope
of this section, here's a glimpse into their basic building blocks:
Explanation:
Python
# Save the model after training
model.save('my_model.h5')
# Later, to load the saved model
loaded_model =
tf.keras.models.load_model('my_model.h5')
# Now you can use the loaded_model for
predictions!
Remember: This is a simplified example. Choosing the optimal saving
format and exploring advanced saving options (like saving only specific
model parts) might be necessary depending on your project's requirements.
There are several frameworks and libraries that can simplify the process of
integrating machine learning models into web applications. TensorFlow.js is
a popular option that allows you to run TensorFlow models directly in a
web browser, eliminating the need for complex server-side setups.
10.3 Best Practices for Deploying and
Monitoring Your Machine Learning
Model: Maintaining Peak Performance
You've trained a remarkable machine learning model, integrated it into a
web application, and unleashed its potential on the world. But just like any
high-performing athlete, your model requires ongoing monitoring and
maintenance to ensure it continues to deliver exceptional results. Here are
some key practices to keep your AI hero in top shape: