0% found this document useful (0 votes)
21 views11 pages

Sneha R

R lang
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)
21 views11 pages

Sneha R

R lang
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
You are on page 1/ 11

RA2332241030015

EXPERIMENT – 11

Algorithm:- Implement SVM algorithm using WDBC datasets.

Here are the steps for implementing the SVM algorithm:

1. Load the data: Load the dataset that you want to classify or predict. The dataset
should be split into a training set and a test set.
2. Normalize the data: Normalize the data by scaling the features to have zero mean
and unit variance.
3. Choose the kernel function: Choose a kernel function that maps the data to a
higherdimensional space. The most commonly used kernel functions are the linear
kernel, the polynomial kernel, and the radial basis function (RBF) kernel.
4. Train the model: Train the SVM model on the training data using the chosen kernel
function.
5. Choose the regularization parameter: Choose the regularization parameter (C) that
controls the trade-off between maximizing the margin and minimizing the
classification error.
6. Test the model: Test the SVM model on the test data to evaluate its performance.
7. Tune the hyperparameters: Tune the hyperparameters of the SVM model using
techniques such as grid search or random search to find the best combination of
kernel function, regularization parameter, and other hyperparameters.
8. Deploy the model: Deploy the trained SVM model to make predictions on new data.

Program Code:-

dataset = read.csv('social.csv')

dataset = dataset[3:5]

dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))


install.packages('caTools')

library(caTools) set.seed(123)

split = sample.split(dataset$Purchased, SplitRatio = 0.75)

Implement SVM algorithm 31


RA2332241030015

training_set = subset(dataset, split == TRUE)


test_set = subset(dataset, split == FALSE training_set[-3]

= scale(training_set[-3]) test_set[-3] = scale(test_set[-3])

install.packages('e1071') library(e1071)
classifier = svm(formula = Purchased ~ .,
data = training_set, type = 'C-
classification' kernel = 'linear')

y_pred = predict(classifier, newdata =


test_set[-3]) cm = table(test_set[, 3],
y_pred) set = test_set

X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01) X2

= seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)

grid_set = expand.grid(X1, X2)

colnames(grid_set) = c('Age', 'EstimatedSalary')

y_grid = predict(classifier, newdata = grid_set)

plot(set[, -3], main = 'SVM (Test set)', xlab =

'Age', ylab = 'Estimated Salary', xlim =

range(X1), ylim = range(X2))

contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)

points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'coral1', 'aquamarine')) points(set,

pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))

OUTPUT:-

Implement SVM algorithm 32


RA2332241030015

Implement SVM algorithm 33


RA2332241030015

Result:- The SVM Algorithm have been implemented successfully.

Implement SVM algorithm 34


RA2332241030015

EXPERIMENT – 12

Aim:- Implement Logistic regression on IRIS datasets.


Algorithm:-
1. Import the dataset: Import the dataset which contains the input variables (or the
independent variables) and the output variable (or the dependent variable).

2. Data Pre-processing: Check for missing values and handle them, handle outliers if
any, and remove or transform variables that do not contribute to the model.

3. Split the dataset: Split the dataset into a training set and a testing set. The training
set is used to train the model, while the testing set is used to evaluate the
performance of the model.

4. Feature Scaling: Scale the input variables (or the independent variables) to have the
same range to ensure that no variable dominates the others in the modeling process.

5. Model Training: Train the logistic regression model using the training set. The logistic
regression model calculates the probability of the output variable taking a particular
value given the input variables (or the independent variables).

6. Model Evaluation: Evaluate the performance of the model using the testing set.
There are several metrics to evaluate the performance of the model, such as
accuracy, precision, recall, F1 score, and area under the ROC curve.

7. Model Improvement: If the model does not perform well, try to improve the model
by changing the hyperparameters, selecting the relevant features, or using other
modeling techniques.

8. Model Deployment: Once the model is finalized and performs well on the testing set,
deploy it to the production environment for making predictions on new data.

Program Code:-

install.packages("dplyr")
install.packages("caTools")
install.packages("ROCR")

# loading packages

library(dplyr)
library(caTools)

Implement Logistic regression 35


35
RA2332241030015

library(ROCR) dataSet =
mtcars View(dataSet)

split <- sample.split(mtcars, SplitRatio = 0.8)

table(split)

train_reg <- subset(mtcars, split == "TRUE")

test_reg <- subset(mtcars, split == "FALSE")

# Training model

logistic_model <- glm(vs ~ wt + disp,


data = train_reg,

family = "binomial")

logistic_model
summary(logistic_model)
predict_reg <-
predict(logistic_model,

test_reg, type = "response") table(predict_reg)

predict_reg <- ifelse(predict_reg >0.5, 1, 0)

table(test_reg$vs, predict_reg)

missing_classerr <- mean(predict_reg != test_reg$vs)

print(paste('Accuracy =', 1 - missing_classerr))

ROCPred <- prediction(predict_reg, test_reg$vs)

ROCPer <- performance(ROCPred, measure = "tpr", x.measure = "fpr")

auc <- performance(ROCPred, measure = "auc")


auc <- [email protected][[1]]

auc plot(ROCPer) plot(ROCPer, colorize = TRUE,


print.cutoffs.at = seq(0.1, by = 0.1),

main = "ROC CURVE")

abline(a = 0, b = 1) auc <- round(auc, 4)

legend(.6, .4, auc, title = "AUC", cex = 1)

Implement Logistic regression 36


RA2332241030015

Output:-

Implement Logistic regression 37


RA2332241030015

Result:- The logistic regression have been implemented successfully.

Implement Logistic regression 38


RA2332241030015

EXPERIMENT – 13

Aim:- Implement Aprori algorithm.

Algorithm:-
1. Read each item in the transaction.
2. Calculate the support of every item.
3. If support is less than minimum support, discard the item. Else, insert it into frequent
itemset.
4. Calculate confidence for each non- empty subset.
5. If confidence is less than minimum confidence, discard the subset. Else, it into strong
rules.

Program Code:-

install.packages("arules")
install.packages("arulesViz")
install.packages("RColorBrewer")
library(arules) library(arulesViz)
library(RColorBrewer)
data("Groceries") rules <-
apriori(Groceries, parameter =
list(supp = 0.01, conf = 0.2))
inspect(rules[1:10])
arules::itemFrequencyPlot

(Groceries, topN = 20,

col = brewer.pal(8, 'Pastel2'),

main = 'Relative Item Frequency Plot',


type = "relative" ylab = "Item Frequency (Relative)")

Implement Aprori algorithm 39


RA2332241030015

Output:-

Implement Aprori algorithm 40


40
RA2332241030015

Result:- The Apriori algorithm have been implemented successfully.

Implement Aprori algorithm 41


41

You might also like