thomasp85 / fiery

A flexible and lightweight web server
https://fiery.data-imaginist.com
Other
240 stars 12 forks source link

Example for a prediction using C5.0 decision tree #6

Closed anubhavdikshit closed 7 years ago

anubhavdikshit commented 7 years ago

Hi,

I was so impressed and amazed at this package, it seemed to have the right balance between complexity and customization, I found you through this post (https://www.r-bloggers.com/a-simple-prediction-web-service-using-the-new-fiery-package/).

I tired to modify your example to handle a JSON as input, specifically using C5.0 decision tree, however I could not manage to do so. I was hoping you could have a document/pdf on github which would use a complex model (random forest, or anything using Caret) where instead of single parameter we send in a JSON.

I know it may seem a rich of me to ask you to do so, but I see tremendous potential to your package, unlike openCPU this is so easier to setup. Thank you so much for this again!

thomasp85 commented 7 years ago

I'm very happy for your impression.

I'm currently in a job transition and further my focus right now is on getting ggforce and ggraph out of the door. Once this has been done I'll begin to pic up the fiery ecosystem again, which has laid dormant the last couple of months... I hope that you can brace yourself with some time.

thomasp85 commented 7 years ago

If you have a very specific problem or error I'll be happy to look at your code and give my opinion though...

anubhavdikshit commented 7 years ago

Thanks for replying to me (impressive response speed!!) I completely understand your situation, I would be waiting for you to work on Fiery, thank you once again!!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 4:06 PM, Thomas Lin Pedersen < notifications@github.com> wrote:

I'm very happy for your impression.

I'm currently in a job transition and further my focus right now is on getting ggforce and ggraph out of the door. Once this has been done I'll begin to pic up the fiery ecosystem again, which has laid dormant the last couple of months... I hope that you can brace yourself with some time.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259102724, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWa7aSj7ObOXQ0tLcZOtX8dnLU1Naks5q8FC7gaJpZM4KsRDY .

anubhavdikshit commented 7 years ago

Thomas,

Please find the code that is slightly modified version of your code. My model is C5.0 based decision tree, which uses multiple columns. Do you need the model as well?

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 4:07 PM, Thomas Lin Pedersen < notifications@github.com> wrote:

If you have a very specific problem or error I'll be happy to look at your code and give my opinion though...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259102871, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWQxDwjZiEwrxPixgajeQgJ1xF762ks5q8FDigaJpZM4KsRDY .

thomasp85 commented 7 years ago

You cannot attach files to a GitHub issue email (I assume that this is what you tried)

anubhavdikshit commented 7 years ago

Oops didnt know that, where can I send the code to you then. Its not on Github as of it!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 4:40 PM, Thomas Lin Pedersen < notifications@github.com> wrote:

You cannot attach files to a GitHub issue email (I assume that this is what you tried)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259109811, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWQVZjKegGY1I0-v4a9Xfp1baYhlcks5q8FixgaJpZM4KsRDY .

thomasp85 commented 7 years ago

Share it through dropbox etc... and paste a link here.

anubhavdikshit commented 7 years ago

Here you go: https://www.dropbox.com/s/zld6f8gefbio29a/Bus_Asset_API.R?dl=0

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 4:50 PM, Thomas Lin Pedersen < notifications@github.com> wrote:

Share it through dropbox etc... and paste a link here.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259111721, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWTPum55L8YSaH0afnBY4xOBSTsXbks5q8FsLgaJpZM4KsRDY .

thomasp85 commented 7 years ago

Thanks - I'll have a look as time permits...

dpastoor commented 7 years ago

@anubhavdikshit I think your issue might be

res <- predict.C5.0(model, newdata = fromJSON(input), type = "class")

don't you want to only have grabbed the query string value to pass into the model, not the entire named list for

res <- predict.C5.0(model, 
-                   newdata = fromJSON(input),
                    type = "class")
res <- predict.C5.0(model,
+                     newdata = fromJSON(input$val), 
type = "class")

assuming input$val is a dataframe you want.

On the other hand, if input is a named list of columns, but you you want to pass a dataframe (per your code comments) into predict.C5.0 then you probably need do coerce to a dataframe rather than its list nature

...
newdata = as.data.frame(fromJSON(input))
...
anubhavdikshit commented 7 years ago

Thank you so much, I will get back to you on this!!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:40 PM, Devin Pastoor notifications@github.com wrote:

@anubhavdikshit https://github.com/anubhavdikshit I think your issue might be

res <- predict.C5.0(model, newdata = fromJSON(input), type = "class")

don't you want to only have grabbed the query string value to pass into the model, not the entire named list for

res <- predict.C5.0(model, - newdata = fromJSON(input), type = "class") res <- predict.C5.0(model,+ newdata = fromJSON(input$val), type = "class")

assuming input$val is a dataframe you want.

On the other hand, if input is a named list of columns, but you you want to pass a dataframe (per your code comments) into predict.C5.0 then you probably need do coerce to a dataframe rather than its list nature

... newdata = as.data.frame(fromJSON(input)) ...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259132303, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWXS22-360nTdxPR7AdWcx3PyFBzhks5q8HTYgaJpZM4KsRDY .

anubhavdikshit commented 7 years ago

Thomas,

This is what i am trying in terminal, curl 127.0.0.1:9123/predict.C5.0 -H "Content-Type: application/json" -d '{"input" : [ {"age":26, "marital" : "MARRIED"}]}'

This is what I get: Error : Argument 'txt' must be a JSON string, URL or file.

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:42 PM, Anubhav Dikshit anubhav@artoo.in wrote:

Thank you so much, I will get back to you on this!!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:40 PM, Devin Pastoor notifications@github.com wrote:

@anubhavdikshit https://github.com/anubhavdikshit I think your issue might be

res <- predict.C5.0(model, newdata = fromJSON(input), type = "class")

don't you want to only have grabbed the query string value to pass into the model, not the entire named list for

res <- predict.C5.0(model, - newdata = fromJSON(input), type = "class") res <- predict.C5.0(model,+ newdata = fromJSON(input$val), type = "class")

assuming input$val is a dataframe you want.

On the other hand, if input is a named list of columns, but you you want to pass a dataframe (per your code comments) into predict.C5.0 then you probably need do coerce to a dataframe rather than its list nature

... newdata = as.data.frame(fromJSON(input)) ...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259132303, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWXS22-360nTdxPR7AdWcx3PyFBzhks5q8HTYgaJpZM4KsRDY .

anubhavdikshit commented 7 years ago

Thomas,

I think without the model, its pretty hard to do anything, so please use this model, https://www.dropbox.com/s/3lkwmnsnysikzcz/tv_model.rda?dl=0

and the predict function will be predict.gam(tv_model, newdata = newdata)

Hope this helps!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:52 PM, Anubhav Dikshit anubhav@artoo.in wrote:

Thomas,

This is what i am trying in terminal, curl 127.0.0.1:9123/predict.C5.0 -H "Content-Type: application/json" -d '{"input" : [ {"age":26, "marital" : "MARRIED"}]}'

This is what I get: Error : Argument 'txt' must be a JSON string, URL or file.

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:42 PM, Anubhav Dikshit anubhav@artoo.in wrote:

Thank you so much, I will get back to you on this!!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 6:40 PM, Devin Pastoor notifications@github.com wrote:

@anubhavdikshit https://github.com/anubhavdikshit I think your issue might be

res <- predict.C5.0(model, newdata = fromJSON(input), type = "class")

don't you want to only have grabbed the query string value to pass into the model, not the entire named list for

res <- predict.C5.0(model, - newdata = fromJSON(input), type = "class") res <- predict.C5.0(model,+ newdata = fromJSON(input$val), type = "class")

assuming input$val is a dataframe you want.

On the other hand, if input is a named list of columns, but you you want to pass a dataframe (per your code comments) into predict.C5.0 then you probably need do coerce to a dataframe rather than its list nature

... newdata = as.data.frame(fromJSON(input)) ...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259132303, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWXS22-360nTdxPR7AdWcx3PyFBzhks5q8HTYgaJpZM4KsRDY .

dpastoor commented 7 years ago

ohh @anubhavdikshit you are trying to pass the json as data, @thomasp85 's example is passing it around as a query string

for example

http://127.0.0.1:9123/predict?val=-1.5

the breakdown is:

note, query parameters are separated by &

so in your case, you might do:

curl 127.0.0.1:9123/predict?age=26&marital=MARRIED

then when you parse the query string you'll get a list like

input <- list(age = "26", marital = "MARRIED") # what you'd get from parseQueryParams() so you will need to convert age to numeric, and perhaps convert to a dataframe while you're at it

library(dplyr)
query_df <- as.data.frame(input) %>% mutate(age = as.numeric(age)
....
predict.gam(tv_model, newdata = query_df,...)
anubhavdikshit commented 7 years ago

@Devin Things make a little more sense now, I have one follow up question, in case I use a different technique, why is the syntax in curl not changing that is

why is it still _curl _127.0.0.1:9123/predict?age=26&marital=MARRIED http://127.0.0.1:9123/predict?age=26&marital=MARRIED

and not curl

127.0.0.1:9123/predict.gam?age=26&marital=MARRIED http://127.0.0.1:9123/predict?age=26&marital=MARRIED And how do i just send a JSON while curl? that is

curl 127.0.0.1:9123/predict.C5.0 http://127.0.0.1:9123/predict.C5.0 -H "Content-Type: application/json" -d '{"input" : [ {"age":26, "marital" : "MARRIED"}]}'

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 7:07 PM, Devin Pastoor notifications@github.com wrote:

ohh @anubhavdikshit https://github.com/anubhavdikshit you are trying to pass the json as data, @thomasp85 https://github.com/thomasp85 's example is passing it around as a query string

for example

http://127.0.0.1:9123/predict?val=-1.5

the breakdown is:

  • predict is the ROUTE
  • ? delineates that anything after will be query parameters

note, query parameters are separated by &

so in your case, you might do:

curl 127.0.0.1:9123/predict?age=26&marital=MARRIED

then when you parse the query string you'll get a list like

input <- list(age = "26", marital = "MARRIED") # what you'd get from parseQueryParams() so you will need to convert age to numeric, and perhaps convert to a dataframe while you're at it

library(dplyr) query_df <- as.data.frame(input) %>% mutate(age = as.numeric(age) .... predict.gam(tv_model, newdata = query_df,...)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259137988, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWY2lUb_PYtvzvuGSijXWDdJ-CsuPks5q8HszgaJpZM4KsRDY .

dpastoor commented 7 years ago

so there are two parts to this answer. First, purely from a naming perspective, the route name is arbitrarily established. This gets a little into API design, but you need to decide if you want your users being explicit about what they are hitting, eg .../predict_gam?<params> or more generic .../predict?<params where you then implement the logic of understanding what you need.

For example, you could have a model_type query param

/predict?model_type=GAM&<morevalues>

then you might have more logic

query  <- get("QUERY_STRING", envir=request)

# handy helper function from the Shiny folks
input <- shiny::parseQueryString(query)

if (input$model_type == "GAM") {
 predict.gam(....)
} else if {
....
}

the predict isn't a magical keyword or anything, you can see in the example

path <- get("PATH_INFO", envir=request)
  if (path == "/info") {
....
 } else if (grepl("^/predict", path)) {
....
}

In this case, the logic is saying if there is a query along the route that starts with the word predict, apply the following logic

You could just as easily change the route to any wording you'd like.

Finally, in sending JSON, I haven't looked into directly how fiery stores data from the response object, however if it is simple values, I would suggest just sending them as query strings.

Use https://insomnia.rest/ (its free!) and you can build up Params very easily so you don't have to type them in manually each time

for example:

image

thomasp85 commented 7 years ago

@dpastoor Thanks for chiming in here! Ive got nothing else to really add...

dpastoor commented 7 years ago

@thomasp85 np, your future package has already saved me untold hours of work so want to give back where I can!

Also when you get some breathing room I'd be interested in helping out with the routr addition some, so feel free to ping me any time.

thomasp85 commented 7 years ago

I will keep that in mind - thanks

but future is the work of @HenrikBengtsson so credit goes to him for that one - I was just one of the first to pick it up :-)

dpastoor commented 7 years ago

ah yes of course - spacey of me - I discovered fiery from discussions about future and have had this on my radar to use for my next REST api I need to build.

Anyway, thanks for your work regardless!

anubhavdikshit commented 7 years ago

Guys,

You are all awesome, I cannot tell you how grateful I as well as many are to you all. Beautiful written and beautifully explained!! The R community is great because of people like you.. Thank you so much once again!

Thanks and Regards, Anubhav Dikshit | Artoo | +91-8197401816

This e-mail and its attachment(s) contain information from Artoo IT Solutions Private Limited which may be privileged or confidential. If you are not the intended recipient, be aware that any disclosure, copying, distribution, transmission or use of the contents of this e-mail and its attachment(s) is prohibited and subject to legal penalties. If you received this message in error you should notify the sender by returning the message immediately and delete any copies from your system. Thank you for your cooperation.

On Tue, Nov 8, 2016 at 7:46 PM, Devin Pastoor notifications@github.com wrote:

ah yes of course - spacey of me - I discovered fiery from discussions about future and have had this on my radar to use for my next REST api I need to build.

Anyway, thanks for your work regardless!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thomasp85/fiery/issues/6#issuecomment-259147264, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZeWa72yQVyzrJXzSlH0yI-nuSR83s3ks5q8IQ4gaJpZM4KsRDY .