Tshepo Chris

Machine Learning using H2O.ai

Import Library

H2O the leading open source machine learning and artificial intelligence platform. This leading platform undoubtedly makes scripting of algorithms seemingly easy. It typically uses driverless technology that promptly removes the specific need to invariably do extensive and costly feature engineering upfront, in addition to automating model validation and tuning. You do not need extensive knowledge of programming to properly understand the platform. It is quite simple and straightforward. It graciously allows organisations to unanimously adopt AI. 

Underneath, the H2O library was imported. This library gallantly helped us create arrays and profile tables, compute graphs and make accurate predictions.

import h2o
h2o.init()


Checking whether there is an H2O instance running at http://localhost:54321 . connected.

H2O_cluster_uptime:3 hours 26 mins
H2O_cluster_timezone:Africa/Harare
H2O_data_parsing_timezone:UTC
H2O_cluster_version:3.30.0.4
H2O_cluster_version_age:17 days
H2O_cluster_name:H2O_from_python_Tshepo_j6l31n
H2O_cluster_total_nodes:1
H2O_cluster_free_memory:1.972 Gb
H2O_cluster_total_cores:8
H2O_cluster_allowed_cores:8
H2O_cluster_status:locked, healthy
H2O_connection_url:http://localhost:54321
H2O_connection_proxy:{“http”: null, “https”: null}
H2O_internal_security:False
H2O_API_Extensions:Amazon S3, Algos, AutoML, Core V3, TargetEncoder, Core V4
Python_version:3.7.4 final

Load data

df = h2o.import_file(r"C:\Users\Tshepo\Desktop\MLAgortihms\Datasets\diabetes.csv")

The h2o.import_file() function was called to load obtained csv file into a pandas data frame. See underneath table to examine the data structure. Bear in mind that time was parsed by calling the parse_dates function and then turned into an index

Parse progress: |█████████████████████████████████████████████████████████| 100%

df.col_names

Assigning x and y

Below are the features that were used as independent variables.

[‘Pregnancies’, ‘Glucose’, ‘BloodPressure’, ‘SkinThickness’, ‘Insulin’, ‘BMI’, ‘DiabetesPedigreeFunction’, ‘Age’]

Outcome was used as a dependent variable.

y = "Outcome"
x = df.col_names
x.remove("Outcome")
df["Outcome"] = df["Outcome"].asfactor()
df["Outcome"].levels()
[['0', '1']]

Split data into training, validation and test data

The data was split into training data and test data using the code underneath. The training data was used to fit the data. Meanwhile, the test data was conveniently used to validate our classification model.

train, valid, test = df.split_frame(ratios=[.8, .1], seed=1234)
print("Train shape: ", train.shape)
print("Valid shape: ", valid.shape)
print("Test shape: ", test.shape)

The shape of the training, validation and test data is as follows.

Train shape:  (613, 9)
Valid shape:  (69, 9)
Test shape:  (86, 9)

Configuring logistic regression model

The code underneath finalizes logistic regression model.

from h2o.estimators.glm import H2OGeneralizedLinearEstimator
glm = H2OGeneralizedLinearEstimator(family="binomial")
glm.train(x=x,y=y,training_frame=train,validation_frame=valid)

glm Model Build progress: |███████████████████████████████████████████████| 100%

Predictions

The predict() function was called to predict salaries. Thereafter, a data frame consisting of predicted values was created.

y_predglm = glm.predict(test)
y_predglm

glm prediction progress: |████████████████████████████████████████████████| 100%

predictp0p1
00.9560330.0439669
00.9774380.0225621
10.583290.41671
00.7538340.246166
10.2691320.730868
00.9524320.0475679
00.8110750.188925
00.9175530.0824472
00.6655980.334402
00.8130010.186999

Actual Values

test

We compute actual values to compare them with predicted values.

PregnanciesGlucoseBloodPressureSkinThicknessInsulinBMIDiabetesPedigreeFunctionAgeOutcome
18966239428.10.167210
8125960000.232541
110330388343.30.183330
111570309634.60.529321
7147760039.40.257431
38858115424.80.267220
2906842038.20.503271
110150153624.20.526260
7114660032.80.258421
01098830032.50.855381

Model evaluation

The section underneath outlines the area under the curve for the training, validation and test data.

train_modelevaluation = glm.model_performance(test_data=train)
valid_modelevaluation = glm.model_performance(test_data=valid)
test_modelevaluation = glm.model_performance(test_data=test)
print("Train AUC: ", train_modelevaluation.auc())
print("Valid AUC:", valid_modelevaluation.auc())
print("Test AUC:", test_modelevaluation.auc())

