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%
| predict | p0 | p1 |
|---|---|---|
| 0 | 0.956033 | 0.0439669 |
| 0 | 0.977438 | 0.0225621 |
| 1 | 0.58329 | 0.41671 |
| 0 | 0.753834 | 0.246166 |
| 1 | 0.269132 | 0.730868 |
| 0 | 0.952432 | 0.0475679 |
| 0 | 0.811075 | 0.188925 |
| 0 | 0.917553 | 0.0824472 |
| 0 | 0.665598 | 0.334402 |
| 0 | 0.813001 | 0.186999 |
Actual Values
test
We compute actual values to compare them with predicted values.
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome |
|---|---|---|---|---|---|---|---|---|
| 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | 0 |
| 8 | 125 | 96 | 0 | 0 | 0 | 0.232 | 54 | 1 |
| 1 | 103 | 30 | 38 | 83 | 43.3 | 0.183 | 33 | 0 |
| 1 | 115 | 70 | 30 | 96 | 34.6 | 0.529 | 32 | 1 |
| 7 | 147 | 76 | 0 | 0 | 39.4 | 0.257 | 43 | 1 |
| 3 | 88 | 58 | 11 | 54 | 24.8 | 0.267 | 22 | 0 |
| 2 | 90 | 68 | 42 | 0 | 38.2 | 0.503 | 27 | 1 |
| 1 | 101 | 50 | 15 | 36 | 24.2 | 0.526 | 26 | 0 |
| 7 | 114 | 66 | 0 | 0 | 32.8 | 0.258 | 42 | 1 |
| 0 | 109 | 88 | 30 | 0 | 32.5 | 0.855 | 38 | 1 |
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:
| 0 | 1 | Error | Rate | ||
|---|---|---|---|---|---|
| 0 | 0 | 306.0 | 90.0 | 0.2273 | (90.0/396.0) |
| 1 | 1 | 49.0 | 168.0 | 0.2258 | (49.0/217.0) |
| 2 | Total | 355.0 | 258.0 | 0.2268 | (139.0/613.0) |
Maximum Metrics: Maximum metrics at their respective thresholds
| metric | threshold | value | idx | |
|---|---|---|---|---|
| 0 | max f1 | 0.335660 | 0.707368 | 195.0 |
| 1 | max f2 | 0.140083 | 0.805215 | 306.0 |
| 2 | max f0point5 | 0.533112 | 0.715922 | 127.0 |
| 3 | max accuracy | 0.533112 | 0.786297 | 127.0 |
| 4 | max precision | 0.993830 | 1.000000 | 0.0 |
| 5 | max recall | 0.014357 | 1.000000 | 393.0 |
| 6 | max specificity | 0.993830 | 1.000000 | 0.0 |
| 7 | max absolute_mcc | 0.335660 | 0.529758 | 195.0 |
| 8 | max min_per_class_accuracy | 0.335660 | 0.772727 | 195.0 |
| 9 | max mean_per_class_accuracy | 0.335660 | 0.773460 | 195.0 |
| 10 | max tns | 0.993830 | 396.000000 | 0.0 |
| 11 | max fns | 0.993830 | 216.000000 | 0.0 |
| 12 | max fps | 0.002006 | 396.000000 | 399.0 |
| 13 | max tps | 0.014357 | 217.000000 | 393.0 |
| 14 | max tnr | 0.993830 | 1.000000 | 0.0 |
| 15 | max fnr | 0.993830 | 0.995392 | 0.0 |
| 16 | max fpr | 0.002006 | 1.000000 | 399.0 |
| 17 | max tpr | 0.014357 | 1.000000 | 393.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 | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0.011419 | 0.959349 | 2.421330 | 2.421330 | 0.857143 | 0.969492 | 0.857143 | 0.969492 | 0.027650 | 0.027650 | 142.132982 | 142.132982 | |
| 1 | 2 | 0.021207 | 0.928114 | 1.883257 | 2.172988 | 0.666667 | 0.943412 | 0.769231 | 0.957455 | 0.018433 | 0.046083 | 88.325653 | 117.298830 | |
| 2 | 3 | 0.030995 | 0.911829 | 2.354071 | 2.230172 | 0.833333 | 0.919732 | 0.789474 | 0.945542 | 0.023041 | 0.069124 | 135.407066 | 123.017220 | |
| 3 | 4 | 0.040783 | 0.896808 | 2.824885 | 2.372903 | 1.000000 | 0.904451 | 0.840000 | 0.935680 | 0.027650 | 0.096774 | 182.488479 | 137.290323 | |
| 4 | 5 | 0.050571 | 0.875446 | 2.354071 | 2.369258 | 0.833333 | 0.882663 | 0.838710 | 0.925419 | 0.023041 | 0.119816 | 135.407066 | 136.925821 | |
| 5 | 6 | 0.101142 | 0.795272 | 2.551509 | 2.460384 | 0.903226 | 0.836540 | 0.870968 | 0.880979 | 0.129032 | 0.248848 | 155.150884 | 146.038353 | |
| 6 | 7 | 0.150082 | 0.720283 | 2.259908 | 2.395011 | 0.800000 | 0.751225 | 0.847826 | 0.838668 | 0.110599 | 0.359447 | 125.990783 | 139.501102 | |
| 7 | 8 | 0.200653 | 0.643244 | 1.913632 | 2.273688 | 0.677419 | 0.686057 | 0.804878 | 0.800205 | 0.096774 | 0.456221 | 91.363163 | 127.368776 | |
| 8 | 9 | 0.300163 | 0.466522 | 1.528216 | 2.026548 | 0.540984 | 0.563476 | 0.717391 | 0.721724 | 0.152074 | 0.608295 | 52.821636 | 102.654779 | |
| 9 | 10 | 0.399674 | 0.355216 | 1.204049 | 1.821762 | 0.426230 | 0.404886 | 0.644898 | 0.642838 | 0.119816 | 0.728111 | 20.404926 | 82.176244 | |
| 10 | 11 | 0.500816 | 0.277274 | 0.911253 | 1.637881 | 0.322581 | 0.314918 | 0.579805 | 0.576613 | 0.092166 | 0.820276 | -8.874684 | 63.788108 | |
| 11 | 12 | 0.600326 | 0.210118 | 0.787263 | 1.496882 | 0.278689 | 0.241369 | 0.529891 | 0.521043 | 0.078341 | 0.898618 | -21.273703 | 49.688189 | |
| 12 | 13 | 0.699837 | 0.146909 | 0.602025 | 1.369641 | 0.213115 | 0.178318 | 0.484848 | 0.472310 | 0.059908 | 0.958525 | -39.797537 | 36.964111 | |
| 13 | 14 | 0.799347 | 0.099871 | 0.185238 | 1.222195 | 0.065574 | 0.120213 | 0.432653 | 0.428478 | 0.018433 | 0.976959 | -81.476165 | 22.219505 | |
| 14 | 15 | 0.898858 | 0.059240 | 0.092619 | 1.097142 | 0.032787 | 0.081178 | 0.388385 | 0.390029 | 0.009217 | 0.986175 | -90.738083 | 9.714219 | |
| 15 | 16 | 1.000000 | 0.001990 | 0.136688 | 1.000000 | 0.048387 | 0.033778 | 0.353997 | 0.353997 | 0.013825 | 1.000000 | -86.331203 | 0.000000 |