Last active
February 13, 2019 19:29
-
-
Save toenuff/b687ed2d99596f874d57a2db3b7a9795 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Published for http://powertoe.wordpress.com | |
# https://powertoe.wordpress.com/2017/10/23/h2o-machine-learning-with-powershell/ | |
function ConvertTo-FormData { | |
param( | |
[Parameter(ValueFromPipeline=$true)] [PSObject] $InputObject | |
) | |
Begin { | |
$output = "" | |
} | |
Process { | |
foreach ($prop in $InputObject.psobject.properties |select -expandproperty name) { | |
if ($InputObject.($prop).gettype().name -eq "Boolean") { | |
if ($InputObject.($prop)) { | |
$output += "$prop=true&" | |
} else { | |
$output += "$prop=false&" | |
} | |
} if ($InputObject.($prop).gettype().isarray) { | |
# hacky for h2o collections | |
if ($InputObject.($prop).name) { | |
$output += "$prop=[{0}]&" -f ($InputObject.($prop).name -join ",") | |
} else { | |
$output += "$prop=[{0}]&" -f ($InputObject.($prop) -join ",") | |
} | |
} | |
else { | |
$output += "$prop=" + $InputObject.($prop) + "&" | |
} | |
} | |
} | |
End { | |
$output.Remove($output.Length-1,1) | |
} | |
} | |
function Wait-H2oJob { | |
param( | |
[Parameter(Mandatory=$true, Position=0)] | |
[String] $JobPath | |
) | |
$notdone = $true | |
while ($notdone) { | |
$status = invoke-restmethod ("http://localhost:54321" + $JobPath) |select -ExpandProperty jobs |select -ExpandProperty status | |
$status | |
if ($status -eq "DONE") { | |
$notdone = $false | |
} else { | |
sleep -Milliseconds 500 | |
} | |
} | |
} | |
$url = "http://localhost:54321/3/{0}" | |
$iris_url = 'https://raw.githubusercontent.com/DarrenCook/h2o/bk/datasets/iris_wheader.csv' | |
"Import the IRIS data" | |
$importfiles_url = $url -f "ImportFiles" | |
$importfiles_body = "path=$iris_url" | |
$ret = Invoke-RestMethod $importfiles_url -Method POST -Body $importfiles_body | |
"Run parse setup to find out how H2o thinks it should parse the data" | |
$parsesetup_url = $url -f "ParseSetup" | |
$parsesetup_body = 'source_frames=[{0}]' -f $iris_url | |
$ret = Invoke-RestMethod $parsesetup_url -Method Post -Body $parsesetup_body | |
"Parse the data into a real H2o dataframe" | |
$parse_url = $url -f "Parse" | |
$parse_body = $ret | select source_frames,parse_type,separator,number_columns,single_quotes,column_names,column_types,check_header,chunk_size |ConvertTo-FormData | |
$parse_body += "&destination_frame=iris&delete_on_done=true" | |
$ret = Invoke-RestMethod $parse_url -Method Post -Body $parse_body | |
Wait-H2oJob $ret.job.key.URL | |
"Split the data into an 90% training set and a 10% testing set" | |
$splitframe_url = $url -f "SplitFrame" | |
$splitframe_body = "dataset=iris&ratios=[.90,.1]&destination_frames=[train,test]" | |
$ret = invoke-restmethod $splitframe_url -Method Post -Body $splitframe_body | |
wait-H2oJob $ret.key.URL | |
"Train a deep learning model using defaults" | |
$deeplearning_url = $url -f "ModelBuilders/deeplearning" | |
$deeplearning_body = 'training_frame=train&response_column=class&model_id=neural' | |
$ret = invoke-restmethod $deeplearning_url -Method Post -Body $deeplearning_body | |
wait-H2oJob $ret.job.key.URL | |
"Predict against the test data and view MSE to see how well it's doing" | |
$predict_url = $url -f "Predictions/models/neural/frames/test" | |
$ret = invoke-restmethod $predict_url -method POST -Body "predictions_frame=predicted_test_data" | |
$ret.model_metrics |select -expandproperty MSE | |
"Load something you want to predict" | |
@" | |
sepal_len, sepal_wid, petal_len, petal_wid | |
5.1,3.5,1.4,0.15 | |
"@ |out-file -encoding ASCII c:\h2o\predict.csv | |
$importfiles_url = $url -f "ImportFiles" | |
$importfiles_body = "path=c:\h2o\predict.csv" | |
$ret = Invoke-RestMethod $importfiles_url -Method POST -Body $importfiles_body | |
"Run parse setup to find out how H2o thinks it should parse the data" | |
$parsesetup_url = $url -f "ParseSetup" | |
$parsesetup_body = 'source_frames=[{0}]' -f $ret.destination_frames[0] | |
$ret = Invoke-RestMethod $parsesetup_url -Method Post -Body $parsesetup_body | |
"Parse the data into a real H2o dataframe" | |
$parse_url = $url -f "Parse" | |
$parse_body = $ret | select source_frames,parse_type,separator,number_columns,single_quotes,column_names,column_types,check_header,chunk_size |ConvertTo-FormData | |
$parse_body += "&destination_frame=predictme&delete_on_done=true" | |
$ret = Invoke-RestMethod $parse_url -Method Post -Body $parse_body | |
Wait-H2oJob $ret.job.key.URL | |
"Let's leverage the data model we built earlier to predict against this new data frame" | |
$predict_url = $url -f "Predictions/models/neural/frames/predictme" | |
$ret = invoke-restmethod $predict_url -method POST -Body "predictions_frame=predictme_results" | |
"Finally, let's look at the data returned by the prediction" | |
$results_url = $url -f "Frames/predictme_results" | |
$ret = invoke-restmethod $results_url | |
$ret.frames.columns |select label, data | |
<# | |
Copyright (c) 2015 Tome Tanasovski | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment