Explain how to implement decision in R

885    Asked by SanjanaShah in Data Science , Asked on Nov 4, 2019
Answered by Sanjana Shah

First we import the dataset

# Importing the dataset com_sale

dataset = read.csv(file.choose())

Now we encode the target feature as factor

# Encoding the target feature as factor

cut(dataset$TaxableIncome,2)

cut(dataset$TaxableIncome,c(0,30000))

dataset$TaxableIncome=cut(dataset$TaxableIncome,2,labels=c("Risky","Good"))

Now we will split the data for training and testing

# Splitting the dataset into the Training set and Test set

# install.packages('caTools')

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)

Now we will fit the model

# Fitting Decision Tree Classification to the Training set

# install.packages('rpart')

library(rpart)

classifier = rpart(formula = TaxableIncome ~ .,

                   data = training_set)

Now we predict and evaluate the model

# Predicting the Test set results

y_pred = predict(classifier, newdata = test_set[-3], type = 'class')

y_pred

# Making the Confusion Matrix

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

cm



Your Answer

Interviews

Parent Categories