0% found this document useful (0 votes)
2 views32 pages

Unit-III Ds With Python

The document covers key concepts in data science, including patterns, features, and various learning methods such as supervised and unsupervised learning. It provides Python examples for visualizing data patterns, performing feature extraction, and implementing classification tasks using logistic regression and SVM. Additionally, it discusses dimensionality reduction techniques and the challenges posed by the curse of dimensionality.
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)
2 views32 pages

Unit-III Ds With Python

The document covers key concepts in data science, including patterns, features, and various learning methods such as supervised and unsupervised learning. It provides Python examples for visualizing data patterns, performing feature extraction, and implementing classification tasks using logistic regression and SVM. Additionally, it discusses dimensionality reduction techniques and the challenges posed by the curse of dimensionality.
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-III

1. Patterns

 Patterns in data science refer to regularities or trends within the data. In Python,
identifying patterns typically involves applying data exploration techniques like
visualization and statistical analysis.

Example using Python:

import [Link] as plt


import numpy as np
# Generate random data with a linear pattern
x = [Link](0, 10, 100)
y = 2 * x + 1 + [Link](100) * 0.5 # Adding noise to the linear pattern
[Link](x, y, color='blue', label='Data points')
[Link](x, 2*x + 1, color='red', label='Pattern: y = 2x + 1')
[Link]('x')
[Link]('y')
[Link]()
[Link]()

This example uses matplotlib to visualize the data and identify a linear pattern.

2. Features

 Features are individual measurable properties of the data used as input for machine
learning models. Python provides libraries like Pandas and NumPy for working with
features.
 In machine learning, feature extraction refers to selecting or transforming the features
that will be used by the model.

Example in Python (using Pandas):

import pandas as pd
# Sample dataset of houses
data = {'Square_Feet': [1500, 1800, 2400, 3000, 3500],
'Bedrooms': [3, 4, 3, 5, 4],
'Price': [400000, 500000, 600000, 650000, 700000]}
df = [Link](data)
print(df)
# Extracting features (Square_Feet and Bedrooms) and target (Price)
features = df[['Square_Feet', 'Bedrooms']]
target = df['Price']

Here, Square_Feet and Bedrooms are features, and Price is the target variable.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
3. Pattern Representation

 Pattern representation in machine learning often involves transforming data into a


mathematical format that algorithms can work with. This might involve using vector
spaces, embeddings, or matrices.
 For example, in natural language processing (NLP), words are often represented as
vectors (embeddings) in a high-dimensional space.

Example of pattern representation using scikit-learn for text data (using TF-IDF for feature
extraction):

