forked from viniciusdel/MachineLearningPredictions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstable_11_17_20_1835.R
More file actions
285 lines (219 loc) · 10 KB
/
stable_11_17_20_1835.R
File metadata and controls
285 lines (219 loc) · 10 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# load the shiny package
library(shiny)
#install.packages("magick")
library(magick)
# Define UI for application
ui <- fluidPage(
# Navigation Bar at the top of the Shiny app
navbarPage(title = span("Machine Learning Algorithms in Action", style = "color: #532d8e; font-family: Futura; font-size: 25px"), id = NULL,
# *************************
# TAB 1: Number Recognition
# mini-project which accepts user-drawn numbers and displays the number as a text output
tabPanel(title = "Number Recognition",
fluidRow(
column(width = 3,
wellPanel(
h4("Click on the plot to start drawing, click again to pause"),
#sliderInput(inputId = "mywidth", "width of the pencil", min = 1, max = 30, step = 1, value = 10),
actionButton(inputId = "reset", label = "reset", style = "background-color: #ff0000"),
actionButton(inputId = "send", label = "send", style = "background-color: #00ff00")
)
)
),
plotOutput("plot", width = "500px", height = "500px",
hover = hoverOpts(id = "hover", delay = 100,
delayType = "throttle", clip = TRUE, nullOutside = TRUE),click = "click")
),
# *************************
# TAB 2: Personal Medical Insurance Costs
# mini-project which looks at the multiple factors that are considered when a person registers for Medical Insurance
tabPanel(title = "Personal Medical Insurance Costs",
sidebarLayout(
sidebarPanel(
width = 3,
h4("Please fill out the following fields:"),
numericInput(inputId = "age", label = "Age, years", value = 1, min = 1, max = 101, step = 1, width = "150px"),
selectInput(inputId = "sex", label = "Sex, gender", choices = c("Male", "Female"), width = "150px"),
h5("Height:"),
wellPanel(
splitLayout(
numericInput(inputId = "feet", label = "ft", value = 5, min = 4, max = 8, step = 1, width = "150px"),
numericInput(inputId = "inches", label = "in.", value = 11, min = 1, max = 11, step = 1, width = "150px"), cellWidths = "150px"),
),
numericInput(inputId = "weight", label = "Weight, lbs", value = 155, min = 85, max = 350, step = 1, width = "150px"),
numericInput(inputId = "children", label = "Children", value = 0, min = 0, max = 10, step = 1, width = "150px"),
radioButtons(inputId = "smoker", label = "Smoker?", choices = c("No" = "no", "Yes" = "yes")),
selectInput(inputId = "region", label = "Region you currently reside in the US", choices = c("northeast", "southeast", "southwest", "northwest"), width = "150px"),
actionButton(inputId = "submit", label = "submit", style = "background-color: #DCDCDC")
),
mainPanel(h5("BMI of individual:"),
textOutput(outputId = "bmi")) # Plots will go inside here
),
),
# *************************
# TAB 3: Diabetes
# page which displays information about the app
tabPanel("Diabetes",
h4("This project...")),
# *************************
# TAB 4: About
# page which displays information about the app
tabPanel("About",
h4("hello"))
)
)
# Define server logic required
server <- function(input, output) {
trainmodels <- function(){
# read in data
library(readr)
#read training data
train_orig <- read_csv("train.csv")
#read testing data
test_orig <- read_csv("test.csv")
# save the training labels
train_orig_labels <- train_orig[, 1]
#convert it to factor (for classification)
train_orig_labels <- as.factor(train_orig_labels$label)
#install.packages("randomForest")
library(randomForest)
numTrees <- 25
# Train on entire training dataset and predict on the test
cat("\n Traning model... \n")
rf <- randomForest(train_orig[-1], train_orig_labels,
xtest=test_orig, ntree=numTrees, keep.forest=TRUE)
# Save an object to a file
saveRDS(rf, file = "rf_trained.rds")
cat("\n Done training! \n")
}
printImg <- function(x){
#install.packages("RSEIS")
library(RSEIS)
#pick an image from the test poll
number <- x
#flip matrix
m = matrix(number, nrow = 28, ncol = 28, byrow = FALSE)
im_numbers <- apply(m, 2, as.numeric)
im_numbers <- mirror.matrix(im_numbers)
#show image
image(1:28, 1:28, im_numbers, col=gray((0:255)/255))
}
# *************************
# TAB 1
library(imager)
im <- load.example('parrots') %>% grayscale
vals = reactiveValues(x = NULL, y = NULL)
draw = reactiveVal(FALSE)
# Drawing on the plot
observeEvent(input$click, handlerExpr = {
temp <- draw(); draw(!temp)
if(!draw()) {
vals$x <- c(vals$x, NA)
vals$y <- c(vals$y, NA)
}})
# Reset button -> Clear the plot
observeEvent(input$reset, handlerExpr = {
vals$x <- NULL; vals$y <- NULL
#clear shown result
})
# Send button -> send the user-drawn number to the machine learning algorithm
observeEvent(input$send, handlerExpr = {
#vals$x <- NULL; vals$y <- NULL
png(file="myPlot.png",
width=500, height=500)
Xcorners <- c(0, 25.7, 0, 25.7)
Ycorners <- c(0, 25.7, 25.7, 0)
slider <- 0
#make line thicc
plot(0:27, 0:27, lwd = 0.1, cex = 0, xlab = "", ylab = "")
points(x = vals$x, y = vals$y, type = "l", lwd = 25)
points(Xcorners, Ycorners, type = "p", pch = 19)
#
#save image
dev.off()
#reload image
img <- image_read("myPlot.png")
#crop sides
margin <- geometry_area(x = 0, y = 0, width = 600, height = 600)
#margin2 <- geometry_size_pixels(0, 0, )
#crop sides
img <- image_crop(img, margin)
#resize to 28x28
img <- image_resize(img, "28x28")
#save image
#image_write(img, path = "myPlot.png", format = "png", quality = 100)
#grayscale
img <- image_convert(img, type = 'Grayscale')
#convert to grayscale
tiffImage <- image_convert(img, "tiff")
# Access data in raw format and convert to integer (it's in RAW)
vec <- as.vector(tiffImage[[1]])
# convert o ascii
vec <- rawToChar(vec, multiple = TRUE)
library(gtools)
#convert ascii to its code
for(i in 1:784){ vec[i] <- asc(vec[i])}
#need to flip these colors
for(i in 1:784){ vec[i] <- 255 - as.numeric(vec[i])}
vec[is.na(vec)] <- 255
"
#filter out smaller values
for(i in 1:784){
if(vec[i] < 15)
vec[i] <- 0
}
"
library(randomForest)
# Restore the object RF trained
rf <- readRDS(file = "rf_trained.rds")
#print(rf)
#make prediction
pred <- predict(object = rf, newdata = vec, type= "response")
#show prediction
cat("\n", as.numeric(pred) - 1)
printImg(vec)
})
observeEvent(input$hover, {
if (draw()) {
vals$x <- c(vals$x, input$hover$x)
vals$y <- c(vals$y, input$hover$y)
}})
output$plot = renderPlot({
plot(x = vals$x, y = vals$y, xlim = c(0, 28), ylim = c(0, 28), ylab = "y", xlab = "x", type = "l", lwd = input$mywidth)
#cat("X value: ", vals$x, "Y Value: ", vals$y, "\n")
})
# *************************
# TAB 2
# submit button -> send all the inputs to the machine learning algorithm
observeEvent(input$submit, handlerExpr = {
# calculating and rendering BMI
output$bmi <- renderText({ c(input$feet, input$inches, input$weight)
# converting BMI from Imperial units to Metric units
heightFeetToInches <- input$feet * 12
heightTotalInches <- heightFeetToInches + input$inches
heightTotalMeters <- heightTotalInches * 2.54 * (1/100)
weightKilograms <- input$weight * 0.453592
bmiMetric <- weightKilograms / (heightTotalMeters^2)
substring(bmiMetric, 1, 4) # trim the amount of digits to 00.0
})
# these values will be sent to the machine learning algorithm
# output$age
# output$sex
# output$children
# output$smoker
# output$region
})
}
#output$values= renderText({
#print("Test string")
#})
# Run the application
shinyApp(ui = ui, server = server)