Complete Machine Learning and Deep Learning With H2O in R

Source file ⇒ CompleteMLDLWith-H2OinR.rmd

Read in Data From Different Sources

getwd()
## [1] "C:/Users/HP/Desktop/CompleteMDLWithH2OinR_MinervaSingh/rfiles"
setwd("C:/Users/HP/Desktop/CompleteMDLWithH2OinR_MinervaSingh/rfiles")

#read in the CSV data  UCL website:
#https://archive.ics.uci.edu/ml/datasets/Wine+Quality

winer1=read.csv("winequality-red.csv",header=T)
#header= T will read in column names as well
head(winer1)
##   fixed.acidity.volatile.acidity.citric.acid.residual.sugar.chlorides.free.sulfur.dioxide.total.sulfur.dioxide.density.pH.sulphates.alcohol.quality
## 1                                                                                                  7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
## 2                                                                                                  7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5
## 3                                                                                               7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5
## 4                                                                                              11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6
## 5                                                                                                  7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
## 6                                                                                                 7.4;0.66;0;1.8;0.075;13;40;0.9978;3.51;0.56;9.4;5
summary(winer1)
##  fixed.acidity.volatile.acidity.citric.acid.residual.sugar.chlorides.free.sulfur.dioxide.total.sulfur.dioxide.density.pH.sulphates.alcohol.quality
##  6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6  :   4                                                                                      
##  7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7   :   4                                                                                      
##  7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 :   4                                                                                      
##  7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6:   4                                                                                      
##  11.5;0.18;0.51;4;0.104;4;23;0.9996;3.28;0.97;10.1;6   :   3                                                                                      
##  6.4;0.64;0.21;1.8;0.081;14;31;0.99689;3.59;0.66;9.8;5 :   3                                                                                      
##  (Other)                                               :1577
winer1=read.csv("winequality-red.csv",header=T,sep=",")
#header= T will read in column names as well
head(winer1)
##   fixed.acidity.volatile.acidity.citric.acid.residual.sugar.chlorides.free.sulfur.dioxide.total.sulfur.dioxide.density.pH.sulphates.alcohol.quality
## 1                                                                                                  7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
## 2                                                                                                  7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5
## 3                                                                                               7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5
## 4                                                                                              11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6
## 5                                                                                                  7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
## 6                                                                                                 7.4;0.66;0;1.8;0.075;13;40;0.9978;3.51;0.56;9.4;5
summary(winer1)
##  fixed.acidity.volatile.acidity.citric.acid.residual.sugar.chlorides.free.sulfur.dioxide.total.sulfur.dioxide.density.pH.sulphates.alcohol.quality
##  6.7;0.46;0.24;1.7;0.077;18;34;0.9948;3.39;0.6;10.6;6  :   4                                                                                      
##  7.2;0.36;0.46;2.1;0.074;24;44;0.99534;3.4;0.85;11;7   :   4                                                                                      
##  7.2;0.695;0.13;2;0.076;12;20;0.99546;3.29;0.54;10.1;5 :   4                                                                                      
##  7.5;0.51;0.02;1.7;0.084;13;31;0.99538;3.36;0.54;10.5;6:   4                                                                                      
##  11.5;0.18;0.51;4;0.104;4;23;0.9996;3.28;0.97;10.1;6   :   3                                                                                      
##  6.4;0.64;0.21;1.8;0.081;14;31;0.99689;3.59;0.66;9.8;5 :   3                                                                                      
##  (Other)                                               :1577
#specify the correct seperator
winer=read.table("winequality-red.csv",header=T,sep=";")
#header= T will read in column names as well
head(winer)
##   fixed.acidity volatile.acidity citric.acid residual.sugar chlorides
## 1           7.4             0.70        0.00            1.9     0.076
## 2           7.8             0.88        0.00            2.6     0.098
## 3           7.8             0.76        0.04            2.3     0.092
## 4          11.2             0.28        0.56            1.9     0.075
## 5           7.4             0.70        0.00            1.9     0.076
## 6           7.4             0.66        0.00            1.8     0.075
##   free.sulfur.dioxide total.sulfur.dioxide density  pH sulphates alcohol
## 1                  11                   34       1 3.5      0.56     9.4
## 2                  25                   67       1 3.2      0.68     9.8
## 3                  15                   54       1 3.3      0.65     9.8
## 4                  17                   60       1 3.2      0.58     9.8
## 5                  11                   34       1 3.5      0.56     9.4
## 6                  13                   40       1 3.5      0.56     9.4
##   quality
## 1       5
## 2       5
## 3       5
## 4       6
## 5       5
## 6       5
summary(winer)
##  fixed.acidity  volatile.acidity  citric.acid   residual.sugar   chlorides   
##  Min.   : 4.6   Min.   :0.12     Min.   :0.00   Min.   : 0.9   Min.   :0.01  
##  1st Qu.: 7.1   1st Qu.:0.39     1st Qu.:0.09   1st Qu.: 1.9   1st Qu.:0.07  
##  Median : 7.9   Median :0.52     Median :0.26   Median : 2.2   Median :0.08  
##  Mean   : 8.3   Mean   :0.53     Mean   :0.27   Mean   : 2.5   Mean   :0.09  
##  3rd Qu.: 9.2   3rd Qu.:0.64     3rd Qu.:0.42   3rd Qu.: 2.6   3rd Qu.:0.09  
##  Max.   :15.9   Max.   :1.58     Max.   :1.00   Max.   :15.5   Max.   :0.61  
##  free.sulfur.dioxide total.sulfur.dioxide    density           pH     
##  Min.   : 1          Min.   :  6          Min.   :0.99   Min.   :2.7  
##  1st Qu.: 7          1st Qu.: 22          1st Qu.:1.00   1st Qu.:3.2  
##  Median :14          Median : 38          Median :1.00   Median :3.3  
##  Mean   :16          Mean   : 46          Mean   :1.00   Mean   :3.3  
##  3rd Qu.:21          3rd Qu.: 62          3rd Qu.:1.00   3rd Qu.:3.4  
##  Max.   :72          Max.   :289          Max.   :1.00   Max.   :4.0  
##    sulphates       alcohol        quality   
##  Min.   :0.33   Min.   : 8.4   Min.   :3.0  
##  1st Qu.:0.55   1st Qu.: 9.5   1st Qu.:5.0  
##  Median :0.62   Median :10.2   Median :6.0  
##  Mean   :0.66   Mean   :10.4   Mean   :5.6  
##  3rd Qu.:0.73   3rd Qu.:11.1   3rd Qu.:6.0  
##  Max.   :2.00   Max.   :14.9   Max.   :8.0
##Read in excel data
#excel
#summary(boston1)
library(readxl)
dfb <- read_excel("boston1.xls")
head(dfb)
## # A tibble: 6 x 10
##      MV INDUS   NOX    RM   TAX    PT LSTAT X__1  X__2  X__3                    
##   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <lgl> <chr>                   
## 1  24    2.31  53.8  6.58   296  15.3  4.98 NA    NA    Subset of Boston housin~
## 2  21.6  7.07  46.9  6.42   242  17.8  9.14 NA    NA    data of Harrison and Ru~
## 3  34.7  7.07  46.9  7.18   242  17.8  4.03 NA    NA    (1978).  Each case is o~
## 4  33.4  2.18  45.8  7.00   222  18.7  2.94 NA    NA    Census tract in the Bos~
## 5  36.2  2.18  45.8  7.15   222  18.7  5.33 NA    NA    <NA>                    
## 6  28.7  2.18  45.8  6.43   222  18.7  5.21 NA    NA    <NA>
summary(dfb)
##        MV         INDUS           NOX           RM           TAX     
##  Min.   : 5   Min.   : 0.5   Min.   :38   Min.   :3.6   Min.   :187  
##  1st Qu.:17   1st Qu.: 5.2   1st Qu.:45   1st Qu.:5.9   1st Qu.:279  
##  Median :21   Median : 9.7   Median :54   Median :6.2   Median :330  
##  Mean   :23   Mean   :11.1   Mean   :55   Mean   :6.3   Mean   :408  
##  3rd Qu.:25   3rd Qu.:18.1   3rd Qu.:62   3rd Qu.:6.6   3rd Qu.:666  
##  Max.   :50   Max.   :27.7   Max.   :87   Max.   :8.8   Max.   :711  
##        PT           LSTAT      X__1           X__2             X__3          
##  Min.   :12.6   Min.   : 2   Mode:logical   Mode:logical   Length:506        
##  1st Qu.:17.4   1st Qu.: 7   NA's:506       NA's:506       Class :character  
##  Median :19.1   Median :11                                 Mode  :character  
##  Mean   :18.5   Mean   :13                                                   
##  3rd Qu.:20.2   3rd Qu.:17                                                   
##  Max.   :22.0   Max.   :38
##################################################################
### Read in data from Wikipedia HTML tables

library(rvest)
#Summer olympics medal tally

url <- "https://en.wikipedia.org/wiki/2016_Summer_Olympics_medal_table"



medal_tally <- url %>% read_html() %>% 
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[2]') %>% html_table(fill=TRUE)
## copy xpath
## //*[@id="mw-content-text"]/div/table[2]
# //*[@id="mw-content-text"]/div/table[2]

medal_tally <- medal_tally[[1]]
head(medal_tally)
##   Rank                 NOC Gold Silver Bronze Total
## 1    1 United States (USA)   46     37     38   121
## 2    2 Great Britain (GBR)   27     23     17    67
## 3    3         China (CHN)   26     18     26    70
## 4    4        Russia (RUS)   19     17     20    56
## 5    5       Germany (GER)   17     10     15    42
## 6    6         Japan (JPN)   12      8     21    41
#WHS Sites in the UK

url2="https://en.wikipedia.org/wiki/List_of_World_Heritage_Sites_in_the_United_Kingdom_and_the_British_Overseas_Territories"


whsuk <- url2 %>% read_html() %>% 
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[3]') %>% html_table(fill=TRUE)

whsuk <- whsuk[[1]]
head(whsuk)
##                                                                 Name Image
## 1                                     Blaenavon Industrial Landscape    NA
## 2                                                    Blenheim Palace    NA
## 3 Canterbury Cathedral, St Augustine's Abbey, and St Martin's Church    NA
## 4                   Castles and Town Walls of King Edward in Gwynedd    NA
## 5                                                       City of Bath    NA
## 6                           Cornwall and West Devon Mining Landscape    NA
##                                                                                                                                                                                                   Location
## 1                                                                     Blaenavon,  Wales51°47'N 3°05'W<U+FEFF> / <U+FEFF>51.78°N 3.08°W<U+FEFF> / 51.78; -3.08<U+FEFF> (Blaenavon Industrial Landscape)[14]
## 2                                             Woodstock, Oxfordshire,  England51°50'28<U+2033>N 1°21'40<U+2033>W<U+FEFF> / <U+FEFF>51.841°N 1.361°W<U+FEFF> / 51.841; -1.361<U+FEFF> (Blenheim Palace)[15]
## 3                         Canterbury, Kent,  England51°17'N 1°05'E<U+FEFF> / <U+FEFF>51.28°N 1.08°E<U+FEFF> / 51.28; 1.08<U+FEFF> (Canterbury Cathedral, St Augustine's Abbey, and St Martin's Church)[16]
## 4 Conwy, Isle of Anglesey and Gwynedd,  Wales53°08'20<U+2033>N 4°16'34<U+2033>W<U+FEFF> / <U+FEFF>53.139°N 4.276°W<U+FEFF> / 53.139; -4.276<U+FEFF> (Castles and Town Walls of King Edward in Gwynedd)[19]
## 5                                                        Bath, Somerset,  England51°22'48<U+2033>N 2°21'36<U+2033>W<U+FEFF> / <U+FEFF>51.380°N 2.360°W<U+FEFF> / 51.380; -2.360<U+FEFF> (City of Bath)[21]
## 6                                                Cornwall and Devon,  England50°08'N 5°23'W<U+FEFF> / <U+FEFF>50.13°N 5.38°W<U+FEFF> / 50.13; -5.38<U+FEFF> (Cornwall and West Devon Mining Landscape)[22]
##                          Date                 UNESCO data
## 1            19th century[14]       984; 2000;iii, iv[14]
## 2               1705–1722[15]        425; 1987;ii, iv[15]
## 3            11th century[16]     496; 1988;i, ii, vi[16]
## 4     13th–14th centuries[19]    374; 1986;i, iii, iv[19]
## 5      1st–19th centuries[21]     428; 1987;i, ii, iv[21]
## 6 18th and 19th centuries[22] 1,215; 2006;ii, iii, iv[22]
##                                                                                                                                                                                                                                                                                                                                                                                               Description
## 1                                                                                                       In the 19th century, Wales was the world's foremost producer of iron and coal. Blaenavon is an example of the landscape created by the industrial processes associated with the production of these materials. The site includes quarries, public buildings, workers' housing, and a railway.[14]
## 2                        Blenheim Palace, the residence of John Churchill, 1st Duke of Marlborough, was designed by architects John Vanbrugh and Nicholas Hawksmoor. The associated park was landscaped by Capability Brown. The palace celebrated victory over the French and is significant for establishing English Romantic Architecture as a separate entity from French Classical Architecture.[15]
## 3                                                                                                  St Martin's Church is the oldest church in England. The church and St Augustine's Abbey were founded during the early stages of the introduction of Christianity to the Anglo-Saxons. The cathedral exhibits Romanesque and Gothic architecture, and is the seat of the Church of England.[16][17][18]
## 4 During the reign of Edward I of England (1272–1307), a series of castles was constructed in Wales with the purpose of subduing the population and establishing English colonies in Wales. The World Heritage Site covers many castles including Beaumaris, Caernarfon, Conwy, and Harlech. The castles of Edward I are considered the pinnacle of military architecture by military historians.[19][20]
## 5                                                                                                                                                             Founded by the Romans as a spa, an important centre of the wool industry in the medieval period, and a spa town in the 18th century, Bath has a varied history. The city is preserved for its Roman remains and Palladian architecture.[21]
## 6                                                                                                                            Tin and copper mining in Devon and Cornwall boomed in the 18th and 19th centuries, and at its peak the area produced two-thirds of the world's copper. The techniques and technology involved in deep mining developed in Devon and Cornwall were used around the world.[22]
#################################################################
### JSON-->Javascript Object Notation

library(rjson)
#name/url of json file
json_file <- "http://api.worldbank.org/country?per_page=10&region=OED&lendingtype=LNX&format=json"

#json data is stored in json_data
json_data <- fromJSON(file=json_file)

#you can see that this json file has two objects in the outer most list
json_data[[1]]
## $page
## [1] 1
## 
## $pages
## [1] 4
## 
## $per_page
## [1] "10"
## 
## $total
## [1] 32
json_data[[2]]
## [[1]]
## [[1]]$id
## [1] "AUS"
## 
## [[1]]$iso2Code
## [1] "AU"
## 
## [[1]]$name
## [1] "Australia"
## 
## [[1]]$region
## [[1]]$region$id
## [1] "EAS"
## 
## [[1]]$region$value
## [1] "East Asia & Pacific"
## 
## 
## [[1]]$adminregion
## [[1]]$adminregion$id
## [1] ""
## 
## [[1]]$adminregion$value
## [1] ""
## 
## 
## [[1]]$incomeLevel
## [[1]]$incomeLevel$id
## [1] "HIC"
## 
## [[1]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[1]]$lendingType
## [[1]]$lendingType$id
## [1] "LNX"
## 
## [[1]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[1]]$capitalCity
## [1] "Canberra"
## 
## [[1]]$longitude
## [1] "149.129"
## 
## [[1]]$latitude
## [1] "-35.282"
## 
## 
## [[2]]
## [[2]]$id
## [1] "AUT"
## 
## [[2]]$iso2Code
## [1] "AT"
## 
## [[2]]$name
## [1] "Austria"
## 
## [[2]]$region
## [[2]]$region$id
## [1] "ECS"
## 
## [[2]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[2]]$adminregion
## [[2]]$adminregion$id
## [1] ""
## 
## [[2]]$adminregion$value
## [1] ""
## 
## 
## [[2]]$incomeLevel
## [[2]]$incomeLevel$id
## [1] "HIC"
## 
## [[2]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[2]]$lendingType
## [[2]]$lendingType$id
## [1] "LNX"
## 
## [[2]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[2]]$capitalCity
## [1] "Vienna"
## 
## [[2]]$longitude
## [1] "16.3798"
## 
## [[2]]$latitude
## [1] "48.2201"
## 
## 
## [[3]]
## [[3]]$id
## [1] "BEL"
## 
## [[3]]$iso2Code
## [1] "BE"
## 
## [[3]]$name
## [1] "Belgium"
## 
## [[3]]$region
## [[3]]$region$id
## [1] "ECS"
## 
## [[3]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[3]]$adminregion
## [[3]]$adminregion$id
## [1] ""
## 
## [[3]]$adminregion$value
## [1] ""
## 
## 
## [[3]]$incomeLevel
## [[3]]$incomeLevel$id
## [1] "HIC"
## 
## [[3]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[3]]$lendingType
## [[3]]$lendingType$id
## [1] "LNX"
## 
## [[3]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[3]]$capitalCity
## [1] "Brussels"
## 
## [[3]]$longitude
## [1] "4.36761"
## 
## [[3]]$latitude
## [1] "50.8371"
## 
## 
## [[4]]
## [[4]]$id
## [1] "CAN"
## 
## [[4]]$iso2Code
## [1] "CA"
## 
## [[4]]$name
## [1] "Canada"
## 
## [[4]]$region
## [[4]]$region$id
## [1] "NAC"
## 
## [[4]]$region$value
## [1] "North America"
## 
## 
## [[4]]$adminregion
## [[4]]$adminregion$id
## [1] ""
## 
## [[4]]$adminregion$value
## [1] ""
## 
## 
## [[4]]$incomeLevel
## [[4]]$incomeLevel$id
## [1] "HIC"
## 
## [[4]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[4]]$lendingType
## [[4]]$lendingType$id
## [1] "LNX"
## 
## [[4]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[4]]$capitalCity
## [1] "Ottawa"
## 
## [[4]]$longitude
## [1] "-75.6919"
## 
## [[4]]$latitude
## [1] "45.4215"
## 
## 
## [[5]]
## [[5]]$id
## [1] "CHE"
## 
## [[5]]$iso2Code
## [1] "CH"
## 
## [[5]]$name
## [1] "Switzerland"
## 
## [[5]]$region
## [[5]]$region$id
## [1] "ECS"
## 
## [[5]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[5]]$adminregion
## [[5]]$adminregion$id
## [1] ""
## 
## [[5]]$adminregion$value
## [1] ""
## 
## 
## [[5]]$incomeLevel
## [[5]]$incomeLevel$id
## [1] "HIC"
## 
## [[5]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[5]]$lendingType
## [[5]]$lendingType$id
## [1] "LNX"
## 
## [[5]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[5]]$capitalCity
## [1] "Bern"
## 
## [[5]]$longitude
## [1] "7.44821"
## 
## [[5]]$latitude
## [1] "46.948"
## 
## 
## [[6]]
## [[6]]$id
## [1] "CZE"
## 
## [[6]]$iso2Code
## [1] "CZ"
## 
## [[6]]$name
## [1] "Czech Republic"
## 
## [[6]]$region
## [[6]]$region$id
## [1] "ECS"
## 
## [[6]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[6]]$adminregion
## [[6]]$adminregion$id
## [1] ""
## 
## [[6]]$adminregion$value
## [1] ""
## 
## 
## [[6]]$incomeLevel
## [[6]]$incomeLevel$id
## [1] "HIC"
## 
## [[6]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[6]]$lendingType
## [[6]]$lendingType$id
## [1] "LNX"
## 
## [[6]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[6]]$capitalCity
## [1] "Prague"
## 
## [[6]]$longitude
## [1] "14.4205"
## 
## [[6]]$latitude
## [1] "50.0878"
## 
## 
## [[7]]
## [[7]]$id
## [1] "DEU"
## 
## [[7]]$iso2Code
## [1] "DE"
## 
## [[7]]$name
## [1] "Germany"
## 
## [[7]]$region
## [[7]]$region$id
## [1] "ECS"
## 
## [[7]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[7]]$adminregion
## [[7]]$adminregion$id
## [1] ""
## 
## [[7]]$adminregion$value
## [1] ""
## 
## 
## [[7]]$incomeLevel
## [[7]]$incomeLevel$id
## [1] "HIC"
## 
## [[7]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[7]]$lendingType
## [[7]]$lendingType$id
## [1] "LNX"
## 
## [[7]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[7]]$capitalCity
## [1] "Berlin"
## 
## [[7]]$longitude
## [1] "13.4115"
## 
## [[7]]$latitude
## [1] "52.5235"
## 
## 
## [[8]]
## [[8]]$id
## [1] "DNK"
## 
## [[8]]$iso2Code
## [1] "DK"
## 
## [[8]]$name
## [1] "Denmark"
## 
## [[8]]$region
## [[8]]$region$id
## [1] "ECS"
## 
## [[8]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[8]]$adminregion
## [[8]]$adminregion$id
## [1] ""
## 
## [[8]]$adminregion$value
## [1] ""
## 
## 
## [[8]]$incomeLevel
## [[8]]$incomeLevel$id
## [1] "HIC"
## 
## [[8]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[8]]$lendingType
## [[8]]$lendingType$id
## [1] "LNX"
## 
## [[8]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[8]]$capitalCity
## [1] "Copenhagen"
## 
## [[8]]$longitude
## [1] "12.5681"
## 
## [[8]]$latitude
## [1] "55.6763"
## 
## 
## [[9]]
## [[9]]$id
## [1] "ESP"
## 
## [[9]]$iso2Code
## [1] "ES"
## 
## [[9]]$name
## [1] "Spain"
## 
## [[9]]$region
## [[9]]$region$id
## [1] "ECS"
## 
## [[9]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[9]]$adminregion
## [[9]]$adminregion$id
## [1] ""
## 
## [[9]]$adminregion$value
## [1] ""
## 
## 
## [[9]]$incomeLevel
## [[9]]$incomeLevel$id
## [1] "HIC"
## 
## [[9]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[9]]$lendingType
## [[9]]$lendingType$id
## [1] "LNX"
## 
## [[9]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[9]]$capitalCity
## [1] "Madrid"
## 
## [[9]]$longitude
## [1] "-3.70327"
## 
## [[9]]$latitude
## [1] "40.4167"
## 
## 
## [[10]]
## [[10]]$id
## [1] "EST"
## 
## [[10]]$iso2Code
## [1] "EE"
## 
## [[10]]$name
## [1] "Estonia"
## 
## [[10]]$region
## [[10]]$region$id
## [1] "ECS"
## 
## [[10]]$region$value
## [1] "Europe & Central Asia"
## 
## 
## [[10]]$adminregion
## [[10]]$adminregion$id
## [1] ""
## 
## [[10]]$adminregion$value
## [1] ""
## 
## 
## [[10]]$incomeLevel
## [[10]]$incomeLevel$id
## [1] "HIC"
## 
## [[10]]$incomeLevel$value
## [1] "High income"
## 
## 
## [[10]]$lendingType
## [[10]]$lendingType$id
## [1] "LNX"
## 
## [[10]]$lendingType$value
## [1] "Not classified"
## 
## 
## [[10]]$capitalCity
## [1] "Tallinn"
## 
## [[10]]$longitude
## [1] "24.7586"
## 
## [[10]]$latitude
## [1] "59.4392"
#you can access any particular object from the json data as shown below
d3 <- lapply(json_data[[2]], function(x) c(x["id"], x["iso2Code"]))

d3 <- do.call(rbind, d3)
d3
##       id    iso2Code
##  [1,] "AUS" "AU"    
##  [2,] "AUT" "AT"    
##  [3,] "BEL" "BE"    
##  [4,] "CAN" "CA"    
##  [5,] "CHE" "CH"    
##  [6,] "CZE" "CZ"    
##  [7,] "DEU" "DE"    
##  [8,] "DNK" "DK"    
##  [9,] "ESP" "ES"    
## [10,] "EST" "EE"
d4 <- lapply(json_data[[2]], function(x) c(x["id"], x["iso2Code"], x$region["id"], x$region["value"], x["capitalCity"]))
d4 <- do.call(rbind, d4)
d4
##       id    iso2Code id    value                   capitalCity 
##  [1,] "AUS" "AU"     "EAS" "East Asia & Pacific"   "Canberra"  
##  [2,] "AUT" "AT"     "ECS" "Europe & Central Asia" "Vienna"    
##  [3,] "BEL" "BE"     "ECS" "Europe & Central Asia" "Brussels"  
##  [4,] "CAN" "CA"     "NAC" "North America"         "Ottawa"    
##  [5,] "CHE" "CH"     "ECS" "Europe & Central Asia" "Bern"      
##  [6,] "CZE" "CZ"     "ECS" "Europe & Central Asia" "Prague"    
##  [7,] "DEU" "DE"     "ECS" "Europe & Central Asia" "Berlin"    
##  [8,] "DNK" "DK"     "ECS" "Europe & Central Asia" "Copenhagen"
##  [9,] "ESP" "ES"     "ECS" "Europe & Central Asia" "Madrid"    
## [10,] "EST" "EE"     "ECS" "Europe & Central Asia" "Tallinn"
#other example


json_file <- "skorea.json"
#json data is stored in json_data

json_data <- fromJSON(file=json_file)

#you can access your data as simply shown below
json_data[[1]]
## $Description
## [1] ""
## 
## $Image
## [1] "/wiki/File:MuryeongsTomb.jpg"
## 
## $Criteria
## [1] "Cultural: (ii)(iii)"
## 
## $Site
## [1] "Baekje Historic Areas"
## 
## $`Area ha (acre)`
## [1] "135 (330)"
## 
## $Location
## [1] "South Chungcheong, North Jeolla"
## 
## $Year
## [1] "2015"
json_data[[2]]
## $Description
## [1] ""
## 
## $Image
## [1] "/wiki/File:Korea-Gwangju-Gochang_Dolmens_5350-06.JPG"
## 
## $Criteria
## [1] "Cultural: (iii)"
## 
## $Site
## [1] "Gochang, Hwasun and Ganghwa Dolmen Sites"
## 
## $`Area ha (acre)`
## [1] ""
## 
## $Location
## [1] "Incheon, North Jeolla, South Jeolla"
## 
## $Year
## [1] "2000"
json_data[[5]]
## $Description
## [1] ""
## 
## $Image
## [1] "/wiki/File:Haeinsa_Temple_(6222053899).jpg"
## 
## $Criteria
## [1] "Cultural: (iv)(vi)"
## 
## $Site
## [1] "Haeinsa Temple Janggyeong Panjeon, the Depositories for the Tripitaka Koreana Woodblocks"
## 
## $`Area ha (acre)`
## [1] ""
## 
## $Location
## [1] "South Gyeongsang"
## 
## $Year
## [1] "1995"
#or you can extract some usefull data
d <- lapply(json_data, function(x) c(x['Image'], x['Criteria'], x['Site'], x['Area ha (acre)']))
d <- do.call(rbind, d)
d
##       Image                                                        
##  [1,] "/wiki/File:MuryeongsTomb.jpg"                               
##  [2,] "/wiki/File:Korea-Gwangju-Gochang_Dolmens_5350-06.JPG"       
##  [3,] "/wiki/File:Juhamnu,_Changdeokgung_-_Seoul,_Korea.JPG"       
##  [4,] "/wiki/File:Korea-Gyeongju-Bunhwangsa-Lanterns-03.jpg"       
##  [5,] "/wiki/File:Haeinsa_Temple_(6222053899).jpg"                 
##  [6,] "/wiki/File:Hahoe_8784.jpg"                                  
##  [7,] "/wiki/File:Hwaseong2.jpg"                                   
##  [8,] "/wiki/File:KOCIS_Halla_Mountain_in_Jeju-do_(6387785543).jpg"
##  [9,] "/wiki/File:Chongmyo_repository_(1509268349).jpg"            
## [10,] "/wiki/File:Khitai5.jpg"                                     
## [11,] "/wiki/File:Sejong_tomb_1.jpg"                               
## [12,] "/wiki/File:Bulguk_Tempel.jpg"                               
##       Criteria                 
##  [1,] "Cultural: (ii)(iii)"    
##  [2,] "Cultural: (iii)"        
##  [3,] "Cultural: (ii)(iii)(iv)"
##  [4,] "Cultural: (ii)(iii)"    
##  [5,] "Cultural: (iv)(vi)"     
##  [6,] "Cultural: (iii)(iv)"    
##  [7,] "Cultural: (ii)(iii)"    
##  [8,] "Natural: (vii)(viii)"   
##  [9,] "Cultural: (iv)"         
## [10,] "Cultural: (ii)(iv)"     
## [11,] "Cultural: (iii)(iv)(vi)"
## [12,] "Cultural: (i)(iv)"      
##       Site                                                                                      
##  [1,] "Baekje Historic Areas"                                                                   
##  [2,] "Gochang, Hwasun and Ganghwa Dolmen Sites"                                                
##  [3,] "Changdeokgung Palace Complex"                                                            
##  [4,] "Gyeongju Historic Areas"                                                                 
##  [5,] "Haeinsa Temple Janggyeong Panjeon, the Depositories for the Tripitaka Koreana Woodblocks"
##  [6,] "Historic Villages of Korea: Hahoe and Yangdong"                                          
##  [7,] "Hwaseong Fortress"                                                                       
##  [8,] "Jeju Volcanic Island and Lava Tubes"                                                     
##  [9,] "Jongmyo Shrine"                                                                          
## [10,] "Namhansanseong"                                                                          
## [11,] "Royal Tombs of the Joseon Dynasty"                                                       
## [12,] "Seokguram Grotto and Bulguksa Temple"                                                    
##       Area ha (acre)  
##  [1,] "135 (330)"     
##  [2,] ""              
##  [3,] ""              
##  [4,] "2,880 (7,100)" 
##  [5,] ""              
##  [6,] "600 (1,500)"   
##  [7,] ""              
##  [8,] "9,475 (23,410)"
##  [9,] "19 (47)"       
## [10,] "409 (1,010)"   
## [11,] "1,891 (4,670)" 
## [12,] ""
#you can access any data you want from this table like shown below
col(d)
##       [,1] [,2] [,3] [,4]
##  [1,]    1    2    3    4
##  [2,]    1    2    3    4
##  [3,]    1    2    3    4
##  [4,]    1    2    3    4
##  [5,]    1    2    3    4
##  [6,]    1    2    3    4
##  [7,]    1    2    3    4
##  [8,]    1    2    3    4
##  [9,]    1    2    3    4
## [10,]    1    2    3    4
## [11,]    1    2    3    4
## [12,]    1    2    3    4
d[,1]
## [[1]]
## [1] "/wiki/File:MuryeongsTomb.jpg"
## 
## [[2]]
## [1] "/wiki/File:Korea-Gwangju-Gochang_Dolmens_5350-06.JPG"
## 
## [[3]]
## [1] "/wiki/File:Juhamnu,_Changdeokgung_-_Seoul,_Korea.JPG"
## 
## [[4]]
## [1] "/wiki/File:Korea-Gyeongju-Bunhwangsa-Lanterns-03.jpg"
## 
## [[5]]
## [1] "/wiki/File:Haeinsa_Temple_(6222053899).jpg"
## 
## [[6]]
## [1] "/wiki/File:Hahoe_8784.jpg"
## 
## [[7]]
## [1] "/wiki/File:Hwaseong2.jpg"
## 
## [[8]]
## [1] "/wiki/File:KOCIS_Halla_Mountain_in_Jeju-do_(6387785543).jpg"
## 
## [[9]]
## [1] "/wiki/File:Chongmyo_repository_(1509268349).jpg"
## 
## [[10]]
## [1] "/wiki/File:Khitai5.jpg"
## 
## [[11]]
## [1] "/wiki/File:Sejong_tomb_1.jpg"
## 
## [[12]]
## [1] "/wiki/File:Bulguk_Tempel.jpg"
d[1,]
## $Image
## [1] "/wiki/File:MuryeongsTomb.jpg"
## 
## $Criteria
## [1] "Cultural: (ii)(iii)"
## 
## $Site
## [1] "Baekje Historic Areas"
## 
## $`Area ha (acre)`
## [1] "135 (330)"
######################################################################

library(h2o)

#Start up a 1-node H2O server on your local machine, and allow it to use all CPU cores and up to 2GB of memory:
#
h2o.init(nthreads=-1, max_mem_size="2G")
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         6 minutes 40 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.31 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
h2o.removeAll() ## clean slate - just in case the cluster was already running


glass = h2o.importFile("glassClass.csv")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
head(glass)
##    RI Na  Mg  Al Si    K  Ca Ba   Fe Type
## 1 1.5 14 4.5 1.1 72 0.06 8.8  0 0.00    1
## 2 1.5 14 3.6 1.4 73 0.48 7.8  0 0.00    1
## 3 1.5 14 3.5 1.5 73 0.39 7.8  0 0.00    1
## 4 1.5 13 3.7 1.3 73 0.57 8.2  0 0.00    1
## 5 1.5 13 3.6 1.2 73 0.55 8.1  0 0.00    1
## 6 1.5 13 3.6 1.6 73 0.64 8.1  0 0.26    1
summary(glass,exact_quantiles=TRUE)
##  RI              Na              Mg              Al             
##  Min.   :1.511   Min.   :10.73   Min.   :0.000   Min.   :0.290  
##  1st Qu.:1.517   1st Qu.:12.91   1st Qu.:2.115   1st Qu.:1.190  
##  Median :1.518   Median :13.30   Median :3.480   Median :1.360  
##  Mean   :1.518   Mean   :13.41   Mean   :2.685   Mean   :1.445  
##  3rd Qu.:1.519   3rd Qu.:13.82   3rd Qu.:3.600   3rd Qu.:1.630  
##  Max.   :1.534   Max.   :17.38   Max.   :4.490   Max.   :3.500  
##  Si              K                Ca               Ba             
##  Min.   :69.81   Min.   :0.0000   Min.   : 5.430   Min.   :0.000  
##  1st Qu.:72.28   1st Qu.:0.1225   1st Qu.: 8.240   1st Qu.:0.000  
##  Median :72.79   Median :0.5550   Median : 8.600   Median :0.000  
##  Mean   :72.65   Mean   :0.4971   Mean   : 8.957   Mean   :0.175  
##  3rd Qu.:73.09   3rd Qu.:0.6100   3rd Qu.: 9.172   3rd Qu.:0.000  
##  Max.   :75.41   Max.   :6.2100   Max.   :16.190   Max.   :3.150  
##  Fe                Type          
##  Min.   :0.00000   Min.   :1.00  
##  1st Qu.:0.00000   1st Qu.:1.00  
##  Median :0.00000   Median :2.00  
##  Mean   :0.05701   Mean   :2.78  
##  3rd Qu.:0.10000   3rd Qu.:3.00  
##  Max.   :0.51000   Max.   :7.00
h2o.table(glass$K)
##      K Counts
## 1 0.00     30
## 2 0.02      1
## 3 0.03      1
## 4 0.04      2
## 5 0.05      1
## 6 0.06      4
## 
## [65 rows x 2 columns]
##split dataset

g.split = h2o.splitFrame(data = glass,ratios = 0.75)
g.train = g.split[[1]]#75%
g.test = g.split[[2]]

Data Preprocessing

Removing NAs

##Randomly distributed NAs
data(airquality)
str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6
summary(airquality)
##      Ozone        Solar.R         Wind           Temp        Month  
##  Min.   :  1   Min.   :  7   Min.   : 1.7   Min.   :56   Min.   :5  
##  1st Qu.: 18   1st Qu.:116   1st Qu.: 7.4   1st Qu.:72   1st Qu.:6  
##  Median : 32   Median :205   Median : 9.7   Median :79   Median :7  
##  Mean   : 42   Mean   :186   Mean   :10.0   Mean   :78   Mean   :7  
##  3rd Qu.: 63   3rd Qu.:259   3rd Qu.:11.5   3rd Qu.:85   3rd Qu.:8  
##  Max.   :168   Max.   :334   Max.   :20.7   Max.   :97   Max.   :9  
##  NA's   :37    NA's   :7                                            
##       Day      
##  Min.   : 1.0  
##  1st Qu.: 8.0  
##  Median :16.0  
##  Mean   :15.8  
##  3rd Qu.:23.0  
##  Max.   :31.0  
## 
aq=na.omit(airquality) #remove rows containing NAs
head(aq)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 7    23     299  8.6   65     5   7
## 8    19      99 13.8   59     5   8
summary(aq)
##      Ozone        Solar.R         Wind           Temp        Month    
##  Min.   :  1   Min.   :  7   Min.   : 2.3   Min.   :57   Min.   :5.0  
##  1st Qu.: 18   1st Qu.:114   1st Qu.: 7.4   1st Qu.:71   1st Qu.:6.0  
##  Median : 31   Median :207   Median : 9.7   Median :79   Median :7.0  
##  Mean   : 42   Mean   :185   Mean   : 9.9   Mean   :78   Mean   :7.2  
##  3rd Qu.: 62   3rd Qu.:256   3rd Qu.:11.5   3rd Qu.:84   3rd Qu.:9.0  
##  Max.   :168   Max.   :334   Max.   :20.7   Max.   :97   Max.   :9.0  
##       Day      
##  Min.   : 1.0  
##  1st Qu.: 9.0  
##  Median :16.0  
##  Mean   :15.9  
##  3rd Qu.:22.5  
##  Max.   :31.0
aq2=airquality[complete.cases(airquality), ] #only retain non-NA rows
head(aq2)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 7    23     299  8.6   65     5   7
## 8    19      99 13.8   59     5   8
summary(aq2)
##      Ozone        Solar.R         Wind           Temp        Month    
##  Min.   :  1   Min.   :  7   Min.   : 2.3   Min.   :57   Min.   :5.0  
##  1st Qu.: 18   1st Qu.:114   1st Qu.: 7.4   1st Qu.:71   1st Qu.:6.0  
##  Median : 31   Median :207   Median : 9.7   Median :79   Median :7.0  
##  Mean   : 42   Mean   :185   Mean   : 9.9   Mean   :78   Mean   :7.2  
##  3rd Qu.: 62   3rd Qu.:256   3rd Qu.:11.5   3rd Qu.:84   3rd Qu.:9.0  
##  Max.   :168   Max.   :334   Max.   :20.7   Max.   :97   Max.   :9.0  
##       Day      
##  Min.   : 1.0  
##  1st Qu.: 9.0  
##  Median :16.0  
##  Mean   :15.9  
##  3rd Qu.:22.5  
##  Max.   :31.0
## replace NAs with 0
aqty=airquality

aqty[is.na(aqty)]<-0
head(aqty)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5     0       0 14.3   56     5   5
## 6    28       0 14.9   66     5   6
summary(aqty)
##      Ozone        Solar.R         Wind           Temp        Month  
##  Min.   :  0   Min.   :  0   Min.   : 1.7   Min.   :56   Min.   :5  
##  1st Qu.:  4   1st Qu.: 95   1st Qu.: 7.4   1st Qu.:72   1st Qu.:6  
##  Median : 21   Median :194   Median : 9.7   Median :79   Median :7  
##  Mean   : 32   Mean   :177   Mean   :10.0   Mean   :78   Mean   :7  
##  3rd Qu.: 46   3rd Qu.:256   3rd Qu.:11.5   3rd Qu.:85   3rd Qu.:8  
##  Max.   :168   Max.   :334   Max.   :20.7   Max.   :97   Max.   :9  
##       Day      
##  Min.   : 1.0  
##  1st Qu.: 8.0  
##  Median :16.0  
##  Mean   :15.8  
##  3rd Qu.:23.0  
##  Max.   :31.0
## replcae missing values with average values

meanOzone=mean(airquality$Ozone,na.rm=T)
# remove NAs while computing mean of Ozone
#with na mean value will be na

aqty.fix=ifelse(is.na(airquality$Ozone),meanOzone,airquality$Ozone)
summary(aqty.fix)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1      21      42      42      46     168
##visualize the patterns of NAs
library(mice)
aqty2=airquality
md.pattern(aqty2)

##     Wind Temp Month Day Solar.R Ozone   
## 111    1    1     1   1       1     1  0
## 35     1    1     1   1       1     0  1
## 5      1    1     1   1       0     1  1
## 2      1    1     1   1       0     0  2
##        0    0     0   0       7    37 44
#111 observations with no values

library(VIM) #visualize the pattern of NAs
mp <- aggr(aqty2, col=c('navyblue','yellow'),
                    numbers=TRUE, sortVars=TRUE,
                    labels=names(aqty2), cex.axis=.7,
                    gap=3, ylab=c("Missing data","Pattern"))

## 
##  Variables sorted by number of missings: 
##  Variable Count
##     Ozone 0.242
##   Solar.R 0.046
##      Wind 0.000
##      Temp 0.000
##     Month 0.000
##       Day 0.000
#72.5% observations in the entire data have no missing values
#22.9% missing values in Ozone

#impute
#500 iterataions of predictive mean mapping for imputing
#5 datasets
im_aqty<- mice(aqty2, m=5, maxit = 50, method = 'pmm', seed = 500)
## 
##  iter imp variable
##   1   1  Ozone  Solar.R
##   1   2  Ozone  Solar.R
##   1   3  Ozone  Solar.R
##   1   4  Ozone  Solar.R
##   1   5  Ozone  Solar.R
##   2   1  Ozone  Solar.R
##   2   2  Ozone  Solar.R
##   2   3  Ozone  Solar.R
##   2   4  Ozone  Solar.R
##   2   5  Ozone  Solar.R
##   3   1  Ozone  Solar.R
##   3   2  Ozone  Solar.R
##   3   3  Ozone  Solar.R
##   3   4  Ozone  Solar.R
##   3   5  Ozone  Solar.R
##   4   1  Ozone  Solar.R
##   4   2  Ozone  Solar.R
##   4   3  Ozone  Solar.R
##   4   4  Ozone  Solar.R
##   4   5  Ozone  Solar.R
##   5   1  Ozone  Solar.R
##   5   2  Ozone  Solar.R
##   5   3  Ozone  Solar.R
##   5   4  Ozone  Solar.R
##   5   5  Ozone  Solar.R
##   6   1  Ozone  Solar.R
##   6   2  Ozone  Solar.R
##   6   3  Ozone  Solar.R
##   6   4  Ozone  Solar.R
##   6   5  Ozone  Solar.R
##   7   1  Ozone  Solar.R
##   7   2  Ozone  Solar.R
##   7   3  Ozone  Solar.R
##   7   4  Ozone  Solar.R
##   7   5  Ozone  Solar.R
##   8   1  Ozone  Solar.R
##   8   2  Ozone  Solar.R
##   8   3  Ozone  Solar.R
##   8   4  Ozone  Solar.R
##   8   5  Ozone  Solar.R
##   9   1  Ozone  Solar.R
##   9   2  Ozone  Solar.R
##   9   3  Ozone  Solar.R
##   9   4  Ozone  Solar.R
##   9   5  Ozone  Solar.R
##   10   1  Ozone  Solar.R
##   10   2  Ozone  Solar.R
##   10   3  Ozone  Solar.R
##   10   4  Ozone  Solar.R
##   10   5  Ozone  Solar.R
##   11   1  Ozone  Solar.R
##   11   2  Ozone  Solar.R
##   11   3  Ozone  Solar.R
##   11   4  Ozone  Solar.R
##   11   5  Ozone  Solar.R
##   12   1  Ozone  Solar.R
##   12   2  Ozone  Solar.R
##   12   3  Ozone  Solar.R
##   12   4  Ozone  Solar.R
##   12   5  Ozone  Solar.R
##   13   1  Ozone  Solar.R
##   13   2  Ozone  Solar.R
##   13   3  Ozone  Solar.R
##   13   4  Ozone  Solar.R
##   13   5  Ozone  Solar.R
##   14   1  Ozone  Solar.R
##   14   2  Ozone  Solar.R
##   14   3  Ozone  Solar.R
##   14   4  Ozone  Solar.R
##   14   5  Ozone  Solar.R
##   15   1  Ozone  Solar.R
##   15   2  Ozone  Solar.R
##   15   3  Ozone  Solar.R
##   15   4  Ozone  Solar.R
##   15   5  Ozone  Solar.R
##   16   1  Ozone  Solar.R
##   16   2  Ozone  Solar.R
##   16   3  Ozone  Solar.R
##   16   4  Ozone  Solar.R
##   16   5  Ozone  Solar.R
##   17   1  Ozone  Solar.R
##   17   2  Ozone  Solar.R
##   17   3  Ozone  Solar.R
##   17   4  Ozone  Solar.R
##   17   5  Ozone  Solar.R
##   18   1  Ozone  Solar.R
##   18   2  Ozone  Solar.R
##   18   3  Ozone  Solar.R
##   18   4  Ozone  Solar.R
##   18   5  Ozone  Solar.R
##   19   1  Ozone  Solar.R
##   19   2  Ozone  Solar.R
##   19   3  Ozone  Solar.R
##   19   4  Ozone  Solar.R
##   19   5  Ozone  Solar.R
##   20   1  Ozone  Solar.R
##   20   2  Ozone  Solar.R
##   20   3  Ozone  Solar.R
##   20   4  Ozone  Solar.R
##   20   5  Ozone  Solar.R
##   21   1  Ozone  Solar.R
##   21   2  Ozone  Solar.R
##   21   3  Ozone  Solar.R
##   21   4  Ozone  Solar.R
##   21   5  Ozone  Solar.R
##   22   1  Ozone  Solar.R
##   22   2  Ozone  Solar.R
##   22   3  Ozone  Solar.R
##   22   4  Ozone  Solar.R
##   22   5  Ozone  Solar.R
##   23   1  Ozone  Solar.R
##   23   2  Ozone  Solar.R
##   23   3  Ozone  Solar.R
##   23   4  Ozone  Solar.R
##   23   5  Ozone  Solar.R
##   24   1  Ozone  Solar.R
##   24   2  Ozone  Solar.R
##   24   3  Ozone  Solar.R
##   24   4  Ozone  Solar.R
##   24   5  Ozone  Solar.R
##   25   1  Ozone  Solar.R
##   25   2  Ozone  Solar.R
##   25   3  Ozone  Solar.R
##   25   4  Ozone  Solar.R
##   25   5  Ozone  Solar.R
##   26   1  Ozone  Solar.R
##   26   2  Ozone  Solar.R
##   26   3  Ozone  Solar.R
##   26   4  Ozone  Solar.R
##   26   5  Ozone  Solar.R
##   27   1  Ozone  Solar.R
##   27   2  Ozone  Solar.R
##   27   3  Ozone  Solar.R
##   27   4  Ozone  Solar.R
##   27   5  Ozone  Solar.R
##   28   1  Ozone  Solar.R
##   28   2  Ozone  Solar.R
##   28   3  Ozone  Solar.R
##   28   4  Ozone  Solar.R
##   28   5  Ozone  Solar.R
##   29   1  Ozone  Solar.R
##   29   2  Ozone  Solar.R
##   29   3  Ozone  Solar.R
##   29   4  Ozone  Solar.R
##   29   5  Ozone  Solar.R
##   30   1  Ozone  Solar.R
##   30   2  Ozone  Solar.R
##   30   3  Ozone  Solar.R
##   30   4  Ozone  Solar.R
##   30   5  Ozone  Solar.R
##   31   1  Ozone  Solar.R
##   31   2  Ozone  Solar.R
##   31   3  Ozone  Solar.R
##   31   4  Ozone  Solar.R
##   31   5  Ozone  Solar.R
##   32   1  Ozone  Solar.R
##   32   2  Ozone  Solar.R
##   32   3  Ozone  Solar.R
##   32   4  Ozone  Solar.R
##   32   5  Ozone  Solar.R
##   33   1  Ozone  Solar.R
##   33   2  Ozone  Solar.R
##   33   3  Ozone  Solar.R
##   33   4  Ozone  Solar.R
##   33   5  Ozone  Solar.R
##   34   1  Ozone  Solar.R
##   34   2  Ozone  Solar.R
##   34   3  Ozone  Solar.R
##   34   4  Ozone  Solar.R
##   34   5  Ozone  Solar.R
##   35   1  Ozone  Solar.R
##   35   2  Ozone  Solar.R
##   35   3  Ozone  Solar.R
##   35   4  Ozone  Solar.R
##   35   5  Ozone  Solar.R
##   36   1  Ozone  Solar.R
##   36   2  Ozone  Solar.R
##   36   3  Ozone  Solar.R
##   36   4  Ozone  Solar.R
##   36   5  Ozone  Solar.R
##   37   1  Ozone  Solar.R
##   37   2  Ozone  Solar.R
##   37   3  Ozone  Solar.R
##   37   4  Ozone  Solar.R
##   37   5  Ozone  Solar.R
##   38   1  Ozone  Solar.R
##   38   2  Ozone  Solar.R
##   38   3  Ozone  Solar.R
##   38   4  Ozone  Solar.R
##   38   5  Ozone  Solar.R
##   39   1  Ozone  Solar.R
##   39   2  Ozone  Solar.R
##   39   3  Ozone  Solar.R
##   39   4  Ozone  Solar.R
##   39   5  Ozone  Solar.R
##   40   1  Ozone  Solar.R
##   40   2  Ozone  Solar.R
##   40   3  Ozone  Solar.R
##   40   4  Ozone  Solar.R
##   40   5  Ozone  Solar.R
##   41   1  Ozone  Solar.R
##   41   2  Ozone  Solar.R
##   41   3  Ozone  Solar.R
##   41   4  Ozone  Solar.R
##   41   5  Ozone  Solar.R
##   42   1  Ozone  Solar.R
##   42   2  Ozone  Solar.R
##   42   3  Ozone  Solar.R
##   42   4  Ozone  Solar.R
##   42   5  Ozone  Solar.R
##   43   1  Ozone  Solar.R
##   43   2  Ozone  Solar.R
##   43   3  Ozone  Solar.R
##   43   4  Ozone  Solar.R
##   43   5  Ozone  Solar.R
##   44   1  Ozone  Solar.R
##   44   2  Ozone  Solar.R
##   44   3  Ozone  Solar.R
##   44   4  Ozone  Solar.R
##   44   5  Ozone  Solar.R
##   45   1  Ozone  Solar.R
##   45   2  Ozone  Solar.R
##   45   3  Ozone  Solar.R
##   45   4  Ozone  Solar.R
##   45   5  Ozone  Solar.R
##   46   1  Ozone  Solar.R
##   46   2  Ozone  Solar.R
##   46   3  Ozone  Solar.R
##   46   4  Ozone  Solar.R
##   46   5  Ozone  Solar.R
##   47   1  Ozone  Solar.R
##   47   2  Ozone  Solar.R
##   47   3  Ozone  Solar.R
##   47   4  Ozone  Solar.R
##   47   5  Ozone  Solar.R
##   48   1  Ozone  Solar.R
##   48   2  Ozone  Solar.R
##   48   3  Ozone  Solar.R
##   48   4  Ozone  Solar.R
##   48   5  Ozone  Solar.R
##   49   1  Ozone  Solar.R
##   49   2  Ozone  Solar.R
##   49   3  Ozone  Solar.R
##   49   4  Ozone  Solar.R
##   49   5  Ozone  Solar.R
##   50   1  Ozone  Solar.R
##   50   2  Ozone  Solar.R
##   50   3  Ozone  Solar.R
##   50   4  Ozone  Solar.R
##   50   5  Ozone  Solar.R
#500 iterataions of predictive mean mapping for imputing

summary(im_aqty)
## Class: mids
## Number of multiple imputations:  5 
## Imputation methods:
##   Ozone Solar.R    Wind    Temp   Month     Day 
##   "pmm"   "pmm"      ""      ""      ""      "" 
## PredictorMatrix:
##         Ozone Solar.R Wind Temp Month Day
## Ozone       0       1    1    1     1   1
## Solar.R     1       0    1    1     1   1
## Wind        1       1    0    1     1   1
## Temp        1       1    1    0     1   1
## Month       1       1    1    1     0   1
## Day         1       1    1    1     1   0
im_aqty$imp$Ozone #values imputed in ozone
##       1  2   3   4   5
## 5     6 32  14  18   6
## 10   12 23  27  21  41
## 25    8 19   6  14  19
## 26   32  9  28  19  28
## 27   18 22  37  18   9
## 32   59 47  44  45  52
## 33   16 16  20  11  18
## 34    1 13  13  37  13
## 35   44 71  40  40  71
## 36   35 64  89  35  39
## 37   14 13  30  30  44
## 39  115 91 135 168  82
## 42   64 77 168  66  76
## 43   61 91 135  82  91
## 45   23 29  45  44  59
## 46   45 63  29  45  32
## 52   16 71  47  52  52
## 53   20 64  35  23  49
## 54   45 37  40  52  35
## 55   20 39  23  20   7
## 56   13 40  45  36  45
## 57   36 35  52  46  44
## 58   32 16  21  23  23
## 59   16 52  31  39  28
## 60   23 14  13  44  24
## 61   40 85  48  71  48
## 65   23 16  23  28  59
## 72   59 47  29  52  35
## 75   35 89  59  59 108
## 83   32 23  23  44  35
## 84   28 47  35   7  35
## 102 115 85  80 168  91
## 103  16 39  16  32  47
## 107  12 22  41  22  23
## 115  24 22  16  21  36
## 119  64 78  82  61  78
## 150  12 32  12  21  16
#get back the completed dataset u
completedData <- complete(im_aqty,1)
head(completedData)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5     6     115 14.3   56     5   5
## 6    28     274 14.9   66     5   6

Using pipe operator %>%

################################################################

library(magrittr)
library(dplyr)

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris %>% head() ## with a pipe-operator
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
## tells R to take the value of that which is to the left 
## and pass it to the right as an argument

## Select Columns

iris %>% select(Species,Petal.Width)
##        Species Petal.Width
## 1       setosa         0.2
## 2       setosa         0.2
## 3       setosa         0.2
## 4       setosa         0.2
## 5       setosa         0.2
## 6       setosa         0.4
## 7       setosa         0.3
## 8       setosa         0.2
## 9       setosa         0.2
## 10      setosa         0.1
## 11      setosa         0.2
## 12      setosa         0.2
## 13      setosa         0.1
## 14      setosa         0.1
## 15      setosa         0.2
## 16      setosa         0.4
## 17      setosa         0.4
## 18      setosa         0.3
## 19      setosa         0.3
## 20      setosa         0.3
## 21      setosa         0.2
## 22      setosa         0.4
## 23      setosa         0.2
## 24      setosa         0.5
## 25      setosa         0.2
## 26      setosa         0.2
## 27      setosa         0.4
## 28      setosa         0.2
## 29      setosa         0.2
## 30      setosa         0.2
## 31      setosa         0.2
## 32      setosa         0.4
## 33      setosa         0.1
## 34      setosa         0.2
## 35      setosa         0.2
## 36      setosa         0.2
## 37      setosa         0.2
## 38      setosa         0.1
## 39      setosa         0.2
## 40      setosa         0.2
## 41      setosa         0.3
## 42      setosa         0.3
## 43      setosa         0.2
## 44      setosa         0.6
## 45      setosa         0.4
## 46      setosa         0.3
## 47      setosa         0.2
## 48      setosa         0.2
## 49      setosa         0.2
## 50      setosa         0.2
## 51  versicolor         1.4
## 52  versicolor         1.5
## 53  versicolor         1.5
## 54  versicolor         1.3
## 55  versicolor         1.5
## 56  versicolor         1.3
## 57  versicolor         1.6
## 58  versicolor         1.0
## 59  versicolor         1.3
## 60  versicolor         1.4
## 61  versicolor         1.0
## 62  versicolor         1.5
## 63  versicolor         1.0
## 64  versicolor         1.4
## 65  versicolor         1.3
## 66  versicolor         1.4
## 67  versicolor         1.5
## 68  versicolor         1.0
## 69  versicolor         1.5
## 70  versicolor         1.1
## 71  versicolor         1.8
## 72  versicolor         1.3
## 73  versicolor         1.5
## 74  versicolor         1.2
## 75  versicolor         1.3
## 76  versicolor         1.4
## 77  versicolor         1.4
## 78  versicolor         1.7
## 79  versicolor         1.5
## 80  versicolor         1.0
## 81  versicolor         1.1
## 82  versicolor         1.0
## 83  versicolor         1.2
## 84  versicolor         1.6
## 85  versicolor         1.5
## 86  versicolor         1.6
## 87  versicolor         1.5
## 88  versicolor         1.3
## 89  versicolor         1.3
## 90  versicolor         1.3
## 91  versicolor         1.2
## 92  versicolor         1.4
## 93  versicolor         1.2
## 94  versicolor         1.0
## 95  versicolor         1.3
## 96  versicolor         1.2
## 97  versicolor         1.3
## 98  versicolor         1.3
## 99  versicolor         1.1
## 100 versicolor         1.3
## 101  virginica         2.5
## 102  virginica         1.9
## 103  virginica         2.1
## 104  virginica         1.8
## 105  virginica         2.2
## 106  virginica         2.1
## 107  virginica         1.7
## 108  virginica         1.8
## 109  virginica         1.8
## 110  virginica         2.5
## 111  virginica         2.0
## 112  virginica         1.9
## 113  virginica         2.1
## 114  virginica         2.0
## 115  virginica         2.4
## 116  virginica         2.3
## 117  virginica         1.8
## 118  virginica         2.2
## 119  virginica         2.3
## 120  virginica         1.5
## 121  virginica         2.3
## 122  virginica         2.0
## 123  virginica         2.0
## 124  virginica         1.8
## 125  virginica         2.1
## 126  virginica         1.8
## 127  virginica         1.8
## 128  virginica         1.8
## 129  virginica         2.1
## 130  virginica         1.6
## 131  virginica         1.9
## 132  virginica         2.0
## 133  virginica         2.2
## 134  virginica         1.5
## 135  virginica         1.4
## 136  virginica         2.3
## 137  virginica         2.4
## 138  virginica         1.8
## 139  virginica         1.8
## 140  virginica         2.1
## 141  virginica         2.4
## 142  virginica         2.3
## 143  virginica         1.9
## 144  virginica         2.3
## 145  virginica         2.5
## 146  virginica         2.3
## 147  virginica         1.9
## 148  virginica         2.0
## 149  virginica         2.3
## 150  virginica         1.8
iris %>% select(Species,Petal.Width) ->col2

col2 %>% head()
##   Species Petal.Width
## 1  setosa         0.2
## 2  setosa         0.2
## 3  setosa         0.2
## 4  setosa         0.2
## 5  setosa         0.2
## 6  setosa         0.4
## Drop a Column

iris %>% select(-Species)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1            5.1         3.5          1.4         0.2
## 2            4.9         3.0          1.4         0.2
## 3            4.7         3.2          1.3         0.2
## 4            4.6         3.1          1.5         0.2
## 5            5.0         3.6          1.4         0.2
## 6            5.4         3.9          1.7         0.4
## 7            4.6         3.4          1.4         0.3
## 8            5.0         3.4          1.5         0.2
## 9            4.4         2.9          1.4         0.2
## 10           4.9         3.1          1.5         0.1
## 11           5.4         3.7          1.5         0.2
## 12           4.8         3.4          1.6         0.2
## 13           4.8         3.0          1.4         0.1
## 14           4.3         3.0          1.1         0.1
## 15           5.8         4.0          1.2         0.2
## 16           5.7         4.4          1.5         0.4
## 17           5.4         3.9          1.3         0.4
## 18           5.1         3.5          1.4         0.3
## 19           5.7         3.8          1.7         0.3
## 20           5.1         3.8          1.5         0.3
## 21           5.4         3.4          1.7         0.2
## 22           5.1         3.7          1.5         0.4
## 23           4.6         3.6          1.0         0.2
## 24           5.1         3.3          1.7         0.5
## 25           4.8         3.4          1.9         0.2
## 26           5.0         3.0          1.6         0.2
## 27           5.0         3.4          1.6         0.4
## 28           5.2         3.5          1.5         0.2
## 29           5.2         3.4          1.4         0.2
## 30           4.7         3.2          1.6         0.2
## 31           4.8         3.1          1.6         0.2
## 32           5.4         3.4          1.5         0.4
## 33           5.2         4.1          1.5         0.1
## 34           5.5         4.2          1.4         0.2
## 35           4.9         3.1          1.5         0.2
## 36           5.0         3.2          1.2         0.2
## 37           5.5         3.5          1.3         0.2
## 38           4.9         3.6          1.4         0.1
## 39           4.4         3.0          1.3         0.2
## 40           5.1         3.4          1.5         0.2
## 41           5.0         3.5          1.3         0.3
## 42           4.5         2.3          1.3         0.3
## 43           4.4         3.2          1.3         0.2
## 44           5.0         3.5          1.6         0.6
## 45           5.1         3.8          1.9         0.4
## 46           4.8         3.0          1.4         0.3
## 47           5.1         3.8          1.6         0.2
## 48           4.6         3.2          1.4         0.2
## 49           5.3         3.7          1.5         0.2
## 50           5.0         3.3          1.4         0.2
## 51           7.0         3.2          4.7         1.4
## 52           6.4         3.2          4.5         1.5
## 53           6.9         3.1          4.9         1.5
## 54           5.5         2.3          4.0         1.3
## 55           6.5         2.8          4.6         1.5
## 56           5.7         2.8          4.5         1.3
## 57           6.3         3.3          4.7         1.6
## 58           4.9         2.4          3.3         1.0
## 59           6.6         2.9          4.6         1.3
## 60           5.2         2.7          3.9         1.4
## 61           5.0         2.0          3.5         1.0
## 62           5.9         3.0          4.2         1.5
## 63           6.0         2.2          4.0         1.0
## 64           6.1         2.9          4.7         1.4
## 65           5.6         2.9          3.6         1.3
## 66           6.7         3.1          4.4         1.4
## 67           5.6         3.0          4.5         1.5
## 68           5.8         2.7          4.1         1.0
## 69           6.2         2.2          4.5         1.5
## 70           5.6         2.5          3.9         1.1
## 71           5.9         3.2          4.8         1.8
## 72           6.1         2.8          4.0         1.3
## 73           6.3         2.5          4.9         1.5
## 74           6.1         2.8          4.7         1.2
## 75           6.4         2.9          4.3         1.3
## 76           6.6         3.0          4.4         1.4
## 77           6.8         2.8          4.8         1.4
## 78           6.7         3.0          5.0         1.7
## 79           6.0         2.9          4.5         1.5
## 80           5.7         2.6          3.5         1.0
## 81           5.5         2.4          3.8         1.1
## 82           5.5         2.4          3.7         1.0
## 83           5.8         2.7          3.9         1.2
## 84           6.0         2.7          5.1         1.6
## 85           5.4         3.0          4.5         1.5
## 86           6.0         3.4          4.5         1.6
## 87           6.7         3.1          4.7         1.5
## 88           6.3         2.3          4.4         1.3
## 89           5.6         3.0          4.1         1.3
## 90           5.5         2.5          4.0         1.3
## 91           5.5         2.6          4.4         1.2
## 92           6.1         3.0          4.6         1.4
## 93           5.8         2.6          4.0         1.2
## 94           5.0         2.3          3.3         1.0
## 95           5.6         2.7          4.2         1.3
## 96           5.7         3.0          4.2         1.2
## 97           5.7         2.9          4.2         1.3
## 98           6.2         2.9          4.3         1.3
## 99           5.1         2.5          3.0         1.1
## 100          5.7         2.8          4.1         1.3
## 101          6.3         3.3          6.0         2.5
## 102          5.8         2.7          5.1         1.9
## 103          7.1         3.0          5.9         2.1
## 104          6.3         2.9          5.6         1.8
## 105          6.5         3.0          5.8         2.2
## 106          7.6         3.0          6.6         2.1
## 107          4.9         2.5          4.5         1.7
## 108          7.3         2.9          6.3         1.8
## 109          6.7         2.5          5.8         1.8
## 110          7.2         3.6          6.1         2.5
## 111          6.5         3.2          5.1         2.0
## 112          6.4         2.7          5.3         1.9
## 113          6.8         3.0          5.5         2.1
## 114          5.7         2.5          5.0         2.0
## 115          5.8         2.8          5.1         2.4
## 116          6.4         3.2          5.3         2.3
## 117          6.5         3.0          5.5         1.8
## 118          7.7         3.8          6.7         2.2
## 119          7.7         2.6          6.9         2.3
## 120          6.0         2.2          5.0         1.5
## 121          6.9         3.2          5.7         2.3
## 122          5.6         2.8          4.9         2.0
## 123          7.7         2.8          6.7         2.0
## 124          6.3         2.7          4.9         1.8
## 125          6.7         3.3          5.7         2.1
## 126          7.2         3.2          6.0         1.8
## 127          6.2         2.8          4.8         1.8
## 128          6.1         3.0          4.9         1.8
## 129          6.4         2.8          5.6         2.1
## 130          7.2         3.0          5.8         1.6
## 131          7.4         2.8          6.1         1.9
## 132          7.9         3.8          6.4         2.0
## 133          6.4         2.8          5.6         2.2
## 134          6.3         2.8          5.1         1.5
## 135          6.1         2.6          5.6         1.4
## 136          7.7         3.0          6.1         2.3
## 137          6.3         3.4          5.6         2.4
## 138          6.4         3.1          5.5         1.8
## 139          6.0         3.0          4.8         1.8
## 140          6.9         3.1          5.4         2.1
## 141          6.7         3.1          5.6         2.4
## 142          6.9         3.1          5.1         2.3
## 143          5.8         2.7          5.1         1.9
## 144          6.8         3.2          5.9         2.3
## 145          6.7         3.3          5.7         2.5
## 146          6.7         3.0          5.2         2.3
## 147          6.3         2.5          5.0         1.9
## 148          6.5         3.0          5.2         2.0
## 149          6.2         3.4          5.4         2.3
## 150          5.9         3.0          5.1         1.8
iris %>% select(-Species) %>% head()
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4
## Select a Few Columns

iris %>% select(Sepal.Length:Petal.Length) %>%head()
##   Sepal.Length Sepal.Width Petal.Length
## 1          5.1         3.5          1.4
## 2          4.9         3.0          1.4
## 3          4.7         3.2          1.3
## 4          4.6         3.1          1.5
## 5          5.0         3.6          1.4
## 6          5.4         3.9          1.7
iris %>% select_if(is.numeric) %>% head()
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4
## Drop a Few Columns

iris %>% select(-(Sepal.Length:Petal.Length)) %>%head()
##   Petal.Width Species
## 1         0.2  setosa
## 2         0.2  setosa
## 3         0.2  setosa
## 4         0.2  setosa
## 5         0.2  setosa
## 6         0.4  setosa
## Select based on column name value

iris %>% select(starts_with("S")) %>% head()
##   Sepal.Length Sepal.Width Species
## 1          5.1         3.5  setosa
## 2          4.9         3.0  setosa
## 3          4.7         3.2  setosa
## 4          4.6         3.1  setosa
## 5          5.0         3.6  setosa
## 6          5.4         3.9  setosa
iris %>% select(ends_with("s"))
##        Species
## 1       setosa
## 2       setosa
## 3       setosa
## 4       setosa
## 5       setosa
## 6       setosa
## 7       setosa
## 8       setosa
## 9       setosa
## 10      setosa
## 11      setosa
## 12      setosa
## 13      setosa
## 14      setosa
## 15      setosa
## 16      setosa
## 17      setosa
## 18      setosa
## 19      setosa
## 20      setosa
## 21      setosa
## 22      setosa
## 23      setosa
## 24      setosa
## 25      setosa
## 26      setosa
## 27      setosa
## 28      setosa
## 29      setosa
## 30      setosa
## 31      setosa
## 32      setosa
## 33      setosa
## 34      setosa
## 35      setosa
## 36      setosa
## 37      setosa
## 38      setosa
## 39      setosa
## 40      setosa
## 41      setosa
## 42      setosa
## 43      setosa
## 44      setosa
## 45      setosa
## 46      setosa
## 47      setosa
## 48      setosa
## 49      setosa
## 50      setosa
## 51  versicolor
## 52  versicolor
## 53  versicolor
## 54  versicolor
## 55  versicolor
## 56  versicolor
## 57  versicolor
## 58  versicolor
## 59  versicolor
## 60  versicolor
## 61  versicolor
## 62  versicolor
## 63  versicolor
## 64  versicolor
## 65  versicolor
## 66  versicolor
## 67  versicolor
## 68  versicolor
## 69  versicolor
## 70  versicolor
## 71  versicolor
## 72  versicolor
## 73  versicolor
## 74  versicolor
## 75  versicolor
## 76  versicolor
## 77  versicolor
## 78  versicolor
## 79  versicolor
## 80  versicolor
## 81  versicolor
## 82  versicolor
## 83  versicolor
## 84  versicolor
## 85  versicolor
## 86  versicolor
## 87  versicolor
## 88  versicolor
## 89  versicolor
## 90  versicolor
## 91  versicolor
## 92  versicolor
## 93  versicolor
## 94  versicolor
## 95  versicolor
## 96  versicolor
## 97  versicolor
## 98  versicolor
## 99  versicolor
## 100 versicolor
## 101  virginica
## 102  virginica
## 103  virginica
## 104  virginica
## 105  virginica
## 106  virginica
## 107  virginica
## 108  virginica
## 109  virginica
## 110  virginica
## 111  virginica
## 112  virginica
## 113  virginica
## 114  virginica
## 115  virginica
## 116  virginica
## 117  virginica
## 118  virginica
## 119  virginica
## 120  virginica
## 121  virginica
## 122  virginica
## 123  virginica
## 124  virginica
## 125  virginica
## 126  virginica
## 127  virginica
## 128  virginica
## 129  virginica
## 130  virginica
## 131  virginica
## 132  virginica
## 133  virginica
## 134  virginica
## 135  virginica
## 136  virginica
## 137  virginica
## 138  virginica
## 139  virginica
## 140  virginica
## 141  virginica
## 142  virginica
## 143  virginica
## 144  virginica
## 145  virginica
## 146  virginica
## 147  virginica
## 148  virginica
## 149  virginica
## 150  virginica
iris %>% select(contains("Width")) %>% head()
##   Sepal.Width Petal.Width
## 1         3.5         0.2
## 2         3.0         0.2
## 3         3.2         0.2
## 4         3.1         0.2
## 5         3.6         0.2
## 6         3.9         0.4
iris %>% select(contains("Length")) %>% head()
##   Sepal.Length Petal.Length
## 1          5.1          1.4
## 2          4.9          1.4
## 3          4.7          1.3
## 4          4.6          1.5
## 5          5.0          1.4
## 6          5.4          1.7

Using tidyverse library

##############################################################

library(tidyverse)

##tidyverse is a coherent system of packages for 
##data manipulation, exploration and visualization that share a common design philosophy.

### underpinned by tibbles

#vignette("tibble")

class(iris)
## [1] "data.frame"
i=as_tibble(iris)

head(i)
## # A tibble: 6 x 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
## 1          5.1         3.5          1.4         0.2 setosa 
## 2          4.9         3            1.4         0.2 setosa 
## 3          4.7         3.2          1.3         0.2 setosa 
## 4          4.6         3.1          1.5         0.2 setosa 
## 5          5           3.6          1.4         0.2 setosa 
## 6          5.4         3.9          1.7         0.4 setosa
library(nycflights13)
        
nycflights13::flights %>% head()
## # A tibble: 6 x 19
##    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
## 1  2013     1     1      517            515         2      830            819
## 2  2013     1     1      533            529         4      850            830
## 3  2013     1     1      542            540         2      923            850
## 4  2013     1     1      544            545        -1     1004           1022
## 5  2013     1     1      554            600        -6      812            837
## 6  2013     1     1      554            558        -4      740            728
## # ... with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## #   hour <dbl>, minute <dbl>, time_hour <dttm>

Basic EDA

data(iris)
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
##examine data distribution

hist(iris$Sepal.Length) # distribution of a varaibles

boxplot(iris$Sepal.Length,main="5 Stat Summary of iris",xlab="Sepal Length")

#relation bw 2 variables
plot(iris$Sepal.Length,iris$Sepal.Width)

#PLOTTING CATEGORICAL VARIABLES

counts <- table(iris$Species)
counts
## 
##     setosa versicolor  virginica 
##         50         50         50
barplot(counts, main="Iris Species", xlab="Samples/species")

barplot(counts, main="Iris Species", xlab="Samples/species",horiz=TRUE)

## Improved data viz

library(ggplot2)

# relation bw Sepal length and width of 3 different species
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

# We see that Iris setosa flowers have the narrowest petals.
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width)

##Add labels to the plot

qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
    xlab = "Sepal Length", ylab = "Petal Length",
    main = "Sepal vs. Petal Length in Iris data")

# Plot the number of movies each director has.
qplot(Species, data = iris, geom = "bar", ylab = "Samples/species")

Unsupervised Classification with H2o

Implement k-Means Classification using H2o

#############

library(caret)
#library(mxnet)

# loading data
df=read.csv("covtype.csv") #predict if the tumor is
#malignant (M) or benign (B)