from sklearn.feature_extraction.text
import TfidfVectorizer
# Sample documents
documents = ["Data science is great", "Python is awesome for data science", "Data science
involves machine learning"]
# TF-IDF Vectorization
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(documents)
# Convert matrix to a DataFrame
df_tfidf =
[Link](tfidf_matrix.toarray(),columns=vectorizer.get_feature_names_out())
print(df_tfidf)

This code converts text data into a matrix of TF-IDF features, representing the patterns of
word importance in the documents.

4. Curse of Dimensionality

 The curse of dimensionality refers to the challenges that arise as the number of
features increases. It can make data sparse, lead to overfitting, and increase
computational complexity.
 The "curse" becomes especially problematic when working with high-dimensional data
(e.g., text data, image data).

Example (simulating the curse of dimensionality):

from [Link] import make_classification


import [Link] as plt
# Create a high-dimensional dataset
X, y = make_classification(n_samples=1000, n_features=50, n_informative=10,
random_state=42)
print(f"Original shape of the dataset: {[Link]}")

In this example, the dataset has 50 features, which could make it difficult to interpret and work
with effectively, demonstrating the curse of dimensionality.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
5. Dimensionality Reduction

 Dimensionality reduction techniques help mitigate the curse of dimensionality by


reducing the number of features in the data, while preserving as much information as
possible. Common techniques include Principal Component Analysis (PCA)
Principal Component Analysis (PCA)

PCA reduces the number of dimensions by projecting the data onto the directions of maximum
variance.

Example using PCA:

from [Link] import PCA


from [Link] import make_classification
import [Link] as plt
# Create high-dimensional data
X, y = make_classification(n_samples=1000, n_features=50, random_state=42)
# Apply PCA to reduce dimensions to 2 for visualization
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
# Plot the data in 2D
[Link](X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis', alpha=0.5)
[Link]('PC1')
[Link]('PC2')
[Link]('2D Representation using PCA')
[Link]()

This code applies PCA to reduce the data from 50 dimensions to 2, making it easier to
visualize.

Supervised Learning
Supervised learning involves training a model on labeled data, where the correct output is
known. The goal is to learn a mapping from inputs (features) to outputs (labels or targets), so
that the model can predict the output for new, unseen data.

Common Supervised Learning Tasks:

 Classification: The goal is to predict a categorical label (e.g., spam vs. not spam,
disease vs. no disease).
 Regression: The goal is to predict a continuous value (e.g., predicting house prices,
stock prices).

Example: Supervised Learning in Python

We’ll use scikit-learn, a popular Python library, to demonstrate a supervised learning example.
Let’s work on a classification task using the Iris dataset.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Classification Example with the Iris Dataset (Logistic Regression)
# Import necessary libraries
import pandas as pd
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 Iris dataset
iris = load_iris()
X = [Link] # Features (sepal length, sepal width, petal length, petal width)
y = [Link] # Labels (species of the iris)
# Split the data into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a logistic regression model
model = LogisticRegression(max_iter=200)
[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:

 We used Logistic Regression for classification.


 We loaded the Iris dataset, which is a well-known dataset in machine learning.
 We split the data into training and testing sets to evaluate the model's performance.

Key Python libraries for Supervised Learning:

 scikit-learn: Provides a wide range of supervised learning algorithms like Logistic


Regression, Support Vector Machines (SVM), Decision Trees, and Random Forests.
 XGBoost: A gradient boosting framework often used for supervised learning tasks.
 TensorFlow/Keras: Used for deep learning models that are also a form of supervised
learning.

Unsupervised Learning
Unsupervised learning, in contrast, involves training a model on data that does not have
labeled outputs. The goal is to find hidden patterns or groupings in the data. Since the data
does not have target labels, unsupervised learning typically deals with tasks like clustering,
anomaly detection, and dimensionality reduction.

Common Unsupervised Learning Tasks:

 Clustering: The goal is to group similar data points together (e.g., customer
segmentation, grouping similar documents).
 Dimensionality Reduction: The goal is to reduce the number of features in the dataset
while preserving important information (e.g., PCA, t-SNE).
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
 Anomaly Detection: Identifying rare or unusual data points (e.g., fraud detection).

Example: Unsupervised Learning in Python

We’ll use the K-means clustering algorithm for an unsupervised learning task and apply it
to the Iris dataset.

Clustering Example with K-means (Iris Dataset)


# Import necessary libraries
from [Link] import KMeans
import [Link] as plt
# Load the Iris dataset
iris = load_iris()
X = [Link] # Features (sepal length, sepal width, petal length, petal width)
# Apply K-means clustering (we will choose 3 clusters because we know there are 3 species)
kmeans = KMeans(n_clusters=3, random_state=42)
[Link](X)
# Get the predicted cluster labels
y_kmeans = [Link](X)
# Plot the results (we will visualize using the first two features for simplicity)
[Link](X[:, 0], X[:, 1], c=y_kmeans, cmap='viridis')
[Link]('Sepal Length')
[Link]('Sepal Width')
[Link]('K-means Clustering on Iris Dataset')
[Link]()

In data science, classification refers to the task of predicting a categorical label based on input
features. It is a supervised learning problem where the goal is to map input data to predefined
categories (or classes). Classification problems can be divided into two categories based on the
model's decision boundary: linear and non-linear classification.

Linear Classification
In linear classification, the decision boundary that separates different classes is a straight line
(or a hyperplane in higher dimensions). These models assume that the data is linearly
separable, meaning there exists a straight line or hyperplane that can perfectly separate the
classes.

Examples of Linear Classification Models:

 Logistic Regression
 Linear Support Vector Machine (SVM)

How Linear Classification Works:

 A linear classifier makes decisions based on a linear combination of input features.


 The decision boundary is represented by a linear equation, e.g., for a two-dimensional
problem:
y=w1x1+w2x2+by = w_1 x_1 + w_2 x_2 + by=w1 x1 +w2 x2 +b where
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
w1w_1w1 , w2w_2w2 , and bbb are the weights and bias, and x1x_1x1 ,
x2x_2x2 are the input features.

If the output yyy is greater than a threshold, it assigns one class, otherwise, it assigns another
class.

Example of Linear Classification in Python (Logistic Regression)

# Import necessary libraries


import pandas as pd
from [Link] import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score
import [Link] as plt

# Generate a synthetic dataset for classification


X, y = make_classification(n_samples=100, n_features=2, n_classes=2, random_state=42)

# Split the 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)

# Train a Logistic Regression model (Linear classifier)


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

# Make predictions
y_pred = [Link](X_test)

# Evaluate the model


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

# Plot the decision boundary


h = .02 # Step size in the mesh
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, h),
[Link](y_min, y_max, h))
Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.75)


