0% found this document useful (0 votes)
4 views19 pages

UNIT-IV DS With Python

The document provides an overview of various neural network architectures including Multilayer Perceptrons (MLPs), Recurrent Neural Networks (RNNs), Convolutional Neural Networks (CNNs), and Long Short-Term Memory (LSTM) networks, detailing their structures, applications, and advantages in data science. It includes practical examples using Python libraries like scikit-learn and TensorFlow for tasks such as classification, regression, and time series prediction. Key concepts such as backpropagation and the unique features of each network type are also discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

UNIT-IV DS With Python

The document provides an overview of various neural network architectures including Multilayer Perceptrons (MLPs), Recurrent Neural Networks (RNNs), Convolutional Neural Networks (CNNs), and Long Short-Term Memory (LSTM) networks, detailing their structures, applications, and advantages in data science. It includes practical examples using Python libraries like scikit-learn and TensorFlow for tasks such as classification, regression, and time series prediction. Key concepts such as backpropagation and the unique features of each network type are also discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-IV

Multilayer Perceptron (MLP)


A Multilayer Perceptron (MLP) is a type of artificial neural network (ANN)
used in data science for tasks like classification, regression, and pattern
recognition. It consists of multiple layers of nodes (neurons), which are
connected by weights, and typically includes one or more hidden layers between
the input and output layers.

Applications of Multilayer Perceptrons in Data Science

MLPs can be used in a wide variety of tasks, including:

 Classification: Assigning categories or labels to input data.


 Regression: Predicting continuous values from input data.
 Pattern Recognition: Recognizing patterns in images, time series data,
or sensor data.
 Anomaly Detection: Identifying outliers or abnormal data points.
 Speech and Text Processing: Analyzing audio signals or text for
sentiment analysis, translation, etc.

MLP using scikit-learn

scikit-learn provides an easy-to-use implementation of MLP for classification


and regression tasks. Here's how you can build a simple MLP for a
classification problem:

Example: Classifying the Iris Dataset using MLP


# Import necessary libraries
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score
# Load Iris dataset
data = load_iris()
X = [Link]
y = [Link]
# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Initialize MLP Classifier
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
mlp = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000,
activation='relu', solver='adam')
# Train the model
[Link](X_train, y_train)
# Make predictions on the test set
y_pred = [Link](X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")

In this example:

 hidden_layer_sizes=(10, 10): Two hidden layers with 10 neurons each.


 activation='relu': Using ReLU as the activation function.
 solver='adam': The optimizer used to adjust weights during training.
 max_iter=1000: The maximum number of iterations for training.

Advantages of MLP

 Universal Approximation: MLPs can approximate any continuous function given


sufficient neurons and layers.
 Flexibility: Can be used for both classification and regression problems.
 Non-linearity: Activation functions add non-linearity to the model, making it capable
of learning complex relationships.

Back propagation
Backpropagation is the process of updating the weights and biases of a neural network in
order to minimize the error of the model during training. It works by computing the gradient
(partial derivatives) of the loss function with respect to each weight and then adjusting the
weights in the opposite direction of the gradient to minimize the loss.

In a neural network, backpropagation is used to propagate the error backward from the
output layer to the input layer, updating the parameters (weights and biases) accordingly.

Steps in Backpropagation:

1. Forward Pass: The input is passed through the network, and the output is computed.
2. Loss Calculation: The difference between the predicted output and the true target is
measured using a loss function.
3. Backward Pass: The error is propagated backward from the output layer to the input
layer, calculating the gradients (derivatives of the loss with respect to weights and
biases).
4. Weights Update: Using the gradients, the weights and biases are updated using an
optimization algorithm (e.g., gradient descent).

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
Example:

from [Link] import load_iris


from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score
# Load Iris dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Initialize MLP Classifier with 2 hidden layers of 10 neurons each
mlp = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000, activation='relu',
solver='adam')
# Train the model
[Link](X_train, y_train)
# Predict on the test set
y_pred = [Link](X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")

Overview of Recurrent Neural Networks (RNN)


Recurrent Neural Networks (RNNs) are a type of artificial neural network designed for
sequence prediction problems. Unlike traditional feedforward neural networks, RNNs have
loops in their architecture, which allows them to maintain memory of previous inputs in a
sequence. This makes RNNs particularly well-suited for tasks such as time-series forecasting,
natural language processing (NLP), and speech recognition, where the order of data and
context are important.

Applications of RNNs in Data Science

RNNs are applied in a variety of real-world scenarios:

1. Natural Language Processing (NLP):


o Text Generation: RNNs can be used to generate text by predicting the next
word or character in a sentence.
o Sentiment Analysis: RNNs are often used to predict sentiment in text, for
example, determining if a product review is positive or negative.
o Machine Translation: Translating text from one language to another.
2. Time Series Prediction:
o RNNs are widely used for forecasting, such as predicting stock prices, sales
data, or weather patterns based on historical data.
3. Speech Recognition:
o RNNs can be used in speech-to-text systems to recognize words or phrases
from audio sequences.
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
4. Video Analysis:
o RNNs can analyze sequences of frames in videos to recognize actions or
detect anomalies.

Example: Time Series Prediction with RNN


import numpy as np
import tensorflow as tf
from [Link] import Sequential
from [Link] import SimpleRNN, Dense
from [Link] import MinMaxScaler
import [Link] as plt

# Generate sine wave data


time_steps = [Link](0, 100, 1000)
data = [Link](time_steps)

# Reshape data to fit the RNN input format (samples, timesteps, features)
data = [Link](-1, 1)

# Normalize data to the range [0, 1]


scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform(data)

# Create a dataset with lookback window of 10 time steps


def create_dataset(data, lookback):
X, y = [], []
for i in range(len(data) - lookback):
[Link](data[i:i+lookback, 0])
[Link](data[i+lookback, 0])
return [Link](X), [Link](y)

lookback = 10
X, y = create_dataset(data_scaled, lookback)

# Reshape X to fit the RNN input format (samples, timesteps, features)


X = [Link]([Link][0], [Link][1], 1)

# Split data into training and test sets (80-20 split)


train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# Build RNN model


model = Sequential()
[Link](SimpleRNN(units=50, activation='relu', input_shape=(X_train.shape[1], 1)))
[Link](Dense(1)) # Output layer

# Compile the model


[Link](optimizer='adam', loss='mean_squared_error')
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
# Train the model
[Link](X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))

# Predict future values


predictions = [Link](X_test)

# Inverse transform the predictions to original scale


predictions = scaler.inverse_transform(predictions)

# Plot the results


[Link](time_steps[train_size+lookback:], scaler.inverse_transform(y_test.reshape(-1, 1)),
color='blue', label='True Data')
[Link](time_steps[train_size+lookback:], predictions, color='red', label='Predicted Data')
[Link]()
[Link]()

Overview of Convolutional Neural Networks (CNNs)


Convolutional Neural Networks (CNNs) are a class of deep neural networks specifically
designed to work with data that has a grid-like topology, such as images. CNNs have become
the go-to architecture for image recognition, object detection, and similar tasks because of
their ability to automatically and adaptively learn spatial hierarchies in images. They are
highly effective for applications involving image data and have revolutionized fields like
computer vision.

Applications of CNNs in Data Science:

1. Image Classification:
o The most common application of CNNs. It involves classifying images into
predefined categories. For example, classifying images of animals as dogs,
cats, or birds.
2. Object Detection:
o CNNs are used to locate objects within images, often drawing bounding boxes
around them. This is widely used in autonomous vehicles and security systems.
3. Face Recognition:
o CNNs are also applied in identifying and verifying individuals from facial
features.
4. Image Segmentation:
o CNNs are used to divide an image into multiple segments (or regions) to make
it easier to analyze. For example, segmenting an image to detect tumors in
medical imaging.
5. Generative Models:
o CNNs are used in Generative Adversarial Networks (GANs) to generate new
images from learned data distributions (such as creating realistic human faces).
6. Video Analysis:
o CNNs can be applied to video data for tasks like action recognition and scene
understanding.
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example: Image Classification

import tensorflow as tf
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from [Link] import mnist
from [Link] import to_categorical
import [Link] as plt

# Load MNIST dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape the data to (28, 28, 1) as CNNs expect 3D inputs (height, width, channels)
X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)

# Normalize pixel values to be between 0 and 1


X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255

# One-hot encode the labels


y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Build the CNN model


model = Sequential()

# Convolutional layer with 32 filters, kernel size 3x3, ReLU activation


[Link](Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))

# Max pooling layer


[Link](MaxPooling2D(pool_size=(2, 2)))

# Add another convolutional layer with 64 filters


[Link](Conv2D(64, kernel_size=(3, 3), activation='relu'))

# Max pooling layer


[Link](MaxPooling2D(pool_size=(2, 2)))

# Flatten the data to feed into a fully connected layer


[Link](Flatten())

# Dense layer with 128 units


[Link](Dense(128, activation='relu'))

# Dropout for regularization


[Link](Dropout(0.2))

# Output layer with 10 units (for 10 classes) and softmax activation


Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
[Link](Dense(10, activation='softmax'))
# Compile the model
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model


[Link](X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test))

# Evaluate the model on the test data


test_loss, test_accuracy = [Link](X_test, y_test)
print(f'Test Accuracy: {test_accuracy * 100:.2f}%')

# Predict and visualize the result on the first test image


predictions = [Link](X_test)
[Link](X_test[0].reshape(28, 28), cmap='gray')
[Link](f"Predicted: {[Link]()}")
[Link]()

Overview of Long Short-Term Memory (LSTM)


Long Short-Term Memory (LSTM) networks are a special type of Recurrent Neural
Network (RNN) designed to capture long-range dependencies in sequential data. LSTMs are
highly effective for tasks involving time-series data, speech recognition, natural language
processing (NLP), and other sequence-based tasks where understanding context over long
sequences is crucial. They solve the vanishing gradient problem that standard RNNs face
when learning from long sequences.

Applications of LSTM in Data Science:

1. Time Series Prediction:


o LSTMs are widely used for forecasting future values in time-series data.
Examples include stock price prediction, weather forecasting, sales predictions,
and more.
2. Natural Language Processing (NLP):
o Text Generation: LSTMs can be used for generating text sequences that
follow a certain structure or style.
o Sentiment Analysis: Analyzing sentiment (positive/negative) in textual data.
o Machine Translation: LSTMs can be used to translate text from one
language to another.
3. Speech Recognition:
o LSTMs are frequently used in speech-to-text systems as they can learn and
recognize patterns in audio sequences over time.
4. Video Analysis:
o LSTMs are used in video classification, where sequential frames in a video are
analyzed to recognize actions or events (e.g., action recognition).
5. Anomaly Detection:
o LSTMs are also used for anomaly detection in time-series data. For example,
they can detect unusual patterns in server logs or detect fraud in financial
transactions.
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example: Time Series Prediction Using LSTM on Sine Wave Data
import numpy as np
import [Link] as plt
from [Link] import Sequential
from [Link] import LSTM, Dense
from [Link] import MinMaxScaler

# Generate sine wave data


time_steps = [Link](0, 100, 1000)
data = [Link](time_steps)

# Normalize the data


scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform([Link](-1, 1))

# Create the dataset with lookback window of 10 time steps


def create_dataset(data, lookback):
X, y = [], []
for i in range(len(data) - lookback):
[Link](data[i:i+lookback, 0])
[Link](data[i+lookback, 0])
return [Link](X), [Link](y)

lookback = 10
X, y = create_dataset(data_scaled, lookback)

# Reshape X to be 3D for LSTM input: (samples, timesteps, features)


X = [Link]([Link][0], [Link][1], 1)

# Split the data into training and testing sets (80-20 split)
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# Build the LSTM model


model = Sequential()
[Link](LSTM(units=50, activation='relu', input_shape=(X_train.shape[1], 1)))
[Link](Dense(1)) # Output layer

# Compile the model


[Link](optimizer='adam', loss='mean_squared_error')

# Train the model


[Link](X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))

# Predict the future values


predictions = [Link](X_test)

# Inverse transform the predictions


Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
predictions = scaler.inverse_transform(predictions)
# Plot the results
[Link](time_steps[train_size+lookback:], scaler.inverse_transform(y_test.reshape(-1, 1)),
color='blue', label='True Data')
[Link](time_steps[train_size+lookback:], predictions, color='red', label='Predicted Data')
[Link]()
[Link]()

Applications of Classification
Classification is a supervised learning task where the goal is to predict the categorical label or
class of an input based on past observations. In data science, classification is used to solve
problems where the output is discrete and consists of a predefined set of classes. Some
common examples of classification problems include spam email detection, sentiment
analysis, and medical diagnosis.