Train AUC: 0.8414036680165713

Valid AUC: 0.774074074074074

Test AUC: 0.855

The section underneath outlines the gini coefficient for the training, validation and test data.

print("Train Gini: ", train_modelevaluation.gini())
print("Valid Gini:", valid_modelevaluation.gini())
print("Test Gini:", test_modelevaluation.gini())

Train Gini: 0.6828073360331426

Valid Gini: 0.548148148148148 Test Gini: 0.71

Test Gini: 0.71

print(train_modelevaluation)

Model Evaluation

In the next section we evaluated the performance of the logistic regression model.

ModelMetricsBinomialGLM: glm
** Reported on test data. **

MSE: 0.15226071742967404
RMSE: 0.3902059935850217
LogLoss: 0.47072042475895653
Null degrees of freedom: 612
Residual degrees of freedom: 604
Null deviance: 796.7599047076832
Residual deviance: 577.1032407544807
AIC: 595.1032407544807
AUC: 0.8414036680165713
AUCPR: 0.7356845220146738
Gini: 0.6828073360331426

Confusion Matrix (Act/Pred) for max f1 @ threshold = 0.3356601829569534:

01ErrorRate
00306.090.00.2273(90.0/396.0)
1149.0168.00.2258(49.0/217.0)
2Total355.0258.00.2268(139.0/613.0)

Maximum Metrics: Maximum metrics at their respective thresholds

metric threshold value idx
0max f10.3356600.707368195.0
1max f20.1400830.805215306.0
2max f0point50.5331120.715922127.0
3max accuracy0.5331120.786297127.0
4max precision0.9938301.0000000.0
5max recall0.0143571.000000393.0
6max specificity0.9938301.0000000.0
7max absolute_mcc0.3356600.529758195.0
8max min_per_class_accuracy0.3356600.772727195.0
9max mean_per_class_accuracy0.3356600.773460195.0
10max tns0.993830396.0000000.0
11max fns0.993830216.0000000.0
12max fps0.002006396.000000399.0
13max tps0.014357217.000000393.0
14max tnr0.9938301.0000000.0
15max fnr0.9938300.9953920.0
16max fpr0.0020061.000000399.0
17max tpr0.0143571.000000393.0

Gains/Lift Table: Avg response rate: 35.40 %, avg score: 35.40 %

group cumulative_data_fraction lower_threshold lift cumulative_lift response_rate score cumulative_response_rate cumulative_score capture_rate cumulative_capture_rate gain cumulative_gain
010.0114190.9593492.4213302.4213300.8571430.9694920.8571430.9694920.0276500.027650142.132982142.132982
120.0212070.9281141.8832572.1729880.6666670.9434120.7692310.9574550.0184330.04608388.325653117.298830
230.0309950.9118292.3540712.2301720.8333330.9197320.7894740.9455420.0230410.069124135.407066123.017220
340.0407830.8968082.8248852.3729031.0000000.9044510.8400000.9356800.0276500.096774182.488479137.290323
450.0505710.8754462.3540712.3692580.8333330.8826630.8387100.9254190.0230410.119816135.407066136.925821
560.1011420.7952722.5515092.4603840.9032260.8365400.8709680.8809790.1290320.248848155.150884146.038353
670.1500820.7202832.2599082.3950110.8000000.7512250.8478260.8386680.1105990.359447125.990783139.501102
780.2006530.6432441.9136322.2736880.6774190.6860570.8048780.8002050.0967740.45622191.363163127.368776
890.3001630.4665221.5282162.0265480.5409840.5634760.7173910.7217240.1520740.60829552.821636102.654779
9100.3996740.3552161.2040491.8217620.4262300.4048860.6448980.6428380.1198160.72811120.40492682.176244
10110.5008160.2772740.9112531.6378810.3225810.3149180.5798050.5766130.0921660.820276-8.87468463.788108
11120.6003260.2101180.7872631.4968820.2786890.2413690.5298910.5210430.0783410.898618-21.27370349.688189
12130.6998370.1469090.6020251.3696410.2131150.1783180.4848480.4723100.0599080.958525-39.79753736.964111
13140.7993470.0998710.1852381.2221950.0655740.1202130.4326530.4284780.0184330.976959-81.47616522.219505
14150.8988580.0592400.0926191.0971420.0327870.0811780.3883850.3900290.0092170.986175-90.7380839.714219
15161.0000000.0019900.1366881.0000000.0483870.0337780.3539970.3539970.0138251.000000-86.3312030.000000