[Link](X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o', s=100)
[Link]('Feature 1')
[Link]('Feature 2')
[Link]('Logistic Regression Decision Boundary')
[Link]()

In this example:
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
 We used Logistic Regression, a linear classifier, to classify data into two classes.
 We visualized the decision boundary, which is a straight line (or hyperplane in higher
dimensions).

When to use Linear Classification:

 When the data is linearly separable or close to linearly separable.


 Models like Logistic Regression or Linear SVM work well when classes can be
separated by a straight line or hyperplane.

Non-linear Classification
In non-linear classification, the decision boundary is not a straight line but can take any
arbitrary shape. This is needed when the classes are not linearly separable.

Examples of Non-linear Classification Models:

 Support Vector Machine (SVM) with non-linear kernels (e.g., RBF kernel)
 Decision Trees
 Random Forests
 K-Nearest Neighbors (KNN)
 Neural Networks

How Non-linear Classification Works:

 Non-linear classifiers can map the data to a higher-dimensional space where linear
separability is possible (e.g., kernel trick in SVM).
 Alternatively, non-linear models like decision trees or neural networks can fit complex
decision boundaries directly in the original space.

Example of Non-linear Classification in Python (SVM with RBF Kernel)

# Import necessary libraries


from [Link] import SVC
from [Link] import accuracy_score
from [Link] import make_classification
from sklearn.model_selection import train_test_split
import numpy as np
import [Link] as plt

# Generate a synthetic dataset for classification


X, y = make_classification(n_samples=100, n_features=2, n_classes=2, n_informative=2,
n_redundant=0, random_state=42)

# Split the 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)

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
# Train a Support Vector Machine with RBF kernel (Non-linear classifier)
model = SVC(kernel='rbf', gamma='auto')
[Link](X_train, y_train)

# Make predictions
y_pred = [Link](X_test)

# Evaluate the model


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

# Plot the decision boundary


h = .02 # Step size in the mesh
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, h),
[Link](y_min, y_max, h))
Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.75)


