官术网_书友最值得收藏!

How to do it...

The section will demonstrate steps to build the GLM model using H2O.

  1. Now, load the occupancy train and test datasets in R:
# Load the occupancy data 
occupancy_train <-read.csv("C:/occupation_detection/datatraining.txt",stringsAsFactors = T)
occupancy_test <- read.csv("C:/occupation_detection/datatest.txt",stringsAsFactors = T)
  1. The following independent (x) and dependent (y) variables will be used to model GLM:
# Define input (x) and output (y) variables"
x = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
y = "Occupancy"
  1. Based on the requirement for H2O, convert the dependent variables into factors as follows:
# Convert the outcome variable into factor
occupancy_train$Occupancy <- as.factor(occupancy_train$Occupancy)
occupancy_test$Occupancy <- as.factor(occupancy_test$Occupancy)
  1. Then, convert the datasets to H2OParsedData objects:
occupancy_train.hex <- as.h2o(x = occupancy_train, destination_frame = "occupancy_train.hex")
occupancy_test.hex <- as.h2o(x = occupancy_test, destination_frame = "occupancy_test.hex")
  1. Once the data is loaded and converted to H2OParsedData objects, run a GLM model using the h2o.glm function. In the current setup, we intend to train for parameters such as five-fold cross validation, elastic net regularization (α = 5), and optimal regularization strength (with lamda_search = TRUE):
# Train the model
occupancy_train.glm <- h2o.glm(x = x, # Vector of predictor variable names
y = y, # Name of response/dependent variable
training_frame = occupancy_train.hex, # 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
)
  1. In addition to the preceding command, you can also define other parameters to fine-tune the model performance. The following list does not cover all the functional parameters, but covers some based on importance. The complete list of parameters can be found in the documentation of the h2o package.
    • Specify the strategy of generating cross-validation samples such as random sampling, stratified sampling, modulo sampling, and auto (select) using fold_assignment. The sampling can also be performed on a particular attribute by specifying the column name (fold_column).
    • Option to handle skewed outcomes (imbalanced data) by specifying weights to each observation using weights_column or performing over/under sampling using balance_classes.
    • Option to handle missing values by mean imputation or observation skip using missing_values_handling.
    • Option to restrict the coefficients to be non-negative using non_negative and constrain their values using beta_constraints.
    • Option to provide prior probability for y==1(logistic regression) in the case of sampled data if its mean of response does not reflect the reality (prior).
    • Specify the variables to be considered for interactions (interactions).
主站蜘蛛池模板: 固始县| 南川市| 伊宁县| 阿勒泰市| 平舆县| 交城县| 永胜县| 东辽县| 青川县| 隆子县| 齐齐哈尔市| 安宁市| 仪征市| 睢宁县| 错那县| 华容县| 海淀区| 郓城县| 台江县| 齐齐哈尔市| 漳平市| 湘潭市| 自治县| 肃北| 民县| 炎陵县| 繁昌县| 崇明县| 赞皇县| 毕节市| 普格县| 东山县| 虹口区| 合阳县| 宜黄县| 青龙| 富宁县| 淮滨县| 兴安盟| 白朗县| 烟台市|