Path: blob/master/Part 9 - Dimension Reduction/Linear Discriminant Analysis/lda.R
1339 views
# LDA12# Importing the dataset3dataset = read.csv('Wine.csv')45# Splitting the dataset into the Training set and Test set6# install.packages('caTools')7library(caTools)8set.seed(123)9split = sample.split(dataset$Customer_Segment, SplitRatio = 0.8)10training_set = subset(dataset, split == TRUE)11test_set = subset(dataset, split == FALSE)1213# Feature Scaling14training_set[-14] = scale(training_set[-14])15test_set[-14] = scale(test_set[-14])1617# Applying LDA18library(MASS)19lda = lda(formula = Customer_Segment ~ ., data = training_set)20training_set = as.data.frame(predict(lda, training_set))21training_set = training_set[c(5, 6, 1)]22test_set = as.data.frame(predict(lda, test_set))23test_set = test_set[c(5, 6, 1)]2425# Fitting SVM to the Training set26# install.packages('e1071')27library(e1071)28classifier = svm(formula = class ~ .,29data = training_set,30type = 'C-classification',31kernel = 'linear')3233# Predicting the Test set results34y_pred = predict(classifier, newdata = test_set[-3])3536# Making the Confusion Matrix37cm = table(test_set[, 3], y_pred)3839# Visualising the Training set results40library(ElemStatLearn)41set = training_set42X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)43X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)44grid_set = expand.grid(X1, X2)45colnames(grid_set) = c('x.LD1', 'x.LD2')46y_grid = predict(classifier, newdata = grid_set)47plot(set[, -3],48main = 'SVM (Training set)',49xlab = 'LD1', ylab = 'LD2',50xlim = range(X1), ylim = range(X2))51contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)52points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'deepskyblue', ifelse(y_grid == 1, 'springgreen3', 'tomato')))53points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')))5455# Visualising the Test set results56library(ElemStatLearn)57set = test_set58X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)59X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)60grid_set = expand.grid(X1, X2)61colnames(grid_set) = c('x.LD1', 'x.LD2')62y_grid = predict(classifier, newdata = grid_set)63plot(set[, -3], main = 'SVM (Test set)',64xlab = 'LD1', ylab = 'LD2',65xlim = range(X1), ylim = range(X2))66contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)67points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'deepskyblue', ifelse(y_grid == 1, 'springgreen3', 'tomato')))68points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')))6970