[Link](X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o', s=100)
[Link]('Feature 1')
[Link]('Feature 2')
[Link]('SVM with RBF Kernel Decision Boundary')
[Link]()

Bayesian Classifier (Naive Bayes)


Bayesian classifiers are based on Bayes' Theorem, which uses prior probabilities and
conditional probabilities to make predictions about a class label (categorical data).

 Type: Probabilistic model


 Working Principle:
o It calculates the posterior probability for each class given the input features and
assigns the class with the highest probability.
o Naïve Bayes is the most popular Bayesian classifier, which assumes
independence among features.
 Use Cases: Text classification (e.g., spam filtering), medical diagnostics.
 Advantages: Simple, fast, works well with small datasets, handles missing data well

Example with Gaussian Naive Bayes (for continuous data):

from sklearn.naive_bayes import GaussianNB


from sklearn.model_selection import train_test_split

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset

data = load_iris()

X = [Link]

y = [Link]

# Split the data

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

# Create a Gaussian Naive Bayes model

model = GaussianNB()

[Link](X_train, y_train)

# Make predictions

y_pred = [Link](X_test)

# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Perceptron
The Perceptron is a linear classifier, which means it works well for linearly separable data.

The Perceptron is one of the simplest neural network models and is used for binary
classification. It learns a linear decision boundary between two classes.

 Type: Linear classifier


 Working Principle: The perceptron updates its weights based on the error between its
predictions and the actual labels. It uses a linear function and a threshold to make
predictions.
 Use Cases: Simple classification problems where the data is linearly separable.
 Advantages: Simple, easy to understand.

Example with Perceptron:

from sklearn.linear_model import Perceptron


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
data = load_iris()
X = [Link]
y = [Link]
# Binary classification for simplicity (Iris setosa vs. others)
y = (y == 0).astype(int) # Setosa is class 0, others are class 1
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Perceptron model
model = Perceptron(max_iter=1000, tol=1e-3)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Nearest Neighbor Classifier (k-NN)


The k-Nearest Neighbors algorithm classifies a data point based on the majority class of its
neighbors.

The K-Nearest Neighbors (KNN) algorithm is a simple, non-parametric classifier that


classifies a point based on the majority class of its nearest neighbors.

 Type: Instance-based learning


 Working Principle: For a given test instance, the algorithm computes the distance to
all training data points and assigns the label of the majority of the k nearest points.
 Use Cases: Image recognition, pattern recognition, recommendation systems.
 Advantages: Easy to implement, no training phase.

Example with k-NN:

from [Link] import KNeighborsClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
data = load_iris()
X = [Link]
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a k-NN model (k=3)
model = KNeighborsClassifier(n_neighbors=3)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Support Vector Machine (SVM) and Use of Kernels


SVM is a powerful classifier that works well for high-dimensional spaces. It tries to find the
hyperplane that best separates the classes.

Support Vector Machines (SVMs) are powerful supervised learning models that can be used
for both classification and regression tasks. SVMs aim to find the optimal hyperplane that
separates the classes.

 Type: Linear/non-linear classifier


 Working Principle: SVM finds the hyperplane that maximizes the margin between
the closest data points of each class (support vectors).
 Use Cases: Text classification, image classification, bioinformatics.
 Advantages: Effective in high-dimensional spaces, works well with clear margins of
separation

Example with Linear Kernel SVM:

from [Link] import SVC


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
# Create a Linear SVM model
model = SVC(kernel='linear')
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Logistic Regression
Logistic Regression is a classification algorithm that predicts the probability of a class. It's
often used for binary classification.

Logistic Regression is a linear model used for binary classification that estimates the
probability of a class using the logistic (sigmoid) function.

 Type: Linear classifier


 Working Principle: The model outputs a probability between 0 and 1, and uses a
threshold (usually 0.5) to classify an instance.
 Use Cases: Binary classification problems like spam detection, disease prediction.
 Advantages: Simple, interpretable, works well for linearly separable data.

Example with Logistic Regression:

from sklearn.linear_model import LogisticRegression


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# For simplicity, we'll classify just two classes
y = (y == 0).astype(int) # Iris setosa vs others
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Logistic Regression model
model = LogisticRegression()
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Naïve Bayes
Naïve Bayes classifiers are based on applying Bayes' theorem with strong (naive)
independence assumptions.

The Naïve Bayes classifier is a probabilistic classifier based on Bayes’ Theorem, which
assumes that the features are conditionally independent given the class.

 Type: Probabilistic model


 Working Principle: It calculates the likelihood of each class based on feature
probabilities and assigns the class with the highest probability.
 Use Cases: Text classification, sentiment analysis, spam detection.
 Advantages: Fast, works well with high-dimensional data, handles missing data.

Example with Multinomial Naive Bayes (for categorical data):

from sklearn.naive_bayes import MultinomialNB


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Multinomial Naive Bayes model
model = MultinomialNB()
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
Decision Trees
Decision trees are a non-linear classifier that splits data based on feature values.

Decision Trees are tree-like structures where each node represents a decision based on the
value of a feature, and the leaves represent class labels.

 Type: Non-linear classifier


 Working Principle: The tree splits the data at each node based on the feature that
provides the best split (usually measured by Gini impurity or entropy).
 Use Cases: Customer segmentation, loan approval, medical diagnosis.
 Advantages: Easy to interpret, no feature scaling required.

Example with Decision Tree Classifier:

from [Link] import DecisionTreeClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Decision Tree model
model = DecisionTreeClassifier(random_state=42)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Random Forests
Random Forests are ensembles of decision trees, which reduce the risk of overfitting.

Random Forests are an ensemble of decision trees. Each tree is trained on a random subset of
the data, and their predictions are aggregated (typically by majority voting for classification
tasks).
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
 Type: Ensemble method (Bagging)
 Working Principle: Random Forest builds multiple decision trees on bootstrapped
samples of the data and averages their predictions to reduce variance.
 Use Cases: Classification and regression tasks in various domains.
 Advantages: Robust to overfitting, handles both numerical and categorical features.

Example with Random Forest Classifier:

from [Link] import RandomForestClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score
# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Random Forest model
model = RandomForestClassifier(n_estimators=100, random_state=42)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Boosting (AdaBoost)
Boosting is an ensemble method that combines weak learners into a strong learner by
iteratively correcting errors made by previous learners.

Boosting is an ensemble technique where models are trained sequentially. Each new model
corrects the errors made by the previous one.

 Types: AdaBoost, Gradient Boosting, XGBoost


 Working Principle: Boosting adjusts the weights of incorrectly classified instances so
that the next model focuses more on them.
 Use Cases: Complex classification tasks, where a strong model is required.
 Advantages: High accuracy, good for imbalanced datasets.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Example with AdaBoost:

from [Link] import AdaBoostClassifier


from [Link] import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score
# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create an AdaBoost model using a Decision Tree as the base learner
model = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=1),
n_estimators=50)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Bagging (Bootstrap Aggregating)


Bagging is another ensemble method where multiple models are trained on random subsets of
the data, and their predictions are averaged (for regression) or voted on (for classification).

Bagging (Bootstrap Aggregating) is an ensemble technique that trains multiple models


(typically decision trees) on different random subsets of the training data and aggregates their
predictions (by averaging or voting).

 Types: Random Forest is an example of bagging.


 Working Principle: Models are trained in parallel, and their predictions are combined.
 Use Cases: Tasks requiring high accuracy and reduced variance.
 Advantages: Reduces overfitting, handles noisy data well.

Example with Bagging:

from [Link] import BaggingClassifier


from [Link] import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
from [Link] import load_iris
from [Link] import accuracy_score
# Load dataset
data = load_iris()
X = [Link]
y = [Link]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a Bagging model
model = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=100)
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))

Clustering: Partitioned and Hierarchical