Python, with libraries like scikit-learn, TensorFlow, and Keras, provides powerful tools for
building, training, and evaluating classification models.

Common Applications of Classification in Data Science:

1. Email Spam Detection:


o Problem: Classifying emails as either spam or non-spam based on the content
of the email.
o Approach: Use algorithms like Naive Bayes, SVM, or deep learning models
to classify emails based on features like frequency of specific words, email
metadata, etc.
o Libraries: scikit-learn, NLTK, TensorFlow/Keras
2. Sentiment Analysis:
o Problem: Classifying the sentiment of text data as positive, negative, or
neutral (e.g., product reviews, social media posts).
o Approach: Use Natural Language Processing (NLP) techniques with models
like Logistic Regression, Naive Bayes, or LSTM networks.
o Libraries: scikit-learn, NLTK, TextBlob, TensorFlow/Keras
3. Medical Diagnosis:
o Problem: Predicting whether a patient has a certain disease (e.g., cancer, heart
disease) based on features like age, gender, medical history, and test results.
o Approach: Supervised learning algorithms such as Decision Trees, Random
Forests, Support Vector Machines (SVM), or deep learning models are used.
o Libraries: scikit-learn, TensorFlow/Keras
4. Image Classification:
o Problem: Classifying images into categories (e.g., detecting whether an image
contains a cat or a dog).
o Approach: Use Convolutional Neural Networks (CNNs) to extract
hierarchical features from the images and make predictions.
o Libraries: TensorFlow/Keras, OpenCV, scikit-learn
5. Customer Churn Prediction:

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
oProblem: Predicting whether a customer will churn (leave) or stay based on
their usage data, demographics, and behavior.
o Approach: Classify customers into "churn" or "no churn" categories using
Logistic Regression, Random Forests, or XGBoost.
o Libraries: scikit-learn, XGBoost, TensorFlow/Keras
6. Fraud Detection:
o Problem: Identifying fraudulent transactions or activities based on transaction
data (e.g., financial transactions).
o Approach: Classification models (e.g., Decision Trees, Random Forests,
Gradient Boosting) to identify suspicious behavior.
o Libraries: scikit-learn, XGBoost
7. Face Recognition:
o Problem: Identifying or verifying people based on facial features.
o Approach: Use deep learning techniques, such as CNNs or pre-trained models
like FaceNet or OpenFace.
o Libraries: TensorFlow/Keras, dlib, OpenCV
8. Speech Recognition:
o Problem: Classifying spoken words into predefined classes (e.g., command
recognition).
o Approach: Use Recurrent Neural Networks (RNNs) or LSTMs to process
speech data and classify speech into categories.
o Libraries: TensorFlow/Keras, librosa, speech_recognition
9. Document Classification:
o Problem: Automatically classifying documents into predefined categories,
such as legal, medical, or technical documents.
o Approach: Text classification algorithms (e.g., Naive Bayes, SVM, CNNs)
are used for document categorization.
o Libraries: scikit-learn, NLTK, TensorFlow/Keras
10. Recommendation Systems (Collaborative Filtering):

 Problem: Predicting the likelihood of a user liking a particular item based on their
past preferences.
 Approach: Use classification models to predict user-item interactions.
 Libraries: scikit-learn, TensorFlow/Keras, surprise
 Classification

Example: Classification using Logistic Regression

from [Link] import load_iris


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score

# Load the dataset


data = load_iris()
X = [Link]
y = [Link]

# Split into training and testing sets


Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train a logistic regression model


model = LogisticRegression(max_iter=200)
[Link](X_train, y_train)

# Make predictions
y_pred = [Link](X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f"Classification Accuracy: {accuracy * 100:.2f}%")

Applications of Regression
Regression is a type of supervised learning where the goal is to predict a continuous
numerical value based on input features. Unlike classification, which predicts discrete labels,
regression models predict continuous values. In data science, regression is used in a wide
variety of applications such as predicting house prices, stock market prices, sales forecasting,
and more.

Python, with libraries like scikit-learn, statsmodels, and TensorFlow/Keras, provides


powerful tools for building and evaluating regression models.

Common Applications of Regression in Data Science:

1. House Price Prediction:


o Problem: Predicting the price of a house based on various features like the
number of bedrooms, square footage, location, etc.
o Approach: Linear Regression, Decision Trees, Random Forest, Gradient
Boosting methods can be used.
o Libraries: scikit-learn, XGBoost, TensorFlow/Keras
2. Stock Price Prediction:
o Problem: Predicting future stock prices based on historical data such as stock
prices, trading volume, and other economic indicators.
o Approach: Time series regression using techniques like ARIMA, LSTM, or
traditional regression algorithms.
o Libraries: pandas, scikit-learn, TensorFlow/Keras
3. Sales Forecasting:
o Problem: Predicting future sales of products based on historical sales data,
seasonality, promotions, and other features.
o Approach: Linear Regression, Polynomial Regression, Random Forest
Regressor, or even deep learning models like LSTMs.
o Libraries: scikit-learn, pandas, TensorFlow/Keras
4. Healthcare Predictive Models:
o Problem: Predicting the progression of diseases, such as predicting the
likelihood of heart disease based on features like age, cholesterol levels,
smoking habits, etc.
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
o Approach: Linear Regression, Lasso Regression, Ridge Regression, or
Random Forest for handling non-linear relationships.
o Libraries: scikit-learn, statsmodels
5. Marketing & Customer Lifetime Value (CLV) Prediction:
o Problem: Predicting the lifetime value of customers for targeted marketing
and improving business strategies.
o Approach: Regression models like Linear Regression, Random Forest, or
XGBoost can be used to predict CLV.
o Libraries: scikit-learn, XGBoost, pandas
6. Energy Consumption Forecasting:
o Problem: Predicting the energy consumption of buildings or regions based on
weather data, historical consumption, and other relevant factors.
o Approach: Regression models such as Linear Regression or more complex
models like Random Forest or XGBoost.
o Libraries: scikit-learn, XGBoost
7. Agricultural Yield Prediction:
o Problem: Predicting the yield of crops based on factors such as soil quality,
weather, and irrigation.
o Approach: Multiple regression techniques (linear regression, decision trees)
or even deep learning models.
o Libraries: scikit-learn, pandas
8. Real-Time Traffic Prediction:
o Problem: Predicting the future traffic conditions based on historical data, time
of day, weather conditions, and other features.
o Approach: Time series regression using methods such as ARIMA, Random
Forest, or even LSTM networks for more complex patterns.
o Libraries: scikit-learn, TensorFlow/Keras, statsmodels
9. Demand Forecasting in Supply Chain:
o Problem: Predicting the future demand for products in a supply chain context
based on historical data, promotions, seasonality, and economic factors.
o Approach: Time series regression models like ARIMA, Linear Regression, or
more complex models like XGBoost.
o Libraries: scikit-learn, XGBoost, statsmodels
10. Customer Behavior Prediction:
o Problem: Predicting the probability of a customer purchasing a product based
on their previous purchase history, demographics, etc.
o Approach: Regression models like Logistic Regression (for probabilities) or
Linear Regression.
o Libraries: scikit-learn, TensorFlow/Keras

Example: Regression using Linear Regression

from [Link] import load_boston


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error

# Load dataset
data = load_boston()
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
X = [Link]
y = [Link]

# Split into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train the model


model = LinearRegression()
[Link](X_train, y_train)

