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

DataScience Unit 5

The document provides an overview of classification techniques in machine learning, detailing types such as binary, multi-class, and multi-label classification, along with popular algorithms like Logistic Regression and KNN. It also covers performance measures for classification models, including accuracy, precision, and ROC curves, as well as applications in various fields. Additionally, it discusses data reading from MySQL and MongoDB in R, highlighting advantages, disadvantages, and example code for implementation.

Uploaded by

nandinipechetti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

DataScience Unit 5

The document provides an overview of classification techniques in machine learning, detailing types such as binary, multi-class, and multi-label classification, along with popular algorithms like Logistic Regression and KNN. It also covers performance measures for classification models, including accuracy, precision, and ROC curves, as well as applications in various fields. Additionally, it discusses data reading from MySQL and MongoDB in R, highlighting advantages, disadvantages, and example code for implementation.

Uploaded by

nandinipechetti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT –V

Classification

1. Classification
Classification is a supervised learning technique used to predict qualitative (categorical)
outcomes. It classifies data into predefined categories such as spam/not spam, disease/no
disease, or pass/fail. The goal is to learn a decision boundary that separates classes.

Types of Classification:
- Binary Classification: Two classes (0/1, Yes/No).
- Multi-Class Classification: More than two classes.
- Multi-Label Classification: Instances may belong to multiple classes.

Classification Process:
1. Collect and label dataset.
2. Split data into training and testing sets.
3. Train the classification algorithm.
4. Predict classes for unseen data.
5. Evaluate model performance.

Popular Classification Algorithms:


Logistic Regression, KNN, Naive Bayes, Decision Trees, Random Forest, SVM, Neural
Networks.

Applications:
Medical diagnosis, spam filtering, sentiment analysis, document classification.

2. Performance Measures
Performance metrics evaluate the effectiveness of classification models.

Confusion Matrix:
TP – True Positive
TN – True Negative
FP – False Positive
FN – False Negative

Accuracy = (TP + TN) / (TP + TN + FP + FN)


Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
F1 Score = 2 * (Precision * Recall) / (Precision + Recall)
Specificity = TN / (TN + FP)

ROC Curve: Graph of TPR vs FPR at different thresholds.


AUC: Measures area under ROC. Higher AUC indicates better model.

Need for Multiple Metrics:


Accuracy alone is not reliable for imbalanced datasets.

3. Logistic Regression
Logistic Regression is used for binary classification. It predicts probability using the
sigmoid function:

h(x) = 1 / (1 + e^-(b0 + b1x))

Decision rule:
If h(x) > 0.5 → Class 1
If h(x) < 0.5 → Class 0

Log-Odds:
log(p / (1 – p)) = b0 + b1x

Assumptions:
- Binary dependent variable
- No multicollinearity
- Linearity in log-odds
- Independent observations

Applications:
Medical diagnosis, credit scoring, fraud detection, marketing analysis.

R Implementation:
model <- glm(Species ~ [Link] + [Link], data=iris_binary, family=binomial)
Explanation:
 We are creating a logistic regression model and saving it in the variable model.
 glm() is the function used to build the model.
 Species ~ [Link] + [Link] means:
o Species is what we want to predict.

o We are using Sepal Length and Sepal Width to make the prediction.

 data = iris_binary means the model uses the dataset called iris_binary.
 family = binomial tells R to perform logistic regression (because the output has
two classes: 0 or 1).
4. K-Nearest Neighbours (KNN)
KNN is a non-parametric, instance-based algorithm. Classification is based on majority
voting of K nearest neighbors.

Process:
1. Choose value of K.
2. Compute Euclidean distance.
3. Select K nearest points.
4. Assign class by majority voting.

Advantages:
Simple, no training phase, effective for small datasets.

Disadvantages:
Slow for large data, sensitive to noise, requires feature scaling.

R Example:
pred <- knn(train[,1:4], test[,1:4], train$Species, k=3)
Explanation:
 We are using the KNN algorithm to predict the species of flowers in the test
data.
 train[,1:4] → the input features from training data
 test[,1:4] → the input features from test data
 train$Species → the correct species of the training flowers
 k=3 → the algorithm looks at the 3 nearest neighbors to decide the class
 The predicted species are stored in pred.

5. Clustering – K-Means Algorithm


K-Means is an unsupervised learning algorithm grouping data into K clusters.

Steps:
1. Choose K.
2. Initialize centroids.
3. Assign points to nearest centroid.
4. Recalculate centroids.
5. Repeat until stability.

Objective:
Minimize within-cluster sum of squares.

Applications:
Market segmentation, image compression, pattern recognition.

R Example:
km <- kmeans(iris[,1:4], centers=3)
Meaning of Each Part
1. iris[,1:4]
 Uses the first 4 columns:
o [Link]

o [Link]

o [Link]

o [Link]

 These are the features used for clustering.


2. centers = 3
 We are asking k-means to create 3 clusters.
 Because the iris dataset has 3 types of flowers.
3. km <-
 Stores the clustering result (cluster numbers, centers, etc.) in km.
6. Time Series Analysis
Time series is a sequence of observations recorded over time.
Components:
- Trend: Long-term direction.
- Seasonality: Regular repeating patterns.
- Cyclic variations.
- Random noise.
Models:
AR, MA, ARMA, ARIMA, SARIMA.
Applications:
Weather forecasting, stock market prediction, sales forecasting.
R Example:
model <- [Link](AirPassengers)
Meaning of Each Part
1. [Link]()
 Automatically checks many ARIMA models.
 Selects the best one based on accuracy.
 Saves you from manually testing p, d, q values.
2. AirPassengers
 A built-in time series dataset in R.
 Contains monthly airline passenger counts from 1949–1960.
3. model <-
 Saves the final selected ARIMA model into the variable model.
7. Social Network Analysis
Social Network Analysis (SNA) is a method used to study the relationships,
connections, and interaction patterns among individuals, groups, or organizations.
It represents these relationships as nodes (people or objects) and edges (connections or
interactions).
SNA helps understand how information flows, who is influential, how communities are
formed, and how groups behave.

Example
Consider a WhatsApp group:
 Each member is a node
 Each message or interaction between members is an edge
 A person who talks to most people has high degree centrality
 A person who connects two sub-groups has high betweenness centrality
This small social network can be analyzed to find influencers and communication
patterns.

Advantages of Social Network Analysis


1. Identifies key influencers and important people in a network.
2. Helps understand information flow and communication patterns.
3. Detects communities, clusters, and subgroups.
4. Useful for predicting behavior based on connections.
5. Helps organizations improve team communication and structure.
6. Useful for analyzing large data from social media platforms.
7. Reveals hidden patterns not visible in traditional analysis.

Disadvantages of Social Network Analysis


1. Requires large, accurate, and complete data to give meaningful results.
2. Analysis becomes complex for very large networks.
3. Privacy concerns when collecting personal relationship data.
4. Networks are dynamic (change over time), so results may become outdated.
5. Requires specialized tools and skills to interpret graphs.
6. Missing data or noise can affect accuracy.

Applications of Social Network Analysis


a) Social Media
b) Business & Management
c) Health & Medicine
d) Crime & Security
e) Education
f) Marketing

6. Tools Used for Social Network Analysis


In R
 igraph
 statnet
 sna
In Python
 NetworkX
 Graph-tool
 PyVis
Standalone Visualization Tools
 Gephi
 Cytoscape
 NodeXL
Graph Databases
 Neo4j
 OrientDB
 ArangoDB
8. Reading Data from MySQL in R
Definition
Reading data from MySQL in R means connecting R to a MySQL database and
importing tables into R for data analysis.
This is done using a database connection package such as RMySQL or DBI.
Explanation of Code
library(RMySQL)

conn <- dbConnect(MySQL(),


user='root',
password='1234',
dbname='company')

data <- dbGetQuery(conn, 'SELECT * FROM employees')


Step-by-step (Simple Explanation)
1. library(RMySQL)
– Loads the RMySQL package so R can talk to MySQL.
2. dbConnect()
– Creates a connection between R and the MySQL server.
– You give username, password, and database name.
3. dbGetQuery()
– Sends an SQL query to MySQL.
– Here, "SELECT * FROM employees" means:
Get all the rows and columns from the employees table.
4. data
– Stores the imported table as a data frame in R.

Example:
Suppose we have a MySQL database company with a table employees:

id name salary

1 Mani 50000

2 Nandini 60000

After running:
data <- dbGetQuery(conn, 'SELECT * FROM employees')
R will contain:
id name salary
1 1 Mani 50000
2 2 Nandini 60000
Advantages:
1. Fast data transfer from MySQL to R.
2. Can run SQL queries directly in R.
3. Good for large datasets stored in databases.
4. Secure connection using username & password.
5. Useful for real-time data analysis.
Disadvantages:
1. Requires MySQL installed and running.
2. Passwords in code may be unsafe if not handled carefully.
3. RMySQL package may need additional configuration on some systems.
4. Large queries may take time or cause memory usage in R.

Applications:
1. Business data analysis (sales, employees, inventory).
2. Machine learning models using stored data.
3. Automated report creation from database tables.
4. Real-time dashboards built with R Shiny.
5. Data cleaning and statistical analysis for research.

Tools Used
 RMySQL (R package)
 DBI (Database Interface package)
 MySQL Server
 MySQL Workbench (optional)
 RStudio (for writing R code)

9. Reading Data from MongoDB in R :


Definition
Reading data from MongoDB in R means using R to connect to a MongoDB NoSQL
database and import collections (documents) into R for data analysis.
We commonly use the mongolite package in R to do this.
Simple Explanation
MongoDB stores data as documents (JSON-like format) instead of tables.
To read data from MongoDB into R:
1. Connect to MongoDB
2. Select the database and collection
3. Run a query
4. Store the data in an R data frame
Example Code :
library(mongolite)

# Connect to MongoDB collection


conn <- mongo(collection = "employees",
db = "company",
url = "mongodb://localhost")
# Read the data
data <- conn$find()
Step-by-step Explanation
 library(mongolite)
Loads the MongoDB package in R.
 mongo(...)
Connects R to MongoDB.
o collection = "employees" → choose the collection

o db = "company" → choose the database

o url = "mongodb://localhost" → MongoDB runs on local system

 conn$find()
Means:
Get all documents from the employees collection
and store them in R as a data frame.

Example
Suppose MongoDB contains:
{ "id": 1, "name": "Mani", "salary": 50000 }
{ "id": 2, "name": "Nandini", "salary": 60000 }
After running:
data <- conn$find()
R will show:

id name salary

1 Mani 50000

2 Nandini 60000

Advantages
1. Easy handling of JSON-like data.
2. Great for unstructured or semi-structured data.
3. Fast reading and writing operations.
4. Flexible queries using MongoDB syntax.
5. Scales well for large data.

Disadvantages
1. Needs MongoDB installed and running.
2. No fixed schema → may cause inconsistent data.
3. Large collections may need powerful memory in R.
4. Fewer R packages available compared to SQL.

You might also like