Clustering is a unsupervised learning technique used to group data into clusters based on
similarities. The goal is to group similar items together while keeping the dissimilar items in
separate groups. There are two main types of clustering: Partitioned Clustering and
Hierarchical Clustering.

1. Partitioned Clustering (K-Means Clustering)

K-Means is one of the most popular partitioned clustering algorithms. It divides the dataset
into k clusters, where each data point belongs to the cluster with the nearest mean.

Working Principle:

1. Initialization: Select k random points as the initial centroids (means).


2. Assignment: Assign each data point to the nearest centroid.
3. Update: Recompute the centroids as the mean of the data points assigned to
each cluster.
4. Repeat: Repeat the assignment and update steps until convergence (when
centroids do not change)

Use Cases: Customer segmentation, image compression, anomaly detection.


Advantages: Simple, fast, scalable.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Disadvantages: The number of clusters k needs to be specified, sensitive to
initialization.

Python Example (K-Means Clustering):

from [Link] import KMeans


import numpy as npimport [Link] as plt
# Sample data (X) with 2 features
X = [Link](100, 2)
# Apply K-Means clustering
kmeans = KMeans(n_clusters=3)
[Link](X)
# Cluster centersprint("Cluster centers:", kmeans.cluster_centers_)
# Predict the cluster for each data point
predictions = [Link](X)
# Visualize the clusters
[Link](X[:, 0], X[:, 1], c=predictions, cmap='viridis')
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red',
marker='X')
[Link]()

2. Hierarchical Clustering

Hierarchical Clustering builds a tree-like structure (dendrogram) to represent the nested


grouping of data. It is divided into two main types:

 Agglomerative (Bottom-Up): Starts with each data point as its own cluster and then
merges the closest clusters iteratively.
 Divisive (Top-Down): Starts with all data points in one cluster and then recursively
splits them.

Working Principle:

Agglomerative:

1. Start by treating each data point as a separate cluster.


2. Find the pair of clusters with the smallest distance between them.
3. Merge the closest clusters and repeat until only one cluster remains or until the
desired number of clusters is reached.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Distance Metrics: Euclidean distance, Manhattan distance, or other distance metrics
can be used to measure the closeness of clusters.

Use Cases: Hierarchical clustering is often used when the number of clusters is not
known in advance, such as for taxonomies, hierarchical data, or dendrogram
visualizations.

Advantages: Does not require specifying the number of clusters, intuitive.

Disadvantages: Computationally expensive, especially with large datasets.

Python Example (Hierarchical Clustering):

from [Link] import AgglomerativeClustering


import numpy as np
import [Link] as plt
# Sample data (X) with 2 features
X = [Link](100, 2)
# Apply Agglomerative Clustering
agg_clustering = AgglomerativeClustering(n_clusters=3)
predictions = agg_clustering.fit_predict(X)
# Visualize the clusters
[Link](X[:, 0], X[:, 1], c=predictions, cmap='viridis')
[Link]()

In this example, AgglomerativeClustering is used to split the data into 3 clusters.

Regression
Regression is a supervised learning technique used for predicting a continuous target variable
based on input features. The goal is to model the relationship between the dependent variable
(y) and one or more independent variables (X).

1. Linear Regression (Least Squares Method)

Linear Regression is one of the simplest and most widely used regression models. It assumes
a linear relationship between the dependent variable and the independent variables. The model
tries to minimize the sum of squared residuals (the difference between actual and predicted
values).

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Working Principle:

The goal is to minimize the least squares loss, which is the sum of squared
differences between actual and predicted values.

Use Cases: Predicting house prices, salary prediction, and any task where a continuous
outcome is predicted.

Advantages: Simple, interpretable.

Disadvantages: Assumes linearity, sensitive to outliers.

Python Example (Linear Regression using Least Squares):

from sklearn.linear_model import LinearRegression


import numpy as npimport [Link] as plt
# Sample data (X) and target variable (y)
X = [Link](100, 1) * 10 # 100 random values in the range [0, 10]
y = 2 * X + 5 + [Link](100, 1) # Linear relationship with noise
# Initialize and fit the model
model = LinearRegression()
[Link](X, y)
# Predict using the model
predictions = [Link](X)
# Visualize the regression line
[Link](X, y, color='blue', label='Actual data')
[Link](X, predictions, color='red', label='Regression line')
[Link]('X')
[Link]('y')
[Link]()
[Link]()
# Model coefficients
print("Intercept:", model.intercept_)
print("Slope (coefficient):", model.coef_)

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Training and testing a classifier
Training and testing a classifier is a core part of machine learning and data science. Below is
an example of how to train and test a classifier in Python using the scikit-learn library. This
example demonstrates the typical workflow with a simple dataset.

Steps:

1. Import necessary libraries.


2. Load the dataset.
3. Preprocess the data (if necessary).
4. Split the dataset into training and testing sets.
5. Choose a classifier.
6. Train the classifier on the training data.
7. Test the classifier on the testing data.
8. Evaluate the model's performance.