# Make predictions
y_pred = [Link](X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")

Applications of Unsupervised Learning


Unsupervised learning is a type of machine learning where the model is trained on data
without labeled responses. The goal of unsupervised learning is typically to uncover hidden
patterns, groupings, or structures within the data. Unlike supervised learning, where you have
labeled data (input-output pairs), unsupervised learning only provides input data, and the
model attempts to discover patterns or structures from the input itself.

Some common unsupervised learning algorithms include Clustering, Dimensionality


Reduction, and Anomaly Detection. In Python, libraries like scikit-learn, TensorFlow,
Keras, and Pandas are commonly used for unsupervised learning tasks.

Common Applications of Unsupervised Learning:

1. Customer Segmentation

 Problem: Segmenting customers into distinct groups based on purchasing behavior,


demographics, or other features. These segments help businesses tailor their
marketing strategies.
 Approach: Clustering algorithms like K-Means, Hierarchical Clustering, and
DBSCAN are used to group customers based on similarities in behavior.
 Libraries: scikit-learn, KMeans, DBSCAN

2. Anomaly Detection

 Problem: Identifying unusual data points or outliers that deviate from the normal
pattern. For example, detecting fraud in financial transactions, identifying rare
diseases, or finding machine faults.
 Approach: Isolation Forest, DBSCAN, and One-Class SVM are commonly used
for anomaly detection.
 Libraries: scikit-learn, PyOD, TensorFlow
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
3. Recommendation Systems

 Problem: Building systems that recommend products, movies, or music based on user
preferences. Unsupervised learning helps in discovering patterns between items and
users.
 Approach: Collaborative Filtering (user-item similarity) and Matrix Factorization
(e.g., Singular Value Decomposition, or SVD) can be used to find similarities
between users or items.
 Libraries: scikit-learn, surprise, TensorFlow

4. Dimensionality Reduction

 Problem: Reducing the number of features in a dataset while retaining as much


information as possible. This is helpful for improving computational efficiency,
visualization, or noise reduction.
 Approach: Principal Component Analysis (PCA) and t-Distributed Stochastic
Neighbor Embedding (t-SNE) are popular techniques for dimensionality reduction.
 Libraries: scikit-learn, PCA, TSNE

5. Topic Modeling

 Problem: Discovering hidden themes or topics within a large collection of text data.
This is useful in document classification, content recommendation, and understanding
large text corpora.
 Approach: Latent Dirichlet Allocation (LDA) and Non-negative Matrix
Factorization (NMF) are common methods for topic modeling.
 Libraries: Gensim, scikit-learn, NLTK

6. Image Compression

 Problem: Reducing the size of image files without losing significant information.
Unsupervised learning techniques can be used for efficient image compression.
 Approach: Autoencoders (a type of neural network) can be used to learn compressed
representations of images.
 Libraries: Keras, TensorFlow

7. Data Preprocessing and Feature Engineering

 Problem: Identifying important features and reducing noise in data. Unsupervised


learning can help in automatically extracting useful features from raw data.
 Approach: Clustering and Dimensionality Reduction are often used in
preprocessing to prepare data for supervised learning tasks.
 Libraries: scikit-learn, PCA, TSNE

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
8. Genomic Data Analysis

 Problem: Understanding complex genomic data and finding relationships between


genes. Unsupervised learning can be applied to segment genes or individuals based on
genetic characteristics.
 Approach: Clustering methods like K-Means or Hierarchical Clustering and
Principal Component Analysis (PCA) are widely used in genomics.
 Libraries: scikit-learn, PCA, KMeans

9. Market Basket Analysis (Association Rule Mining)

 Problem: Finding associations between products purchased together. For example,


"people who buy bread also buy butter." This is helpful for cross-selling and
recommendation strategies.
 Approach: Apriori Algorithm and FP-growth for mining frequent itemsets and
association rules.
 Libraries: mlxtend, apyori

10. Speech Recognition

 Problem: Recognizing speech patterns from raw audio input. Unsupervised learning
techniques can help identify patterns and group similar types of sounds.
 Approach: Clustering and Dimensionality Reduction methods can be applied to
analyze raw speech data.
 Libraries: librosa, scikit-learn, TensorFlow
 Unsupervised Learning

Example: Clustering using K-Means

from [Link] import KMeans


from [Link] import load_iris
import [Link] as plt

# Load dataset
data = load_iris()
X = [Link]

# Perform KMeans clustering


kmeans = KMeans(n_clusters=3)
y_kmeans = kmeans.fit_predict(X)

# Plot the clusters


[Link](X[:, 0], X[:, 1], c=y_kmeans, cmap='viridis')
[Link]()

Hyperparameter Tuning

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
Hyperparameter tuning is a critical step in machine learning that involves selecting the best
set of hyperparameters for a model to achieve the best performance. Hyperparameters are
parameters that are set before the learning process begins, and they control the learning
process itself. These parameters are not learned from the data but are set manually or
automatically during training.

Types of Hyperparameter Tuning:

 Grid Search: Exhaustively searches through a manually specified subset of


hyperparameters.
 Random Search: Randomly searches hyperparameters from a specified range.
 Bayesian Optimization: Uses probabilistic models to predict the best set of
hyperparameters based on past search results.
 Genetic Algorithms: Mimics the process of natural evolution to optimize
hyperparameters.
 Hyperband: A newer approach that combines the principles of random search and
bandit algorithms.

Example 1: Hyperparameter Tuning with Grid Search

from [Link] import load_iris


from sklearn.model_selection import train_test_split, GridSearchCV
from [Link] import RandomForestClassifier

# Load dataset
data = load_iris()
X = [Link]
y = [Link]

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize Random Forest Classifier


rf = RandomForestClassifier(random_state=42)

# Define the parameter grid for hyperparameter tuning


param_grid = {
'n_estimators': [10, 50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}

# Initialize GridSearchCV with 5-fold cross-validation


grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv=5,
scoring='accuracy')

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
# Fit GridSearchCV
grid_search.fit(X_train, y_train)

# Best hyperparameters and score


print("Best Hyperparameters: ", grid_search.best_params_)
print("Best Accuracy: ", grid_search.best_score_)

Epochs and Batch Size in Data Science with Python


In machine learning and deep learning, especially when training models using
gradient-based optimization techniques (like stochastic gradient descent), the
concepts of epochs and batch size play a crucial role in controlling the training
process.

1. Epochs

An epoch refers to one complete forward and backward pass of all the training
examples through the model. In simple terms, an epoch means that each training
sample has been seen by the model once.

For example, if you have 1000 training samples, one epoch will involve passing
all 1000 samples through the model.

 Multiple Epochs: In practice, we train for multiple epochs to enable the


model to learn from the data in small steps. If we only train for one epoch,
the model might not learn well enough.
 Overfitting: Too many epochs can lead to overfitting (when the model
becomes too tailored to the training data and performs poorly on unseen
data).
 Underfitting: Too few epochs might lead to underfitting (when the
model hasn’t learned enough from the data).

Example of epochs:

If you're training a neural network with 1000 training samples, and you train it
for 10 epochs, the model will see each sample 10 times.

2. Batch Size

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA
The batch size refers to the number of training samples used in one iteration of
model training before the model's weights are updated.

Mini-batch gradient descent: In practice, rather than updating the


weights after every single sample (which is computationally expensive),
or after seeing the entire dataset (which might be slow), we use mini-
batch gradient descent where the model’s weights are updated after a
small batch of samples.

o For example, if you have 1000 samples and a batch size of 100,
you will have 10 iterations (1000 / 100 = 10) for each epoch.

Small Batch Size: Using small batch sizes (e.g., 32 or 64) can help in
reducing variance in the training process, which can lead to better
generalization.

Large Batch Size: Using larger batch sizes might speed up training but
could lead to poor generalization and overfitting.

Key Points:

 Small Batch Size: Can improve generalization, but training will take more time
because the model updates weights more frequently.
 Large Batch Size: Speeds up training but might lead to overfitting.

Example of Epochs and Batch Size in Python using Keras

Here’s an example of how to use epochs and batch size in a neural network training process
using Keras (which is a high-level API for building neural networks in Python):

# Importing necessary libraries


import numpy as np
from [Link] import Sequential
from [Link] import Dense
from [Link] import mnist
from [Link] import np_utils

# Load MNIST dataset

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Preprocess the data

X_train = X_train.reshape(X_train.shape[0], 784).astype('float32') / 255

X_test = X_test.reshape(X_test.shape[0], 784).astype('float32') / 255


Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
# One-hot encode the labels

y_train = np_utils.to_categorical(y_train, 10)

y_test = np_utils.to_categorical(y_test, 10)

# Define the model

model = Sequential()

[Link](Dense(128, input_dim=784, activation='relu'))

[Link](Dense(10, activation='softmax'))

# Compile the model

[Link](loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model with specified epochs and batch size

history = [Link](X_train, y_train, epochs=10, batch_size=64, validation_data=(X_test,


y_test))

# Evaluate the model

score = [Link](X_test, y_test)


print(f"Test accuracy: {score[1]}")

Explanation:

1. Epochs: The epochs=10 parameter means the model will train for 10 full passes over
the training data.
2. Batch Size: The batch_size=64 means the model will update weights after every 64
samples.
3. Model Architecture: A simple neural network is created using Keras with one hidden
layer of 128 neurons and an output layer with 10 neurons (for the 10 possible classes
in MNIST).
4. Data Preprocessing: The MNIST dataset is reshaped and normalized to be between 0
and 1.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. Of MCA

You might also like