-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrac_machineLearningCP.Rmd
More file actions
62 lines (58 loc) · 2.03 KB
/
Prac_machineLearningCP.Rmd
File metadata and controls
62 lines (58 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: "Untitled"
author: "Uttaran Tribedi"
date: "9/12/2019"
output: html_document
---
library(dplyr)
library(tidyr)
library(caret)
library(randomForest)
```
<font size="4">Setting workspace directory and loading given files into the workspace</font>
```{r}
df_train<-read.csv("training.csv")
df_test<-read.csv("test.csv")
```
<font size="4">Displaying basic information about the given datasets</font>
```{r}
summary(df_train)
summary(df_test)
head(df_train)
head(df_test)
```
<font size="4">Removing columns that do not impact construction of model along with columns consisting of NA values</font>
```{r}
df_train<- df_train[,c(8:160)]
df_test<- df_test[,c(8:160)]
df_train<- df_train[, colSums(is.na(df_train))==0]
df_test<- df_test[, colSums(is.na(df_test))==0]
```
<font size="4">Making sure test data matches train data to build the most accurate model possible</font>
```{r}
col_classifier<- intersect(colnames(df_train[,1:85]),colnames(df_test[1:52]))
df_train_2<- df_train[,col_classifier,drop=F]
df_test_2<- df_test[,col_classifier,drop=F]
df_train_2<- cbind(df_train_2, classe=df_train$classe)
df_test_2<- cbind(df_test_2, problem_id=df_test$problem_id)
```
<font size="4">Dividing train data into two parts for cross validation purposes</font>
```{r}
train<--createDataPartition(y=df_train_2$classe,p=0.7,list=FALSE)
train_set<-df_train_2[train,]
tribedi_set<-df_train_2[-train,]
```
<font size="4">Building a model with the training set and testing given model on the validation set</font>
```{r}
modl<- train(classe~., data=train_set, method='rf',trControl=trainControl(method = 'cv', number = 5), na.action=na.omit)
prediction<- predict(modl,tribedi_set,type="raw")
confusionMatrix(tribedi_set$classe,prediction)
```
<font size="4">Applying model to test data</font>
```{r}
prediction2 <- predict(modl, df_test_2, type="raw")
prediction2
```
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.