We'll use a simple dataset, such as the Iris dataset, for this example. It contains data about
different species of flowers, and the task is to classify the flowers based on their features
(sepal length, sepal width, petal length, petal width).

Example code:

# Import necessary libraries


from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, classification_report
# Load the Iris dataset
iris = load_iris()
X = [Link] # Features
y = [Link] # Target labels (species)
# 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)
# Optionally, scale the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Initialize the classifier (Random Forest in this case)
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
clf = RandomForestClassifier(random_state=42)
# Train the classifier on the training data
[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:.4f}")
# Print a detailed classification reportprint("\nClassification Report:")
print(classification_report(y_test, y_pred))

Explanation:

Loading the dataset: We use load_iris() from [Link] to load the Iris dataset.

Splitting the dataset: We split the data into training and testing sets using
train_test_split() from sklearn.model_selection. In this case, we use 70% for training
and 30% for testing.

Scaling the data: We use StandardScaler to scale the features (optional but often
important for models like SVM or k-NN). This step normalizes the data so that all
features have zero mean and unit variance.

Choosing the classifier: We use RandomForestClassifier from [Link]. This


is an ensemble learning method that typically performs well without much parameter
tuning.

Training the classifier: The classifier is trained using the fit() method on the training
data.

Testing and evaluation:

1. We use the predict() method to get predictions on the test data.


2. The performance is evaluated using accuracy_score() to calculate the overall
accuracy and classification_report() to get a more detailed analysis, including
precision, recall, and F1-score.

Evaluation metrics
In regression analysis, evaluating the performance of a model is crucial to understanding how
well it predicts the target variable. Three commonly used metrics are:

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Root Mean Squared Error (RMSE): Measures the square root of the average squared
differences between predicted and actual values. It gives an idea of how far the
predictions are from the actual values, with larger errors penalized more heavily.

Mean Absolute Error (MAE): Measures the average of the absolute differences
between predicted and actual values. It gives a simple idea of the average error in the
model's predictions.

Coefficient of Determination (R-squared): Represents the proportion of the variance


in the dependent variable that is predictable from the independent variables. It ranges
from 0 to 1, with higher values indicating better model fit.

Example with Python (Using scikit-learn and the Boston Housing Dataset):

We'll use the Boston housing dataset, which is available in scikit-learn, for this example. The
goal is to predict the house prices based on various features (e.g., average number of rooms,
crime rate, etc.).

Code Example:

# Import necessary libraries


import numpy as np
import pandas as pd
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, mean_absolute_error, r2_score

# Load the Boston Housing dataset

boston = load_boston()

X = [Link] # Features

y = [Link] # Target variable (house prices)

# 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 and train a linear regression model

model = LinearRegression()

[Link](X_train, y_train)

# Predict house prices on the test set

y_pred = [Link](X_test)

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
# Calculate RMSE (Root Mean Squared Error)

rmse = [Link](mean_squared_error(y_test, y_pred))


print(f"RMSE: {rmse:.4f}")

# Calculate MAE (Mean Absolute Error)

mae = mean_absolute_error(y_test, y_pred)


print(f"MAE: {mae:.4f}")

# Calculate R-squared (Coefficient of Determination)

r2 = r2_score(y_test, y_pred)
print(f"R-squared: {r2:.4f}")

Explanation:

Loading the dataset: The Boston housing dataset is loaded using load_boston() from
[Link]. This dataset contains 506 samples with 13 features related to the
housing market in Boston.

Splitting the dataset: We split the dataset into training (70%) and testing (30%) sets
using train_test_split().

Training the model: We use LinearRegression from scikit-learn to fit the model on
the training data (X_train and y_train).

Making predictions: After the model is trained, we use it to make predictions (y_pred)
on the test set (X_test).

Evaluating performance:

1. RMSE is calculated using mean_squared_error() and [Link]() to get the square


root of the mean squared error.
2. MAE is calculated using mean_absolute_error(), which gives the average of
the absolute errors.
3. R-squared is calculated using r2_score(), which tells us how well the model
explains the variance in the target variable.

Cross-Validation
Cross-validation involves splitting your dataset into several subsets (folds) and training and
testing the model multiple times, each time with a different fold used as the test set and the
remaining folds as the training set. This helps provide a more robust measure of model
performance.

K-Fold Cross-Validation
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
 The dataset is split into K equal-sized folds (e.g., 5 or 10).
 For each fold, the model is trained on the other K-1 folds and tested on the current fold.
 This process is repeated K times (once for each fold), and the model’s performance is
averaged over the K iterations.

Use Cross-Validation

