0% found this document useful (0 votes)
11 views4 pages

Jaswinder Pal Singh 2024-04-05: Library Data Print Unique

The document outlines a data analysis process using the iris dataset in R, including data partitioning, model training with decision trees, and accuracy evaluation. It demonstrates the creation of a full decision tree model and a smaller model, reporting training and test accuracies of approximately 100% and 90.67%, respectively. The analysis utilizes libraries such as caret and rpart for model building and evaluation.
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)
11 views4 pages

Jaswinder Pal Singh 2024-04-05: Library Data Print Unique

The document outlines a data analysis process using the iris dataset in R, including data partitioning, model training with decision trees, and accuracy evaluation. It demonstrates the creation of a full decision tree model and a smaller model, reporting training and test accuracies of approximately 100% and 90.67%, respectively. The analysis utilizes libraries such as caret and rpart for model building and evaluation.
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
You are on page 1/ 4

12.

1
Jaswinder pal singh

2024-04-05
library(datasets)

data(iris)

print(unique(iris$Species))

## [1] setosa versicolor virginica


## Levels: setosa versicolor virginica

print(names(iris[,1:4]))

## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"

print(nrow(iris))

## [1] 150

print(iris[1,])

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species


## 1 5.1 3.5 1.4 0.2 setosa

library(caret)

## Loading required package: ggplot2

## Loading required package: lattice

set.seed(121)

index = createDataPartition(iris$Species,p=0.5,list=FALSE)

train = iris[index,]

test = iris[-index,]

library(rattle)

## Loading required package: tibble

## Loading required package: bitops

## Rattle: A free graphical interface for data science with R.


## Version 5.5.1 Copyright (c) 2006-2021 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart)

clf_full = rpart(Species~., data = train,

control = rpart.control(minsplit=2, minbucket = 1, cp=0))

fancyRpartPlot(clf_full)

print(test[1,])

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species


## 3 4.7 3.2 1.3 0.2 setosa

predict(clf_full, newdata = test[1,])

## setosa versicolor virginica


## 3 1 0 0

total = nrow(train)

correct = 0

for(i in 1:total){

if(train$Species[i] == predict(clf_full, newdata = train[i,], type =


"class")){
correct = correct + 1

accuracy = correct / total

accuracy

## [1] 1

sum(predict(clf_full, newdata = test, type = "class") ==


test$Species)/nrow(test)

## [1] 0.88

clf_small = rpart(Species~., data =train, control =


rpart.control(maxdepth = 2))

fancyRpartPlot(clf_small)

train_acc = sum(predict(clf_small, newdata = train, type = "class") ==


test$Species)/nrow(train)

test_acc = sum(predict(clf_small, newdata = test, type = "class") ==


test$Species)/nrow(test)
print(cat("Training accuracy: ", train_acc, "Test accuracy: ",
test_acc))

## Training accuracy: 0.9866667 Test accuracy: 0.9066667NULL

You might also like