-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANN_ClassApr7th'2018.R
94 lines (72 loc) · 2.55 KB
/
ANN_ClassApr7th'2018.R
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#########################################################
## Purpose: Create pretty classification tree
## Developer: KD
##
#########################################################
#########################################################
## Step 0: Clear the environment
##
##
#########################################################
rm(list=ls())
#########################################################
## Step 1: Load the relavent packages
##
##
#########################################################
installed.packages()
#install.packages("rpart") # CART standard package
?install.packages()
#install.packages("rpart")
#install.packages("rpart.plot") # Enhanced tree plots
#install.packages("rattle") # Fancy tree plot
#install.packages("RColorBrewer") # colors needed for rattle
library(rpart)
library(rpart.plot) # Enhanced tree plots
library(rattle) # Fancy tree plot
library(RColorBrewer) # colors needed for rattle
#########################################################
## Step 2: example
##
##
#########################################################
rm(list=ls())
dsn<-
read.csv("C://AIMS/Stevens_/CS513_datamining/Other/Titanic_rows.csv")
#View(dsn)
#attach(dsn)
#detach(dsn)
set.seed(123)
?ifelse
index<-sort(sample(nrow(dsn),round(.25*nrow(dsn))))
training<-dsn[-index,]
test<-dsn[index,]
?rpart()
#Grow the tree
CART_class<-rpart( Survived~.,data=training)
rpart.plot(CART_class)
CART_predict<-predict(CART_class,test)
str(CART_predict)
CART_predict_cat<-ifelse(CART_predict[,1]<=.5,'Yes','No')
table(Actual=test[,4],CART=CART_predict_cat)
CART_wrong<-sum(test[,4]!=CART_predict_cat)
CART_error_rate<-CART_wrong/length(test[,4])
CART_error_rate
CART_predict2<-predict(CART_class,test, type="class")
CART_wrong2<-sum(test[,4]!=CART_predict2)
CART_error_rate2<-CART_wrong2/length(test[,4])
CART_error_rate2
rm(list=ls())
traininginput<-runif(50,min=0,max=100)
trainingoutput<-sqrt(traininginput)
trainingdata<-cbind(traininginput,trainingoutput)
colnames(trainingdata)<-c("Input","Output")
install.packages("neuralnet")
library("neuralnet")
net.sqrt<-neuralnet(Output~Input,trainingdata,hidden = 10,threshold = 0.01)
plot(net.sqrt)
testdata<-as.data.frame((1:10)^2)
View(testdata)
net.results<-compute(net.sqrt, testdata)
cleanoutput<-cbind(testdata,sqrt(testdata),as.data.frame(net.results$net.result))
colnames(cleanoutput)<-c("Input","Expected Output","Neural Net Output")