head(df)
##   Elevation Aspect Slope Horizontal_Distance_To_Hydrology
## 1      2596     51     3                              258
## 2      2590     56     2                              212
## 3      2804    139     9                              268
## 4      2785    155    18                              242
## 5      2595     45     2                              153
## 6      2579    132     6                              300
##   Vertical_Distance_To_Hydrology Horizontal_Distance_To_Roadways Hillshade_9am
## 1                              0                             510           221
## 2                             -6                             390           220
## 3                             65                            3180           234
## 4                            118                            3090           238
## 5                             -1                             391           220
## 6                            -15                              67           230
##   Hillshade_Noon Hillshade_3pm Horizontal_Distance_To_Fire_Points
## 1            232           148                               6279
## 2            235           151                               6225
## 3            238           135                               6121
## 4            238           122                               6211
## 5            234           150                               6172
## 6            237           140                               6031
##   Wilderness_Area1 Wilderness_Area2 Wilderness_Area3 Wilderness_Area4
## 1                1                0                0                0
## 2                1                0                0                0
## 3                1                0                0                0
## 4                1                0                0                0
## 5                1                0                0                0
## 6                1                0                0                0
##   Soil_Type1 Soil_Type2 Soil_Type3 Soil_Type4 Soil_Type5 Soil_Type6 Soil_Type7
## 1          0          0          0          0          0          0          0
## 2          0          0          0          0          0          0          0
## 3          0          0          0          0          0          0          0
## 4          0          0          0          0          0          0          0
## 5          0          0          0          0          0          0          0
## 6          0          0          0          0          0          0          0
##   Soil_Type8 Soil_Type9 Soil_Type10 Soil_Type11 Soil_Type12 Soil_Type13
## 1          0          0           0           0           0           0
## 2          0          0           0           0           0           0
## 3          0          0           0           0           1           0
## 4          0          0           0           0           0           0
## 5          0          0           0           0           0           0
## 6          0          0           0           0           0           0
##   Soil_Type14 Soil_Type15 Soil_Type16 Soil_Type17 Soil_Type18 Soil_Type19
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type20 Soil_Type21 Soil_Type22 Soil_Type23 Soil_Type24 Soil_Type25
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type26 Soil_Type27 Soil_Type28 Soil_Type29 Soil_Type30 Soil_Type31
## 1           0           0           0           1           0           0
## 2           0           0           0           1           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           1           0
## 5           0           0           0           1           0           0
## 6           0           0           0           1           0           0
##   Soil_Type32 Soil_Type33 Soil_Type34 Soil_Type35 Soil_Type36 Soil_Type37
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type38 Soil_Type39 Soil_Type40 Cover_Type
## 1           0           0           0          5
## 2           0           0           0          5
## 3           0           0           0          2
## 4           0           0           0          2
## 5           0           0           0          5
## 6           0           0           0          2
ncol(df)
## [1] 55
library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         7 minutes 17 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.38 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(df, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   Elevation Aspect Slope Horizontal_Distance_To_Hydrology
## 1      2596     51     3                              258
## 2      2590     56     2                              212
## 3      2804    139     9                              268
## 4      2785    155    18                              242
## 5      2595     45     2                              153
## 6      2579    132     6                              300
##   Vertical_Distance_To_Hydrology Horizontal_Distance_To_Roadways Hillshade_9am
## 1                              0                             510           221
## 2                             -6                             390           220
## 3                             65                            3180           234
## 4                            118                            3090           238
## 5                             -1                             391           220
## 6                            -15                              67           230
##   Hillshade_Noon Hillshade_3pm Horizontal_Distance_To_Fire_Points
## 1            232           148                               6279
## 2            235           151                               6225
## 3            238           135                               6121
## 4            238           122                               6211
## 5            234           150                               6172
## 6            237           140                               6031
##   Wilderness_Area1 Wilderness_Area2 Wilderness_Area3 Wilderness_Area4
## 1                1                0                0                0
## 2                1                0                0                0
## 3                1                0                0                0
## 4                1                0                0                0
## 5                1                0                0                0
## 6                1                0                0                0
##   Soil_Type1 Soil_Type2 Soil_Type3 Soil_Type4 Soil_Type5 Soil_Type6 Soil_Type7
## 1          0          0          0          0          0          0          0
## 2          0          0          0          0          0          0          0
## 3          0          0          0          0          0          0          0
## 4          0          0          0          0          0          0          0
## 5          0          0          0          0          0          0          0
## 6          0          0          0          0          0          0          0
##   Soil_Type8 Soil_Type9 Soil_Type10 Soil_Type11 Soil_Type12 Soil_Type13
## 1          0          0           0           0           0           0
## 2          0          0           0           0           0           0
## 3          0          0           0           0           1           0
## 4          0          0           0           0           0           0
## 5          0          0           0           0           0           0
## 6          0          0           0           0           0           0
##   Soil_Type14 Soil_Type15 Soil_Type16 Soil_Type17 Soil_Type18 Soil_Type19
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type20 Soil_Type21 Soil_Type22 Soil_Type23 Soil_Type24 Soil_Type25
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type26 Soil_Type27 Soil_Type28 Soil_Type29 Soil_Type30 Soil_Type31
## 1           0           0           0           1           0           0
## 2           0           0           0           1           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           1           0
## 5           0           0           0           1           0           0
## 6           0           0           0           1           0           0
##   Soil_Type32 Soil_Type33 Soil_Type34 Soil_Type35 Soil_Type36 Soil_Type37
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type38 Soil_Type39 Soil_Type40 Cover_Type
## 1           0           0           0          5
## 2           0           0           0          5
## 3           0           0           0          2
## 4           0           0           0          2
## 5           0           0           0          5
## 6           0           0           0          2
str(d.hex)
## Class 'H2OFrame' <environment: 0x000000003d89b4f8> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 581012
##  - attr(*, "ncol")= int 55
##  - attr(*, "types")=List of 55
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##  - attr(*, "data")='data.frame': 10 obs. of  55 variables:
##   ..$ Elevation                         : num  2596 2590 2804 2785 2595 ...
##   ..$ Aspect                            : num  51 56 139 155 45 132 45 49 45 59
##   ..$ Slope                             : num  3 2 9 18 2 6 7 4 9 10
##   ..$ Horizontal_Distance_To_Hydrology  : num  258 212 268 242 153 300 270 234 240 247
##   ..$ Vertical_Distance_To_Hydrology    : num  0 -6 65 118 -1 -15 5 7 56 11
##   ..$ Horizontal_Distance_To_Roadways   : num  510 390 3180 3090 391 67 633 573 666 636
##   ..$ Hillshade_9am                     : num  221 220 234 238 220 230 222 222 223 228
##   ..$ Hillshade_Noon                    : num  232 235 238 238 234 237 225 230 221 219
##   ..$ Hillshade_3pm                     : num  148 151 135 122 150 140 138 144 133 124
##   ..$ Horizontal_Distance_To_Fire_Points: num  6279 6225 6121 6211 6172 ...
##   ..$ Wilderness_Area1                  : num  1 1 1 1 1 1 1 1 1 1
##   ..$ Wilderness_Area2                  : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Wilderness_Area3                  : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Wilderness_Area4                  : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type1                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type2                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type3                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type4                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type5                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type6                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type7                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type8                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type9                        : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type10                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type11                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type12                       : num  0 0 1 0 0 0 0 0 0 0
##   ..$ Soil_Type13                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type14                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type15                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type16                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type17                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type18                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type19                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type20                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type21                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type22                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type23                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type24                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type25                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type26                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type27                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type28                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type29                       : num  1 1 0 0 1 1 1 1 1 1
##   ..$ Soil_Type30                       : num  0 0 0 1 0 0 0 0 0 0
##   ..$ Soil_Type31                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type32                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type33                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type34                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type35                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type36                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type37                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type38                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type39                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Soil_Type40                       : num  0 0 0 0 0 0 0 0 0 0
##   ..$ Cover_Type                        : num  5 5 2 2 5 2 5 5 5 5
d.km=h2o.kmeans(training_frame = d.hex, k = 7, x =
             1:54) ##7 categories and 54 predictors
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |======================================================================| 100%
par(mfrow = c(1,2))
d.ctrs = as.data.frame(d.km@model$centers)
plot(d.ctrs[,1:2])
plot(d.ctrs[,13:14])
title("K-Means Centers", outer = TRUE, line = -2.0)

Implement PCA With H2O

library(caret)
#library(mxnet)

# loading data
df=read.csv("Seabmass_typ.csv") #predict if the tumor is
#malignant (M) or benign (B)

head(df)
##   Country AGB bio01 bio02 bio03 bio04 bio05 bio06 bio07 bio08 bio09 bio10 bio11
## 1 Vietnam 127    27   6.6    79    64    31    23   8.0    27    27    28    26
## 2 Vietnam 103    27   5.1    64    90    31    23   8.0    27    26    28    26
## 3 Vietnam 101    27   5.0    64    83    31    24   7.9    28    27    29    26
## 4 Vietnam  50    27   5.0    64    83    31    24   7.9    28    27    29    26
## 5 Vietnam 236    27   5.2    64    96    31    23   8.2    27    26    28    26
## 6 Vietnam 119    27   6.8    79    63    31    23   8.1    27    27    28    26
##   bio12 bio13 bio14 bio15 bio16 bio17 bio18 bio19 CanopyHt_1 cwd1km DEM_1km
## 1  2497   357    30    67  1044   156   565   591        3.3     61     3.4
## 2  1460   238     1    85   663     8   449    31        4.3     62     2.2
## 3  2290   345     7    70  1003    54   635   122        0.0     36     2.0
## 4  2290   345     7    70  1003    54   635   122        0.0     36     2.0
## 5  1421   237     1    84   645    10   434    31        8.5     60     4.0
## 6  2517   358    31    68  1045   159   563   567        0.0     59     3.9
##   Slope1km soil1 soil2 soil4 SoilMois_1 Class
## 1  0.00048     1     1     1        132 Delta
## 2  0.00035     1     1     1        127 Delta
## 3  0.00086     1     1     1         90 Delta
## 4  0.00113     1     1     1         90 Delta
## 5  0.00023     1     1     1        132 Delta
## 6  0.00039     1     1     1        143 Delta
ncol(df)
## [1] 30
library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         7 minutes 55 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.37 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(df, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   Country AGB bio01 bio02 bio03 bio04 bio05 bio06 bio07 bio08 bio09 bio10 bio11
## 1 Vietnam 127    27   6.6    79    64    31    23   8.0    27    27    28    26
## 2 Vietnam 103    27   5.1    64    90    31    23   8.0    27    26    28    26
## 3 Vietnam 101    27   5.0    64    83    31    24   7.9    28    27    29    26
## 4 Vietnam  50    27   5.0    64    83    31    24   7.9    28    27    29    26
## 5 Vietnam 236    27   5.2    64    96    31    23   8.2    27    26    28    26
## 6 Vietnam 119    27   6.8    79    63    31    23   8.1    27    27    28    26
##   bio12 bio13 bio14 bio15 bio16 bio17 bio18 bio19 CanopyHt_1 cwd1km DEM_1km
## 1  2497   357    30    67  1044   156   565   591        3.3     61     3.4
## 2  1460   238     1    85   663     8   449    31        4.3     62     2.2
## 3  2290   345     7    70  1003    54   635   122        0.0     36     2.0
## 4  2290   345     7    70  1003    54   635   122        0.0     36     2.0
## 5  1421   237     1    84   645    10   434    31        8.5     60     4.0
## 6  2517   358    31    68  1045   159   563   567        0.0     59     3.9
##   Slope1km soil1 soil2 soil4 SoilMois_1 Class
## 1  0.00048     1     1     1        132 Delta
## 2  0.00035     1     1     1        127 Delta
## 3  0.00086     1     1     1         90 Delta
## 4  0.00113     1     1     1         90 Delta
## 5  0.00023     1     1     1        132 Delta
## 6  0.00039     1     1     1        143 Delta
str(d.hex)
## Class 'H2OFrame' <environment: 0x000000003daedb00> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 120
##  - attr(*, "ncol")= int 30
##  - attr(*, "types")=List of 30
##   ..$ : chr "enum"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "real"
##   ..$ : chr "enum"
##  - attr(*, "data")='data.frame': 10 obs. of  30 variables:
##   ..$ Country   : Factor w/ 5 levels "Burma","Indonesia",..: 5 5 5 5 5 5 5 5 5 5
##   ..$ AGB       : num  127.3 103.1 101 50.1 236.1 ...
##   ..$ bio01     : num  27.2 27 27.5 27.5 27.1 ...
##   ..$ bio02     : num  6.65 5.11 5.03 5.03 5.24 ...
##   ..$ bio03     : num  79.2 63.9 63.7 63.7 63.9 ...
##   ..$ bio04     : num  64 89.8 83.1 83.1 95.9 ...
##   ..$ bio05     : num  31.3 31 31.4 31.4 31.2 ...
##   ..$ bio06     : num  23 23 23.5 23.5 23 ...
##   ..$ bio07     : num  7.98 8 7.9 7.9 8.2 ...
##   ..$ bio08     : num  27.1 27.2 27.6 27.6 27.2 ...
##   ..$ bio09     : num  26.8 26.3 26.8 26.8 26.3 ...
##   ..$ bio10     : num  28.1 28.1 28.5 28.5 28.3 ...
##   ..$ bio11     : num  26.4 25.8 26.4 26.4 25.8 ...
##   ..$ bio12     : num  2497 1460 2290 2290 1421 ...
##   ..$ bio13     : num  357 238 345 345 237 ...
##   ..$ bio14     : num  30.4 1 7 7 1 ...
##   ..$ bio15     : num  67.1 84.8 69.7 69.7 83.9 ...
##   ..$ bio16     : num  1044 663 1003 1003 645 ...
##   ..$ bio17     : num  156 8 54 54 10 ...
##   ..$ bio18     : num  565 449 635 635 434 ...
##   ..$ bio19     : num  591 31 122 122 31 ...
##   ..$ CanopyHt_1: num  3.32 4.34 0 0 8.45 ...
##   ..$ cwd1km    : num  61.2 62.4 36 36 60 ...
##   ..$ DEM_1km   : num  3.4 2.2 2 2 4 ...
##   ..$ Slope1km  : num  0.000478 0.000351 0.000864 0.001125 0.000231 ...
##   ..$ soil1     : num  1 1 1 1 1 1 1 1 1 1
##   ..$ soil2     : num  1 1 1 1 1 1 1 1 1 1
##   ..$ soil4     : num  1 1 1 1 1 1 1 1 1 1
##   ..$ SoilMois_1: num  132.4 127.2 90.2 90.2 132 ...
##   ..$ Class     : Factor w/ 4 levels "","Delta","Estuary",..: 2 2 2 2 2 2 2 2 2 2
pca1=h2o.prcomp(training_frame = d.hex, x=2:29,k=10)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
## training data frame, x predictors, k=no of PCs
pca1
## Model Details:
## ==============
## 
## H2ODimReductionModel: pca
## Model ID:  PCA_model_R_1590343748665_1605 
## Importance of components: 
##                                pc1        pc2        pc3       pc4       pc5
## Standard deviation     4269.490516 609.799088 237.352230 86.149300 62.943873
## Proportion of Variance    0.976294   0.019916   0.003017  0.000397  0.000212
## Cumulative Proportion     0.976294   0.996210   0.999227  0.999625  0.999837
##                              pc6       pc7       pc8      pc9     pc10
## Standard deviation     42.976617 24.090042 18.304677 9.715752 8.551729
## Proportion of Variance  0.000099  0.000031  0.000018 0.000005 0.000004
## Cumulative Proportion   0.999936  0.999967  0.999985 0.999990 0.999994
## 
## 
## H2ODimReductionMetrics: pca
## 
## No model metrics available for PCA
pca1=h2o.prcomp(training_frame = d.hex,x=2:29,k=10,transform = 'NORMALIZE')
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
screeplot(pca1)

pca1
## Model Details:
## ==============
## 
## H2ODimReductionModel: pca
## Model ID:  PCA_model_R_1590343748665_1606 
## Importance of components: 
##                             pc1      pc2      pc3      pc4      pc5      pc6
## Standard deviation     1.175571 0.494469 0.410985 0.276184 0.253576 0.218807
## Proportion of Variance 0.649576 0.114924 0.079393 0.035853 0.030224 0.022504
## Cumulative Proportion  0.649576 0.764500 0.843893 0.879746 0.909970 0.932474
##                             pc7      pc8      pc9     pc10
## Standard deviation     0.184299 0.162108 0.140943 0.136902
## Proportion of Variance 0.015965 0.012352 0.009337 0.008809
## Cumulative Proportion  0.948439 0.960791 0.970128 0.978938
## 
## 
## H2ODimReductionMetrics: pca
## 
## No model metrics available for PCA
######Generalized Low Rank Decomposition

model=h2o.glrm(training_frame = d.hex, k = 5, loss = "Quadratic", regularization_x = "L1",
         gamma_x = 0.5, gamma_y = 0, max_iterations = 1000)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |======================================================================| 100%
model
## Model Details:
## ==============
## 
## H2ODimReductionModel: glrm
## Model ID:  GLRM_model_R_1590343748665_1607 
## Model Summary: 
##   number_of_iterations final_step_size final_objective_value
## 1                    8         0.00009       318441551.15294
## 
## 
## H2ODimReductionMetrics: glrm
## ** Reported on training data. **
## 
## Sum of Squared Error (Numeric):  318440336
## Misclassification Error (Categorical):  118
## Number of Numeric Entries:  3360
## Number of Categorical Entries:  240
# Decompose training frame into XY
X <- h2o.getFrame(model@model$representation_name)
Y <- model@model$archetypes

library(plotly)

# Visualize first two archetypes of Y
archetypes_y <- as.data.frame(t(Y))
archetypes_y$attribute <- rownames(archetypes_y)

m <- archetypes_y[archetypes_y$attribute %in% c("bio1", "bio2", "soil1", "soil4", "price", "Class", "bio15"), ]
a <- list(
  x = m$Arch1,
  y = m$Arch2,
  text = m$attribute,
  xref = "x",
  yref = "y"
)
p<-plot_ly(data = archetypes_y, x = ~Arch1, y = ~Arch2, mode = "markers", text = ~attribute, type = "scatter") %>% layout(annotations = a)
p
### Variables close to each other are similar

# Visualize first two archetypes of X
archetypes_x <- as.data.frame(X)
archetypes_x$id <- as.character(as.matrix(df$Country))

set.seed(1234)
sample_indices <- sample(c(1:nrow(archetypes_x)), 100)
archetypes_x <- archetypes_x[sample_indices, ]
# Plot
p<-plot_ly(data = archetypes_x, x = ~Arch1, y = ~Arch2, mode = "markers", 
        text = ~paste0("Country: ", id),type = "scatter")
p

Supervised Classification with H2O

Generalized Linear Models with H2O

df=read.csv("LoanDefault.csv")

head(df)
##   REPAY LOAN MORTDUE  VALUE  REASON    JOB  YOJ DEROG DELINQ CLAGE NINQ CLNO
## 1   BAD 1100   25860  39025 HomeImp  Other 10.5  0.00   0.00    94  1.0    9
## 2   BAD 1300   70053  68400 HomeImp  Other  7.0  0.00   2.00   122  0.0   14
## 3   BAD 1500   13500  16700 HomeImp  Other  4.0  0.00   0.00   149  1.0   10
## 4   BAD 1500   73761 101776 DebtCon  Other  8.9  0.25   0.45   180  1.2   21
## 5  GOOD 1700   97800 112000 HomeImp Office  3.0  0.00   0.00    93  0.0   14
## 6   BAD 1700   30548  40320 HomeImp  Other  9.0  0.00   0.00   101  1.0    8
##   DEBTINC
## 1      34
## 2      34
## 3      34
## 4      34
## 5      34
## 6      37
summary(df)
##   REPAY           LOAN          MORTDUE           VALUE            REASON    
##  BAD :1189   Min.   : 1100   Min.   :  2063   Min.   :  8000   DebtCon:4111  
##  GOOD:4771   1st Qu.:11100   1st Qu.: 48139   1st Qu.: 66490   HomeImp:1849  
##              Median :16300   Median : 69529   Median : 90000                 
##              Mean   :18608   Mean   : 73761   Mean   :101776                 
##              3rd Qu.:23300   3rd Qu.: 88200   3rd Qu.:119005                 
##              Max.   :89900   Max.   :399550   Max.   :855909                 
##       JOB            YOJ         DEROG          DELINQ         CLAGE     
##  Mgr    : 808   Min.   : 0   Min.   : 0.0   Min.   : 0.0   Min.   :   0  
##  Office : 987   1st Qu.: 3   1st Qu.: 0.0   1st Qu.: 0.0   1st Qu.: 117  
##  Other  :2504   Median : 8   Median : 0.0   Median : 0.0   Median : 178  
##  ProfExe:1343   Mean   : 9   Mean   : 0.3   Mean   : 0.4   Mean   : 180  
##  Sales  : 115   3rd Qu.:12   3rd Qu.: 0.0   3rd Qu.: 0.4   3rd Qu.: 227  
##  Self   : 203   Max.   :41   Max.   :10.0   Max.   :15.0   Max.   :1168  
##       NINQ           CLNO       DEBTINC   
##  Min.   : 0.0   Min.   : 0   Min.   :  1  
##  1st Qu.: 0.0   1st Qu.:15   1st Qu.: 31  
##  Median : 1.0   Median :21   Median : 34  
##  Mean   : 1.2   Mean   :21   Mean   : 34  
##  3rd Qu.: 2.0   3rd Qu.:26   3rd Qu.: 38  
##  Max.   :17.0   Max.   :71   Max.   :203
library(caret)

trainIndex <- createDataPartition(df$REPAY, p = .75, 
                                  list = FALSE, 
                                  times = 1)
dfTrain <- df[ trainIndex,] #75% data training
dfTest  <- df[-trainIndex,] #25% testing
Yte <- df[-trainIndex,1] #test Y

library(h2o)
h2o.init(nthreads = 20, max_mem_size = "16g")
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         8 minutes 738 milliseconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.38 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
# Convert to h2o Frame and identify inputs and output: 
test <- as.h2o(dfTest)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
train <- as.h2o(dfTrain)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
y <- "REPAY" #name of the response variable column
X <- setdiff(names(train), y) # names of the predictors

# Train the GLM model
train.glm <- h2o.glm(x = X, # Vector of predictor variable names
                               y = y, # Name of response/dependent variable
                               training_frame = train, # Training data
                               seed = 1234567,        # Seed for random numbers
                               family = "binomial",   # Outcome variable
                               lambda_search = TRUE,  # Optimum regularisation lambda
                               alpha = 0.5,           # Elastic net regularisation
                               nfolds = 5             # N-fold cross validation
)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |======================================================================| 100%
# Training accuracy (AUC)
train.glm@model$training_metrics@metrics$AUC
## [1] 0.79
# Cross validation accuracy (AUC)
train.glm@model$cross_validation_metrics@metrics$AUC
## [1] 0.79
# Predict on test data
library(tidyverse)

pred_class = h2o.predict(train.glm, test) %>% as.data.frame() %>% pull(predict)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
## predicted values of Y 

#library(caret)
confusionMatrix(pred_class, Yte, positive = "BAD")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  BAD GOOD
##       BAD    89   24
##       GOOD  208 1168
##                                               
##                Accuracy : 0.844               
##                  95% CI : (0.825, 0.862)      
##     No Information Rate : 0.801               
##     P-Value [Acc > NIR] : 0.00000842          
##                                               
##                   Kappa : 0.364               
##                                               
##  Mcnemar's Test P-Value : < 0.0000000000000002
##                                               
##             Sensitivity : 0.2997              
##             Specificity : 0.9799              
##          Pos Pred Value : 0.7876              
##          Neg Pred Value : 0.8488              
##              Prevalence : 0.1995              
##          Detection Rate : 0.0598              
##    Detection Prevalence : 0.0759              
##       Balanced Accuracy : 0.6398              
##                                               
##        'Positive' Class : BAD                 
## 
#compute variable importance and performance
h2o.varimp_plot(train.glm, num_of_features = 5)

Implement Random Forest For Binary Classification Problem with H2O

df=read.csv("LoanDefault.csv")

head(df)
##   REPAY LOAN MORTDUE  VALUE  REASON    JOB  YOJ DEROG DELINQ CLAGE NINQ CLNO
## 1   BAD 1100   25860  39025 HomeImp  Other 10.5  0.00   0.00    94  1.0    9
## 2   BAD 1300   70053  68400 HomeImp  Other  7.0  0.00   2.00   122  0.0   14
## 3   BAD 1500   13500  16700 HomeImp  Other  4.0  0.00   0.00   149  1.0   10
## 4   BAD 1500   73761 101776 DebtCon  Other  8.9  0.25   0.45   180  1.2   21
## 5  GOOD 1700   97800 112000 HomeImp Office  3.0  0.00   0.00    93  0.0   14
## 6   BAD 1700   30548  40320 HomeImp  Other  9.0  0.00   0.00   101  1.0    8
##   DEBTINC
## 1      34
## 2      34
## 3      34
## 4      34
## 5      34
## 6      37
summary(df)
##   REPAY           LOAN          MORTDUE           VALUE            REASON    
##  BAD :1189   Min.   : 1100   Min.   :  2063   Min.   :  8000   DebtCon:4111  
##  GOOD:4771   1st Qu.:11100   1st Qu.: 48139   1st Qu.: 66490   HomeImp:1849  
##              Median :16300   Median : 69529   Median : 90000                 
##              Mean   :18608   Mean   : 73761   Mean   :101776                 
##              3rd Qu.:23300   3rd Qu.: 88200   3rd Qu.:119005                 
##              Max.   :89900   Max.   :399550   Max.   :855909                 
##       JOB            YOJ         DEROG          DELINQ         CLAGE     
##  Mgr    : 808   Min.   : 0   Min.   : 0.0   Min.   : 0.0   Min.   :   0  
##  Office : 987   1st Qu.: 3   1st Qu.: 0.0   1st Qu.: 0.0   1st Qu.: 117  
##  Other  :2504   Median : 8   Median : 0.0   Median : 0.0   Median : 178  
##  ProfExe:1343   Mean   : 9   Mean   : 0.3   Mean   : 0.4   Mean   : 180  
##  Sales  : 115   3rd Qu.:12   3rd Qu.: 0.0   3rd Qu.: 0.4   3rd Qu.: 227  
##  Self   : 203   Max.   :41   Max.   :10.0   Max.   :15.0   Max.   :1168  
##       NINQ           CLNO       DEBTINC   
##  Min.   : 0.0   Min.   : 0   Min.   :  1  
##  1st Qu.: 0.0   1st Qu.:15   1st Qu.: 31  
##  Median : 1.0   Median :21   Median : 34  
##  Mean   : 1.2   Mean   :21   Mean   : 34  
##  3rd Qu.: 2.0   3rd Qu.:26   3rd Qu.: 38  
##  Max.   :17.0   Max.   :71   Max.   :203
library(caret)

trainIndex <- createDataPartition(df$REPAY, p = .75, 
                                  list = FALSE, 
                                  times = 1)

fitControl <- trainControl(## 10-fold CV
  method = "repeatedcv",
  number = 10,
  ## repeated ten times
  repeats = 10)

dfTrain <- df[ trainIndex,] #75% data training
dfTest  <- df[-trainIndex,] #25% testing
Yte <- df[-trainIndex,1] #test Y

library(h2o)
h2o.init(nthreads = 20, max_mem_size = "16g")
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         8 minutes 5 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.38 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
# Convert to h2o Frame and identify inputs and output: 
test <- as.h2o(dfTest)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
train <- as.h2o(dfTrain)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
y <- "REPAY"
x <- setdiff(names(train), y) #predictors

# Train default Random Forest: 
default_rf <- h2o.randomForest(x = x, y = y, 
                               training_frame = train, 
                               stopping_rounds = 5, 
                               stopping_tolerance = 0.001, 
                               stopping_metric = "AUC", 
                               seed = 29, 
                               balance_classes = FALSE, 
                               nfolds = 10)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |======================================================================| 100%
# Model performance based on test data: 

library(tidyverse)

pred_class <- h2o.predict(default_rf, test) %>% as.data.frame() %>% pull(predict)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
#library(caret)
confusionMatrix(pred_class, Yte, positive = "BAD")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  BAD GOOD
##       BAD   217   30
##       GOOD   80 1162
##                                               
##                Accuracy : 0.926               
##                  95% CI : (0.912, 0.939)      
##     No Information Rate : 0.801               
##     P-Value [Acc > NIR] : < 0.0000000000000002
##                                               
##                   Kappa : 0.753               
##                                               
##  Mcnemar's Test P-Value : 0.00000298          
##                                               
##             Sensitivity : 0.731               
##             Specificity : 0.975               
##          Pos Pred Value : 0.879               
##          Neg Pred Value : 0.936               
##              Prevalence : 0.199               
##          Detection Rate : 0.146               
##    Detection Prevalence : 0.166               
##       Balanced Accuracy : 0.853               
##                                               
##        'Positive' Class : BAD                 
## 
# Training accuracy (AUC)
default_rf@model$training_metrics@metrics$AUC
## [1] 0.96
# Cross validation accuracy (AUC)
default_rf@model$cross_validation_metrics@metrics$AUC
## [1] 0.96
#compute variable importance and performance
h2o.varimp_plot(default_rf, num_of_features = 5)

Implement Random Forest For Multiple Classification Problem with H2o

library(h2o)
h2o.init(nthreads = 20, max_mem_size = "16g")
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         8 minutes 18 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.36 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
df=h2o.importFile("covtype.csv")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |======================================================================| 100%
head(df)
##   Elevation Aspect Slope Horizontal_Distance_To_Hydrology
## 1      2596     51     3                              258
## 2      2590     56     2                              212
## 3      2804    139     9                              268
## 4      2785    155    18                              242
## 5      2595     45     2                              153
## 6      2579    132     6                              300
##   Vertical_Distance_To_Hydrology Horizontal_Distance_To_Roadways Hillshade_9am
## 1                              0                             510           221
## 2                             -6                             390           220
## 3                             65                            3180           234
## 4                            118                            3090           238
## 5                             -1                             391           220
## 6                            -15                              67           230
##   Hillshade_Noon Hillshade_3pm Horizontal_Distance_To_Fire_Points
## 1            232           148                               6279
## 2            235           151                               6225
## 3            238           135                               6121
## 4            238           122                               6211
## 5            234           150                               6172
## 6            237           140                               6031
##   Wilderness_Area1 Wilderness_Area2 Wilderness_Area3 Wilderness_Area4
## 1                1                0                0                0
## 2                1                0                0                0
## 3                1                0                0                0
## 4                1                0                0                0
## 5                1                0                0                0
## 6                1                0                0                0
##   Soil_Type1 Soil_Type2 Soil_Type3 Soil_Type4 Soil_Type5 Soil_Type6 Soil_Type7
## 1          0          0          0          0          0          0          0
## 2          0          0          0          0          0          0          0
## 3          0          0          0          0          0          0          0
## 4          0          0          0          0          0          0          0
## 5          0          0          0          0          0          0          0
## 6          0          0          0          0          0          0          0
##   Soil_Type8 Soil_Type9 Soil_Type10 Soil_Type11 Soil_Type12 Soil_Type13
## 1          0          0           0           0           0           0
## 2          0          0           0           0           0           0
## 3          0          0           0           0           1           0
## 4          0          0           0           0           0           0
## 5          0          0           0           0           0           0
## 6          0          0           0           0           0           0
##   Soil_Type14 Soil_Type15 Soil_Type16 Soil_Type17 Soil_Type18 Soil_Type19
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type20 Soil_Type21 Soil_Type22 Soil_Type23 Soil_Type24 Soil_Type25
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type26 Soil_Type27 Soil_Type28 Soil_Type29 Soil_Type30 Soil_Type31
## 1           0           0           0           1           0           0
## 2           0           0           0           1           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           1           0
## 5           0           0           0           1           0           0
## 6           0           0           0           1           0           0
##   Soil_Type32 Soil_Type33 Soil_Type34 Soil_Type35 Soil_Type36 Soil_Type37
## 1           0           0           0           0           0           0
## 2           0           0           0           0           0           0
## 3           0           0           0           0           0           0
## 4           0           0           0           0           0           0
## 5           0           0           0           0           0           0
## 6           0           0           0           0           0           0
##   Soil_Type38 Soil_Type39 Soil_Type40 Cover_Type
## 1           0           0           0          5
## 2           0           0           0          5
## 3           0           0           0          2
## 4           0           0           0          2
## 5           0           0           0          5
## 6           0           0           0          2
df$Cover_Type=as.factor(df$Cover_Type) #response variable

summary(df)
##  Elevation      Aspect          Slope          Horizontal_Distance_To_Hydrology
##  Min.   :1859   Min.   :  0.0   Min.   : 0.0   Min.   :   0.0                  
##  1st Qu.:2809   1st Qu.: 58.0   1st Qu.: 9.0   1st Qu.: 107.6                  
##  Median :2995   Median :127.0   Median :13.0   Median : 216.7                  
##  Mean   :2959   Mean   :155.7   Mean   :14.1   Mean   : 269.4                  
##  3rd Qu.:3163   3rd Qu.:260.0   3rd Qu.:18.0   3rd Qu.: 383.1                  
##  Max.   :3858   Max.   :360.0   Max.   :66.0   Max.   :1397.0                  
##  Vertical_Distance_To_Hydrology Horizontal_Distance_To_Roadways Hillshade_9am  
##  Min.   :-173.00                Min.   :   0                    Min.   :  0.0  
##  1st Qu.:   7.00                1st Qu.:1103                    1st Qu.:198.0  
##  Median :  30.00                Median :1993                    Median :218.0  
##  Mean   :  46.42                Mean   :2350                    Mean   :212.1  
##  3rd Qu.:  69.00                3rd Qu.:3324                    3rd Qu.:231.0  
##  Max.   : 601.00                Max.   :7117                    Max.   :254.0  
##  Hillshade_Noon  Hillshade_3pm   Horizontal_Distance_To_Fire_Points
##  Min.   :  0.0   Min.   :  0.0   Min.   :   0                      
##  1st Qu.:213.0   1st Qu.:119.0   1st Qu.:1019                      
##  Median :226.0   Median :143.0   Median :1707                      
##  Mean   :223.3   Mean   :142.5   Mean   :1980                      
##  3rd Qu.:237.0   3rd Qu.:168.0   3rd Qu.:2547                      
##  Max.   :254.0   Max.   :254.0   Max.   :7173                      
##  Wilderness_Area1 Wilderness_Area2  Wilderness_Area3 Wilderness_Area4 
##  Min.   :0.0000   Min.   :0.00000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.:0.00000  
##  Median :0.0000   Median :0.00000   Median :0.0000   Median :0.00000  
##  Mean   :0.4489   Mean   :0.05143   Mean   :0.4361   Mean   :0.06363  
##  3rd Qu.:1.0000   3rd Qu.:0.00000   3rd Qu.:1.0000   3rd Qu.:0.00000  
##  Max.   :1.0000   Max.   :1.00000   Max.   :1.0000   Max.   :1.00000  
##  Soil_Type1         Soil_Type2        Soil_Type3         Soil_Type4       
##  Min.   :0.000000   Min.   :0.00000   Min.   :0.000000   Min.   :0.00000  
##  1st Qu.:0.000000   1st Qu.:0.00000   1st Qu.:0.000000   1st Qu.:0.00000  
##  Median :0.000000   Median :0.00000   Median :0.000000   Median :0.00000  
##  Mean   :0.005217   Mean   :0.01295   Mean   :0.008301   Mean   :0.02134  
##  3rd Qu.:0.000000   3rd Qu.:0.00000   3rd Qu.:0.000000   3rd Qu.:0.00000  
##  Max.   :1.000000   Max.   :1.00000   Max.   :1.000000   Max.   :1.00000  
##  Soil_Type5         Soil_Type6        Soil_Type7          Soil_Type8         
##  Min.   :0.000000   Min.   :0.00000   Min.   :0.0000000   Min.   :0.0000000  
##  1st Qu.:0.000000   1st Qu.:0.00000   1st Qu.:0.0000000   1st Qu.:0.0000000  
##  Median :0.000000   Median :0.00000   Median :0.0000000   Median :0.0000000  
##  Mean   :0.002749   Mean   :0.01132   Mean   :0.0001807   Mean   :0.0003081  
##  3rd Qu.:0.000000   3rd Qu.:0.00000   3rd Qu.:0.0000000   3rd Qu.:0.0000000  
##  Max.   :1.000000   Max.   :1.00000   Max.   :1.0000000   Max.   :1.0000000  
##  Soil_Type9         Soil_Type10       Soil_Type11       Soil_Type12      
##  Min.   :0.000000   Min.   :0.00000   Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.000000   1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.000000   Median :0.00000   Median :0.00000   Median :0.00000  
##  Mean   :0.001974   Mean   :0.05617   Mean   :0.02136   Mean   :0.05158  
##  3rd Qu.:0.000000   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :1.000000   Max.   :1.00000   Max.   :1.00000   Max.   :1.00000  
##  Soil_Type13    Soil_Type14        Soil_Type15           Soil_Type16       
##  Min.   :0.00   Min.   :0.000000   Min.   :0.000000000   Min.   :0.000000  
##  1st Qu.:0.00   1st Qu.:0.000000   1st Qu.:0.000000000   1st Qu.:0.000000  
##  Median :0.00   Median :0.000000   Median :0.000000000   Median :0.000000  
##  Mean   :0.03   Mean   :0.001031   Mean   :0.000005163   Mean   :0.004897  
##  3rd Qu.:0.00   3rd Qu.:0.000000   3rd Qu.:0.000000000   3rd Qu.:0.000000  
##  Max.   :1.00   Max.   :1.000000   Max.   :1.000000000   Max.   :1.000000  
##  Soil_Type17       Soil_Type18        Soil_Type19        Soil_Type20      
##  Min.   :0.00000   Min.   :0.000000   Min.   :0.000000   Min.   :0.00000  
##  1st Qu.:0.00000   1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.:0.00000  
##  Median :0.00000   Median :0.000000   Median :0.000000   Median :0.00000  
##  Mean   :0.00589   Mean   :0.003268   Mean   :0.006921   Mean   :0.01594  
##  3rd Qu.:0.00000   3rd Qu.:0.000000   3rd Qu.:0.000000   3rd Qu.:0.00000  
##  Max.   :1.00000   Max.   :1.000000   Max.   :1.000000   Max.   :1.00000  
##  Soil_Type21        Soil_Type22       Soil_Type23      Soil_Type24      
##  Min.   :0.000000   Min.   :0.00000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.:0.000000   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.:0.00000  
##  Median :0.000000   Median :0.00000   Median :0.0000   Median :0.00000  
##  Mean   :0.001442   Mean   :0.05744   Mean   :0.0994   Mean   :0.03662  
##  3rd Qu.:0.000000   3rd Qu.:0.00000   3rd Qu.:0.0000   3rd Qu.:0.00000  
##  Max.   :1.000000   Max.   :1.00000   Max.   :1.0000   Max.   :1.00000  
##  Soil_Type25         Soil_Type26        Soil_Type27        Soil_Type28       
##  Min.   :0.0000000   Min.   :0.000000   Min.   :0.000000   Min.   :0.000000  
##  1st Qu.:0.0000000   1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.:0.000000  
##  Median :0.0000000   Median :0.000000   Median :0.000000   Median :0.000000  
##  Mean   :0.0008158   Mean   :0.004456   Mean   :0.001869   Mean   :0.001628  
##  3rd Qu.:0.0000000   3rd Qu.:0.000000   3rd Qu.:0.000000   3rd Qu.:0.000000  
##  Max.   :1.0000000   Max.   :1.000000   Max.   :1.000000   Max.   :1.000000  
##  Soil_Type29      Soil_Type30       Soil_Type31       Soil_Type32      
##  Min.   :0.0000   Min.   :0.00000   Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.0000   Median :0.00000   Median :0.00000   Median :0.00000  
##  Mean   :0.1984   Mean   :0.05193   Mean   :0.04417   Mean   :0.09039  
##  3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :1.0000   Max.   :1.00000   Max.   :1.00000   Max.   :1.00000  
##  Soil_Type33       Soil_Type34        Soil_Type35        Soil_Type36        
##  Min.   :0.00000   Min.   :0.000000   Min.   :0.000000   Min.   :0.0000000  
##  1st Qu.:0.00000   1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.:0.0000000  
##  Median :0.00000   Median :0.000000   Median :0.000000   Median :0.0000000  
##  Mean   :0.07772   Mean   :0.002773   Mean   :0.003255   Mean   :0.0002048  
##  3rd Qu.:0.00000   3rd Qu.:0.000000   3rd Qu.:0.000000   3rd Qu.:0.0000000  
##  Max.   :1.00000   Max.   :1.000000   Max.   :1.000000   Max.   :1.0000000  
##  Soil_Type37         Soil_Type38      Soil_Type39       Soil_Type40      
##  Min.   :0.0000000   Min.   :0.0000   Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.0000000   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.0000000   Median :0.0000   Median :0.00000   Median :0.00000  
##  Mean   :0.0005129   Mean   :0.0268   Mean   :0.02376   Mean   :0.01506  
##  3rd Qu.:0.0000000   3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :1.0000000   Max.   :1.0000   Max.   :1.00000   Max.   :1.00000  
##  Cover_Type
##  2:283301  
##  1:211840  
##  3: 35754  
##  7: 20510  
##  6: 17367  
##  5:  9493
### split data for training, validation and testing
splits <- h2o.splitFrame(df,c(0.7,0.1),seed=1234)

train <- h2o.assign(splits[[1]], "train.hex")  #70% 
## assign the first result the R variable train
## and the H2O name train.hex
valid <- h2o.assign(splits[[2]], "valid.hex")   ## R valid, H2O valid.hex
test <- h2o.assign(splits[[3]], "test.hex")     ## R test, H2O test.hex

rf1=h2o.randomForest(training_frame = train,        ## the H2O frame for training
                     validation_frame = valid,      ## the H2O frame for validation (not required)
                     x=1:12,                        ## the predictor columns, by column index
                     y=13,                          ## the target index (what we are predicting)
                     model_id = "rf_covType_v1",    ## name the model in H2O
                     ##   not required, but helps use Flow
                     ntrees = 150,                  ## use a maximum of 200 trees to create the
                     ##  random forest model. The default is 50.
                     ##  I have increased it because I will let 
                     ##  the early stopping criteria decide when
                     ##  the random forest is sufficiently accurate
                     stopping_rounds = 2,           ## Stop fitting new trees when the 2-tree
                     ##  average is within 0.001 (default) of 
                     ##  the prior two 2-tree averages.
                     ##  Can be thought of as a convergence setting
                     score_each_iteration = T,      ## Predict against training and validation for
                     ##  each tree. Default will skip several.
                     seed = 1000000)                ## Set the random seed so that this can be
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |======================================================================| 100%
##  reproduced.

summary(rf1)  
## Model Details:
## ==============
## 
## H2ORegressionModel: drf
## Model Key:  rf_covType_v1 
## Model Summary: 
##   number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1              20                       20              306131        20
##   max_depth mean_depth min_leaves max_leaves mean_leaves
## 1        20   20.00000        992       1555  1212.85000
## 
## H2ORegressionMetrics: drf
## ** Reported on training data. **
## ** Metrics reported on Out-Of-Bag training samples **
## 
## MSE:  0.0018
## RMSE:  0.043
## MAE:  0.0051
## RMSLE:  0.031
## Mean Residual Deviance :  0.0018
## 
## 
## H2ORegressionMetrics: drf
## ** Reported on validation data. **
## 
## MSE:  0.0014
## RMSE:  0.037
## MAE:  0.005
## RMSLE:  0.027
## Mean Residual Deviance :  0.0014
## 
## 
## 
## 
## Scoring History: 
##             timestamp   duration number_of_trees training_rmse training_mae
## 1 2020-05-24 23:47:33  0.001 sec               0            NA           NA
## 2 2020-05-24 23:47:34  0.348 sec               1       0.06631      0.00475
## 3 2020-05-24 23:47:34  0.662 sec               2       0.06426      0.00490
## 4 2020-05-24 23:47:34  0.964 sec               3       0.06143      0.00486
## 5 2020-05-24 23:47:35  1.269 sec               4       0.05858      0.00474
##   training_deviance validation_rmse validation_mae validation_deviance
## 1                NA              NA             NA                  NA
## 2           0.00440         0.06592        0.00476             0.00435
## 3           0.00413         0.05138        0.00461             0.00264
## 4           0.00377         0.04616        0.00451             0.00213
## 5           0.00343         0.04422        0.00459             0.00196
## 
## ---
##              timestamp   duration number_of_trees training_rmse training_mae
## 16 2020-05-24 23:47:40  6.487 sec              15       0.04471      0.00503
## 17 2020-05-24 23:47:40  6.908 sec              16       0.04421      0.00505
## 18 2020-05-24 23:47:41  7.324 sec              17       0.04393      0.00507
## 19 2020-05-24 23:47:41  7.952 sec              18       0.04351      0.00508
## 20 2020-05-24 23:47:42  8.563 sec              19       0.04336      0.00515
## 21 2020-05-24 23:47:42  9.118 sec              20       0.04291      0.00514
##    training_deviance validation_rmse validation_mae validation_deviance
## 16           0.00200         0.03764        0.00483             0.00142
## 17           0.00195         0.03758        0.00486             0.00141
## 18           0.00193         0.03754        0.00487             0.00141
## 19           0.00189         0.03721        0.00487             0.00138
## 20           0.00188         0.03755        0.00497             0.00141
## 21           0.00184         0.03723        0.00495             0.00139
## 
## Variable Importances: (Extract with `h2o.varimp`) 
## =================================================
## 
## Variable Importances: 
##                              variable relative_importance scaled_importance
## 1                    Wilderness_Area1       863430.062500          1.000000
## 2                           Elevation       179718.796875          0.208145
## 3                    Wilderness_Area2       151199.687500          0.175115
## 4     Horizontal_Distance_To_Roadways       127215.937500          0.147338
## 5  Horizontal_Distance_To_Fire_Points        81181.640625          0.094022
## 6                               Slope         8564.956055          0.009920
## 7                              Aspect         8198.708984          0.009496
## 8                      Hillshade_Noon         7518.732910          0.008708
## 9      Vertical_Distance_To_Hydrology         6939.757324          0.008037
## 10   Horizontal_Distance_To_Hydrology         6224.021973          0.007208
## 11                      Hillshade_9am         3533.488281          0.004092
## 12                      Hillshade_3pm         2926.276611          0.003389
##    percentage
## 1    0.596847
## 2    0.124231
## 3    0.104517
## 4    0.087938
## 5    0.056117
## 6    0.005921
## 7    0.005667
## 8    0.005197
## 9    0.004797
## 10   0.004302
## 11   0.002443
## 12   0.002023
h2o.varimp(rf1)
## Variable Importances: 
##                              variable relative_importance scaled_importance
## 1                    Wilderness_Area1       863430.062500          1.000000
## 2                           Elevation       179718.796875          0.208145
## 3                    Wilderness_Area2       151199.687500          0.175115
## 4     Horizontal_Distance_To_Roadways       127215.937500          0.147338
## 5  Horizontal_Distance_To_Fire_Points        81181.640625          0.094022
## 6                               Slope         8564.956055          0.009920
## 7                              Aspect         8198.708984          0.009496
## 8                      Hillshade_Noon         7518.732910          0.008708
## 9      Vertical_Distance_To_Hydrology         6939.757324          0.008037
## 10   Horizontal_Distance_To_Hydrology         6224.021973          0.007208
## 11                      Hillshade_9am         3533.488281          0.004092
## 12                      Hillshade_3pm         2926.276611          0.003389
##    percentage
## 1    0.596847
## 2    0.124231
## 3    0.104517
## 4    0.087938
## 5    0.056117
## 6    0.005921
## 7    0.005667
## 8    0.005197
## 9    0.004797
## 10   0.004302
## 11   0.002443
## 12   0.002023
rf1@model$validation_metrics 
## H2ORegressionMetrics: drf
## ** Reported on validation data. **
## 
## MSE:  0.0014
## RMSE:  0.037
## MAE:  0.005
## RMSLE:  0.027
## Mean Residual Deviance :  0.0014
rf.VI=h2o.varimp(rf1)
print(rf.VI)
## Variable Importances: 
##                              variable relative_importance scaled_importance
## 1                    Wilderness_Area1       863430.062500          1.000000
## 2                           Elevation       179718.796875          0.208145
## 3                    Wilderness_Area2       151199.687500          0.175115
## 4     Horizontal_Distance_To_Roadways       127215.937500          0.147338
## 5  Horizontal_Distance_To_Fire_Points        81181.640625          0.094022
## 6                               Slope         8564.956055          0.009920
## 7                              Aspect         8198.708984          0.009496
## 8                      Hillshade_Noon         7518.732910          0.008708
## 9      Vertical_Distance_To_Hydrology         6939.757324          0.008037
## 10   Horizontal_Distance_To_Hydrology         6224.021973          0.007208
## 11                      Hillshade_9am         3533.488281          0.004092
## 12                      Hillshade_3pm         2926.276611          0.003389
##    percentage
## 1    0.596847
## 2    0.124231
## 3    0.104517
## 4    0.087938
## 5    0.056117
## 6    0.005921
## 7    0.005667
## 8    0.005197
## 9    0.004797
## 10   0.004302
## 11   0.002443
## 12   0.002023
print("Variable importance from Random Forest without normalization")
## [1] "Variable importance from Random Forest without normalization"
print(t(rf.VI[,1:2]))
##                     [,1]               [,2]        [,3]              
## variable            "Wilderness_Area1" "Elevation" "Wilderness_Area2"
## relative_importance "863430"           "179719"    "151200"          
##                     [,4]                             
## variable            "Horizontal_Distance_To_Roadways"
## relative_importance "127216"                         
##                     [,5]                                 [,6]     [,7]    
## variable            "Horizontal_Distance_To_Fire_Points" "Slope"  "Aspect"
## relative_importance " 81182"                             "  8565" "  8199"
##                     [,8]             [,9]                            
## variable            "Hillshade_Noon" "Vertical_Distance_To_Hydrology"
## relative_importance "  7519"         "  6940"                        
##                     [,10]                              [,11]          
## variable            "Horizontal_Distance_To_Hydrology" "Hillshade_9am"
## relative_importance "  6224"                           "  3533"       
##                     [,12]          
## variable            "Hillshade_3pm"
## relative_importance "  2926"
# RF variable importance With normalization, i.e scale =T (divide mean decrease accuracy by standard deviation)
norm_rf.VI <- rf.VI$relative_importance/max(rf.VI$relative_importance)
print("Variable importance from Random Forest with normalization")
## [1] "Variable importance from Random Forest with normalization"
print(t(rf.VI[,c(1,3)]))
##                   [,1]               [,2]        [,3]              
## variable          "Wilderness_Area1" "Elevation" "Wilderness_Area2"
## scaled_importance "1.0000"           "0.2081"    "0.1751"          
##                   [,4]                             
## variable          "Horizontal_Distance_To_Roadways"
## scaled_importance "0.1473"                         
##                   [,5]                                 [,6]     [,7]    
## variable          "Horizontal_Distance_To_Fire_Points" "Slope"  "Aspect"
## scaled_importance "0.0940"                             "0.0099" "0.0095"
##                   [,8]             [,9]                            
## variable          "Hillshade_Noon" "Vertical_Distance_To_Hydrology"
## scaled_importance "0.0087"         "0.0080"                        
##                   [,10]                              [,11]          
## variable          "Horizontal_Distance_To_Hydrology" "Hillshade_9am"
## scaled_importance "0.0072"                           "0.0041"       
##                   [,12]          
## variable          "Hillshade_3pm"
## scaled_importance "0.0034"
# Plot variable importance from Random Forest
barplot(rf.VI$scaled_importance,beside=T,names.arg=rf.VI$variable,las=2,main="VI from RF")

Gradient Boosting Machines (GBM) for Binary Classification using H2o

df=read.csv("LoanDefault.csv")

head(df)
##   REPAY LOAN MORTDUE  VALUE  REASON    JOB  YOJ DEROG DELINQ CLAGE NINQ CLNO
## 1   BAD 1100   25860  39025 HomeImp  Other 10.5  0.00   0.00    94  1.0    9
## 2   BAD 1300   70053  68400 HomeImp  Other  7.0  0.00   2.00   122  0.0   14
## 3   BAD 1500   13500  16700 HomeImp  Other  4.0  0.00   0.00   149  1.0   10
## 4   BAD 1500   73761 101776 DebtCon  Other  8.9  0.25   0.45   180  1.2   21
## 5  GOOD 1700   97800 112000 HomeImp Office  3.0  0.00   0.00    93  0.0   14
## 6   BAD 1700   30548  40320 HomeImp  Other  9.0  0.00   0.00   101  1.0    8
##   DEBTINC
## 1      34
## 2      34
## 3      34
## 4      34
## 5      34
## 6      37
summary(df)
##   REPAY           LOAN          MORTDUE           VALUE            REASON    
##  BAD :1189   Min.   : 1100   Min.   :  2063   Min.   :  8000   DebtCon:4111  
##  GOOD:4771   1st Qu.:11100   1st Qu.: 48139   1st Qu.: 66490   HomeImp:1849  
##              Median :16300   Median : 69529   Median : 90000                 
##              Mean   :18608   Mean   : 73761   Mean   :101776                 
##              3rd Qu.:23300   3rd Qu.: 88200   3rd Qu.:119005                 
##              Max.   :89900   Max.   :399550   Max.   :855909                 
##       JOB            YOJ         DEROG          DELINQ         CLAGE     
##  Mgr    : 808   Min.   : 0   Min.   : 0.0   Min.   : 0.0   Min.   :   0  
##  Office : 987   1st Qu.: 3   1st Qu.: 0.0   1st Qu.: 0.0   1st Qu.: 117  
##  Other  :2504   Median : 8   Median : 0.0   Median : 0.0   Median : 178  
##  ProfExe:1343   Mean   : 9   Mean   : 0.3   Mean   : 0.4   Mean   : 180  
##  Sales  : 115   3rd Qu.:12   3rd Qu.: 0.0   3rd Qu.: 0.4   3rd Qu.: 227  
##  Self   : 203   Max.   :41   Max.   :10.0   Max.   :15.0   Max.   :1168  
##       NINQ           CLNO       DEBTINC   
##  Min.   : 0.0   Min.   : 0   Min.   :  1  
##  1st Qu.: 0.0   1st Qu.:15   1st Qu.: 31  
##  Median : 1.0   Median :21   Median : 34  
##  Mean   : 1.2   Mean   :21   Mean   : 34  
##  3rd Qu.: 2.0   3rd Qu.:26   3rd Qu.: 38  
##  Max.   :17.0   Max.   :71   Max.   :203
library(caret)

trainIndex <- createDataPartition(df$REPAY, p = .75, 
                                  list = FALSE, 
                                  times = 1)

fitControl <- trainControl(## 10-fold CV
  method = "repeatedcv",
  number = 10,
  ## repeated ten times
  repeats = 10)

dfTrain <- df[ trainIndex,] #75% data training
dfTest  <- df[-trainIndex,] #25% testing
Yte <- df[-trainIndex,1] #test Y

library(h2o)
h2o.init(nthreads = 20, max_mem_size = "16g")
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         8 minutes 34 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.69 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
# Convert to h2o Frame and identify inputs and output: 
test <- as.h2o(dfTest)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
train <- as.h2o(dfTrain)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
y <- "REPAY"
x <- setdiff(names(train), y) #predictors

# Train default Random Forest: 
default_gbm <- h2o.gbm(x = x, y = y, 
                               training_frame = train, 
                               stopping_rounds = 5, 
                               stopping_tolerance = 0.001, 
                               stopping_metric = "AUC", 
                               seed = 29, 
                               balance_classes = FALSE, 
                               nfolds = 10)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |======================================================================| 100%
# Model performance based on test data: 

library(tidyverse)

pred_class <- h2o.predict(default_gbm, test) %>% as.data.frame() %>% pull(predict)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
#library(caret)
confusionMatrix(pred_class, Yte, positive = "BAD")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  BAD GOOD
##       BAD   196   51
##       GOOD  101 1141
##                                               
##                Accuracy : 0.898               
##                  95% CI : (0.881, 0.913)      
##     No Information Rate : 0.801               
##     P-Value [Acc > NIR] : < 0.0000000000000002
##                                               
##                   Kappa : 0.659               
##                                               
##  Mcnemar's Test P-Value : 0.0000705           
##                                               
##             Sensitivity : 0.660               
##             Specificity : 0.957               
##          Pos Pred Value : 0.794               
##          Neg Pred Value : 0.919               
##              Prevalence : 0.199               
##          Detection Rate : 0.132               
##    Detection Prevalence : 0.166               
##       Balanced Accuracy : 0.809               
##                                               
##        'Positive' Class : BAD                 
## 
# Training accuracy (AUC)
default_gbm@model$training_metrics@metrics$AUC
## [1] 0.97
# Cross validation accuracy (AUC)
default_gbm@model$cross_validation_metrics@metrics$AUC
## [1] 0.93
#compute variable importance and performance
h2o.varimp_plot(default_gbm, num_of_features = 5)

# Eval performance:
perf <- h2o.performance(default_gbm)
perf
## H2OBinomialMetrics: gbm
## ** Reported on training data. **
## 
## MSE:  0.055
## RMSE:  0.24
## LogLoss:  0.2
## Mean Per-Class Error:  0.14
## AUC:  0.97
## AUCPR:  0.99
## Gini:  0.93
## R^2:  0.65
## 
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
##        BAD GOOD    Error       Rate
## BAD    671  221 0.247758   =221/892
## GOOD   111 3468 0.031014  =111/3579
## Totals 782 3689 0.074256  =332/4471
## 
## Maximum Metrics: Maximum metrics at their respective thresholds
##                         metric threshold       value idx
## 1                       max f1  0.533824    0.954320 218
## 2                       max f2  0.351409    0.976892 281
## 3                 max f0point5  0.824648    0.961664 128
## 4                 max accuracy  0.537417    0.925744 217
## 5                max precision  0.980550    1.000000   0
## 6                   max recall  0.188421    1.000000 328
## 7              max specificity  0.980550    1.000000   0
## 8             max absolute_mcc  0.734786    0.771097 162
## 9   max min_per_class_accuracy  0.820411    0.908072 130
## 10 max mean_per_class_accuracy  0.824648    0.909619 128
## 11                     max tns  0.980550  892.000000   0
## 12                     max fns  0.980550 3574.000000   0
## 13                     max fps  0.009547  892.000000 399
## 14                     max tps  0.188421 3579.000000 328
## 15                     max tnr  0.980550    1.000000   0
## 16                     max fnr  0.980550    0.998603   0
## 17                     max fpr  0.009547    1.000000 399
## 18                     max tpr  0.188421    1.000000 328
## 
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
# retrieve the AUC for the performance object:
h2o.auc(perf)
## [1] 0.97
# retrieve the AUC for both the training and validation data:
h2o.auc(default_gbm, train=TRUE, valid=FALSE, xval=FALSE)
## [1] 0.97
plot(perf, type="roc")

Artificial Neural Networks (ANN) and Deep Neural Networks with H2o

Implement an ANN with H2o For Multi-Class Supervised Classification using H2o

############################################################

library(nnet)
library(caret)

set.seed(1234)

# loading data
f.data = read.csv("dataset.csv")

head(f.data)
##   label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10
## 1     2      0      0      0      0      0      0      0      0      0       0
## 2     9      0      0      0      0      0      0      0      0      0       0
## 3     6      0      0      0      0      0      0      0      5      0       0
## 4     0      0      0      0      1      2      0      0      0      0       0
## 5     3      0      0      0      0      0      0      0      0      0       0
## 6     4      0      0      0      5      4      5      5      3      5       6
##   pixel11 pixel12 pixel13 pixel14 pixel15 pixel16 pixel17 pixel18 pixel19
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     105      92     101     107     100     132       0       0
## 4     114     183     112      55      23      72     102     165     160
## 5       0       0      46       0      21      68       0       0       0
## 6       2       0       0       0       0       0       0       0       2
##   pixel20 pixel21 pixel22 pixel23 pixel24 pixel25 pixel26 pixel27 pixel28
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       2       4       0       1       0       0       0       0       0
## 4      28       0       0       0       1       0       0       0       0
## 5       0       0       0       0       0       0       0       0       0
## 6       3       7       4       4       5       5       0       0       0
##   pixel29 pixel30 pixel31 pixel32 pixel33 pixel34 pixel35 pixel36 pixel37
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0       0
## 4       0       0       0       0       1       0       0      24     188
## 5       0       0       0       0       0       0       0       0      25
## 6       0       0       0       4       5       4       5      11       2
##   pixel38 pixel39 pixel40 pixel41 pixel42 pixel43 pixel44 pixel45 pixel46
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     150     227     211     171     191     236     228     172
## 4     163      93     136     153     168     252     174     136     166
## 5     187     189       0     119     173     239     112       0     189
## 6       0       0      21     231     156     134     177     194       1
##   pixel47 pixel48 pixel49 pixel50 pixel51 pixel52 pixel53 pixel54 pixel55
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       1       0       0       0       0
## 4     130     123     131      66       0       0       1       0       0
## 5     125       0       0       0       0       0       0       0       0
## 6       0       0       0      10       5       5       4       0       0
##   pixel56 pixel57 pixel58 pixel59 pixel60 pixel61 pixel62 pixel63 pixel64
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0      48
## 4       0       0       0       0       2       0      10     157     216
## 5       0       0       0       0       0       0       0       0      14
## 6       0       0       0       0       4       5       3       7       0
##   pixel65 pixel66 pixel67 pixel68 pixel69 pixel70 pixel71 pixel72 pixel73
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     153     209     210     187     199     142     157     209     179
## 4     226     208     142      66     115     149     230     190     196
## 5     219     224     211      92      52     227     224     127      92
## 6       0      26     112     168     162     212     226     199     146
##   pixel74 pixel75 pixel76 pixel77 pixel78 pixel79 pixel80 pixel81 pixel82
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     199     233     138      44       0       0       0       0       0
## 4     198     172     222     107     165     211       0       0       0
## 5     196     237     136       0       0       0       0       0       0
## 6     168     124      25       0       0       0       4       5       0
##   pixel83 pixel84 pixel85 pixel86 pixel87 pixel88 pixel89 pixel90 pixel91
## 1       0       0       0       0       0       0       4       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0     120
## 4       0       0       0       0       0       0       0     118     214
## 5       0       0       0       0       0       0       0       0       0
## 6       0       0       0       0       0       4       5       8       0
##   pixel92 pixel93 pixel94 pixel95 pixel96 pixel97 pixel98 pixel99 pixel100
## 1       0       0       0      62      61      21      29      23       51
## 2       0       0       0       0       0       0       0       0        0
## 3     218     215     207     198     198     223     219     214      225
## 4     174     168     109     200     124     150     143      58       63
## 5     106     238     202     205     224     225     217     218      222
## 6      15      95     142     170     144     123     156     172      140
##   pixel101 pixel102 pixel103 pixel104 pixel105 pixel106 pixel107 pixel108
## 1      136       61        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      190      177      195      209      204      139        0        1
## 4       89      137       97      168      138      142      195      156
## 5      227      202      206      218        0        0        0        0
## 6      131      135      160      137      141       53        0        0
##   pixel109 pixel110 pixel111 pixel112 pixel113 pixel114 pixel115 pixel116
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        3        0        0        0        0        0        0        5
##   pixel117 pixel118 pixel119 pixel120 pixel121 pixel122 pixel123 pixel124
## 1        0        0        0       88      201      228      225      255
## 2        0        0        0        0        0        0        0        0
## 3        0        0      208      200      197      202      193      203
## 4       25      140       70       80       43       71       96       93
## 5        0        0        0      166      239      216      214      192
## 6        6        0        0      165      130      120      130       56
##   pixel125 pixel126 pixel127 pixel128 pixel129 pixel130 pixel131 pixel132
## 1      115       62      137      255      235      222      255      135
## 2        0        0        0        0        0        0        0        0
## 3      207      228      245      196      197      202      193      190
## 4      151      121      197      143      107       82      101      111
## 5      181      205      204      180      187      205      208      230
## 6      124      122       68      143      111       98      132      125
##   pixel133 pixel134 pixel135 pixel136 pixel137 pixel138 pixel139 pixel140
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      167      203       27        0        0        0        0        0
## 4       80      137      193      208        6        0        0        0
## 5       45        0        0        0        0        0        0        0
## 6      111      164       60        0        6        0        0        0
##   pixel141 pixel142 pixel143 pixel144 pixel145 pixel146 pixel147 pixel148
## 1        0        0        0        0        0       47      252      234
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       89      217      200
## 4        0        0        0        0       74      194      107      146
## 5        0        0        0        0        0        0        0      220
## 6        0        0        0        3        7        0       99      165
##   pixel149 pixel150 pixel151 pixel152 pixel153 pixel154 pixel155 pixel156
## 1      238      224      215      215      229      108      180      207
## 2        0        0        0        0        1        1        0        0
## 3      211      175      189      205      199      218      216      181
## 4      178      185      182       77      185      218      210      175
## 5      235      233      213      205      201      185      184      200
## 6      135      154       98       66      130      141      114      143
##   pixel157 pixel158 pixel159 pixel160 pixel161 pixel162 pixel163 pixel164
## 1      214      224      231      249      254       45        0        0
## 2        0        0       81       21        0        1        0        1
## 3      179      185      192      198      184      198      121        0
## 4      174      235      217      217      129      180      210      208
## 5      199      223      224      232      101        0        0        0
## 6      113       71      107      147      117      176      172        0
##   pixel165 pixel166 pixel167 pixel168 pixel169 pixel170 pixel171 pixel172
## 1        0        0        0        0        0        0        1        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4       89        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        5        0        0        0        0        0        0        4
##   pixel173 pixel174 pixel175 pixel176 pixel177 pixel178 pixel179 pixel180
## 1        0      214      222      210      213      224      225      217
## 2        0        0        0        0        0        0        0        1
## 3        0      140      220      204      223      187      182      200
## 4      179      213      203      177      228      192      193      162
## 5        0        0        0      252      238      226      226      221
## 6        0        0      197      188      169      148      146      132
##   pixel181 pixel182 pixel183 pixel184 pixel185 pixel186 pixel187 pixel188
## 1      220      254      233      219      221      217      223      221
## 2        0        0        0        0        0       14      200       49
## 3      207      217      213      210      207      180      161      192
## 4      143      172      196      205      181      180      140      134
## 5      224      195      198      223      229      205      202      235
## 6      110      133      107      139      131      133      141      156
##   pixel189 pixel190 pixel191 pixel192 pixel193 pixel194 pixel195 pixel196
## 1      240      254        0        0        1        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      184      199      162        0        0        0        0        0
## 4      176      194      171      170       65        0        0        0
## 5      158        0        0        0        0        0        0        0
## 6      143      170      186        0        0        0        0        0
##   pixel197 pixel198 pixel199 pixel200 pixel201 pixel202 pixel203 pixel204
## 1        1        0        0        0      128      237      207      224
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      166      218      203
## 4        0        0        0        0      184      194      229      209
## 5        0        0        0        0        0        0        9      228
## 6        0        0        0        6        0        0      225      130
##   pixel205 pixel206 pixel207 pixel208 pixel209 pixel210 pixel211 pixel212
## 1      224      207      216      214      210      208      211      221
## 2        2        1        1        1        0        1        0        0
## 3      195      185      196      189      167      181      206      177
## 4      176      198      129      227      225      140      196      130
## 5      241      125      238      228      225      212      215      222
## 6      140      169      148      110       45      117      137      118
##   pixel213 pixel214 pixel215 pixel216 pixel217 pixel218 pixel219 pixel220
## 1      208      219      213      226      211      237      150        0
## 2        0      162      184      165       20        0        1        1
## 3      176      181      165      187      179      189      183        0
## 4      179      145      109       79      182      223      164      195
## 5      250      168      141      243      197        0        0        0
## 6       53       97      147      147      154      133      202       18
##   pixel221 pixel222 pixel223 pixel224 pixel225 pixel226 pixel227 pixel228
## 1        0        0        0        0        0        2        0        0
## 2        3        4        1        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4      233        0        0        0        0        0        0       38
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        7
##   pixel229 pixel230 pixel231 pixel232 pixel233 pixel234 pixel235 pixel236
## 1      237      222      215      207      210      212      213      206
## 2        0        0        0        0        0        0        0        1
## 3        0      196      215      211      206      201      196      180
## 4      180      177      213      202      159      129       98      179
## 5        0        0       51      232      247       34      225      237
## 6        0       48      211      135      124      140       83      158
##   pixel237 pixel238 pixel239 pixel240 pixel241 pixel242 pixel243 pixel244
## 1      214      213      214      213      210      215      214      206
## 2        2        0        2        0      120      183      154      206
## 3      188      210      206      190      173      180      187      192
## 4      149       90      187      211       61      134       91       57
## 5      219      222      223      216      248      182      124      246
## 6      145      160      126      151      136      156      113      110
##   pixel245 pixel246 pixel247 pixel248 pixel249 pixel250 pixel251 pixel252
## 1      199      218      255       13        0        2        0        0
## 2       32        0        5        5        0        0        0        0
## 3      184      192      199        0        0        0        0        0
## 4      118      212      220      218      207        0        0        0
## 5      200        0        0        0        0        0        0        0
## 6      167      168      217       93        0        0        0        0
##   pixel253 pixel254 pixel255 pixel256 pixel257 pixel258 pixel259 pixel260
## 1        0        4        0       85      228      210      218      200
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      212      217      224
## 4        0        0        0      114      154      142      182      219
## 5        0        0        0        0        0        0       69      236
## 6        0        0        0        6        0      104      219      208
##   pixel261 pixel262 pixel263 pixel264 pixel265 pixel266 pixel267 pixel268
## 1      211      208      203      215      210      209      209      210
## 2        0        0        0        2        0        3        0       76
## 3      211      216      201      189      184      202      210      194
## 4      130       88       81       52       54      106       93      110
## 5      255        0      218      240      219      221      222      214
## 6      166      157      130      170      137      144      155      160
##   pixel269 pixel270 pixel271 pixel272 pixel273 pixel274 pixel275 pixel276
## 1      213      211      210      217      206      213      231      175
## 2      227      151      139      158        0        0        0        0
## 3      182      173      173      176      192      195      208        0
## 4      159      222      227       83      117      253      218      210
## 5      252      153       71      248      215        0        0        0
## 6      150      160      158      120      194      220      213      128
##   pixel277 pixel278 pixel279 pixel280 pixel281 pixel282 pixel283 pixel284
## 1        0        0        0        0        0        0        0      217
## 2        0       66       23        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4      206       48        0        0        0        0        0       18
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        6
##   pixel285 pixel286 pixel287 pixel288 pixel289 pixel290 pixel291 pixel292
## 1      224      215      206      205      204      217      230      222
## 2        0        0        0        0        1        0        1        0
## 3        0      222      215      228      203      212      199      198
## 4      127      208      228      185      172      240       91      126
## 5        0        0       82      248      247        0      231      236
## 6        0      133      213      235      206      169      194      139
##   pixel293 pixel294 pixel295 pixel296 pixel297 pixel298 pixel299 pixel300
## 1      215      224      233      228      232      228      224      207
## 2        0        0       55      224      162      143      121      204
## 3      191      186      228      195      191      161      178      177
## 4      208      165      154      213      214      229      215      175
## 5      219      221      221      212      253      109        0      247
## 6       76      119      178      139       99      120      199      167
##   pixel301 pixel302 pixel303 pixel304 pixel305 pixel306 pixel307 pixel308
## 1      212      215      213      229       31        0        4        0
## 2      236      144       21       74       90      190        8        0
## 3      198      194      210       29        0        0        0        0
## 4      222      204      153      130      125       39        0        0
## 5      224       19        0        0        0        0        0        0
## 6      190      226      210      152        0        0        0        0
##   pixel309 pixel310 pixel311 pixel312 pixel313 pixel314 pixel315 pixel316
## 1        1        0       21      225      212      212      203      211
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      244      215      235
## 4        0        0        0        0        0        0       28        0
## 5        0        0        0        0        0        0      106      252
## 6        0        0        0        4        0      165      209      233
##   pixel317 pixel318 pixel319 pixel320 pixel321 pixel322 pixel323 pixel324
## 1      225      193      139      136      195      147      156      139
## 2        0        1        0        0        0       42      228      160
## 3      206      214      202      171      201      178      203      190
## 4      212      228      170      221      205      225      228      210
## 5      198        0      250      230      220      219      219      212
## 6      237      131      158      151      124      147      157      149
##   pixel325 pixel326 pixel327 pixel328 pixel329 pixel330 pixel331 pixel332
## 1      128      162      197      223      207      220      213      232
## 2      148      125      165      223      208      221      246      211
## 3      180      187      196      183      206      195      206       44
## 4      178      214       89      117      213        0        0        0
## 5      252      129        0      255      232       59        0        0
## 6      108      137      187       97      147      233      204      184
##   pixel333 pixel334 pixel335 pixel336 pixel337 pixel338 pixel339 pixel340
## 1      177        0        0        0        0        0      123      226
## 2      174      151        0        0        0        0        0        1
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel341 pixel342 pixel343 pixel344 pixel345 pixel346 pixel347 pixel348
## 1      207      211      209      205      228      158       90      103
## 2        3        0        1        0        3        0        9        0
## 3        0      233      221      237      182      206      207      195
## 4        0        0        0        0      196      141       56      176
## 5        0        0      134      252      150        0      254      215
## 6        0      199      202      233      245      165       76      138
##   pixel349 pixel350 pixel351 pixel352 pixel353 pixel354 pixel355 pixel356
## 1      186      138      100      121      147      158      183      226
## 2       23      230      168      147      119      163      227      206
## 3      200      192      224      191      182      177      180      186
## 4      210      164      162       91      135      196      158       69
## 5      219      219      219      216      233      222        0      253
## 6      174      160      148      159      155      163      125       90
##   pixel357 pixel358 pixel359 pixel360 pixel361 pixel362 pixel363 pixel364
## 1      208      214      209      216      255       13        0        1
## 2      200      190      206      186      139      171       41        0
## 3      178      196      207       60        0        0        0        0
## 4      181        0        0        3        2        3        0        0
## 5      237       89        0        0        0        0        0        0
## 6      230      228      203      210        0        0        0        0
##   pixel365 pixel366 pixel367 pixel368 pixel369 pixel370 pixel371 pixel372
## 1        0        0      226      219      202      208      206      205
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       16      249      224      229
## 4        0        0        0        0        0        1        0        0
## 5        0        0        0        0        0        0      163      253
## 6        0        0        0        0        0      218      196      238
##   pixel373 pixel374 pixel375 pixel376 pixel377 pixel378 pixel379 pixel380
## 1      216      184      156      150      193      170      164      168
## 2        3        4        0       16      209      185      139      118
## 3      192      215      207      198      198      192      226      193
## 4      150      190       88       50      145      194      159      120
## 5      126      115      253      211      222      220      219      219
## 6      249      204      101      143      184      171      149      163
##   pixel381 pixel382 pixel383 pixel384 pixel385 pixel386 pixel387 pixel388
## 1      188      186      200      219      216      213      213      211
## 2      181      227      209      204      210      199      225      157
## 3      192      180      176      185      196      200      217       87
## 4      136      207      230      144      171        4        0        1
## 5      213      253       54      206      243      144        0        0
## 6      173      158      103      132      255      217      205      222
##   pixel389 pixel390 pixel391 pixel392 pixel393 pixel394 pixel395 pixel396
## 1      233      148        0        0        0       45      227      204
## 2      127      190      111        0        1        3        2        1
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel397 pixel398 pixel399 pixel400 pixel401 pixel402 pixel403 pixel404
## 1      214      211      218      222      221      230      229      221
## 2        3        2        3        0        0        0       67      214
## 3       53      222      223      235      205      220      200      151
## 4        0        0        0        0      187      220      182       72
## 5        0        0      205      255      149      209      234      219
## 6        0      234      193      237      255      169      139      199
##   pixel405 pixel406 pixel407 pixel408 pixel409 pixel410 pixel411 pixel412
## 1      213      224      233      226      220      219      221      224
## 2      187      139      111      191      229      203      208      208
## 3      206      198      214      199      171      161      197      186
## 4      139      199      192      232      255      244      198      170
## 5      221      220      219      219      213      244      164      224
## 6      182      181      154      171      186      181      154      148
##   pixel413 pixel414 pixel415 pixel416 pixel417 pixel418 pixel419 pixel420
## 1      223      217      210      218      213      254        0        0
## 2      196      223      188      100      128      155      112        0
## 3      218      198      216       88        0        0        0        0
## 4      189        2        0        3        0        0        0        0
## 5      240      195        0        0        0        0        0        0
## 6      242      216      208      220        0        0        0        0
##   pixel421 pixel422 pixel423 pixel424 pixel425 pixel426 pixel427 pixel428
## 1        0      157      226      203      207      211      209      215
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       75      223      223      237
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        9        0
## 6        0        0        0        0        7      239      190      243
##   pixel429 pixel430 pixel431 pixel432 pixel433 pixel434 pixel435 pixel436
## 1      205      198      207      208      201      201      197      203
## 2       48      161      218      171      147      116      185      235
## 3      199      219      203      193      204      201      220      201
## 4      186      175      181       93      164      230      134      153
## 5       20      253      219      221      219      219      219      219
## 6      231      103      164      144       88      188      157      177
##   pixel437 pixel438 pixel439 pixel440 pixel441 pixel442 pixel443 pixel444
## 1      205      210      207      213      214      214      214      213
## 2      201      209      203      204      220      208      126      133
## 3      193      176      189      167      218      202      198      109
## 4      142      137       79      143      183        0        0        2
## 5      221      224      230        0        0        0        0        0
## 6      154      118      181      105      226      223      210      219
##   pixel445 pixel446 pixel447 pixel448 pixel449 pixel450 pixel451 pixel452
## 1      208      234      107        0        0      235      213      204
## 2      162      164      187        0        0       12       16        9
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel453 pixel454 pixel455 pixel456 pixel457 pixel458 pixel459 pixel460
## 1      211      210      209      213      202      197      204      215
## 2        4       19       73      170      211      188      145      149
## 3       79      250      226      243      196      222      201      206
## 4        0        0        0        0       93       72      144      138
## 5        0        0        0        0       49      254      217      221
## 6       30      242      187      246      196       93      162      115
##   pixel461 pixel462 pixel463 pixel464 pixel465 pixel466 pixel467 pixel468
## 1      217      213      212      210      206      212      203      211
## 2      148      136      222      218      207      208      215      224
## 3      205      207      222      185      194      186      187      144
## 4      164      113      124       98       80       57       97      138
## 5      219      219      219      220      221      216      253        7
## 6       91      187      151      182      114       65      158      116
##   pixel469 pixel470 pixel471 pixel472 pixel473 pixel474 pixel475 pixel476
## 1      218      215      214      208      209      222      230        0
## 2      193      140      146      162      156      118      203       37
## 3      207      208      226      111        0        0        0        0
## 4      124        4        0        3        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6      176      220      216      223        0        0        0        0
##   pixel477 pixel478 pixel479 pixel480 pixel481 pixel482 pixel483 pixel484
## 1       52      255      207      200      208      213      210      210
## 2       47      255      188      187      182      203      206      182
## 3        0        0        0        0       87      249      222      240
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        6        0
## 6        0        0        0        0       46      206      187      237
##   pixel485 pixel486 pixel487 pixel488 pixel489 pixel490 pixel491 pixel492
## 1      208      207      202      201      209      216      216      216
## 2      147      140      148      148      148      134      180      218
## 3      226      193      187      205      206      198      212      190
## 4      149      171       98       70      180      152      227      173
## 5       94      229      216      220      218      218      219      220
## 6      156      106      186      180      178      173      160      176
##   pixel493 pixel494 pixel495 pixel496 pixel497 pixel498 pixel499 pixel500
## 1      216      214      212      205      215      201      228      208
## 2      221      225      204      156      134      139      144      140
## 3      189      185      185      191      211      212      210      113
## 4      143      180      183      127      206       38        0        5
## 5      219      218      226       52        0        5        0        0
## 6      180      161      188      136      133      226      219      219
##   pixel501 pixel502 pixel503 pixel504 pixel505 pixel506 pixel507 pixel508
## 1      214      212      218       25      118      217      201      206
## 2      123       97      206       30       88      203      164      167
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       20        0        0        0        0        0        0        0
##   pixel509 pixel510 pixel511 pixel512 pixel513 pixel514 pixel515 pixel516
## 1      208      213      208      205      206      210      211      202
## 2      170      161      147      147      156      159      153      152
## 3       95      249      227      236      219      213      186      203
## 4        1        0        0        0      195      210      226      113
## 5        0        0        4        0      119      231      214      220
## 6       76      208      185      249      152      149      171      110
##   pixel517 pixel518 pixel519 pixel520 pixel521 pixel522 pixel523 pixel524
## 1      199      207      208      209      210      207      210      210
## 2      155      150      134      136      153      145      136      151
## 3      219      198      206      218      194      177      181      195
## 4      187      224      210      191      181      224      212      198
## 5      217      218      219      220      221      217      224       34
## 6      163      174      160      167      190      138      149      154
##   pixel525 pixel526 pixel527 pixel528 pixel529 pixel530 pixel531 pixel532
## 1      245      139      119      255      202      203      236      114
## 2      164      168      170      166      153      130      200       10
## 3      148      229      206      122        0        0        0        0
## 4      172       36        0        6        0        0        0        0
## 5        0        3        0        0        0        0        0        0
## 6      147      225      216      206       45        0        0        0
##   pixel533 pixel534 pixel535 pixel536 pixel537 pixel538 pixel539 pixel540
## 1      171      238      212      203      220      216      217      209
## 2      131      195      150      133      132      142      160      166
## 3        0        0        0        0      106      248      228      224
## 4        0        0        0        0        1        0        0        0
## 5        0        0        0        0        0        0        3        0
## 6        0        0        0        0       89      202      186      226
##   pixel541 pixel542 pixel543 pixel544 pixel545 pixel546 pixel547 pixel548
## 1      207      205      210      211      206      204      206      209
## 2      162      153      152      153      151      154      163      147
## 3      206      202      200      206      191      190      194      203
## 4      153      197      171      175      161      171      199      224
## 5      113      231      213      222      217      218      219      220
## 6      152      167      122       74      151      187      149      166
##   pixel549 pixel550 pixel551 pixel552 pixel553 pixel554 pixel555 pixel556
## 1      211      215      210      206      221      242        0      224
## 2      152      163      205      208      174      175      178      181
## 3      196      180      180      185      148      228      214      117
## 4      187      206      192      176      179       48        0        6
## 5      219      219      219       10        0        2        0        0
## 6      182      113       71      133      168      208      218      179
##   pixel557 pixel558 pixel559 pixel560 pixel561 pixel562 pixel563 pixel564
## 1      234      230      181       26       39      145      201      255
## 2      168      150      196        1      155      228      197      172
## 3        0        0        0        0        0        0        0        0
## 4        1        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       25        0        0        0        0        0        0        0
##   pixel565 pixel566 pixel567 pixel568 pixel569 pixel570 pixel571 pixel572
## 1      157      115      250      200      207      206      207      213
## 2      138      123      121      122      126      129      135      139
## 3      110      242      225      239      218      204      201      202
## 4        0        0        0        0      180      195      136      230
## 5        0        0        3        0       51      227      215      221
## 6      108      193      180      197      152      155      160      115
##   pixel573 pixel574 pixel575 pixel576 pixel577 pixel578 pixel579 pixel580
## 1      216      206      205      206      207      206      215      207
## 2      144      158      177      192      239      244      178      176
## 3      211      193      197      217      203      183      184      175
## 4      240      148      204      140      173      199      193      156
## 5      217      217      219      220      218      219      213        0
## 6      168      192      154      161      193      141      122      159
##   pixel581 pixel582 pixel583 pixel584 pixel585 pixel586 pixel587 pixel588
## 1      221      238        0        0      188       85        0        0
## 2      183      179      185      177      147      148      192        0
## 3      212      219      206      122        0        0        0        0
## 4      213       37        0        5        0        0        0        0
## 5        0        1        0        0        0        0        0        0
## 6      156      193      213      170       29        0        0        0
##   pixel589 pixel590 pixel591 pixel592 pixel593 pixel594 pixel595 pixel596
## 1        0        0        0       31        0      129      253      190
## 2        0       80      196      199      212      209      190      176
## 3        0        0        0        0      122      249      234      241
## 4        0        0        0        0        0        1        0        0
## 5        0        0        0        0        0        0        2        0
## 6        0        0        0        0      141      193      170      169
##   pixel597 pixel598 pixel599 pixel600 pixel601 pixel602 pixel603 pixel604
## 1      207      208      208      208      209      211      211      209
## 2      170      178      183      192      204      204      202      144
## 3      216      215      196      200      222      208      195      225
## 4      150      101       72      167      158       95      177      234
## 5        0      216      217      219      218      218      219      219
## 6      151      161      163      153      168      198      155      153
##   pixel605 pixel606 pixel607 pixel608 pixel609 pixel610 pixel611 pixel612
## 1      209      209      212      201      226      165        0        0
## 2       59        3        0       67      217      184      188      178
## 3      212      181      186      183      227      214      211      121
## 4      113      142      112       59      152       22        0        3
## 5      218      220      246        0        0        2        0        0
## 6      188      164      161      170      148      195      210      188
##   pixel613 pixel614 pixel615 pixel616 pixel617 pixel618 pixel619 pixel620
## 1        0        0        0        0        2        0        0        0
## 2      166      160      188       13        0        0        0        3
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       60        0        0        0        0        0        0        0
##   pixel621 pixel622 pixel623 pixel624 pixel625 pixel626 pixel627 pixel628
## 1        0       89      254      199      199      192      196      198
## 2       51      105      179      217      235      227      208      201
## 3      148      230      237      228      214      213      201      207
## 4        0        0        0        0      197      178       82       47
## 5        0        0        2        0        0      217      216      216
## 6      178      184      188      163      148      160      163      164
##   pixel629 pixel630 pixel631 pixel632 pixel633 pixel634 pixel635 pixel636
## 1      199      201      202      203      204      203      203      200
## 2      133       58        0        0        0        0        0       16
## 3      214      201      204      235      191      161      185      180
## 4       64      106      112      121      110      189      225      121
## 5      219      218      219      219      217      222      235        0
## 6      162      202      155      156      187      168      152      172
##   pixel637 pixel638 pixel639 pixel640 pixel641 pixel642 pixel643 pixel644
## 1      222      155        0        3        3        3        2        0
## 2      187      194      184      185      175      181      131        0
## 3      216      201      212      119        0        0        0        0
## 4       98       58        0        4        0        0        0        0
## 5        0        2        0        0        0        0        0        0
## 6      148      173      207      224      100        0        0        0
##   pixel645 pixel646 pixel647 pixel648 pixel649 pixel650 pixel651 pixel652
## 1        0        0        1        5        0        0      255      218
## 2        0        3        0        0        0        0        0        0
## 3        0        0        0        0      151      230      235      229
## 4        0        0        0        0        0        2        0        0
## 5        0        0        0        0        0        0        2        0
## 6        0        0        0        0      188      181      204      194
##   pixel653 pixel654 pixel655 pixel656 pixel657 pixel658 pixel659 pixel660
## 1      226      232      228      224      222      220      219      219
## 2        0        0        0        0        0        0        0        5
## 3      218      209      198      193      226      182      189      240
## 4      202      219      161      135      205      200      156      195
## 5        0      212      216      217      219      218      219      219
## 6      146      158      161      169      177      185      154      148
##   pixel661 pixel662 pixel663 pixel664 pixel665 pixel666 pixel667 pixel668
## 1      217      221      220      212      236       95        0        2
## 2        5        5        0        0        0        0        0        0
## 3      200      186      188      188      218      216      209      110
## 4      231      234      218      182      223       99        0        6
## 5      216      224      204        0        0        2        0        0
## 6      168      169      157      161      155      188      209      190
##   pixel669 pixel670 pixel671 pixel672 pixel673 pixel674 pixel675 pixel676
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       62        0        0        0        0        0        0        0
##   pixel677 pixel678 pixel679 pixel680 pixel681 pixel682 pixel683 pixel684
## 1        0        0      155      194      168      170      171      173
## 2        0        0        0        0        0        0        0        0
## 3       96      237      233      237      207      200      203      191
## 4        0        4        0        0      188      152      118      222
## 5        0        0        4        0        3      217      216      217
## 6      117      198      189       80      216      201      205      205
##   pixel685 pixel686 pixel687 pixel688 pixel689 pixel690 pixel691 pixel692
## 1      173      179      177      175      172      171      167      161
## 2        0        0        0        0        0        0        0        0
## 3      213      170      186      239      209      186      173      186
## 4      214      203      233      226      193      200      173       53
## 5      219      218      220      220      218      222      209        0
## 6      209      233      189      197      211      202      200      201
##   pixel693 pixel694 pixel695 pixel696 pixel697 pixel698 pixel699 pixel700
## 1      180        0        0        1        0        1        0        0
## 2        0        0        0        0        0        0        0        0
## 3      156      199      217       84        0        0        0        0
## 4      166       97        0        6        0        0        0        0
## 5        0        2        0        0        0        0        0        0
## 6      210      150      220      197       72        0        0        0
##   pixel701 pixel702 pixel703 pixel704 pixel705 pixel706 pixel707 pixel708
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       89      237      207      121
## 4        0        0        0        0        0        3        0        2
## 5        0        0        0        0        0        0        3        0
## 6        0        0        0        0      151      214      181        6
##   pixel709 pixel710 pixel711 pixel712 pixel713 pixel714 pixel715 pixel716
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      255      193      183      193      209      224      208      224
## 4      182      152       51       89      174      183      168      112
## 5        4      214      211      213      213      215      213      211
## 6      153       82       56       30        3        0       73       74
##   pixel717 pixel718 pixel719 pixel720 pixel721 pixel722 pixel723 pixel724
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      203      160      184      223        0      186      216      102
## 4      109      181      170      136      108       60        0        4
## 5      214      220      200        0        0        1        0        0
## 6       41       28       30       46       78       13      201      146
##   pixel725 pixel726 pixel727 pixel728 pixel729 pixel730 pixel731 pixel732
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       39        0        0        0        0        0        0        0
##   pixel733 pixel734 pixel735 pixel736 pixel737 pixel738 pixel739 pixel740
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      108      242      253        0      112      255      217      211
## 4        0        2        0        5      194      193      204      104
## 5        0        0        3        0        0      214      232      229
## 6        7       34       58        0        0        0        0        0
##   pixel741 pixel742 pixel743 pixel744 pixel745 pixel746 pixel747 pixel748
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      227      245      195      194      211      220      214       74
## 4      116      241      217      196      171      249      207      197
## 5      249      245      248      252      230      237      229        0
## 6        0        0        0        0        0        0        0        0
##   pixel749 pixel750 pixel751 pixel752 pixel753 pixel754 pixel755 pixel756
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0      255      222      128        0        0        0        0
## 4      202       45        0        3        0        0        0        0
## 5        0        1        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel757 pixel758 pixel759 pixel760 pixel761 pixel762 pixel763 pixel764
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       44       12        0
## 4        0        0        0        0        0        0        1        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        4        0        0        0        2
##   pixel765 pixel766 pixel767 pixel768 pixel769 pixel770 pixel771 pixel772
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0       40      134      162      191      214      163      146
## 4        0        0       22       21       25       69       52       45
## 5        0       68      116      112      136      147      144      121
## 6        4        9        7        8        7        5        4        8
##   pixel773 pixel774 pixel775 pixel776 pixel777 pixel778 pixel779 pixel780
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      165       79        0        0        0       30       43        0
## 4       74       39        3        0        0        0        0        1
## 5      102       63        0        0        0        0        0        0
## 6       12        5        7        8        7        4        3        7
##   pixel781 pixel782 pixel783 pixel784
## 1        0        0        0        0
## 2        0        0        0        0
## 3        0        0        0        0
## 4        0        0        0        0
## 5        0        0        0        0
## 6        5        0        0        0
f.data$label = as.factor(f.data$label)#response

class(f.data$label)
## [1] "factor"
summary(f.data$label)
##    0    1    2    3    4    5    6    7    8    9 
## 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000
str(f.data)
## 'data.frame':    60000 obs. of  785 variables:
##  $ label   : Factor w/ 10 levels "0","1","2","3",..: 3 10 7 1 4 5 5 6 5 9 ...
##  $ pixel1  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel2  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel3  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel4  : int  0 0 0 1 0 5 0 0 0 0 ...
##  $ pixel5  : int  0 0 0 2 0 4 0 0 0 0 ...
##  $ pixel6  : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel7  : int  0 0 0 0 0 5 0 0 3 0 ...
##  $ pixel8  : int  0 0 5 0 0 3 0 0 2 0 ...
##  $ pixel9  : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel10 : int  0 0 0 0 0 6 0 0 0 0 ...
##  $ pixel11 : int  0 0 0 114 0 2 0 0 60 0 ...
##  $ pixel12 : int  0 0 105 183 0 0 0 0 234 1 ...
##  $ pixel13 : int  0 0 92 112 46 0 159 0 215 0 ...
##  $ pixel14 : int  0 0 101 55 0 0 161 0 229 0 ...
##  $ pixel15 : int  0 0 107 23 21 0 143 0 223 141 ...
##  $ pixel16 : int  0 0 100 72 68 0 180 0 231 45 ...
##  $ pixel17 : int  0 0 132 102 0 0 142 0 107 0 ...
##  $ pixel18 : int  0 0 0 165 0 0 21 0 0 1 ...
##  $ pixel19 : int  0 0 0 160 0 2 0 0 0 0 ...
##  $ pixel20 : int  0 0 2 28 0 3 0 0 0 0 ...
##  $ pixel21 : int  0 0 4 0 0 7 0 0 0 0 ...
##  $ pixel22 : int  0 0 0 0 0 4 0 0 4 0 ...
##  $ pixel23 : int  0 0 1 0 0 4 0 0 0 0 ...
##  $ pixel24 : int  0 0 0 1 0 5 0 0 0 0 ...
##  $ pixel25 : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel26 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel27 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel28 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel29 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel30 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel31 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel32 : int  0 0 0 0 0 4 0 0 0 0 ...
##  $ pixel33 : int  0 0 0 1 0 5 0 0 0 0 ...
##  $ pixel34 : int  0 0 0 0 0 4 0 0 2 0 ...
##  $ pixel35 : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel36 : int  0 0 0 24 0 11 0 0 0 0 ...
##  $ pixel37 : int  0 0 0 188 25 2 2 0 0 0 ...
##  $ pixel38 : int  0 0 0 163 187 0 0 0 0 0 ...
##  $ pixel39 : int  0 0 150 93 189 0 0 0 224 0 ...
##  $ pixel40 : int  0 0 227 136 0 21 251 0 219 6 ...
##  $ pixel41 : int  0 0 211 153 119 231 249 0 128 0 ...
##  $ pixel42 : int  0 0 171 168 173 156 239 0 175 86 ...
##  $ pixel43 : int  0 0 191 252 239 134 254 0 136 103 ...
##  $ pixel44 : int  0 0 236 174 112 177 249 0 151 65 ...
##  $ pixel45 : int  0 0 228 136 0 194 238 0 254 42 ...
##  $ pixel46 : int  0 0 172 166 189 1 255 0 53 0 ...
##  $ pixel47 : int  0 0 0 130 125 0 33 0 0 3 ...
##  $ pixel48 : int  0 0 0 123 0 0 0 0 0 0 ...
##  $ pixel49 : int  0 0 0 131 0 0 3 0 0 0 ...
##  $ pixel50 : int  0 0 0 66 0 10 0 0 0 0 ...
##  $ pixel51 : int  0 0 1 0 0 5 0 0 0 0 ...
##  $ pixel52 : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel53 : int  0 0 0 1 0 4 0 0 0 0 ...
##  $ pixel54 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel55 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel56 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel57 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel58 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel59 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel60 : int  0 0 0 2 0 4 0 0 0 0 ...
##  $ pixel61 : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel62 : int  0 0 0 10 0 3 0 0 0 0 ...
##  $ pixel63 : int  0 0 0 157 0 7 0 0 8 0 ...
##  $ pixel64 : int  0 0 48 216 14 0 0 0 95 0 ...
##  $ pixel65 : int  0 0 153 226 219 0 0 0 161 0 ...
##  $ pixel66 : int  0 0 209 208 224 26 0 0 244 0 ...
##  $ pixel67 : int  0 0 210 142 211 112 128 0 211 0 ...
##  $ pixel68 : int  0 0 187 66 92 168 251 0 210 1 ...
##  $ pixel69 : int  0 0 199 115 52 162 213 0 143 0 ...
##  $ pixel70 : int  0 0 142 149 227 212 204 0 140 162 ...
##  $ pixel71 : int  0 0 157 230 224 226 189 0 148 0 ...
##  $ pixel72 : int  0 0 209 190 127 199 209 0 127 88 ...
##  $ pixel73 : int  0 0 179 196 92 146 204 0 220 150 ...
##  $ pixel74 : int  0 0 199 198 196 168 241 0 228 0 ...
##  $ pixel75 : int  0 0 233 172 237 124 190 0 215 7 ...
##  $ pixel76 : int  0 0 138 222 136 25 0 0 183 0 ...
##  $ pixel77 : int  0 0 44 107 0 0 0 0 150 0 ...
##  $ pixel78 : int  0 0 0 165 0 0 0 0 23 0 ...
##  $ pixel79 : int  0 0 0 211 0 0 0 0 0 0 ...
##  $ pixel80 : int  0 0 0 0 0 4 0 0 1 0 ...
##  $ pixel81 : int  0 0 0 0 0 5 0 0 0 0 ...
##  $ pixel82 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel83 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel84 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel85 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel86 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel87 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pixel88 : int  0 0 0 0 0 4 0 0 0 0 ...
##  $ pixel89 : int  4 0 0 0 0 5 0 0 0 0 ...
##  $ pixel90 : int  0 0 0 118 0 8 0 0 106 0 ...
##  $ pixel91 : int  0 0 120 214 0 0 0 0 216 0 ...
##  $ pixel92 : int  0 0 218 174 106 15 0 0 225 0 ...
##  $ pixel93 : int  0 0 215 168 238 95 63 0 222 0 ...
##  $ pixel94 : int  0 0 207 109 202 142 194 0 218 0 ...
##  $ pixel95 : int  62 0 198 200 205 170 220 0 213 3 ...
##  $ pixel96 : int  61 0 198 124 224 144 250 0 184 0 ...
##  $ pixel97 : int  21 0 223 150 225 123 239 0 255 45 ...
##  $ pixel98 : int  29 0 219 143 217 156 241 0 214 138 ...
##   [list output truncated]
library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         9 minutes 3 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.70 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(f.data, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10
## 1     2      0      0      0      0      0      0      0      0      0       0
## 2     9      0      0      0      0      0      0      0      0      0       0
## 3     6      0      0      0      0      0      0      0      5      0       0
## 4     0      0      0      0      1      2      0      0      0      0       0
## 5     3      0      0      0      0      0      0      0      0      0       0
## 6     4      0      0      0      5      4      5      5      3      5       6
##   pixel11 pixel12 pixel13 pixel14 pixel15 pixel16 pixel17 pixel18 pixel19
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     105      92     101     107     100     132       0       0
## 4     114     183     112      55      23      72     102     165     160
## 5       0       0      46       0      21      68       0       0       0
## 6       2       0       0       0       0       0       0       0       2
##   pixel20 pixel21 pixel22 pixel23 pixel24 pixel25 pixel26 pixel27 pixel28
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       2       4       0       1       0       0       0       0       0
## 4      28       0       0       0       1       0       0       0       0
## 5       0       0       0       0       0       0       0       0       0
## 6       3       7       4       4       5       5       0       0       0
##   pixel29 pixel30 pixel31 pixel32 pixel33 pixel34 pixel35 pixel36 pixel37
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0       0
## 4       0       0       0       0       1       0       0      24     188
## 5       0       0       0       0       0       0       0       0      25
## 6       0       0       0       4       5       4       5      11       2
##   pixel38 pixel39 pixel40 pixel41 pixel42 pixel43 pixel44 pixel45 pixel46
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     150     227     211     171     191     236     228     172
## 4     163      93     136     153     168     252     174     136     166
## 5     187     189       0     119     173     239     112       0     189
## 6       0       0      21     231     156     134     177     194       1
##   pixel47 pixel48 pixel49 pixel50 pixel51 pixel52 pixel53 pixel54 pixel55
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       1       0       0       0       0
## 4     130     123     131      66       0       0       1       0       0
## 5     125       0       0       0       0       0       0       0       0
## 6       0       0       0      10       5       5       4       0       0
##   pixel56 pixel57 pixel58 pixel59 pixel60 pixel61 pixel62 pixel63 pixel64
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0      48
## 4       0       0       0       0       2       0      10     157     216
## 5       0       0       0       0       0       0       0       0      14
## 6       0       0       0       0       4       5       3       7       0
##   pixel65 pixel66 pixel67 pixel68 pixel69 pixel70 pixel71 pixel72 pixel73
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     153     209     210     187     199     142     157     209     179
## 4     226     208     142      66     115     149     230     190     196
## 5     219     224     211      92      52     227     224     127      92
## 6       0      26     112     168     162     212     226     199     146
##   pixel74 pixel75 pixel76 pixel77 pixel78 pixel79 pixel80 pixel81 pixel82
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     199     233     138      44       0       0       0       0       0
## 4     198     172     222     107     165     211       0       0       0
## 5     196     237     136       0       0       0       0       0       0
## 6     168     124      25       0       0       0       4       5       0
##   pixel83 pixel84 pixel85 pixel86 pixel87 pixel88 pixel89 pixel90 pixel91
## 1       0       0       0       0       0       0       4       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0     120
## 4       0       0       0       0       0       0       0     118     214
## 5       0       0       0       0       0       0       0       0       0
## 6       0       0       0       0       0       4       5       8       0
##   pixel92 pixel93 pixel94 pixel95 pixel96 pixel97 pixel98 pixel99 pixel100
## 1       0       0       0      62      61      21      29      23       51
## 2       0       0       0       0       0       0       0       0        0
## 3     218     215     207     198     198     223     219     214      225
## 4     174     168     109     200     124     150     143      58       63
## 5     106     238     202     205     224     225     217     218      222
## 6      15      95     142     170     144     123     156     172      140
##   pixel101 pixel102 pixel103 pixel104 pixel105 pixel106 pixel107 pixel108
## 1      136       61        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      190      177      195      209      204      139        0        1
## 4       89      137       97      168      138      142      195      156
## 5      227      202      206      218        0        0        0        0
## 6      131      135      160      137      141       53        0        0
##   pixel109 pixel110 pixel111 pixel112 pixel113 pixel114 pixel115 pixel116
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        3        0        0        0        0        0        0        5
##   pixel117 pixel118 pixel119 pixel120 pixel121 pixel122 pixel123 pixel124
## 1        0        0        0       88      201      228      225      255
## 2        0        0        0        0        0        0        0        0
## 3        0        0      208      200      197      202      193      203
## 4       25      140       70       80       43       71       96       93
## 5        0        0        0      166      239      216      214      192
## 6        6        0        0      165      130      120      130       56
##   pixel125 pixel126 pixel127 pixel128 pixel129 pixel130 pixel131 pixel132
## 1      115       62      137      255      235      222      255      135
## 2        0        0        0        0        0        0        0        0
## 3      207      228      245      196      197      202      193      190
## 4      151      121      197      143      107       82      101      111
## 5      181      205      204      180      187      205      208      230
## 6      124      122       68      143      111       98      132      125
##   pixel133 pixel134 pixel135 pixel136 pixel137 pixel138 pixel139 pixel140
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      167      203       27        0        0        0        0        0
## 4       80      137      193      208        6        0        0        0
## 5       45        0        0        0        0        0        0        0
## 6      111      164       60        0        6        0        0        0
##   pixel141 pixel142 pixel143 pixel144 pixel145 pixel146 pixel147 pixel148
## 1        0        0        0        0        0       47      252      234
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       89      217      200
## 4        0        0        0        0       74      194      107      146
## 5        0        0        0        0        0        0        0      220
## 6        0        0        0        3        7        0       99      165
##   pixel149 pixel150 pixel151 pixel152 pixel153 pixel154 pixel155 pixel156
## 1      238      224      215      215      229      108      180      207
## 2        0        0        0        0        1        1        0        0
## 3      211      175      189      205      199      218      216      181
## 4      178      185      182       77      185      218      210      175
## 5      235      233      213      205      201      185      184      200
## 6      135      154       98       66      130      141      114      143
##   pixel157 pixel158 pixel159 pixel160 pixel161 pixel162 pixel163 pixel164
## 1      214      224      231      249      254       45        0        0
## 2        0        0       81       21        0        1        0        1
## 3      179      185      192      198      184      198      121        0
## 4      174      235      217      217      129      180      210      208
## 5      199      223      224      232      101        0        0        0
## 6      113       71      107      147      117      176      172        0
##   pixel165 pixel166 pixel167 pixel168 pixel169 pixel170 pixel171 pixel172
## 1        0        0        0        0        0        0        1        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4       89        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        5        0        0        0        0        0        0        4
##   pixel173 pixel174 pixel175 pixel176 pixel177 pixel178 pixel179 pixel180
## 1        0      214      222      210      213      224      225      217
## 2        0        0        0        0        0        0        0        1
## 3        0      140      220      204      223      187      182      200
## 4      179      213      203      177      228      192      193      162
## 5        0        0        0      252      238      226      226      221
## 6        0        0      197      188      169      148      146      132
##   pixel181 pixel182 pixel183 pixel184 pixel185 pixel186 pixel187 pixel188
## 1      220      254      233      219      221      217      223      221
## 2        0        0        0        0        0       14      200       49
## 3      207      217      213      210      207      180      161      192
## 4      143      172      196      205      181      180      140      134
## 5      224      195      198      223      229      205      202      235
## 6      110      133      107      139      131      133      141      156
##   pixel189 pixel190 pixel191 pixel192 pixel193 pixel194 pixel195 pixel196
## 1      240      254        0        0        1        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      184      199      162        0        0        0        0        0
## 4      176      194      171      170       65        0        0        0
## 5      158        0        0        0        0        0        0        0
## 6      143      170      186        0        0        0        0        0
##   pixel197 pixel198 pixel199
## 1        1        0        0
## 2        0        0        0
## 3        0        0        0
## 4        0        0        0
## 5        0        0        0
## 6        0        0        0
g.split = h2o.splitFrame(data = d.hex,ratios = 0.75)
train = g.split[[1]]#75% training data
test = g.split[[2]]

### 1 hidden layer with 10 neurons 
##ann
fmnist_nn = h2o.deeplearning(x = 2:785,
                                y = "label", 
                                training_frame = train,
                                distribution = "multinomial",
                                model_id = "fmnist_nn",
                                l2 = 0.4,
                                ignore_const_cols = FALSE,
                                hidden = 15, 
                                export_weights_and_biases = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
h2o.confusionMatrix(fmnist_nn, test)
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
##           0    1    2    3    4    5   6    7    8    9  Error             Rate
## 0      1191    3   12  164   12   26  40    0   18    1 0.1881 =    276 / 1,467
## 1         7 1443   24   83    5    0   7    0    2    0 0.0815 =    128 / 1,571
## 2        30    3  848   15  402   36 118    0   16    0 0.4223 =    620 / 1,468
## 3        53    9    4 1372   27   15  32    0    1    1 0.0938 =    142 / 1,514
## 4         5    8   98  126 1134   24  54    0   12    0 0.2238 =    327 / 1,461
## 5         0    0    0    1    0 1345   0   99    3   64 0.1104 =    167 / 1,512
## 6       343    3  174   95  501   57 260    2   34    1 0.8231 =  1,210 / 1,470
## 7         0    0    0    0    0  217   0 1206    2   79 0.1981 =    298 / 1,504
## 8        11    0   19   34    9   65   7   29 1330    4 0.1180 =    178 / 1,508
## 9         0    1    0    1    0   63   0  117    0 1393 0.1156 =    182 / 1,575
## Totals 1640 1470 1179 1891 2090 1848 518 1453 1418 1543 0.2344 = 3,528 / 15,050

Implement a DNN with H2o ForMulti-Class Supervised Classification using H20

set.seed(1234)

# loading data
f.data = read.csv("dataset.csv")

##1)T-shirt/top (2) Trouser (3) Pullover (4)Dress (5)Coat
## 6)Sandal (7)Shirt (8) Sneaker (9) Bag (10) Ankle boot

head(f.data)
##   label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10
## 1     2      0      0      0      0      0      0      0      0      0       0
## 2     9      0      0      0      0      0      0      0      0      0       0
## 3     6      0      0      0      0      0      0      0      5      0       0
## 4     0      0      0      0      1      2      0      0      0      0       0
## 5     3      0      0      0      0      0      0      0      0      0       0
## 6     4      0      0      0      5      4      5      5      3      5       6
##   pixel11 pixel12 pixel13 pixel14 pixel15 pixel16 pixel17 pixel18 pixel19
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     105      92     101     107     100     132       0       0
## 4     114     183     112      55      23      72     102     165     160
## 5       0       0      46       0      21      68       0       0       0
## 6       2       0       0       0       0       0       0       0       2
##   pixel20 pixel21 pixel22 pixel23 pixel24 pixel25 pixel26 pixel27 pixel28
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       2       4       0       1       0       0       0       0       0
## 4      28       0       0       0       1       0       0       0       0
## 5       0       0       0       0       0       0       0       0       0
## 6       3       7       4       4       5       5       0       0       0
##   pixel29 pixel30 pixel31 pixel32 pixel33 pixel34 pixel35 pixel36 pixel37
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0       0
## 4       0       0       0       0       1       0       0      24     188
## 5       0       0       0       0       0       0       0       0      25
## 6       0       0       0       4       5       4       5      11       2
##   pixel38 pixel39 pixel40 pixel41 pixel42 pixel43 pixel44 pixel45 pixel46
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     150     227     211     171     191     236     228     172
## 4     163      93     136     153     168     252     174     136     166
## 5     187     189       0     119     173     239     112       0     189
## 6       0       0      21     231     156     134     177     194       1
##   pixel47 pixel48 pixel49 pixel50 pixel51 pixel52 pixel53 pixel54 pixel55
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       1       0       0       0       0
## 4     130     123     131      66       0       0       1       0       0
## 5     125       0       0       0       0       0       0       0       0
## 6       0       0       0      10       5       5       4       0       0
##   pixel56 pixel57 pixel58 pixel59 pixel60 pixel61 pixel62 pixel63 pixel64
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0      48
## 4       0       0       0       0       2       0      10     157     216
## 5       0       0       0       0       0       0       0       0      14
## 6       0       0       0       0       4       5       3       7       0
##   pixel65 pixel66 pixel67 pixel68 pixel69 pixel70 pixel71 pixel72 pixel73
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     153     209     210     187     199     142     157     209     179
## 4     226     208     142      66     115     149     230     190     196
## 5     219     224     211      92      52     227     224     127      92
## 6       0      26     112     168     162     212     226     199     146
##   pixel74 pixel75 pixel76 pixel77 pixel78 pixel79 pixel80 pixel81 pixel82
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     199     233     138      44       0       0       0       0       0
## 4     198     172     222     107     165     211       0       0       0
## 5     196     237     136       0       0       0       0       0       0
## 6     168     124      25       0       0       0       4       5       0
##   pixel83 pixel84 pixel85 pixel86 pixel87 pixel88 pixel89 pixel90 pixel91
## 1       0       0       0       0       0       0       4       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0     120
## 4       0       0       0       0       0       0       0     118     214
## 5       0       0       0       0       0       0       0       0       0
## 6       0       0       0       0       0       4       5       8       0
##   pixel92 pixel93 pixel94 pixel95 pixel96 pixel97 pixel98 pixel99 pixel100
## 1       0       0       0      62      61      21      29      23       51
## 2       0       0       0       0       0       0       0       0        0
## 3     218     215     207     198     198     223     219     214      225
## 4     174     168     109     200     124     150     143      58       63
## 5     106     238     202     205     224     225     217     218      222
## 6      15      95     142     170     144     123     156     172      140
##   pixel101 pixel102 pixel103 pixel104 pixel105 pixel106 pixel107 pixel108
## 1      136       61        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      190      177      195      209      204      139        0        1
## 4       89      137       97      168      138      142      195      156
## 5      227      202      206      218        0        0        0        0
## 6      131      135      160      137      141       53        0        0
##   pixel109 pixel110 pixel111 pixel112 pixel113 pixel114 pixel115 pixel116
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        3        0        0        0        0        0        0        5
##   pixel117 pixel118 pixel119 pixel120 pixel121 pixel122 pixel123 pixel124
## 1        0        0        0       88      201      228      225      255
## 2        0        0        0        0        0        0        0        0
## 3        0        0      208      200      197      202      193      203
## 4       25      140       70       80       43       71       96       93
## 5        0        0        0      166      239      216      214      192
## 6        6        0        0      165      130      120      130       56
##   pixel125 pixel126 pixel127 pixel128 pixel129 pixel130 pixel131 pixel132
## 1      115       62      137      255      235      222      255      135
## 2        0        0        0        0        0        0        0        0
## 3      207      228      245      196      197      202      193      190
## 4      151      121      197      143      107       82      101      111
## 5      181      205      204      180      187      205      208      230
## 6      124      122       68      143      111       98      132      125
##   pixel133 pixel134 pixel135 pixel136 pixel137 pixel138 pixel139 pixel140
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      167      203       27        0        0        0        0        0
## 4       80      137      193      208        6        0        0        0
## 5       45        0        0        0        0        0        0        0
## 6      111      164       60        0        6        0        0        0
##   pixel141 pixel142 pixel143 pixel144 pixel145 pixel146 pixel147 pixel148
## 1        0        0        0        0        0       47      252      234
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       89      217      200
## 4        0        0        0        0       74      194      107      146
## 5        0        0        0        0        0        0        0      220
## 6        0        0        0        3        7        0       99      165
##   pixel149 pixel150 pixel151 pixel152 pixel153 pixel154 pixel155 pixel156
## 1      238      224      215      215      229      108      180      207
## 2        0        0        0        0        1        1        0        0
## 3      211      175      189      205      199      218      216      181
## 4      178      185      182       77      185      218      210      175
## 5      235      233      213      205      201      185      184      200
## 6      135      154       98       66      130      141      114      143
##   pixel157 pixel158 pixel159 pixel160 pixel161 pixel162 pixel163 pixel164
## 1      214      224      231      249      254       45        0        0
## 2        0        0       81       21        0        1        0        1
## 3      179      185      192      198      184      198      121        0
## 4      174      235      217      217      129      180      210      208
## 5      199      223      224      232      101        0        0        0
## 6      113       71      107      147      117      176      172        0
##   pixel165 pixel166 pixel167 pixel168 pixel169 pixel170 pixel171 pixel172
## 1        0        0        0        0        0        0        1        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4       89        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        5        0        0        0        0        0        0        4
##   pixel173 pixel174 pixel175 pixel176 pixel177 pixel178 pixel179 pixel180
## 1        0      214      222      210      213      224      225      217
## 2        0        0        0        0        0        0        0        1
## 3        0      140      220      204      223      187      182      200
## 4      179      213      203      177      228      192      193      162
## 5        0        0        0      252      238      226      226      221
## 6        0        0      197      188      169      148      146      132
##   pixel181 pixel182 pixel183 pixel184 pixel185 pixel186 pixel187 pixel188
## 1      220      254      233      219      221      217      223      221
## 2        0        0        0        0        0       14      200       49
## 3      207      217      213      210      207      180      161      192
## 4      143      172      196      205      181      180      140      134
## 5      224      195      198      223      229      205      202      235
## 6      110      133      107      139      131      133      141      156
##   pixel189 pixel190 pixel191 pixel192 pixel193 pixel194 pixel195 pixel196
## 1      240      254        0        0        1        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      184      199      162        0        0        0        0        0
## 4      176      194      171      170       65        0        0        0
## 5      158        0        0        0        0        0        0        0
## 6      143      170      186        0        0        0        0        0
##   pixel197 pixel198 pixel199 pixel200 pixel201 pixel202 pixel203 pixel204
## 1        1        0        0        0      128      237      207      224
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      166      218      203
## 4        0        0        0        0      184      194      229      209
## 5        0        0        0        0        0        0        9      228
## 6        0        0        0        6        0        0      225      130
##   pixel205 pixel206 pixel207 pixel208 pixel209 pixel210 pixel211 pixel212
## 1      224      207      216      214      210      208      211      221
## 2        2        1        1        1        0        1        0        0
## 3      195      185      196      189      167      181      206      177
## 4      176      198      129      227      225      140      196      130
## 5      241      125      238      228      225      212      215      222
## 6      140      169      148      110       45      117      137      118
##   pixel213 pixel214 pixel215 pixel216 pixel217 pixel218 pixel219 pixel220
## 1      208      219      213      226      211      237      150        0
## 2        0      162      184      165       20        0        1        1
## 3      176      181      165      187      179      189      183        0
## 4      179      145      109       79      182      223      164      195
## 5      250      168      141      243      197        0        0        0
## 6       53       97      147      147      154      133      202       18
##   pixel221 pixel222 pixel223 pixel224 pixel225 pixel226 pixel227 pixel228
## 1        0        0        0        0        0        2        0        0
## 2        3        4        1        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4      233        0        0        0        0        0        0       38
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        7
##   pixel229 pixel230 pixel231 pixel232 pixel233 pixel234 pixel235 pixel236
## 1      237      222      215      207      210      212      213      206
## 2        0        0        0        0        0        0        0        1
## 3        0      196      215      211      206      201      196      180
## 4      180      177      213      202      159      129       98      179
## 5        0        0       51      232      247       34      225      237
## 6        0       48      211      135      124      140       83      158
##   pixel237 pixel238 pixel239 pixel240 pixel241 pixel242 pixel243 pixel244
## 1      214      213      214      213      210      215      214      206
## 2        2        0        2        0      120      183      154      206
## 3      188      210      206      190      173      180      187      192
## 4      149       90      187      211       61      134       91       57
## 5      219      222      223      216      248      182      124      246
## 6      145      160      126      151      136      156      113      110
##   pixel245 pixel246 pixel247 pixel248 pixel249 pixel250 pixel251 pixel252
## 1      199      218      255       13        0        2        0        0
## 2       32        0        5        5        0        0        0        0
## 3      184      192      199        0        0        0        0        0
## 4      118      212      220      218      207        0        0        0
## 5      200        0        0        0        0        0        0        0
## 6      167      168      217       93        0        0        0        0
##   pixel253 pixel254 pixel255 pixel256 pixel257 pixel258 pixel259 pixel260
## 1        0        4        0       85      228      210      218      200
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      212      217      224
## 4        0        0        0      114      154      142      182      219
## 5        0        0        0        0        0        0       69      236
## 6        0        0        0        6        0      104      219      208
##   pixel261 pixel262 pixel263 pixel264 pixel265 pixel266 pixel267 pixel268
## 1      211      208      203      215      210      209      209      210
## 2        0        0        0        2        0        3        0       76
## 3      211      216      201      189      184      202      210      194
## 4      130       88       81       52       54      106       93      110
## 5      255        0      218      240      219      221      222      214
## 6      166      157      130      170      137      144      155      160
##   pixel269 pixel270 pixel271 pixel272 pixel273 pixel274 pixel275 pixel276
## 1      213      211      210      217      206      213      231      175
## 2      227      151      139      158        0        0        0        0
## 3      182      173      173      176      192      195      208        0
## 4      159      222      227       83      117      253      218      210
## 5      252      153       71      248      215        0        0        0
## 6      150      160      158      120      194      220      213      128
##   pixel277 pixel278 pixel279 pixel280 pixel281 pixel282 pixel283 pixel284
## 1        0        0        0        0        0        0        0      217
## 2        0       66       23        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4      206       48        0        0        0        0        0       18
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        6
##   pixel285 pixel286 pixel287 pixel288 pixel289 pixel290 pixel291 pixel292
## 1      224      215      206      205      204      217      230      222
## 2        0        0        0        0        1        0        1        0
## 3        0      222      215      228      203      212      199      198
## 4      127      208      228      185      172      240       91      126
## 5        0        0       82      248      247        0      231      236
## 6        0      133      213      235      206      169      194      139
##   pixel293 pixel294 pixel295 pixel296 pixel297 pixel298 pixel299 pixel300
## 1      215      224      233      228      232      228      224      207
## 2        0        0       55      224      162      143      121      204
## 3      191      186      228      195      191      161      178      177
## 4      208      165      154      213      214      229      215      175
## 5      219      221      221      212      253      109        0      247
## 6       76      119      178      139       99      120      199      167
##   pixel301 pixel302 pixel303 pixel304 pixel305 pixel306 pixel307 pixel308
## 1      212      215      213      229       31        0        4        0
## 2      236      144       21       74       90      190        8        0
## 3      198      194      210       29        0        0        0        0
## 4      222      204      153      130      125       39        0        0
## 5      224       19        0        0        0        0        0        0
## 6      190      226      210      152        0        0        0        0
##   pixel309 pixel310 pixel311 pixel312 pixel313 pixel314 pixel315 pixel316
## 1        1        0       21      225      212      212      203      211
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0      244      215      235
## 4        0        0        0        0        0        0       28        0
## 5        0        0        0        0        0        0      106      252
## 6        0        0        0        4        0      165      209      233
##   pixel317 pixel318 pixel319 pixel320 pixel321 pixel322 pixel323 pixel324
## 1      225      193      139      136      195      147      156      139
## 2        0        1        0        0        0       42      228      160
## 3      206      214      202      171      201      178      203      190
## 4      212      228      170      221      205      225      228      210
## 5      198        0      250      230      220      219      219      212
## 6      237      131      158      151      124      147      157      149
##   pixel325 pixel326 pixel327 pixel328 pixel329 pixel330 pixel331 pixel332
## 1      128      162      197      223      207      220      213      232
## 2      148      125      165      223      208      221      246      211
## 3      180      187      196      183      206      195      206       44
## 4      178      214       89      117      213        0        0        0
## 5      252      129        0      255      232       59        0        0
## 6      108      137      187       97      147      233      204      184
##   pixel333 pixel334 pixel335 pixel336 pixel337 pixel338 pixel339 pixel340
## 1      177        0        0        0        0        0      123      226
## 2      174      151        0        0        0        0        0        1
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel341 pixel342 pixel343 pixel344 pixel345 pixel346 pixel347 pixel348
## 1      207      211      209      205      228      158       90      103
## 2        3        0        1        0        3        0        9        0
## 3        0      233      221      237      182      206      207      195
## 4        0        0        0        0      196      141       56      176
## 5        0        0      134      252      150        0      254      215
## 6        0      199      202      233      245      165       76      138
##   pixel349 pixel350 pixel351 pixel352 pixel353 pixel354 pixel355 pixel356
## 1      186      138      100      121      147      158      183      226
## 2       23      230      168      147      119      163      227      206
## 3      200      192      224      191      182      177      180      186
## 4      210      164      162       91      135      196      158       69
## 5      219      219      219      216      233      222        0      253
## 6      174      160      148      159      155      163      125       90
##   pixel357 pixel358 pixel359 pixel360 pixel361 pixel362 pixel363 pixel364
## 1      208      214      209      216      255       13        0        1
## 2      200      190      206      186      139      171       41        0
## 3      178      196      207       60        0        0        0        0
## 4      181        0        0        3        2        3        0        0
## 5      237       89        0        0        0        0        0        0
## 6      230      228      203      210        0        0        0        0
##   pixel365 pixel366 pixel367 pixel368 pixel369 pixel370 pixel371 pixel372
## 1        0        0      226      219      202      208      206      205
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       16      249      224      229
## 4        0        0        0        0        0        1        0        0
## 5        0        0        0        0        0        0      163      253
## 6        0        0        0        0        0      218      196      238
##   pixel373 pixel374 pixel375 pixel376 pixel377 pixel378 pixel379 pixel380
## 1      216      184      156      150      193      170      164      168
## 2        3        4        0       16      209      185      139      118
## 3      192      215      207      198      198      192      226      193
## 4      150      190       88       50      145      194      159      120
## 5      126      115      253      211      222      220      219      219
## 6      249      204      101      143      184      171      149      163
##   pixel381 pixel382 pixel383 pixel384 pixel385 pixel386 pixel387 pixel388
## 1      188      186      200      219      216      213      213      211
## 2      181      227      209      204      210      199      225      157
## 3      192      180      176      185      196      200      217       87
## 4      136      207      230      144      171        4        0        1
## 5      213      253       54      206      243      144        0        0
## 6      173      158      103      132      255      217      205      222
##   pixel389 pixel390 pixel391 pixel392 pixel393 pixel394 pixel395 pixel396
## 1      233      148        0        0        0       45      227      204
## 2      127      190      111        0        1        3        2        1
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel397 pixel398 pixel399 pixel400 pixel401 pixel402 pixel403 pixel404
## 1      214      211      218      222      221      230      229      221
## 2        3        2        3        0        0        0       67      214
## 3       53      222      223      235      205      220      200      151
## 4        0        0        0        0      187      220      182       72
## 5        0        0      205      255      149      209      234      219
## 6        0      234      193      237      255      169      139      199
##   pixel405 pixel406 pixel407 pixel408 pixel409 pixel410 pixel411 pixel412
## 1      213      224      233      226      220      219      221      224
## 2      187      139      111      191      229      203      208      208
## 3      206      198      214      199      171      161      197      186
## 4      139      199      192      232      255      244      198      170
## 5      221      220      219      219      213      244      164      224
## 6      182      181      154      171      186      181      154      148
##   pixel413 pixel414 pixel415 pixel416 pixel417 pixel418 pixel419 pixel420
## 1      223      217      210      218      213      254        0        0
## 2      196      223      188      100      128      155      112        0
## 3      218      198      216       88        0        0        0        0
## 4      189        2        0        3        0        0        0        0
## 5      240      195        0        0        0        0        0        0
## 6      242      216      208      220        0        0        0        0
##   pixel421 pixel422 pixel423 pixel424 pixel425 pixel426 pixel427 pixel428
## 1        0      157      226      203      207      211      209      215
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       75      223      223      237
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        9        0
## 6        0        0        0        0        7      239      190      243
##   pixel429 pixel430 pixel431 pixel432 pixel433 pixel434 pixel435 pixel436
## 1      205      198      207      208      201      201      197      203
## 2       48      161      218      171      147      116      185      235
## 3      199      219      203      193      204      201      220      201
## 4      186      175      181       93      164      230      134      153
## 5       20      253      219      221      219      219      219      219
## 6      231      103      164      144       88      188      157      177
##   pixel437 pixel438 pixel439 pixel440 pixel441 pixel442 pixel443 pixel444
## 1      205      210      207      213      214      214      214      213
## 2      201      209      203      204      220      208      126      133
## 3      193      176      189      167      218      202      198      109
## 4      142      137       79      143      183        0        0        2
## 5      221      224      230        0        0        0        0        0
## 6      154      118      181      105      226      223      210      219
##   pixel445 pixel446 pixel447 pixel448 pixel449 pixel450 pixel451 pixel452
## 1      208      234      107        0        0      235      213      204
## 2      162      164      187        0        0       12       16        9
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel453 pixel454 pixel455 pixel456 pixel457 pixel458 pixel459 pixel460
## 1      211      210      209      213      202      197      204      215
## 2        4       19       73      170      211      188      145      149
## 3       79      250      226      243      196      222      201      206
## 4        0        0        0        0       93       72      144      138
## 5        0        0        0        0       49      254      217      221
## 6       30      242      187      246      196       93      162      115
##   pixel461 pixel462 pixel463 pixel464 pixel465 pixel466 pixel467 pixel468
## 1      217      213      212      210      206      212      203      211
## 2      148      136      222      218      207      208      215      224
## 3      205      207      222      185      194      186      187      144
## 4      164      113      124       98       80       57       97      138
## 5      219      219      219      220      221      216      253        7
## 6       91      187      151      182      114       65      158      116
##   pixel469 pixel470 pixel471 pixel472 pixel473 pixel474 pixel475 pixel476
## 1      218      215      214      208      209      222      230        0
## 2      193      140      146      162      156      118      203       37
## 3      207      208      226      111        0        0        0        0
## 4      124        4        0        3        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6      176      220      216      223        0        0        0        0
##   pixel477 pixel478 pixel479 pixel480 pixel481 pixel482 pixel483 pixel484
## 1       52      255      207      200      208      213      210      210
## 2       47      255      188      187      182      203      206      182
## 3        0        0        0        0       87      249      222      240
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        6        0
## 6        0        0        0        0       46      206      187      237
##   pixel485 pixel486 pixel487 pixel488 pixel489 pixel490 pixel491 pixel492
## 1      208      207      202      201      209      216      216      216
## 2      147      140      148      148      148      134      180      218
## 3      226      193      187      205      206      198      212      190
## 4      149      171       98       70      180      152      227      173
## 5       94      229      216      220      218      218      219      220
## 6      156      106      186      180      178      173      160      176
##   pixel493 pixel494 pixel495 pixel496 pixel497 pixel498 pixel499 pixel500
## 1      216      214      212      205      215      201      228      208
## 2      221      225      204      156      134      139      144      140
## 3      189      185      185      191      211      212      210      113
## 4      143      180      183      127      206       38        0        5
## 5      219      218      226       52        0        5        0        0
## 6      180      161      188      136      133      226      219      219
##   pixel501 pixel502 pixel503 pixel504 pixel505 pixel506 pixel507 pixel508
## 1      214      212      218       25      118      217      201      206
## 2      123       97      206       30       88      203      164      167
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       20        0        0        0        0        0        0        0
##   pixel509 pixel510 pixel511 pixel512 pixel513 pixel514 pixel515 pixel516
## 1      208      213      208      205      206      210      211      202
## 2      170      161      147      147      156      159      153      152
## 3       95      249      227      236      219      213      186      203
## 4        1        0        0        0      195      210      226      113
## 5        0        0        4        0      119      231      214      220
## 6       76      208      185      249      152      149      171      110
##   pixel517 pixel518 pixel519 pixel520 pixel521 pixel522 pixel523 pixel524
## 1      199      207      208      209      210      207      210      210
## 2      155      150      134      136      153      145      136      151
## 3      219      198      206      218      194      177      181      195
## 4      187      224      210      191      181      224      212      198
## 5      217      218      219      220      221      217      224       34
## 6      163      174      160      167      190      138      149      154
##   pixel525 pixel526 pixel527 pixel528 pixel529 pixel530 pixel531 pixel532
## 1      245      139      119      255      202      203      236      114
## 2      164      168      170      166      153      130      200       10
## 3      148      229      206      122        0        0        0        0
## 4      172       36        0        6        0        0        0        0
## 5        0        3        0        0        0        0        0        0
## 6      147      225      216      206       45        0        0        0
##   pixel533 pixel534 pixel535 pixel536 pixel537 pixel538 pixel539 pixel540
## 1      171      238      212      203      220      216      217      209
## 2      131      195      150      133      132      142      160      166
## 3        0        0        0        0      106      248      228      224
## 4        0        0        0        0        1        0        0        0
## 5        0        0        0        0        0        0        3        0
## 6        0        0        0        0       89      202      186      226
##   pixel541 pixel542 pixel543 pixel544 pixel545 pixel546 pixel547 pixel548
## 1      207      205      210      211      206      204      206      209
## 2      162      153      152      153      151      154      163      147
## 3      206      202      200      206      191      190      194      203
## 4      153      197      171      175      161      171      199      224
## 5      113      231      213      222      217      218      219      220
## 6      152      167      122       74      151      187      149      166
##   pixel549 pixel550 pixel551 pixel552 pixel553 pixel554 pixel555 pixel556
## 1      211      215      210      206      221      242        0      224
## 2      152      163      205      208      174      175      178      181
## 3      196      180      180      185      148      228      214      117
## 4      187      206      192      176      179       48        0        6
## 5      219      219      219       10        0        2        0        0
## 6      182      113       71      133      168      208      218      179
##   pixel557 pixel558 pixel559 pixel560 pixel561 pixel562 pixel563 pixel564
## 1      234      230      181       26       39      145      201      255
## 2      168      150      196        1      155      228      197      172
## 3        0        0        0        0        0        0        0        0
## 4        1        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       25        0        0        0        0        0        0        0
##   pixel565 pixel566 pixel567 pixel568 pixel569 pixel570 pixel571 pixel572
## 1      157      115      250      200      207      206      207      213
## 2      138      123      121      122      126      129      135      139
## 3      110      242      225      239      218      204      201      202
## 4        0        0        0        0      180      195      136      230
## 5        0        0        3        0       51      227      215      221
## 6      108      193      180      197      152      155      160      115
##   pixel573 pixel574 pixel575 pixel576 pixel577 pixel578 pixel579 pixel580
## 1      216      206      205      206      207      206      215      207
## 2      144      158      177      192      239      244      178      176
## 3      211      193      197      217      203      183      184      175
## 4      240      148      204      140      173      199      193      156
## 5      217      217      219      220      218      219      213        0
## 6      168      192      154      161      193      141      122      159
##   pixel581 pixel582 pixel583 pixel584 pixel585 pixel586 pixel587 pixel588
## 1      221      238        0        0      188       85        0        0
## 2      183      179      185      177      147      148      192        0
## 3      212      219      206      122        0        0        0        0
## 4      213       37        0        5        0        0        0        0
## 5        0        1        0        0        0        0        0        0
## 6      156      193      213      170       29        0        0        0
##   pixel589 pixel590 pixel591 pixel592 pixel593 pixel594 pixel595 pixel596
## 1        0        0        0       31        0      129      253      190
## 2        0       80      196      199      212      209      190      176
## 3        0        0        0        0      122      249      234      241
## 4        0        0        0        0        0        1        0        0
## 5        0        0        0        0        0        0        2        0
## 6        0        0        0        0      141      193      170      169
##   pixel597 pixel598 pixel599 pixel600 pixel601 pixel602 pixel603 pixel604
## 1      207      208      208      208      209      211      211      209
## 2      170      178      183      192      204      204      202      144
## 3      216      215      196      200      222      208      195      225
## 4      150      101       72      167      158       95      177      234
## 5        0      216      217      219      218      218      219      219
## 6      151      161      163      153      168      198      155      153
##   pixel605 pixel606 pixel607 pixel608 pixel609 pixel610 pixel611 pixel612
## 1      209      209      212      201      226      165        0        0
## 2       59        3        0       67      217      184      188      178
## 3      212      181      186      183      227      214      211      121
## 4      113      142      112       59      152       22        0        3
## 5      218      220      246        0        0        2        0        0
## 6      188      164      161      170      148      195      210      188
##   pixel613 pixel614 pixel615 pixel616 pixel617 pixel618 pixel619 pixel620
## 1        0        0        0        0        2        0        0        0
## 2      166      160      188       13        0        0        0        3
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       60        0        0        0        0        0        0        0
##   pixel621 pixel622 pixel623 pixel624 pixel625 pixel626 pixel627 pixel628
## 1        0       89      254      199      199      192      196      198
## 2       51      105      179      217      235      227      208      201
## 3      148      230      237      228      214      213      201      207
## 4        0        0        0        0      197      178       82       47
## 5        0        0        2        0        0      217      216      216
## 6      178      184      188      163      148      160      163      164
##   pixel629 pixel630 pixel631 pixel632 pixel633 pixel634 pixel635 pixel636
## 1      199      201      202      203      204      203      203      200
## 2      133       58        0        0        0        0        0       16
## 3      214      201      204      235      191      161      185      180
## 4       64      106      112      121      110      189      225      121
## 5      219      218      219      219      217      222      235        0
## 6      162      202      155      156      187      168      152      172
##   pixel637 pixel638 pixel639 pixel640 pixel641 pixel642 pixel643 pixel644
## 1      222      155        0        3        3        3        2        0
## 2      187      194      184      185      175      181      131        0
## 3      216      201      212      119        0        0        0        0
## 4       98       58        0        4        0        0        0        0
## 5        0        2        0        0        0        0        0        0
## 6      148      173      207      224      100        0        0        0
##   pixel645 pixel646 pixel647 pixel648 pixel649 pixel650 pixel651 pixel652
## 1        0        0        1        5        0        0      255      218
## 2        0        3        0        0        0        0        0        0
## 3        0        0        0        0      151      230      235      229
## 4        0        0        0        0        0        2        0        0
## 5        0        0        0        0        0        0        2        0
## 6        0        0        0        0      188      181      204      194
##   pixel653 pixel654 pixel655 pixel656 pixel657 pixel658 pixel659 pixel660
## 1      226      232      228      224      222      220      219      219
## 2        0        0        0        0        0        0        0        5
## 3      218      209      198      193      226      182      189      240
## 4      202      219      161      135      205      200      156      195
## 5        0      212      216      217      219      218      219      219
## 6      146      158      161      169      177      185      154      148
##   pixel661 pixel662 pixel663 pixel664 pixel665 pixel666 pixel667 pixel668
## 1      217      221      220      212      236       95        0        2
## 2        5        5        0        0        0        0        0        0
## 3      200      186      188      188      218      216      209      110
## 4      231      234      218      182      223       99        0        6
## 5      216      224      204        0        0        2        0        0
## 6      168      169      157      161      155      188      209      190
##   pixel669 pixel670 pixel671 pixel672 pixel673 pixel674 pixel675 pixel676
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       62        0        0        0        0        0        0        0
##   pixel677 pixel678 pixel679 pixel680 pixel681 pixel682 pixel683 pixel684
## 1        0        0      155      194      168      170      171      173
## 2        0        0        0        0        0        0        0        0
## 3       96      237      233      237      207      200      203      191
## 4        0        4        0        0      188      152      118      222
## 5        0        0        4        0        3      217      216      217
## 6      117      198      189       80      216      201      205      205
##   pixel685 pixel686 pixel687 pixel688 pixel689 pixel690 pixel691 pixel692
## 1      173      179      177      175      172      171      167      161
## 2        0        0        0        0        0        0        0        0
## 3      213      170      186      239      209      186      173      186
## 4      214      203      233      226      193      200      173       53
## 5      219      218      220      220      218      222      209        0
## 6      209      233      189      197      211      202      200      201
##   pixel693 pixel694 pixel695 pixel696 pixel697 pixel698 pixel699 pixel700
## 1      180        0        0        1        0        1        0        0
## 2        0        0        0        0        0        0        0        0
## 3      156      199      217       84        0        0        0        0
## 4      166       97        0        6        0        0        0        0
## 5        0        2        0        0        0        0        0        0
## 6      210      150      220      197       72        0        0        0
##   pixel701 pixel702 pixel703 pixel704 pixel705 pixel706 pixel707 pixel708
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0       89      237      207      121
## 4        0        0        0        0        0        3        0        2
## 5        0        0        0        0        0        0        3        0
## 6        0        0        0        0      151      214      181        6
##   pixel709 pixel710 pixel711 pixel712 pixel713 pixel714 pixel715 pixel716
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      255      193      183      193      209      224      208      224
## 4      182      152       51       89      174      183      168      112
## 5        4      214      211      213      213      215      213      211
## 6      153       82       56       30        3        0       73       74
##   pixel717 pixel718 pixel719 pixel720 pixel721 pixel722 pixel723 pixel724
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      203      160      184      223        0      186      216      102
## 4      109      181      170      136      108       60        0        4
## 5      214      220      200        0        0        1        0        0
## 6       41       28       30       46       78       13      201      146
##   pixel725 pixel726 pixel727 pixel728 pixel729 pixel730 pixel731 pixel732
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6       39        0        0        0        0        0        0        0
##   pixel733 pixel734 pixel735 pixel736 pixel737 pixel738 pixel739 pixel740
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      108      242      253        0      112      255      217      211
## 4        0        2        0        5      194      193      204      104
## 5        0        0        3        0        0      214      232      229
## 6        7       34       58        0        0        0        0        0
##   pixel741 pixel742 pixel743 pixel744 pixel745 pixel746 pixel747 pixel748
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      227      245      195      194      211      220      214       74
## 4      116      241      217      196      171      249      207      197
## 5      249      245      248      252      230      237      229        0
## 6        0        0        0        0        0        0        0        0
##   pixel749 pixel750 pixel751 pixel752 pixel753 pixel754 pixel755 pixel756
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0      255      222      128        0        0        0        0
## 4      202       45        0        3        0        0        0        0
## 5        0        1        0        0        0        0        0        0
## 6        0        0        0        0        0        0        0        0
##   pixel757 pixel758 pixel759 pixel760 pixel761 pixel762 pixel763 pixel764
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       44       12        0
## 4        0        0        0        0        0        0        1        0
## 5        0        0        0        0        0        0        0        0
## 6        0        0        0        4        0        0        0        2
##   pixel765 pixel766 pixel767 pixel768 pixel769 pixel770 pixel771 pixel772
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0       40      134      162      191      214      163      146
## 4        0        0       22       21       25       69       52       45
## 5        0       68      116      112      136      147      144      121
## 6        4        9        7        8        7        5        4        8
##   pixel773 pixel774 pixel775 pixel776 pixel777 pixel778 pixel779 pixel780
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      165       79        0        0        0       30       43        0
## 4       74       39        3        0        0        0        0        1
## 5      102       63        0        0        0        0        0        0
## 6       12        5        7        8        7        4        3        7
##   pixel781 pixel782 pixel783 pixel784
## 1        0        0        0        0
## 2        0        0        0        0
## 3        0        0        0        0
## 4        0        0        0        0
## 5        0        0        0        0
## 6        5        0        0        0
f.data$label = as.factor(f.data$label)#response

class(f.data$label)
## [1] "factor"
summary(f.data$label)
##    0    1    2    3    4    5    6    7    8    9 
## 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000
library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         10 minutes 17 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.32 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(f.data, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   label pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 pixel10
## 1     2      0      0      0      0      0      0      0      0      0       0
## 2     9      0      0      0      0      0      0      0      0      0       0
## 3     6      0      0      0      0      0      0      0      5      0       0
## 4     0      0      0      0      1      2      0      0      0      0       0
## 5     3      0      0      0      0      0      0      0      0      0       0
## 6     4      0      0      0      5      4      5      5      3      5       6
##   pixel11 pixel12 pixel13 pixel14 pixel15 pixel16 pixel17 pixel18 pixel19
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     105      92     101     107     100     132       0       0
## 4     114     183     112      55      23      72     102     165     160
## 5       0       0      46       0      21      68       0       0       0
## 6       2       0       0       0       0       0       0       0       2
##   pixel20 pixel21 pixel22 pixel23 pixel24 pixel25 pixel26 pixel27 pixel28
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       2       4       0       1       0       0       0       0       0
## 4      28       0       0       0       1       0       0       0       0
## 5       0       0       0       0       0       0       0       0       0
## 6       3       7       4       4       5       5       0       0       0
##   pixel29 pixel30 pixel31 pixel32 pixel33 pixel34 pixel35 pixel36 pixel37
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0       0
## 4       0       0       0       0       1       0       0      24     188
## 5       0       0       0       0       0       0       0       0      25
## 6       0       0       0       4       5       4       5      11       2
##   pixel38 pixel39 pixel40 pixel41 pixel42 pixel43 pixel44 pixel45 pixel46
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0     150     227     211     171     191     236     228     172
## 4     163      93     136     153     168     252     174     136     166
## 5     187     189       0     119     173     239     112       0     189
## 6       0       0      21     231     156     134     177     194       1
##   pixel47 pixel48 pixel49 pixel50 pixel51 pixel52 pixel53 pixel54 pixel55
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       1       0       0       0       0
## 4     130     123     131      66       0       0       1       0       0
## 5     125       0       0       0       0       0       0       0       0
## 6       0       0       0      10       5       5       4       0       0
##   pixel56 pixel57 pixel58 pixel59 pixel60 pixel61 pixel62 pixel63 pixel64
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0      48
## 4       0       0       0       0       2       0      10     157     216
## 5       0       0       0       0       0       0       0       0      14
## 6       0       0       0       0       4       5       3       7       0
##   pixel65 pixel66 pixel67 pixel68 pixel69 pixel70 pixel71 pixel72 pixel73
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     153     209     210     187     199     142     157     209     179
## 4     226     208     142      66     115     149     230     190     196
## 5     219     224     211      92      52     227     224     127      92
## 6       0      26     112     168     162     212     226     199     146
##   pixel74 pixel75 pixel76 pixel77 pixel78 pixel79 pixel80 pixel81 pixel82
## 1       0       0       0       0       0       0       0       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3     199     233     138      44       0       0       0       0       0
## 4     198     172     222     107     165     211       0       0       0
## 5     196     237     136       0       0       0       0       0       0
## 6     168     124      25       0       0       0       4       5       0
##   pixel83 pixel84 pixel85 pixel86 pixel87 pixel88 pixel89 pixel90 pixel91
## 1       0       0       0       0       0       0       4       0       0
## 2       0       0       0       0       0       0       0       0       0
## 3       0       0       0       0       0       0       0       0     120
## 4       0       0       0       0       0       0       0     118     214
## 5       0       0       0       0       0       0       0       0       0
## 6       0       0       0       0       0       4       5       8       0
##   pixel92 pixel93 pixel94 pixel95 pixel96 pixel97 pixel98 pixel99 pixel100
## 1       0       0       0      62      61      21      29      23       51
## 2       0       0       0       0       0       0       0       0        0
## 3     218     215     207     198     198     223     219     214      225
## 4     174     168     109     200     124     150     143      58       63
## 5     106     238     202     205     224     225     217     218      222
## 6      15      95     142     170     144     123     156     172      140
##   pixel101 pixel102 pixel103 pixel104 pixel105 pixel106 pixel107 pixel108
## 1      136       61        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      190      177      195      209      204      139        0        1
## 4       89      137       97      168      138      142      195      156
## 5      227      202      206      218        0        0        0        0
## 6      131      135      160      137      141       53        0        0
##   pixel109 pixel110 pixel111 pixel112 pixel113 pixel114 pixel115 pixel116
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4        0        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        3        0        0        0        0        0        0        5
##   pixel117 pixel118 pixel119 pixel120 pixel121 pixel122 pixel123 pixel124
## 1        0        0        0       88      201      228      225      255
## 2        0        0        0        0        0        0        0        0
## 3        0        0      208      200      197      202      193      203
## 4       25      140       70       80       43       71       96       93
## 5        0        0        0      166      239      216      214      192
## 6        6        0        0      165      130      120      130       56
##   pixel125 pixel126 pixel127 pixel128 pixel129 pixel130 pixel131 pixel132
## 1      115       62      137      255      235      222      255      135
## 2        0        0        0        0        0        0        0        0
## 3      207      228      245      196      197      202      193      190
## 4      151      121      197      143      107       82      101      111
## 5      181      205      204      180      187      205      208      230
## 6      124      122       68      143      111       98      132      125
##   pixel133 pixel134 pixel135 pixel136 pixel137 pixel138 pixel139 pixel140
## 1        0        0        0        0        0        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      167      203       27        0        0        0        0        0
## 4       80      137      193      208        6        0        0        0
## 5       45        0        0        0        0        0        0        0
## 6      111      164       60        0        6        0        0        0
##   pixel141 pixel142 pixel143 pixel144 pixel145 pixel146 pixel147 pixel148
## 1        0        0        0        0        0       47      252      234
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0       89      217      200
## 4        0        0        0        0       74      194      107      146
## 5        0        0        0        0        0        0        0      220
## 6        0        0        0        3        7        0       99      165
##   pixel149 pixel150 pixel151 pixel152 pixel153 pixel154 pixel155 pixel156
## 1      238      224      215      215      229      108      180      207
## 2        0        0        0        0        1        1        0        0
## 3      211      175      189      205      199      218      216      181
## 4      178      185      182       77      185      218      210      175
## 5      235      233      213      205      201      185      184      200
## 6      135      154       98       66      130      141      114      143
##   pixel157 pixel158 pixel159 pixel160 pixel161 pixel162 pixel163 pixel164
## 1      214      224      231      249      254       45        0        0
## 2        0        0       81       21        0        1        0        1
## 3      179      185      192      198      184      198      121        0
## 4      174      235      217      217      129      180      210      208
## 5      199      223      224      232      101        0        0        0
## 6      113       71      107      147      117      176      172        0
##   pixel165 pixel166 pixel167 pixel168 pixel169 pixel170 pixel171 pixel172
## 1        0        0        0        0        0        0        1        0
## 2        0        0        0        0        0        0        0        0
## 3        0        0        0        0        0        0        0        0
## 4       89        0        0        0        0        0        0        0
## 5        0        0        0        0        0        0        0        0
## 6        5        0        0        0        0        0        0        4
##   pixel173 pixel174 pixel175 pixel176 pixel177 pixel178 pixel179 pixel180
## 1        0      214      222      210      213      224      225      217
## 2        0        0        0        0        0        0        0        1
## 3        0      140      220      204      223      187      182      200
## 4      179      213      203      177      228      192      193      162
## 5        0        0        0      252      238      226      226      221
## 6        0        0      197      188      169      148      146      132
##   pixel181 pixel182 pixel183 pixel184 pixel185 pixel186 pixel187 pixel188
## 1      220      254      233      219      221      217      223      221
## 2        0        0        0        0        0       14      200       49
## 3      207      217      213      210      207      180      161      192
## 4      143      172      196      205      181      180      140      134
## 5      224      195      198      223      229      205      202      235
## 6      110      133      107      139      131      133      141      156
##   pixel189 pixel190 pixel191 pixel192 pixel193 pixel194 pixel195 pixel196
## 1      240      254        0        0        1        0        0        0
## 2        0        0        0        0        0        0        0        0
## 3      184      199      162        0        0        0        0        0
## 4      176      194      171      170       65        0        0        0
## 5      158        0        0        0        0        0        0        0
## 6      143      170      186        0        0        0        0        0
##   pixel197 pixel198 pixel199
## 1        1        0        0
## 2        0        0        0
## 3        0        0        0
## 4        0        0        0
## 5        0        0        0
## 6        0        0        0
g.split = h2o.splitFrame(data = d.hex,ratios = 0.75)
train = g.split[[1]]#75% training data
test = g.split[[2]]


### DNN with 3 hidden layers (1000 neurons each)
### activation function: Rectifier with droput (improve generalization.)
### epochs:The number of iterations to be carried out. More epochs=more accuracy

### Adaptive learning rates self adjust to avoid local minima or slow convergence.
###  learning rate annealing gradually reduces learning
### Momentum modifies back-propagation by allowing prior iterations to influence
##the current update. 

#This takes lot of time
#fmnist_nn = h2o.deeplearning(x = 2:785,
                                    # y = "label",
                                    # training_frame = train,
                                    # distribution = "multinomial",
                                    # model_id = "fmnist_nn",
                                    # activation = "RectifierWithDropout",
                                    # hidden=c(1000, 1000, 2000),
                                    # epochs = 180,
                                    # adaptive_rate = FALSE,
                                    # rate=0.01,
                                    # rate_annealing = 1.0e-6,
                                    # rate_decay = 1.0,
                                    # momentum_start = 0.4,
                                    # momentum_ramp = 384000,
                                    # momentum_stable = 0.98, 
                                    # input_dropout_ratio = 0.22,
                                    # l1 = 1.0e-5,
                                    # max_w2 = 15.0, 
                                    # initial_weight_distribution = "Normal",
                                    # initial_weight_scale = 0.01,
                                    # nesterov_accelerated_gradient = TRUE,
                                    # loss = "CrossEntropy",
                                    # fast_mode = TRUE,
                                    # diagnostics = TRUE,
                                    # ignore_const_cols = TRUE,
                                    # force_load_balance = TRUE,
                                    # seed = 3.656455e+18)

#h2o.confusionMatrix(fmnist_nn, test)

## DNN with 2 hidden layers
## default- 2 hidden layers (200 neurons each)
m = h2o.deeplearning(x = 2:785,
                     y = "label",
                     training_frame = train,
                     distribution = "multinomial",
                     model_id = "m",
                     activation = "Tanh",
                     l2 = 0.00001,
                     hidden = c(50,50),
                     loss = "CrossEntropy")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |======================================================================| 100%
h2o.confusionMatrix(m, test)
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
##           0    1    2    3    4    5    6    7    8    9  Error
## 0      1237    4   25   37    5    1  149    0    9    0 0.1568
## 1         7 1527    1   21    5    2    8    0    0    0 0.0280
## 2        18    1 1230   17  109    3   86    0    4    0 0.1621
## 3        49   20   15 1333   64    2   25    0    6    0 0.1196
## 4         4    2  176   45 1147    0   83    0    4    0 0.2149
## 5         0    0    0    0    0 1442    0   26    8   36 0.0463
## 6       174    5  164   35  100    1  977    1   11    2 0.3354
## 7         0    0    0    0    0   48    0 1378    8   70 0.0838
## 8        14    1   11    8    7    5   20    4 1437    1 0.0471
## 9         0    2    0    0    0   24    0   42    0 1507 0.0432
## Totals 1503 1562 1622 1496 1437 1528 1348 1451 1487 1616 0.1219
##                    Rate
## 0      =    230 / 1,467
## 1      =     44 / 1,571
## 2      =    238 / 1,468
## 3      =    181 / 1,514
## 4      =    314 / 1,461
## 5      =     70 / 1,512
## 6      =    493 / 1,470
## 7      =    126 / 1,504
## 8      =     71 / 1,508
## 9      =     68 / 1,575
## Totals = 1,835 / 15,050
(acc=1-(1811 / 15050))
## [1] 0.88

variable importance

library(caret)
#library(mxnet)

# loading data
tumor=read.csv("cancer_tumor.csv") #predict if the tumor is
#malignant (M) or benign (B)

head(tumor)
##         id diagnosis radius_mean texture_mean perimeter_mean area_mean
## 1   842302         M          18           10            123      1001
## 2   842517         M          21           18            133      1326
## 3 84300903         M          20           21            130      1203
## 4 84348301         M          11           20             78       386
## 5 84358402         M          20           14            135      1297
## 6   843786         M          12           16             83       477
##   smoothness_mean compactness_mean concavity_mean concave.points_mean
## 1           0.118            0.278          0.300               0.147
## 2           0.085            0.079          0.087               0.070
## 3           0.110            0.160          0.197               0.128
## 4           0.142            0.284          0.241               0.105
## 5           0.100            0.133          0.198               0.104
## 6           0.128            0.170          0.158               0.081
##   symmetry_mean fractal_dimension_mean radius_se texture_se perimeter_se
## 1          0.24                  0.079      1.09       0.91          8.6
## 2          0.18                  0.057      0.54       0.73          3.4
## 3          0.21                  0.060      0.75       0.79          4.6
## 4          0.26                  0.097      0.50       1.16          3.4
## 5          0.18                  0.059      0.76       0.78          5.4
## 6          0.21                  0.076      0.33       0.89          2.2
##   area_se smoothness_se compactness_se concavity_se concave.points_se
## 1     153        0.0064          0.049        0.054             0.016
## 2      74        0.0052          0.013        0.019             0.013
## 3      94        0.0062          0.040        0.038             0.021
## 4      27        0.0091          0.075        0.057             0.019
## 5      94        0.0115          0.025        0.057             0.019
## 6      27        0.0075          0.033        0.037             0.011
##   symmetry_se fractal_dimension_se radius_worst texture_worst perimeter_worst
## 1       0.030               0.0062           25            17             185
## 2       0.014               0.0035           25            23             159
## 3       0.022               0.0046           24            26             152
## 4       0.060               0.0092           15            26              99
## 5       0.018               0.0051           23            17             152
## 6       0.022               0.0051           15            24             103
##   area_worst smoothness_worst compactness_worst concavity_worst
## 1       2019             0.16              0.67            0.71
## 2       1956             0.12              0.19            0.24
## 3       1709             0.14              0.42            0.45
## 4        568             0.21              0.87            0.69
## 5       1575             0.14              0.20            0.40
## 6        742             0.18              0.52            0.54
##   concave.points_worst symmetry_worst fractal_dimension_worst  X
## 1                 0.27           0.46                   0.119 NA
## 2                 0.19           0.28                   0.089 NA
## 3                 0.24           0.36                   0.088 NA
## 4                 0.26           0.66                   0.173 NA
## 5                 0.16           0.24                   0.077 NA
## 6                 0.17           0.40                   0.124 NA
#drop columns
drops =c("id","X")
df=tumor[ , !(names(tumor) %in% drops)]

head(df)
##   diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean
## 1         M          18           10            123      1001           0.118
## 2         M          21           18            133      1326           0.085
## 3         M          20           21            130      1203           0.110
## 4         M          11           20             78       386           0.142
## 5         M          20           14            135      1297           0.100
## 6         M          12           16             83       477           0.128
##   compactness_mean concavity_mean concave.points_mean symmetry_mean
## 1            0.278          0.300               0.147          0.24
## 2            0.079          0.087               0.070          0.18
## 3            0.160          0.197               0.128          0.21
## 4            0.284          0.241               0.105          0.26
## 5            0.133          0.198               0.104          0.18
## 6            0.170          0.158               0.081          0.21
##   fractal_dimension_mean radius_se texture_se perimeter_se area_se
## 1                  0.079      1.09       0.91          8.6     153
## 2                  0.057      0.54       0.73          3.4      74
## 3                  0.060      0.75       0.79          4.6      94
## 4                  0.097      0.50       1.16          3.4      27
## 5                  0.059      0.76       0.78          5.4      94
## 6                  0.076      0.33       0.89          2.2      27
##   smoothness_se compactness_se concavity_se concave.points_se symmetry_se
## 1        0.0064          0.049        0.054             0.016       0.030
## 2        0.0052          0.013        0.019             0.013       0.014
## 3        0.0062          0.040        0.038             0.021       0.022
## 4        0.0091          0.075        0.057             0.019       0.060
## 5        0.0115          0.025        0.057             0.019       0.018
## 6        0.0075          0.033        0.037             0.011       0.022
##   fractal_dimension_se radius_worst texture_worst perimeter_worst area_worst
## 1               0.0062           25            17             185       2019
## 2               0.0035           25            23             159       1956
## 3               0.0046           24            26             152       1709
## 4               0.0092           15            26              99        568
## 5               0.0051           23            17             152       1575
## 6               0.0051           15            24             103        742
##   smoothness_worst compactness_worst concavity_worst concave.points_worst
## 1             0.16              0.67            0.71                 0.27
## 2             0.12              0.19            0.24                 0.19
## 3             0.14              0.42            0.45                 0.24
## 4             0.21              0.87            0.69                 0.26
## 5             0.14              0.20            0.40                 0.16
## 6             0.18              0.52            0.54                 0.17
##   symmetry_worst fractal_dimension_worst
## 1           0.46                   0.119
## 2           0.28                   0.089
## 3           0.36                   0.088
## 4           0.66                   0.173
## 5           0.24                   0.077
## 6           0.40                   0.124
df=na.omit(df)


library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         13 minutes 54 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.35 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(df, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean
## 1         M          18           10            123      1001           0.118
## 2         M          21           18            133      1326           0.085
## 3         M          20           21            130      1203           0.110
## 4         M          11           20             78       386           0.142
## 5         M          20           14            135      1297           0.100
## 6         M          12           16             83       477           0.128
##   compactness_mean concavity_mean concave.points_mean symmetry_mean
## 1            0.278          0.300               0.147          0.24
## 2            0.079          0.087               0.070          0.18
## 3            0.160          0.197               0.128          0.21
## 4            0.284          0.241               0.105          0.26
## 5            0.133          0.198               0.104          0.18
## 6            0.170          0.158               0.081          0.21
##   fractal_dimension_mean radius_se texture_se perimeter_se area_se
## 1                  0.079      1.09       0.91          8.6     153
## 2                  0.057      0.54       0.73          3.4      74
## 3                  0.060      0.75       0.79          4.6      94
## 4                  0.097      0.50       1.16          3.4      27
## 5                  0.059      0.76       0.78          5.4      94
## 6                  0.076      0.33       0.89          2.2      27
##   smoothness_se compactness_se concavity_se concave.points_se symmetry_se
## 1        0.0064          0.049        0.054             0.016       0.030
## 2        0.0052          0.013        0.019             0.013       0.014
## 3        0.0062          0.040        0.038             0.021       0.022
## 4        0.0091          0.075        0.057             0.019       0.060
## 5        0.0115          0.025        0.057             0.019       0.018
## 6        0.0075          0.033        0.037             0.011       0.022
##   fractal_dimension_se radius_worst texture_worst perimeter_worst area_worst
## 1               0.0062           25            17             185       2019
## 2               0.0035           25            23             159       1956
## 3               0.0046           24            26             152       1709
## 4               0.0092           15            26              99        568
## 5               0.0051           23            17             152       1575
## 6               0.0051           15            24             103        742
##   smoothness_worst compactness_worst concavity_worst concave.points_worst
## 1             0.16              0.67            0.71                 0.27
## 2             0.12              0.19            0.24                 0.19
## 3             0.14              0.42            0.45                 0.24
## 4             0.21              0.87            0.69                 0.26
## 5             0.14              0.20            0.40                 0.16
## 6             0.18              0.52            0.54                 0.17
##   symmetry_worst fractal_dimension_worst
## 1           0.46                   0.119
## 2           0.28                   0.089
## 3           0.36                   0.088
## 4           0.66                   0.173
## 5           0.24                   0.077
## 6           0.40                   0.124
str(d.hex)
## Class 'H2OFrame' <environment: 0x000000003e2546c0> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 569
##  - attr(*, "ncol")= int 31
##  - attr(*, "types")=List of 31
##   ..$ : chr "enum"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##  - attr(*, "data")='data.frame': 10 obs. of  31 variables:
##   ..$ diagnosis              : Factor w/ 2 levels "B","M": 2 2 2 2 2 2 2 2 2 2
##   ..$ radius_mean            : num  18 20.6 19.7 11.4 20.3 ...
##   ..$ texture_mean           : num  10.4 17.8 21.2 20.4 14.3 ...
##   ..$ perimeter_mean         : num  122.8 132.9 130 77.6 135.1 ...
##   ..$ area_mean              : num  1001 1326 1203 386 1297 ...
##   ..$ smoothness_mean        : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...
##   ..$ compactness_mean       : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...
##   ..$ concavity_mean         : num  0.3001 0.0869 0.1974 0.2414 0.198 ...
##   ..$ concave.points_mean    : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...
##   ..$ symmetry_mean          : num  0.242 0.181 0.207 0.26 0.181 ...
##   ..$ fractal_dimension_mean : num  0.0787 0.0567 0.06 0.0974 0.0588 ...
##   ..$ radius_se              : num  1.095 0.543 0.746 0.496 0.757 ...
##   ..$ texture_se             : num  0.905 0.734 0.787 1.156 0.781 ...
##   ..$ perimeter_se           : num  8.59 3.4 4.58 3.44 5.44 ...
##   ..$ area_se                : num  153.4 74.1 94 27.2 94.4 ...
##   ..$ smoothness_se          : num  0.0064 0.00522 0.00615 0.00911 0.01149 ...
##   ..$ compactness_se         : num  0.049 0.0131 0.0401 0.0746 0.0246 ...
##   ..$ concavity_se           : num  0.0537 0.0186 0.0383 0.0566 0.0569 ...
##   ..$ concave.points_se      : num  0.0159 0.0134 0.0206 0.0187 0.0188 ...
##   ..$ symmetry_se            : num  0.03 0.0139 0.0225 0.0596 0.0176 ...
##   ..$ fractal_dimension_se   : num  0.00619 0.00353 0.00457 0.00921 0.00511 ...
##   ..$ radius_worst           : num  25.4 25 23.6 14.9 22.5 ...
##   ..$ texture_worst          : num  17.3 23.4 25.5 26.5 16.7 ...
##   ..$ perimeter_worst        : num  184.6 158.8 152.5 98.9 152.2 ...
##   ..$ area_worst             : num  2019 1956 1709 568 1575 ...
##   ..$ smoothness_worst       : num  0.162 0.124 0.144 0.21 0.137 ...
##   ..$ compactness_worst      : num  0.666 0.187 0.424 0.866 0.205 ...
##   ..$ concavity_worst        : num  0.712 0.242 0.45 0.687 0.4 ...
##   ..$ concave.points_worst   : num  0.265 0.186 0.243 0.258 0.163 ...
##   ..$ symmetry_worst         : num  0.46 0.275 0.361 0.664 0.236 ...
##   ..$ fractal_dimension_worst: num  0.1189 0.089 0.0876 0.173 0.0768 ...
#set variables
y <- "diagnosis"

x <- setdiff(colnames(d.hex),y)

#train the model - without hidden layer
deepmodel = h2o.deeplearning(x = x
                              ,y = y
                              ,training_frame = d.hex
                              ,standardize = T
                              ,model_id = "deep_model"
                              ,activation = "Rectifier"
                              ,epochs = 100
                              ,seed = 1
                              ,nfolds = 5
                              ,variable_importances = T)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
#compute variable importance and performance
h2o.varimp_plot(deepmodel,num_of_features = 20)

h2o.performance(deepmodel,xval = T)
## H2OBinomialMetrics: deeplearning
## ** Reported on cross-validation data. **
## ** 5-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
## 
## MSE:  0.038
## RMSE:  0.2
## LogLoss:  0.27
## Mean Per-Class Error:  0.04
## AUC:  0.99
## AUCPR:  0.99
## Gini:  0.98
## 
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
##          B   M    Error     Rate
## B      354   3 0.008403   =3/357
## M       15 197 0.070755  =15/212
## Totals 369 200 0.031634  =18/569
## 
## Maximum Metrics: Maximum metrics at their respective thresholds
##                         metric threshold      value idx
## 1                       max f1  0.974770   0.956311  26
## 2                       max f2  0.221098   0.947712  49
## 3                 max f0point5  0.997343   0.978916  22
## 4                 max accuracy  0.997343   0.968366  22
## 5                max precision  1.000000   1.000000   0
## 6                   max recall  0.000000   1.000000 291
## 7              max specificity  1.000000   1.000000   0
## 8             max absolute_mcc  0.997343   0.933026  22
## 9   max min_per_class_accuracy  0.245683   0.943978  47
## 10 max mean_per_class_accuracy  0.974770   0.960421  26
## 11                     max tns  1.000000 357.000000   0
## 12                     max fns  1.000000  43.000000   0
## 13                     max fps  0.000000 357.000000 395
## 14                     max tps  0.000000 212.000000 291
## 15                     max tnr  1.000000   1.000000   0
## 16                     max fnr  1.000000   0.202830   0
## 17                     max fpr  0.000000   1.000000 395
## 18                     max tpr  0.000000   1.000000 291
## 
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
### Identify the Important Predictors (Variable Importance) Using H2o

DNN For Regression using H2o

##### regression 

library(tidyverse)
library(ggplot2)
library(mlbench)

data(BostonHousing)
head(BostonHousing)
##     crim zn indus chas  nox  rm age dis rad tax ptratio   b lstat medv
## 1 0.0063 18   2.3    0 0.54 6.6  65 4.1   1 296      15 397   5.0   24
## 2 0.0273  0   7.1    0 0.47 6.4  79 5.0   2 242      18 397   9.1   22
## 3 0.0273  0   7.1    0 0.47 7.2  61 5.0   2 242      18 393   4.0   35
## 4 0.0324  0   2.2    0 0.46 7.0  46 6.1   3 222      19 395   2.9   33
## 5 0.0690  0   2.2    0 0.46 7.1  54 6.1   3 222      19 397   5.3   36
## 6 0.0299  0   2.2    0 0.46 6.4  59 6.1   3 222      19 394   5.2   29
b=BostonHousing


str(b) #1-13 are perdictors
## 'data.frame':    506 obs. of  14 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : num  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ b      : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
## medv is a response variable (continuous numerical variable)

library(h2o)
h2o.init()
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         15 minutes 34 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.41 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(b, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##     crim zn indus chas  nox  rm age dis rad tax ptratio   b lstat medv
## 1 0.0063 18   2.3    0 0.54 6.6  65 4.1   1 296      15 397   5.0   24
## 2 0.0273  0   7.1    0 0.47 6.4  79 5.0   2 242      18 397   9.1   22
## 3 0.0273  0   7.1    0 0.47 7.2  61 5.0   2 242      18 393   4.0   35
## 4 0.0324  0   2.2    0 0.46 7.0  46 6.1   3 222      19 395   2.9   33
## 5 0.0690  0   2.2    0 0.46 7.1  54 6.1   3 222      19 397   5.3   36
## 6 0.0299  0   2.2    0 0.46 6.4  59 6.1   3 222      19 394   5.2   29
str(d.hex)
## Class 'H2OFrame' <environment: 0x0000000046499ba8> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 506
##  - attr(*, "ncol")= int 14
##  - attr(*, "types")=List of 14
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "enum"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "int"
##   ..$ : chr "int"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##  - attr(*, "data")='data.frame': 10 obs. of  14 variables:
##   ..$ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##   ..$ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5
##   ..$ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87
##   ..$ chas   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1
##   ..$ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524
##   ..$ rm     : num  6.58 6.42 7.18 7 7.15 ...
##   ..$ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9
##   ..$ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##   ..$ rad    : num  1 2 2 3 3 3 5 5 5 5
##   ..$ tax    : num  296 242 242 222 222 222 311 311 311 311
##   ..$ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2
##   ..$ b      : num  397 397 393 395 397 ...
##   ..$ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##   ..$ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9
summary(d.hex)
##  crim               zn               indus            chas   nox             
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.460   0:471  Min.   :0.3850  
##  1st Qu.: 0.00632   1st Qu.:  0.00   1st Qu.: 5.179   1: 35  1st Qu.:0.4487  
##  Median : 0.18426   Median :  0.00   Median : 9.681          Median :0.5376  
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.137          Mean   :0.5547  
##  3rd Qu.: 3.65409   3rd Qu.: 12.50   3rd Qu.:18.083          3rd Qu.:0.6236  
##  Max.   :88.97620   Max.   :100.00   Max.   :27.740          Max.   :0.8710  
##  rm              age              dis              rad             
##  Min.   :3.561   Min.   :  2.90   Min.   : 1.130   Min.   : 1.000  
##  1st Qu.:5.883   1st Qu.: 44.97   1st Qu.: 2.097   1st Qu.: 4.000  
##  Median :6.207   Median : 77.47   Median : 3.203   Median : 5.000  
##  Mean   :6.285   Mean   : 68.57   Mean   : 3.795   Mean   : 9.549  
##  3rd Qu.:6.622   3rd Qu.: 94.05   3rd Qu.: 5.185   3rd Qu.:24.000  
##  Max.   :8.780   Max.   :100.00   Max.   :12.127   Max.   :24.000  
##  tax             ptratio         b                lstat          
##  Min.   :187.0   Min.   :12.60   Min.   :  0.32   Min.   : 1.73  
##  1st Qu.:279.0   1st Qu.:17.39   1st Qu.:375.19   1st Qu.: 6.93  
##  Median :330.0   Median :19.04   Median :391.35   Median :11.35  
##  Mean   :408.2   Mean   :18.46   Mean   :356.67   Mean   :12.65  
##  3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.11   3rd Qu.:16.94  
##  Max.   :711.0   Max.   :22.00   Max.   :396.90   Max.   :37.97  
##  medv           
##  Min.   : 5.00  
##  1st Qu.:16.99  
##  Median :21.20  
##  Mean   :22.53  
##  3rd Qu.:24.98  
##  Max.   :50.00
ncol(d.hex)
## [1] 14
g.split = h2o.splitFrame(data = d.hex,ratios = 0.75)
train = g.split[[1]]#75% training data
test = g.split[[2]]

##2 layer dnn
m = h2o.deeplearning(x = 1:13,
                     y = "medv",
                     training_frame = train,
                     #distribution = "multinomial",
                     model_id = "m",
                     activation = "Tanh",
                     l2 = 0.00001,
                     hidden = c(162,162,100),
                     )
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |======================================================================| 100%
preds=as.vector(h2o.predict(m,test))
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
mean(abs(preds-as.vector(test$medv)))#mse 
## [1] 2.6

Deep Learning Based Unsupervised Classification using H2o

Autoencoders for Unsupervised Learning using H2o

library(ggplot2)

set.seed(1234)

# loading data
tumor=read.csv("cancer_tumor.csv") #predict if the tumor is
#malignant (M) or benign (B)

head(tumor)
##         id diagnosis radius_mean texture_mean perimeter_mean area_mean
## 1   842302         M          18           10            123      1001
## 2   842517         M          21           18            133      1326
## 3 84300903         M          20           21            130      1203
## 4 84348301         M          11           20             78       386
## 5 84358402         M          20           14            135      1297
## 6   843786         M          12           16             83       477
##   smoothness_mean compactness_mean concavity_mean concave.points_mean
## 1           0.118            0.278          0.300               0.147
## 2           0.085            0.079          0.087               0.070
## 3           0.110            0.160          0.197               0.128
## 4           0.142            0.284          0.241               0.105
## 5           0.100            0.133          0.198               0.104
## 6           0.128            0.170          0.158               0.081
##   symmetry_mean fractal_dimension_mean radius_se texture_se perimeter_se
## 1          0.24                  0.079      1.09       0.91          8.6
## 2          0.18                  0.057      0.54       0.73          3.4
## 3          0.21                  0.060      0.75       0.79          4.6
## 4          0.26                  0.097      0.50       1.16          3.4
## 5          0.18                  0.059      0.76       0.78          5.4
## 6          0.21                  0.076      0.33       0.89          2.2
##   area_se smoothness_se compactness_se concavity_se concave.points_se
## 1     153        0.0064          0.049        0.054             0.016
## 2      74        0.0052          0.013        0.019             0.013
## 3      94        0.0062          0.040        0.038             0.021
## 4      27        0.0091          0.075        0.057             0.019
## 5      94        0.0115          0.025        0.057             0.019
## 6      27        0.0075          0.033        0.037             0.011
##   symmetry_se fractal_dimension_se radius_worst texture_worst perimeter_worst
## 1       0.030               0.0062           25            17             185
## 2       0.014               0.0035           25            23             159
## 3       0.022               0.0046           24            26             152
## 4       0.060               0.0092           15            26              99
## 5       0.018               0.0051           23            17             152
## 6       0.022               0.0051           15            24             103
##   area_worst smoothness_worst compactness_worst concavity_worst
## 1       2019             0.16              0.67            0.71
## 2       1956             0.12              0.19            0.24
## 3       1709             0.14              0.42            0.45
## 4        568             0.21              0.87            0.69
## 5       1575             0.14              0.20            0.40
## 6        742             0.18              0.52            0.54
##   concave.points_worst symmetry_worst fractal_dimension_worst  X
## 1                 0.27           0.46                   0.119 NA
## 2                 0.19           0.28                   0.089 NA
## 3                 0.24           0.36                   0.088 NA
## 4                 0.26           0.66                   0.173 NA
## 5                 0.16           0.24                   0.077 NA
## 6                 0.17           0.40                   0.124 NA
#drop columns
drops =c("id","X")
df=tumor[ , !(names(tumor) %in% drops)]

head(df)
##   diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean
## 1         M          18           10            123      1001           0.118
## 2         M          21           18            133      1326           0.085
## 3         M          20           21            130      1203           0.110
## 4         M          11           20             78       386           0.142
## 5         M          20           14            135      1297           0.100
## 6         M          12           16             83       477           0.128
##   compactness_mean concavity_mean concave.points_mean symmetry_mean
## 1            0.278          0.300               0.147          0.24
## 2            0.079          0.087               0.070          0.18
## 3            0.160          0.197               0.128          0.21
## 4            0.284          0.241               0.105          0.26
## 5            0.133          0.198               0.104          0.18
## 6            0.170          0.158               0.081          0.21
##   fractal_dimension_mean radius_se texture_se perimeter_se area_se
## 1                  0.079      1.09       0.91          8.6     153
## 2                  0.057      0.54       0.73          3.4      74
## 3                  0.060      0.75       0.79          4.6      94
## 4                  0.097      0.50       1.16          3.4      27
## 5                  0.059      0.76       0.78          5.4      94
## 6                  0.076      0.33       0.89          2.2      27
##   smoothness_se compactness_se concavity_se concave.points_se symmetry_se
## 1        0.0064          0.049        0.054             0.016       0.030
## 2        0.0052          0.013        0.019             0.013       0.014
## 3        0.0062          0.040        0.038             0.021       0.022
## 4        0.0091          0.075        0.057             0.019       0.060
## 5        0.0115          0.025        0.057             0.019       0.018
## 6        0.0075          0.033        0.037             0.011       0.022
##   fractal_dimension_se radius_worst texture_worst perimeter_worst area_worst
## 1               0.0062           25            17             185       2019
## 2               0.0035           25            23             159       1956
## 3               0.0046           24            26             152       1709
## 4               0.0092           15            26              99        568
## 5               0.0051           23            17             152       1575
## 6               0.0051           15            24             103        742
##   smoothness_worst compactness_worst concavity_worst concave.points_worst
## 1             0.16              0.67            0.71                 0.27
## 2             0.12              0.19            0.24                 0.19
## 3             0.14              0.42            0.45                 0.24
## 4             0.21              0.87            0.69                 0.26
## 5             0.14              0.20            0.40                 0.16
## 6             0.18              0.52            0.54                 0.17
##   symmetry_worst fractal_dimension_worst
## 1           0.46                   0.119
## 2           0.28                   0.089
## 3           0.36                   0.088
## 4           0.66                   0.173
## 5           0.24                   0.077
## 6           0.40                   0.124
table(df$diagnosis)
## 
##   B   M 
## 357 212
prop.table(table(df$diagnosis))
## 
##    B    M 
## 0.63 0.37
library(h2o)
h2o.init(max_mem_size = "2G", 
         nthreads = 2, 
         ip = "localhost", 
         port = 54321)
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         15 minutes 38 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.42 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(df, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean
## 1         M          18           10            123      1001           0.118
## 2         M          21           18            133      1326           0.085
## 3         M          20           21            130      1203           0.110
## 4         M          11           20             78       386           0.142
## 5         M          20           14            135      1297           0.100
## 6         M          12           16             83       477           0.128
##   compactness_mean concavity_mean concave.points_mean symmetry_mean
## 1            0.278          0.300               0.147          0.24
## 2            0.079          0.087               0.070          0.18
## 3            0.160          0.197               0.128          0.21
## 4            0.284          0.241               0.105          0.26
## 5            0.133          0.198               0.104          0.18
## 6            0.170          0.158               0.081          0.21
##   fractal_dimension_mean radius_se texture_se perimeter_se area_se
## 1                  0.079      1.09       0.91          8.6     153
## 2                  0.057      0.54       0.73          3.4      74
## 3                  0.060      0.75       0.79          4.6      94
## 4                  0.097      0.50       1.16          3.4      27
## 5                  0.059      0.76       0.78          5.4      94
## 6                  0.076      0.33       0.89          2.2      27
##   smoothness_se compactness_se concavity_se concave.points_se symmetry_se
## 1        0.0064          0.049        0.054             0.016       0.030
## 2        0.0052          0.013        0.019             0.013       0.014
## 3        0.0062          0.040        0.038             0.021       0.022
## 4        0.0091          0.075        0.057             0.019       0.060
## 5        0.0115          0.025        0.057             0.019       0.018
## 6        0.0075          0.033        0.037             0.011       0.022
##   fractal_dimension_se radius_worst texture_worst perimeter_worst area_worst
## 1               0.0062           25            17             185       2019
## 2               0.0035           25            23             159       1956
## 3               0.0046           24            26             152       1709
## 4               0.0092           15            26              99        568
## 5               0.0051           23            17             152       1575
## 6               0.0051           15            24             103        742
##   smoothness_worst compactness_worst concavity_worst concave.points_worst
## 1             0.16              0.67            0.71                 0.27
## 2             0.12              0.19            0.24                 0.19
## 3             0.14              0.42            0.45                 0.24
## 4             0.21              0.87            0.69                 0.26
## 5             0.14              0.20            0.40                 0.16
## 6             0.18              0.52            0.54                 0.17
##   symmetry_worst fractal_dimension_worst
## 1           0.46                   0.119
## 2           0.28                   0.089
## 3           0.36                   0.088
## 4           0.66                   0.173
## 5           0.24                   0.077
## 6           0.40                   0.124
str(d.hex)
## Class 'H2OFrame' <environment: 0x000000003d73a7f8> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 569
##  - attr(*, "ncol")= int 31
##  - attr(*, "types")=List of 31
##   ..$ : chr "enum"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##  - attr(*, "data")='data.frame': 10 obs. of  31 variables:
##   ..$ diagnosis              : Factor w/ 2 levels "B","M": 2 2 2 2 2 2 2 2 2 2
##   ..$ radius_mean            : num  18 20.6 19.7 11.4 20.3 ...
##   ..$ texture_mean           : num  10.4 17.8 21.2 20.4 14.3 ...
##   ..$ perimeter_mean         : num  122.8 132.9 130 77.6 135.1 ...
##   ..$ area_mean              : num  1001 1326 1203 386 1297 ...
##   ..$ smoothness_mean        : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...
##   ..$ compactness_mean       : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...
##   ..$ concavity_mean         : num  0.3001 0.0869 0.1974 0.2414 0.198 ...
##   ..$ concave.points_mean    : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...
##   ..$ symmetry_mean          : num  0.242 0.181 0.207 0.26 0.181 ...
##   ..$ fractal_dimension_mean : num  0.0787 0.0567 0.06 0.0974 0.0588 ...
##   ..$ radius_se              : num  1.095 0.543 0.746 0.496 0.757 ...
##   ..$ texture_se             : num  0.905 0.734 0.787 1.156 0.781 ...
##   ..$ perimeter_se           : num  8.59 3.4 4.58 3.44 5.44 ...
##   ..$ area_se                : num  153.4 74.1 94 27.2 94.4 ...
##   ..$ smoothness_se          : num  0.0064 0.00522 0.00615 0.00911 0.01149 ...
##   ..$ compactness_se         : num  0.049 0.0131 0.0401 0.0746 0.0246 ...
##   ..$ concavity_se           : num  0.0537 0.0186 0.0383 0.0566 0.0569 ...
##   ..$ concave.points_se      : num  0.0159 0.0134 0.0206 0.0187 0.0188 ...
##   ..$ symmetry_se            : num  0.03 0.0139 0.0225 0.0596 0.0176 ...
##   ..$ fractal_dimension_se   : num  0.00619 0.00353 0.00457 0.00921 0.00511 ...
##   ..$ radius_worst           : num  25.4 25 23.6 14.9 22.5 ...
##   ..$ texture_worst          : num  17.3 23.4 25.5 26.5 16.7 ...
##   ..$ perimeter_worst        : num  184.6 158.8 152.5 98.9 152.2 ...
##   ..$ area_worst             : num  2019 1956 1709 568 1575 ...
##   ..$ smoothness_worst       : num  0.162 0.124 0.144 0.21 0.137 ...
##   ..$ compactness_worst      : num  0.666 0.187 0.424 0.866 0.205 ...
##   ..$ concavity_worst        : num  0.712 0.242 0.45 0.687 0.4 ...
##   ..$ concave.points_worst   : num  0.265 0.186 0.243 0.258 0.163 ...
##   ..$ symmetry_worst         : num  0.46 0.275 0.361 0.664 0.236 ...
##   ..$ fractal_dimension_worst: num  0.1189 0.089 0.0876 0.173 0.0768 ...
NN_model = h2o.deeplearning(
  x = 2:31,
  training_frame = d.hex,
  hidden = c(100, 50, 2, 50, 100 ),
  epochs = 100,
  activation = "Tanh",
  autoencoder = TRUE
)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |======================================================================| 100%
train_supervised_features2 = h2o.deepfeatures(NN_model, d.hex, layer=3)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
plotdata2 = as.data.frame(train_supervised_features2)
plotdata2$label = as.character(as.vector(d.hex[,1]))

qplot(DF.L3.C1, DF.L3.C2, data = plotdata2, color = label, main = "Neural network: 100 - 50 - 2 - 50 - 100 ")

More Autoencoders : Credit Card Fraud Detection using H2o

library(tidyverse)
library(ggplot2)

creditcard = read.csv("creditcard.csv")

head(creditcard) #v1-v28 are numerical perdictors
##   Time    V1     V2   V3    V4    V5     V6     V7     V8    V9    V10   V11
## 1    0 -1.36 -0.073 2.54  1.38 -0.34  0.462  0.240  0.099  0.36  0.091 -0.55
## 2    0  1.19  0.266 0.17  0.45  0.06 -0.082 -0.079  0.085 -0.26 -0.167  1.61
## 3    1 -1.36 -1.340 1.77  0.38 -0.50  1.800  0.791  0.248 -1.51  0.208  0.62
## 4    1 -0.97 -0.185 1.79 -0.86 -0.01  1.247  0.238  0.377 -1.39 -0.055 -0.23
## 5    2 -1.16  0.878 1.55  0.40 -0.41  0.096  0.593 -0.271  0.82  0.753 -0.82
## 6    2 -0.43  0.961 1.14 -0.17  0.42 -0.030  0.476  0.260 -0.57 -0.371  1.34
##      V12   V13   V14   V15   V16    V17    V18    V19    V20     V21     V22
## 1 -0.618 -0.99 -0.31  1.47 -0.47  0.208  0.026  0.404  0.251 -0.0183  0.2778
## 2  1.065  0.49 -0.14  0.64  0.46 -0.115 -0.183 -0.146 -0.069 -0.2258 -0.6387
## 3  0.066  0.72 -0.17  2.35 -2.89  1.110 -0.121 -2.262  0.525  0.2480  0.7717
## 4  0.178  0.51 -0.29 -0.63 -1.06 -0.684  1.966 -1.233 -0.208 -0.1083  0.0053
## 5  0.538  1.35 -1.12  0.18 -0.45 -0.237 -0.038  0.803  0.409 -0.0094  0.7983
## 6  0.360 -0.36 -0.14  0.52  0.40 -0.058  0.069 -0.033  0.085 -0.2083 -0.5598
##      V23    V24   V25   V26    V27    V28 Amount Class
## 1 -0.110  0.067  0.13 -0.19  0.134 -0.021  149.6     0
## 2  0.101 -0.340  0.17  0.13 -0.009  0.015    2.7     0
## 3  0.909 -0.689 -0.33 -0.14 -0.055 -0.060  378.7     0
## 4 -0.190 -1.176  0.65 -0.22  0.063  0.061  123.5     0
## 5 -0.137  0.141 -0.21  0.50  0.219  0.215   70.0     0
## 6 -0.026 -0.371 -0.23  0.11  0.254  0.081    3.7     0
## Class is a response variable (0: no fraud ; 1: fraud)

library(h2o)
h2o.init()
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         16 minutes 15 seconds 
##     H2O cluster timezone:       Asia/Kolkata 
##     H2O data parsing timezone:  UTC 
##     H2O cluster version:        3.30.0.1 
##     H2O cluster version age:    1 month and 21 days  
##     H2O cluster name:           H2O_started_from_R_HP_fjx183 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   1.46 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     H2O API Extensions:         Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4 
##     R Version:                  R version 3.5.0 (2018-04-23)
d.hex = as.h2o(creditcard, destination_frame= "d.hex")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
##hex file is h2o compatible

head(d.hex)
##   Time    V1     V2   V3    V4    V5     V6     V7     V8    V9    V10   V11
## 1    0 -1.36 -0.073 2.54  1.38 -0.34  0.462  0.240  0.099  0.36  0.091 -0.55
## 2    0  1.19  0.266 0.17  0.45  0.06 -0.082 -0.079  0.085 -0.26 -0.167  1.61
## 3    1 -1.36 -1.340 1.77  0.38 -0.50  1.800  0.791  0.248 -1.51  0.208  0.62
## 4    1 -0.97 -0.185 1.79 -0.86 -0.01  1.247  0.238  0.377 -1.39 -0.055 -0.23
## 5    2 -1.16  0.878 1.55  0.40 -0.41  0.096  0.593 -0.271  0.82  0.753 -0.82
## 6    2 -0.43  0.961 1.14 -0.17  0.42 -0.030  0.476  0.260 -0.57 -0.371  1.34
##      V12   V13   V14   V15   V16    V17    V18    V19    V20     V21     V22
## 1 -0.618 -0.99 -0.31  1.47 -0.47  0.208  0.026  0.404  0.251 -0.0183  0.2778
## 2  1.065  0.49 -0.14  0.64  0.46 -0.115 -0.183 -0.146 -0.069 -0.2258 -0.6387
## 3  0.066  0.72 -0.17  2.35 -2.89  1.110 -0.121 -2.262  0.525  0.2480  0.7717
## 4  0.178  0.51 -0.29 -0.63 -1.06 -0.684  1.966 -1.233 -0.208 -0.1083  0.0053
## 5  0.538  1.35 -1.12  0.18 -0.45 -0.237 -0.038  0.803  0.409 -0.0094  0.7983
## 6  0.360 -0.36 -0.14  0.52  0.40 -0.058  0.069 -0.033  0.085 -0.2083 -0.5598
##      V23    V24   V25   V26    V27    V28 Amount Class
## 1 -0.110  0.067  0.13 -0.19  0.134 -0.021  149.6     0
## 2  0.101 -0.340  0.17  0.13 -0.009  0.015    2.7     0
## 3  0.909 -0.689 -0.33 -0.14 -0.055 -0.060  378.7     0
## 4 -0.190 -1.176  0.65 -0.22  0.063  0.061  123.5     0
## 5 -0.137  0.141 -0.21  0.50  0.219  0.215   70.0     0
## 6 -0.026 -0.371 -0.23  0.11  0.254  0.081    3.7     0
str(d.hex)
## Class 'H2OFrame' <environment: 0x000000002771f740> 
##  - attr(*, "op")= chr "Parse"
##  - attr(*, "id")= chr "d.hex"
##  - attr(*, "eval")= logi FALSE
##  - attr(*, "nrow")= int 284807
##  - attr(*, "ncol")= int 31
##  - attr(*, "types")=List of 31
##   ..$ : chr "int"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "real"
##   ..$ : chr "int"
##  - attr(*, "data")='data.frame': 10 obs. of  31 variables:
##   ..$ Time  : num  0 0 1 1 2 2 4 7 7 9
##   ..$ V1    : num  -1.36 1.192 -1.358 -0.966 -1.158 ...
##   ..$ V2    : num  -0.0728 0.2662 -1.3402 -0.1852 0.8777 ...
##   ..$ V3    : num  2.536 0.166 1.773 1.793 1.549 ...
##   ..$ V4    : num  1.378 0.448 0.38 -0.863 0.403 ...
##   ..$ V5    : num  -0.3383 0.06 -0.5032 -0.0103 -0.4072 ...
##   ..$ V6    : num  0.4624 -0.0824 1.8005 1.2472 0.0959 ...
##   ..$ V7    : num  0.2396 -0.0788 0.7915 0.2376 0.5929 ...
##   ..$ V8    : num  0.0987 0.0851 0.2477 0.3774 -0.2705 ...
##   ..$ V9    : num  0.364 -0.255 -1.515 -1.387 0.818 ...
##   ..$ V10   : num  0.0908 -0.167 0.2076 -0.055 0.7531 ...
##   ..$ V11   : num  -0.552 1.613 0.625 -0.226 -0.823 ...
##   ..$ V12   : num  -0.6178 1.0652 0.0661 0.1782 0.5382 ...
##   ..$ V13   : num  -0.991 0.489 0.717 0.508 1.346 ...
##   ..$ V14   : num  -0.311 -0.144 -0.166 -0.288 -1.12 ...
##   ..$ V15   : num  1.468 0.636 2.346 -0.631 0.175 ...
##   ..$ V16   : num  -0.47 0.464 -2.89 -1.06 -0.451 ...
##   ..$ V17   : num  0.208 -0.115 1.11 -0.684 -0.237 ...
##   ..$ V18   : num  0.0258 -0.1834 -0.1214 1.9658 -0.0382 ...
##   ..$ V19   : num  0.404 -0.146 -2.262 -1.233 0.803 ...
##   ..$ V20   : num  0.2514 -0.0691 0.525 -0.208 0.4085 ...
##   ..$ V21   : num  -0.01831 -0.22578 0.248 -0.1083 -0.00943 ...
##   ..$ V22   : num  0.27784 -0.63867 0.77168 0.00527 0.79828 ...
##   ..$ V23   : num  -0.11 0.101 0.909 -0.19 -0.137 ...
##   ..$ V24   : num  0.0669 -0.3398 -0.6893 -1.1756 0.1413 ...
##   ..$ V25   : num  0.129 0.167 -0.328 0.647 -0.206 ...
##   ..$ V26   : num  -0.189 0.126 -0.139 -0.222 0.502 ...
##   ..$ V27   : num  0.13356 -0.00898 -0.05535 0.06272 0.21942 ...
##   ..$ V28   : num  -0.0211 0.0147 -0.0598 0.0615 0.2152 ...
##   ..$ Amount: num  149.62 2.69 378.66 123.5 69.99 ...
##   ..$ Class : num  0 0 0 0 0 0 0 0 0 0
summary(d.hex)
##  Time             V1                             
##  Min.   :     0   Min.   :-56.40750963130000173  
##  1st Qu.: 54084   1st Qu.: -0.95909150689599998  
##  Median : 84669   Median : -0.01729247293550000  
##  Mean   : 94814   Mean   :  0.00000000000000115  
##  3rd Qu.:139271   3rd Qu.:  1.27768119876000008  
##  Max.   :172792   Max.   :  2.45492999121000022  
##  V2                                V3                             
##  Min.   :-72.7157275628999997252   Min.   :-48.32558936240000236  
##  1st Qu.: -0.6879005823299999545   1st Qu.: -0.94720002252299995  
##  Median : -0.0244863864562000003   Median :  0.14925478558600000  
##  Mean   :  0.0000000000000003624   Mean   : -0.00000000000000189  
##  3rd Qu.:  0.7337012659710000495   3rd Qu.:  1.01487700251000001  
##  Max.   : 22.0577289904999993553   Max.   :  9.38255843281999979  
##  V4                              V5                               
##  Min.   :-5.683171198170000160   Min.   :-113.743306711000002451  
##  1st Qu.:-0.855648938571999973   1st Qu.:  -0.700582571818999966  
##  Median :-0.020983874996300000   Median :  -0.106402681467999999  
##  Mean   : 0.000000000000002082   Mean   :   0.000000000000001175  
##  3rd Qu.: 0.723447127652000033   3rd Qu.:   0.487777208883000024  
##  Max.   :16.875344033600001126   Max.   :  34.801665876699999558  
##  V6                               V7                               
##  Min.   :-26.160505935799999833   Min.   :-43.5572415712000022836  
##  1st Qu.: -0.797662407982000055   1st Qu.: -0.7149436014429999542  
##  Median : -0.300351750573000009   Median : -0.0583566593771000022  
##  Mean   :  0.000000000000001526   Mean   : -0.0000000000000006131  
##  3rd Qu.:  0.395883169799000012   3rd Qu.:  0.4340835471719999838  
##  Max.   : 73.301625545999996802   Max.   :120.5894939450000009629  
##  V8                                V9                              
##  Min.   :-73.2167184552999970037   Min.   :-13.434066318199999301  
##  1st Qu.: -0.2223837549029999971   1st Qu.: -0.661279511072999981  
##  Median : -0.0359359012623000004   Median : -0.051669231640099997  
##  Mean   :  0.0000000000000001533   Mean   : -0.000000000000002453  
##  3rd Qu.:  0.2437358791989999995   3rd Qu.:  0.586970108718000039  
##  Max.   : 20.0072083650999985593   Max.   : 15.594994607100000295  
##  V10                              V11                            
##  Min.   :-24.588262437200000932   Min.   :-4.797473464800000365  
##  1st Qu.: -0.566563353970000017   1st Qu.:-0.778357056303999983  
##  Median : -0.131562766948999987   Median :-0.038436043861400003  
##  Mean   :  0.000000000000002264   Mean   : 0.000000000000001622  
##  3rd Qu.:  0.448438015746000007   3rd Qu.: 0.735117741874000030  
##  Max.   : 23.745136120699999793   Max.   :12.018913181600000328  
##  V12                              V13                             
##  Min.   :-18.683714633299999264   Min.   :-5.7918812063200002527  
##  1st Qu.: -0.429625217559999995   1st Qu.:-0.6502130686849999508  
##  Median :  0.127549023329000005   Median :-0.0171936246040000004  
##  Mean   : -0.000000000000001194   Mean   : 0.0000000000000008399  
##  3rd Qu.:  0.605126944090999985   3rd Qu.: 0.6545821119720000025  
##  Max.   :  7.848392075639999632   Max.   : 7.1268829585900004275  
##  V14                             V15                            
##  Min.   :-19.21432549029999848   Min.   :-4.498944676769999873  
##  1st Qu.: -0.44769672722999998   1st Qu.:-0.592952284610000047  
##  Median :  0.02816073744240000   Median : 0.035751970292299998  
##  Mean   :  0.00000000000000126   Mean   : 0.000000000000004803  
##  3rd Qu.:  0.47427711057299998   3rd Qu.: 0.637702852645000040  
##  Max.   : 10.52676605179999925   Max.   : 8.877741597740000046  
##  V16                              V17                              
##  Min.   :-14.129854517500000100   Min.   :-25.1627993693000000519  
##  1st Qu.: -0.482739258251000003   1st Qu.: -0.4862938999299999954  
##  Median :  0.051825164346399999   Median : -0.0732979924922999987  
##  Mean   :  0.000000000000001404   Mean   : -0.0000000000000003992  
##  3rd Qu.:  0.492054688838000021   3rd Qu.:  0.3741142405649999780  
##  Max.   : 17.315111517599998336   Max.   :  9.2535262504699993258  
##  V18                              V19                            
##  Min.   :-9.4987459210500002627   Min.   :-7.213527430180000088  
##  1st Qu.:-0.5131401852550000475   1st Qu.:-0.465029532267999979  
##  Median :-0.0042466565293000002   Median :-0.004031573397670000  
##  Mean   : 0.0000000000000009836   Mean   : 0.000000000000001028  
##  3rd Qu.: 0.4901070570900000201   3rd Qu.: 0.456966385473000003  
##  Max.   : 5.0410691854099995979   Max.   : 5.591971427339999856  
##  V20                               V21                              
##  Min.   :-54.4977204946000028940   Min.   :-34.8303821447999979455  
##  1st Qu.: -0.2127553932360000122   1st Qu.: -0.2778778795280000269  
##  Median : -0.1188367684929999973   Median : -0.0297449943196999994  
##  Mean   :  0.0000000000000006451   Mean   :  0.0000000000000001916  
##  3rd Qu.:  0.0690004809926000051   3rd Qu.:  0.1563546695870000058  
##  Max.   : 39.4209042482000029395   Max.   : 27.2028391573000014603  
##  V22                               V23                              
##  Min.   :-10.9331436977000002742   Min.   :-44.8077352038000000789  
##  1st Qu.: -0.5580065444559999532   1st Qu.: -0.1638698133569999882  
##  Median : -0.0006644659787960000   Median : -0.0291975195698000005  
##  Mean   : -0.0000000000000002938   Mean   :  0.0000000000000002682  
##  3rd Qu.:  0.5138051449239999791   3rd Qu.:  0.1054747742169999947  
##  Max.   : 10.5030900899000005921   Max.   : 22.5284116897999986406  
##  V24                             V25                              
##  Min.   :-2.836626918699999944   Min.   :-10.2953970750000003420  
##  1st Qu.:-0.357954116132000011   1st Qu.: -0.3190050529160000092  
##  Median : 0.035368214814400001   Median :  0.0016646906501800000  
##  Mean   : 0.000000000000004468   Mean   :  0.0000000000000005109  
##  3rd Qu.: 0.436111721816999975   3rd Qu.:  0.3401494199699999776  
##  Max.   : 4.584549136899999766   Max.   :  7.5195886787099999182  
##  V26                             V27                              
##  Min.   :-2.604550552810000141   Min.   :-22.5656793208000010509  
##  1st Qu.:-0.327205179638999977   1st Qu.: -0.0818601886112999971  
##  Median :-0.057841748404500001   Median : -0.0276823111843999999  
##  Mean   : 0.000000000000001685   Mean   : -0.0000000000000003674  
##  3rd Qu.: 0.236009267487999996   3rd Qu.:  0.0806734436694000051  
##  Max.   : 3.517345611620000145   Max.   : 31.6121981060999992508  
##  V28                               Amount             Class             
##  Min.   :-15.4300839055000000855   Min.   :    0.00   Min.   :0.000000  
##  1st Qu.: -0.0553816875165000014   1st Qu.:    0.00   1st Qu.:0.000000  
##  Median : -0.0061037957920700003   Median :    0.00   Median :0.000000  
##  Mean   : -0.0000000000000001221   Mean   :   88.35   Mean   :0.001727  
##  3rd Qu.:  0.0431740959324000001   3rd Qu.:   77.07   3rd Qu.:0.000000  
##  Max.   : 33.8478078189000015641   Max.   :25691.16   Max.   :1.000000
ncol(d.hex)
## [1] 31
g.split = h2o.splitFrame(data = d.hex,ratios = 0.75)
train = g.split[[1]]#75% training data
test = g.split[[2]]

##3 hidden layers
model_unsup = h2o.deeplearning(x = 1:30,
                             training_frame = train,
                             model_id = "model_unsup",
                             autoencoder = TRUE,
                             reproducible = TRUE, #slow - turn off for real problems
                             ignore_const_cols = FALSE,
                             seed = 42,
                             hidden = c(10, 10, 10), 
                             epochs = 100,
                             activation = "Tanh")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |======================================================================| 100%

Dr. Nishant Upadhyay
Infosys-Analytics

2020-05-24