1.
Exploratory Data Analysis
1) Univariate Analysis of the Iris Dataset Using Scatter Plots
The iris dataset is built into R and can be loaded directly:
data(iris)
Univariate analysis means studying each variable individually.
A simple way to visualize one variable at a time is to create a scatter plot of the variable vs. the
observation number.
1. Scatter plot of Sepal Length:
plot(iris$[Link],
main = "Univariate Scatter Plot: Sepal Length",
xlab = "Observation Index",
ylab = "Sepal Length (cm)",
pch = 19)
2. Scatter plot of Sepal Width:
plot(iris$[Link],
main = "Univariate Scatter Plot: Sepal Width",
xlab = "Observation Index",
ylab = "Sepal Width (cm)",
pch = 19)
3. Scatter plot of Petal Length:
4. Scatter plot of Petal Width:
Conclusion :
Using the standard plot() function in R, scatter plots of sepal length, sepal width, petal length, and
petal width can be created by plotting each variable against the observation index. These plots help in
understanding the spread, variation, and patterns within each individual attribute of the iris dataset.
[Link] Algorithms I
We will use R Studio to generate a Decision Tree model to classify the famous iris dataset.
The iris dataset has 150 rows. Each row has the data of the iris plant under five attributes - sepal length,
sepal width, petal length, petal width, and species. There are three different kinds of iris in the dataset
and each type has 50 rows of data. The three types are – setosa, versicolor and virginica. The iris dataset
is already present in R Studio and does not need to be loaded.
The objective of the exercise is to build a Decision Tree model which can correctly classify a new iris
flower into one the three groups - setosa, versicolor and virginica.
# Decision Tree on iris dataset
library(rpart)
library([Link])
# Split data into training and testing sets
[Link](522)
train_index <- sample(1:nrow(iris), 0.75 * nrow(iris))
trainSet <- iris[train_index, ]
testSet <- iris[-train_index, ]
# Build the decision tree model
treeModel <- rpart(Species ~ ., data = trainSet, method = "class")
# Plot the tree
[Link](treeModel)
Output:
Conclusion:
The decision tree model successfully classifies iris flowers based on their features, showing that decision
trees are an easy and effective method for prediction.
[Link] Algorithms II
We will use R Studio to generate a K-NN model to analyze the famous iris dataset.
The objective of the exercise is to build a k-NN model which can correctly classify the iris flowers into
three groups - setosa, versicolor, and virginica
#KNN on iris dataset
library(class)
data(iris)
# 1. Normalize only the first 4 numeric columns
normalize <- function(x) { (x - min(x)) / (max(x) - min(x)) }
[Link] <- [Link](lapply(iris[, 1:4], normalize))
# 2. Create training and test sets
[Link] <- [Link][1:130, ]
[Link] <- [Link][131:150, ]
[Link] <- iris[1:130, 5]
[Link] <- iris[131:150, 5]
# 3. Apply KNN (k = 16)
model <- knn(train = [Link], test = [Link],
cl = [Link], k = 16)
# 4. Compare actual vs predicted
table([Link], model)
Output
The KNN model was able to correctly classify the iris flowers by comparing them with the closest
matching training samples.
4. Regression Algorithms I
Cloth manufacturers need to create patterns for clothing so that the clothes are likely
to fit their buyers. For the clothes to fit people well, designers must understand the relationship that
exists between the different parts of the human body. For example, a shirt designer must take into
consideration the length of a person’s arm in relation to the length of their upper body.
The question we need to answer is - Is forearm length relate to height? If so, can we use for forearm
length to predict height?
Consider the following data as the basis for this assignment.
# Sample data (Forearm length and Height in cm)
forearm <- c(26, 27, 25, 28, 30, 29, 24, 31, 27, 26)
height <- c(155,158,150,160,170,165,148,175,159,156)
# Create a data frame
body <- [Link](forearm, height)
# 1. View the data
head(body)
# 2. Create a scatter plot to see the relationship
plot(body$forearm, body$height,
main = "Scatter Plot: Height vs Forearm Length",
xlab = "Forearm Length (cm)",
ylab = "Height (cm)",
pch = 19)
# 3. Fit a simple linear regression model
model <- lm(height ~ forearm, data = body)
# 4. Display the model summary (slope + intercept)
summary(model)
# 5. Draw the line of best fit on the scatter plot
abline(model, col = "blue", lwd = 2)
# 6. Predict height for a given forearm length
predict(model, newdata = [Link](forearm = 30))
Summary:
We first create two variables → forearm length and height.
A scatter plot shows the relationship between the two variables.
Using lm(), we fit a linear regression line (line of best fit).
The regression equation helps predict height from any forearm length
Conclusion:
The scatter plot and regression line clearly show a strong positive relationship between forearm length
and height. As a person's forearm length increases, their height also tends to increase. This means
forearm length can be used as a good predictor of height using the linear regression model.
5. Regression Algorithms II
The comparison of multiple linear regression and logistic regression.
R script that compares multiple linear regression and logistic regression using the built-in mtcars
dataset.
predictor variables (wt, hp, disp) are used for both models.
For linear regression we predict mpg (numeric);
for logistic regression we predict am (0 = automatic, 1 = manual).
# Simple comparison of Linear vs Logistic Regression
data(mtcars)
# 1. MULTIPLE LINEAR REGRESSION
# Predict mpg (numeric) using wt and hp
lm_model <- lm(mpg ~ wt + hp, data = mtcars)
# Show results
summary(lm_model)
# Predict mpg for a car with wt = 3 and hp = 110
predict(lm_model, newdata = [Link](wt = 3, hp = 110))
output
# 2. LOGISTIC REGRESSION
# Predict am (0 = automatic, 1 = manual) using wt and hp
glm_model <- glm(am ~ wt + hp, data = mtcars, family = binomial)
# Show results
summary(glm_model)
# Predict probability of a manual car for wt = 3 and hp = 110
predict(glm_model, newdata = [Link](wt = 3, hp = 110), type = "response")
Output
Summary
Multiple Linear Regression is used when we want to predict a numerical value, while Logistic Regression
is used when we want to predict a category such as 0 or 1.
Linear regression fits a straight line to the data, whereas logistic regression fits an S-shaped curve to
estimate probabilities.
Thus, the choice of regression depends on the type of output we need to predict.
Conclusion:
Using the mtcars dataset, the multiple linear regression model showed how car weight and horsepower
affect the fuel efficiency (mpg), giving a numerical prediction for a new car. The logistic regression
model, using the same predictors, estimated the probability of a car being manual or automatic. This
comparison shows that linear regression is suitable for predicting continuous values, while logistic
regression is used for predicting categories based on probabilities.
[Link] Learning-K Mean
K-Means Clustering on Iris Data:
# Load dataset
data(iris)
# Use only numeric columns (here: [Link] and [Link])
iris_data <- iris[, 1:2]
# View first few rows
head(iris_data)
# 1. Run K-means with 3 clusters
[Link](123) # for same result every time
km_result <- kmeans(iris_data, centers = 3)
# 2. Print cluster results
km_result$cluster
# 3. Plot the clusters
plot(iris_data,
col = km_result$cluster,
main = "K-Means Clustering on Iris Data",
xlab = "Sepal Length",
ylab = "Sepal Width",
pch = 19)
# Add cluster centers to the plot
points(km_result$centers, col = 1:3, pch = 8, cex = 2)
Experiment 6:
K-Means clustering successfully grouped the data into natural clusters based on similarities in features.
By using just two variables, we could visually see how the points formed clear groups, showing that K-
Means is an effective unsupervised learning method for identifying patterns without any prior labels.
K-Means Script (Using mtcars dataset)
# -----------------------------------------
# K-Means Clustering – Example 2 (mtcars)
# Load dataset
data(mtcars)
# Select two features for clustering: weight (wt) and mileage (mpg)
car_data <- mtcars[, c("wt", "mpg")]
# View first few rows
head(car_data)
# 1. Run K-means with 3 clusters
[Link](50) # for reproducible results
km_result <- kmeans(car_data, centers = 3)
# 2. Print cluster assignments
km_result$cluster
# 3. Plot the clusters
plot(car_data,
col = km_result$cluster,
main = "K-Means Clustering on mtcars Data",
xlab = "Weight (wt)",
ylab = "Mileage (mpg)",
pch = 19)
# Mark the cluster centers
points(km_result$centers,
col = 1:3,
pch = 8,
cex = 2)
Conclusion:
Using K-Means clustering on the mtcars dataset, the cars were grouped into three clusters based on
their weight and mileage. The scatter plot clearly shows how cars with similar characteristics fall into the
same cluster. This demonstrates that K-Means is useful for discovering hidden patterns in data without
needing any labels, making it an effective unsupervised learning technique.