1. Avoid Overfitting: By training and testing on different subsets, the model is less likely
to memorize the data and is more likely to generalize well.
2. Better Estimate of Model Performance: Cross-validation gives a more reliable
estimate of how the model will perform on unseen data.
3. Efficient Use of Data: It allows every data point to be used for both training and
testing, which can be important if you have a small dataset.

Example of Cross-Validation in Python Using scikit-learn

We’ll use the Iris dataset for this example and perform K-Fold Cross-Validation using
Logistic Regression as the model.

Step-by-Step Example:

# Import necessary libraries


from [Link] import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features

y = [Link] # Target labels

# Initialize the Logistic Regression model

model = LogisticRegression(max_iter=200)

# Initialize K-Fold Cross-Validation (StratifiedKFold for classification tasks)

cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)

# Perform cross-validation and calculate accuracy scores

scores = cross_val_score(model, X, y, cv=cv, scoring='accuracy')

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
# Print the cross-validation scores (accuracy for each fold)
print(f"Cross-validation scores: {scores}")

# Calculate the mean and standard deviation of the cross-validation scoresprint(f"Mean


accuracy: {[Link]():.4f}")
print(f"Standard deviation of accuracy: {[Link]():.4f}")

Class imbalance
Class imbalance is a common issue in many machine learning problems, where one class is
underrepresented compared to the other(s). This can lead to models that perform poorly,
especially for the minority class, because the model tends to favor the majority class.
Fortunately, there are several ways to handle class imbalance effectively.

Here are some common strategies to address class imbalance in data science, along with
examples using Python:

a. Oversampling the Minority Class (SMOTE)

SMOTE (Synthetic Minority Over-sampling Technique) is one of the most popular methods
for oversampling the minority class. It generates synthetic samples for the minority class by
interpolating between existing samples.

from imblearn.over_sampling import SMOTE


from [Link] import make_classification
from collections import Counter

# Create an imbalanced dataset

X, y = make_classification(n_samples=1000, n_features=20, n_informative=2,

n_redundant=10, n_classes=2, weights=[0.9, 0.1], random_state=42)

print(f"Original class distribution: {Counter(y)}")

# Apply SMOTE to oversample the minority class

smote = SMOTE(random_state=42)

X_res, y_res = smote.fit_resample(X, y)

print(f"Resampled class distribution: {Counter(y_res)}")

 Explanation:
o We first create an imbalanced dataset using make_classification() where
90% of the data belongs to one class and 10% to the other.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
o SMOTE() is applied to oversample the minority class by generating synthetic
data points.
o The result is a balanced dataset with equal representation of both classes.

b. Undersampling the Majority Class

Undersampling involves reducing the number of instances in the majority class to balance the
dataset. However, this method can lead to the loss of valuable information.

from imblearn.under_sampling import RandomUnderSampler


from [Link] import make_classification
from collections import Counter

# Create an imbalanced dataset

X, y = make_classification(n_samples=1000, n_features=20, n_informative=2,

n_redundant=10, n_classes=2, weights=[0.9, 0.1], random_state=42)

print(f"Original class distribution: {Counter(y)}")

# Apply undersampling to balance the dataset

undersample = RandomUnderSampler(random_state=42)

X_res, y_res = undersample.fit_resample(X, y)

print(f"Resampled class distribution: {Counter(y_res)}")

 Explanation:
o We use RandomUnderSampler() to randomly remove samples from the
majority class, leading to a balanced class distribution.

Precision, Recall, ROC, and AUC in Data Science with Python


In machine learning, especially in the context of binary classification or imbalanced datasets,
accuracy may not always be the best evaluation metric. Precision, Recall, ROC (Receiver
Operating Characteristic), and AUC (Area Under the Curve) are more informative metrics for
evaluating model performance. Let's go through each one in detail with Python examples.

1. Precision

Precision is the ratio of correctly predicted positive observations to the total predicted
positives. It tells you how many of the predicted positive results were actually positive.

Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}Precision=TP+FPTP

Where:

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
 TP: True Positive
 FP: False Positive

Example in Python:

from [Link] import precision_score


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

# Create a sample dataset

X, y = make_classification(n_samples=1000, n_features=20, n_informative=2,

n_redundant=10, n_classes=2, weights=[0.8, 0.2], random_state=42)

# Split the data

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=1000)

[Link](X_train, y_train)

# Make predictions

y_pred = [Link](X_test)

# Calculate precision

precision = precision_score(y_test, y_pred)


print(f"Precision: {precision}")

Output:

Precision: 0.859649122807

 Interpretation: The precision value of 0.86 means that 86% of the predicted positive
class labels are correct.

2. Recall

Recall (also known as Sensitivity or True Positive Rate) is the ratio of correctly predicted
positive observations to all observations in the actual positive class. It tells you how many of
the actual positive cases were correctly predicted.

Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN}Recall=TP+FNTP

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
Where:

 TP: True Positive


 FN: False Negative

Example in Python:

