Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

klasifikasi

Ella kurnia

2022-11-29
#Library yang dibutuhkan

library(dplyr)

##
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':


##
## filter, lag

## The following objects are masked from 'package:base':


##
## intersect, setdiff, setequal, union

library(ggplot2)
library(tidyverse)

## -- Attaching packages --------------------------------------- tidyverse


1.3.2 --

## v tibble 3.1.8 v purrr 0.3.5


## v tidyr 1.2.1 v stringr 1.4.1
## v readr 2.1.3 v forcats 0.5.2
## -- Conflicts ------------------------------------------
tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()

library(caret)

## Loading required package: lattice


##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift

library (skimr)
library(scales)

##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor

library(corrplot)

## corrplot 0.92 loaded

library(RColorBrewer)
library(caTools)
library(e1071)
library(class)
library(randomForest)

## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
##
## The following object is masked from 'package:ggplot2':
##
## margin
##
## The following object is masked from 'package:dplyr':
##
## combine

library(mlbench)

#import dan cleaning dataset


dataset = read.csv("C:/Users/Gadget House/Downloads/sales.csv")
dataset <- dataset[,-1]
dataset <- dataset[,-1]
dataset <- dataset[,-1]
dataset <- dataset[,-1]
dataset <- dataset[,-1]
dataset <- dataset[,-1]
dataset <- dataset[,-2]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
dataset <- dataset[,-5]
# Encoding the target feature as factor
dataset= dataset %>%
mutate(Sales = as.factor(ifelse(Sales <= 300, "0","1")))
dataset$Sales = factor(dataset$Sales, levels = c(0, 1))

#Membagi data

library(caTools)
set.seed(123)
split = sample.split(dataset$Sales, SplitRatio = 0.75)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)

dim(training_set)

## [1] 3186 5

dim(test_set)

## [1] 1062 5

topredict_set<-test_set[2:5] # Menghapus Target Sales


dim(topredict_set)

## [1] 1062 4

model_naive <- naiveBayes(Sales ~ ., data = training_set)


#Implementasi Naive Bayes
preds_naive <- predict(model_naive, newdata = topredict_set)
#Memprediksi Target Class untuk validasi
conf_matrix_naive <- table(preds_naive, test_set$Sales)

confusionMatrix (conf_matrix_naive)

## Confusion Matrix and Statistics


##
##
## preds_naive 0 1
## 0 845 55
## 1 49 113
##
## Accuracy : 0.9021
## 95% CI : (0.8826, 0.9193)
## No Information Rate : 0.8418
## P-Value [Acc > NIR] : 7.483e-09
##
## Kappa : 0.6269
##
## Mcnemar's Test P-Value : 0.6239
##
## Sensitivity : 0.9452
## Specificity : 0.6726
## Pos Pred Value : 0.9389
## Neg Pred Value : 0.6975
## Prevalence : 0.8418
## Detection Rate : 0.7957
## Detection Prevalence : 0.8475
## Balanced Accuracy : 0.8089
##
## 'Positive' Class : 0
##

You might also like