from [Link] import recall_score

# Calculate recall

recall = recall_score(y_test, y_pred)


print(f"Recall: {recall}")

Output:

Recall: 0.68

 Interpretation: The recall value of 0.68 means that 68% of the actual positive cases
were correctly predicted.

3. ROC Curve

The ROC Curve (Receiver Operating Characteristic curve) is a graphical representation of a


classifier's performance across all classification thresholds. It plots the True Positive Rate
(Recall) against the False Positive Rate (FPR), which is:

FPR=FPFP+TN\text{FPR} = \frac{FP}{FP + TN}FPR=FP+TNFP

The ROC curve helps visualize the trade-off between the True Positive Rate and the False
Positive Rate.

Example in Python:

import [Link] as pltfrom [Link] import roc_curve

# Get the predicted probabilities for the positive class

y_prob = model.predict_proba(X_test)[:, 1]

# Calculate FPR, TPR, and thresholds

fpr, tpr, thresholds = roc_curve(y_test, y_prob)

# Plot the ROC curve

[Link](figsize=(8, 6))

[Link](fpr, tpr, color='blue', label='ROC Curve')

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
[Link]([0, 1], [0, 1], linestyle='--', color='gray', label='Random Classifier')

[Link]('Receiver Operating Characteristic (ROC) Curve')

[Link]('False Positive Rate (FPR)')

[Link]('True Positive Rate (TPR)')

[Link]()

[Link]()

 Interpretation: The ROC curve shows the trade-off between recall and false positive
rate. The closer the curve is to the top-left corner, the better the classifier is.

4. AUC (Area Under the Curve)

The AUC (Area Under the Curve) is the area under the ROC curve and is a single value that
summarizes the performance of a classifier. The AUC score tells you how well the model can
distinguish between classes. It ranges from 0 to 1:

 AUC = 1: Perfect classifier


 AUC = 0.5: Model is no better than random chance (like flipping a coin)
 AUC < 0.5: Model performs worse than random guessing

Example in Python:

from [Link] import roc_auc_score

# Calculate AUC

auc = roc_auc_score(y_test, y_prob)


print(f"AUC: {auc}")

Confusion Matrix and Classification Accuracy

In machine learning, the Confusion Matrix and Classification Accuracy are essential tools
for evaluating classification models. The Confusion Matrix provides a detailed breakdown of
model performance, while Classification Accuracy gives a general sense of how well the
model is performing overall.

Let's break these concepts down and explore them using Python with examples.

1. Confusion Matrix

A Confusion Matrix is a table used to describe the performance of a classification model. It


compares the predicted values with the actual values. The matrix consists of the following four
components:

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA
 True Positive (TP): The number of positive samples correctly classified as positive.
 True Negative (TN): The number of negative samples correctly classified as negative.
 False Positive (FP): The number of negative samples incorrectly classified as positive.
 False Negative (FN): The number of positive samples incorrectly classified as
negative.

Here’s the general structure of a Confusion Matrix:

Actual/Predicted Positive (Predicted) Negative (Predicted)

Positive TP FN

Negative FP TN

Example:

from [Link] import confusion_matrix, classification_report


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import make_classification
import seaborn as sns
import [Link] as plt

# Create a sample imbalanced dataset

X, y = make_classification(n_samples=1000, n_features=20, n_informative=2,

n_redundant=10, n_classes=2, weights=[0.8, 0.2], random_state=42)

# Split the data

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=1000)

[Link](X_train, y_train)

# Make predictions

y_pred = [Link](X_test)

# Compute the confusion matrix

cm = confusion_matrix(y_test, y_pred)

# Plot the confusion matrix as a heatmap


Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. of MCA
[Link](cm, annot=True, fmt='d', cmap='Blues', xticklabels=["Negative", "Positive"],
yticklabels=["Negative", "Positive"])

[Link]('Confusion Matrix')

[Link]('Predicted')

[Link]('Actual')

[Link]()

# Print the confusion matrixprint("Confusion Matrix:")print(cm)

# Detailed classification report


print("\nClassification Report:")
print(classification_report(y_test, y_pred))

Explanation:

 The confusion_matrix() function computes the confusion matrix.


 The classification_report() function gives precision, recall, F1-score, and support for
each class.

2. Classification Accuracy

Classification Accuracy is the ratio of the number of correct predictions to the total number
of predictions. It is a basic metric that gives you a quick view of overall model performance.

Example in Python:

from [Link] import accuracy_score

# Compute classification accuracy

accuracy = accuracy_score(y_test, y_pred)


print(f"Classification Accuracy: {accuracy:.4f}")

Output:

Classification Accuracy: 0.8700

 Interpretation: The accuracy of 0.87 means that the model correctly predicted 87%
of the test instances.

Prepared by: B. Sai Hemanth,


Assistant Professor, Dept. of MCA

You might also like