### Get H2O Parse Setup Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/h2o.html Use `h2o.parse_setup` to get H2O's best guess for the structure of a data file. The returned dictionary can be modified and then fed into `h2o.parse_raw` to create an H2OFrame. ```python >>> h2o.parse_setup() ``` -------------------------------- ### Initialize and Connect to Local H2O Server Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/backend.html Use this function to connect to a local H2O server. If no local server is running, it will start one automatically before connecting. This is a convenient way to get started with H2O locally. ```python h2o.init() ``` -------------------------------- ### Initiate and Start Grid Search Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/grid/grid_search.html Demonstrates how to set up and start a grid search for a Deep Learning model. It involves importing necessary classes, loading data, preprocessing, defining hyperparameters, creating a GridSearch object, and starting the search. ```python from h2o.estimators import H2ODeepLearningEstimator from h2o.grid.grid_search import H2OGridSearch insurance = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/glm_test/insurance.csv") insurance["offset"] = insurance["Holders"].log() insurance["Group"] = insurance["Group"].asfactor() insurance["Age"] = insurance["Age"].asfactor() insurance["District"] = insurance["District"].asfactor() hyper_params = {'huber_alpha': [0.2,0.5], 'quantile_alpha': [0.2,0.6]} gs = H2OGridSearch(H2ODeepLearningEstimator(epochs=5), hyper_params) gs.start(x=list(range(3)),y="Claims", training_frame=insurance) ``` -------------------------------- ### Start and connect to a local H2O cluster Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/backend.html Demonstrates starting multiple local H2O servers using `H2OLocalServer.start()` and then connecting to one of them using `h2o.connect()`. ```APIDOC ## Start and Connect to Local H2O Cluster ### Description Starts multiple local H2O servers and connects to one of them. ### Method ```python from h2o.backend import H2OLocalServer for _ in range(5): hs = H2OLocalServer.start() h2o.connect(server=hs) ``` ### Parameters * **server** (`H2OLocalServer`) - An instance of `H2OLocalServer` to connect to. ``` -------------------------------- ### H2OCoxProportionalHazardsEstimator with custom init Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/coxph.html Example of initializing and training the H2OCoxProportionalHazardsEstimator with a custom starting value for coefficients. The 'init' parameter sets the coefficient starting value. ```python >>> heart = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/coxph_test/heart.csv") >>> predictor = "age" >>> response = "event" >>> heart_coxph = H2OCoxProportionalHazardsEstimator(start_column="start", ... stop_column="stop", ... init=2.9) >>> heart_coxph.train(x=predictor, ... y=response, ... training_frame=heart) >>> heart_coxph.scoring_history() ``` -------------------------------- ### Binomial Classification Example Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/model_categories.html Example demonstrating the setup and training of a GBM model for binomial classification and finding the threshold by max metric. ```APIDOC ## Binomial Classification >>> response_col = "economy_20mpg" >>> distribution = "bernoulli" >>> predictors = ["displacement", "power", "weight", ... "acceleration", "year"] >>> from h2o.estimators.gbm import H2OGradientBoostingEstimator >>> gbm = H2OGradientBoostingEstimator(nfolds=3, ... distribution=distribution, ... fold_assignment="Random") >>> gbm.train(y=response_col, ... x=predictors, ... validation_frame=valid, ... training_frame=train) >>> max_metric = gbm.find_threshold_by_max_metric(metric="f2", ... train=True) >>> max_metric ``` -------------------------------- ### Start and Connect to Multiple Local H2O Servers Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/backend.html This snippet demonstrates how to start multiple H2O servers locally to form a cluster, and then connect to one of them. It requires importing the H2OLocalServer class. ```python from h2o.backend import H2OLocalServer for _ in range(5): hs = H2OLocalServer.start() h2o.connect(server=hs) ``` -------------------------------- ### H2OUpliftRandomForestEstimator Example Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html Example of how to initialize and train an H2OUpliftRandomForestEstimator with specified parameters and evaluate its performance. ```APIDOC ## Example Usage ```python >>> import h2o >>> from h2o.estimators import H2OUpliftRandomForestEstimator >>> h2o.init() >>> data = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/uplift/criteo_uplift_13k.csv") >>> predictors = ["f1", "f2", "f3", "f4", "f5", "f6","f7", "f8"] >>> response = "conversion" >>> data[response] = data[response].asfactor() >>> treatment_column = "treatment" >>> data[treatment_column] = data[treatment_column].asfactor() >>> train, valid = data.split_frame(ratios=[.8], seed=1234) >>> uplift_model = H2OUpliftRandomForestEstimator(ntrees=10, ... max_depth=5, ... uplift_metric="KL", ... min_rows=10, ... seed=1234, ... auuc_type="qini", ... treatment_column=treatment_column) >>> uplift_model.train(x=predictors, ... y=response, ... training_frame=train, ... validation_frame=valid) >>> uplift_model.model_performance() ``` ``` -------------------------------- ### H2OKMeansEstimator Training Example Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/kmeans.html Example demonstrating how to train an H2OKMeansEstimator model. ```APIDOC ## H2OKMeansEstimator.train ### Description Train the H2OKMeansEstimator model. ### Method POST ### Endpoint `/99/Grid` ### Parameters #### Path Parameters None #### Query Parameters - **training_frame** (H2OFrame) - Required - The H2OFrame containing the training data. - **validation_frame** (H2OFrame, optional) - The H2OFrame containing the validation data. - **x** (List[str]) - List of predictor column names. ### Request Example ```python >>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv") >>> predictors = ["AGE", "RACE", "DPROS", "DCAPS", "PSA", "VOL", "GLEASON"] >>> train, valid = prostate.split_frame(ratios=[.8], seed=1234) >>> pros_km = H2OKMeansEstimator(seed=1234) >>> pros_km.train(x=predictors, training_frame=train, validation_frame=valid) >>> pros_km.scoring_history() ``` ### Response #### Success Response (200) - **model** (object) - The trained K-Means model. #### Response Example ```json { "model": { ... } } ``` ``` -------------------------------- ### Parse Raw Data with Setup Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/h2o.html Parses a dataset using a provided parse setup structure. Use this when you have pre-configured parsing options from `h2o.parse_setup()`. ```python fraw = h2o.import_file(("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip", parse=False)) h2o.parse_raw(h2o.parse_setup(fraw), id='prostate.csv', first_line_is_header=0) ``` -------------------------------- ### Start H2O Local Server Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/backend/server.html Launches a local H2O server instance with various configuration options. Use this to start a server with specific memory, threading, logging, and network settings. ```python assert_is_type(jar_path, None, str) assert_is_type(port, None, int, str) assert_is_type(name, None, str) assert_is_type(nthreads, -1, BoundInt(1, 4096)) assert_is_type(enable_assertions, bool) assert_is_type(min_mem_size, None, int) assert_is_type(max_mem_size, None, BoundInt(1 << 25)) assert_is_type(log_dir, str, None) assert_is_type(log_level, str, None) assert_satisfies(log_level, log_level in [None, "TRACE", "DEBUG", "INFO", "WARN", "ERRR", "FATA"]) assert_is_type(max_log_file_size, str, None) assert_is_type(ice_root, None, I(str, os.path.isdir)) assert_is_type(extra_classpath, None, [str]) assert_is_type(jvm_custom_args, list, None) assert_is_type(bind_to_localhost, bool) if jar_path: assert_satisfies(jar_path, jar_path.endswith("h2o.jar")) if min_mem_size is not None and max_mem_size is not None and min_mem_size > max_mem_size: raise H2OValueError("`min_mem_size`=%d is larger than the `max_mem_size`=%d" % (min_mem_size, max_mem_size)) if port is None: port = "54321+" baseport = None # TODO: get rid of this port gimmick and have 2 separate parameters. if is_type(port, str): if port.isdigit(): port = int(port) else: if not(port[-1] == "+" and port[:-1].isdigit()): raise H2OValueError("`port` should be of the form 'DDDD+', where D is a digit. Got: %s" % port) baseport = int(port[:-1]) port = 0 hs = H2OLocalServer() hs._verbose = bool(verbose) hs._jar_path = hs._find_jar(jar_path) hs._extra_classpath = extra_classpath hs._ice_root = ice_root hs._name = name if not ice_root: hs._ice_root = tempfile.mkdtemp() hs._tempdir = hs._ice_root if verbose: print("Attempting to start a local H2O server...") hs._launch_server(port=port, baseport=baseport, nthreads=int(nthreads), ea=enable_assertions, mmax=max_mem_size, mmin=min_mem_size, jvm_custom_args=jvm_custom_args, bind_to_localhost=bind_to_localhost, log_dir=log_dir, log_level=log_level, max_log_file_size=max_log_file_size) if verbose: print(" Server is running at %s://%s:%d" % (hs.scheme, hs.ip, hs.port)) atexit.register(lambda: hs.shutdown()) return hs ``` -------------------------------- ### H2OLocalServer.start() Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/backend/server.html Launches a new H2O server instance on the local machine with configurable parameters. ```APIDOC ## start ### Description Start new H2O server on the local machine. :param jar_path: Path to the h2o.jar executable. If not given, then we will search for h2o.jar in the locations returned by ``._jar_paths()``. :param nthreads: Number of threads in the thread pool. This should be related to the number of CPUs used. -1 means use all CPUs on the host. A positive integer specifies the number of CPUs directly. :param enable_assertions: If True, pass ``-ea`` option to the JVM. :param max_mem_size: Maximum heap size (jvm option Xmx), in bytes. :param min_mem_size: Minimum heap size (jvm option Xms), in bytes. :param log_dir: Directory for H2O logs to be stored if a new instance is started. Default directory is determined by H2O internally. :param log_level: The logger level for H2O if a new instance is started. :param max_log_file_size: Maximum size of INFO and DEBUG log files. The file is rolled over after a specified size has been reached. (The default is 3MB. Minimum is 1MB and maximum is 99999MB) :param ice_root: A directory where H2O stores its temporary files. Default location is determined by ``tempfile.mkdtemp()``. :param port: Port where to start the new server. This could be either an integer, or a string of the form "DDDDD+", indicating that the server should start looking for an open port starting from DDDDD and up. :param name: name of the h2o cluster to be started. :param extra_classpath: List of paths to libraries that should be included on the Java classpath. :param verbose: If True, then connection info will be printed to the stdout. :param jvm_custom_args: Custom, user-defined arguments for the JVM H2O is instantiated in. :param bind_to_localhost: A flag indicating whether access to the H2O instance should be restricted to the local machine (default) or if it can be reached from other computers on the network. Only applicable when H2O is started from the Python client. :returns: a new H2OLocalServer instance. ``` -------------------------------- ### h2o.import_file and h2o.parse_setup Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/h2o.html Demonstrates how to import a file into an H2OFrame and set up parsing parameters, including column types and missing value strings. ```APIDOC ## h2o.import_file and h2o.parse_setup ### Description Import a file into an H2OFrame and set up parsing parameters. This includes specifying column names, types, and handling of missing values. ### Parameters #### `h2o.import_file` Parameters * **path** (string) - The path to the file to import. * **parse** (boolean) - Whether to parse the file immediately after importing. Defaults to True. #### `h2o.parse_setup` Parameters * **frame** - The H2OFrame to set up parsing for. * **destination_frame** (string) - The name for the resulting parsed frame. * **header** (integer) - Specifies if the first line of the file is a header (1) or not (0). Defaults to 0. * **separator** (string) - The character used to separate values in the file. Defaults to ",". * **column_names** (list of strings) - A list of column names to use for the frame. * **column_types** (list or dictionary) - A list of types or a dictionary of column names to types to specify whether columns should be forced to a certain type upon import parsing. Possible types include: "unknown", "uuid", "string", "numeric", "enum", "time". * **na_strings** (list or dictionary) - A list of strings, or a list of lists of strings, or a dictionary of column names to strings which are to be interpreted as missing values. * **skipped_columns** (list of integers) - An integer list of column indices to skip and not parse into the final frame. * **force_col_types** (boolean) - If True, will force the column types to be either the ones in Parquet schema for Parquet files or the ones specified in column_types. Defaults to False. * **custom_non_data_line_markers** (string) - If a line in imported file starts with any character in given string it will NOT be imported. * **partition_by** (list of strings) - A list of columns the dataset has been partitioned by. None by default. * **quotechar** (string) - A hint for the parser which character to expect as quoting character. Only single quote, double quote or None (default) are allowed. * **escapechar** (string) - (Optional) One ASCII character used to escape other characters. * **tz_adjust_to_local** (boolean) - (Optional) Adjust the imported data timezone from GMT to cluster timezone. ### Returns a dictionary containing parse parameters guessed by the H2O backend. ### Request Example ```python col_headers = ["ID","CAPSULE","AGE","RACE", "DPROS","DCAPS","PSA","VOL","GLEASON"] col_types=['enum','enum','numeric','enum', 'enum','enum','numeric','numeric','numeric'] hex_key = "training_data.hex" fraw = h2o.import_file(("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip"), parse=False) setup = h2o.parse_setup(fraw, destination_frame=hex_key, header=1, separator=',', column_names=col_headers, column_types=col_types, na_strings=["NA"]) print(setup) ``` ``` -------------------------------- ### Train XGBoost Model with Col Sample Rate Per Tree Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/xgboost.html This example demonstrates training an H2O XGBoost model using the `col_sample_rate_per_tree` parameter. The setup and training process are consistent with other column sampling examples. ```python airlines= h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/allyears2k_headers.zip") airlines["Year"] = airlines["Year"].asfactor() airlines["Month"] = airlines["Month"].asfactor() airlines["DayOfWeek"] = airlines["DayOfWeek"].asfactor() airlines["Cancelled"] = airlines["Cancelled"].asfactor() airlines['FlightNum'] = airlines['FlightNum'].asfactor() predictors = ["Origin", "Dest", "Year", "UniqueCarrier", "DayOfWeek", "Month", "Distance", "FlightNum"] response = "IsDepDelayed" train, valid= airlines.split_frame(ratios=[.8], seed=1234) airlines_xgb = H2OXGBoostEstimator(col_sample_rate_per_tree=.7, seed=1234) airlines_xgb.train(x=predictors, y=response, training_frame=train, validation_frame=valid) print(airlines_xgb.auc(valid=True)) ``` -------------------------------- ### h2o.backend.H2OLocalServer.start Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/genindex.html Starts a local H2O cluster. ```APIDOC ## H2OLocalServer.start ### Description Starts a local H2O cluster on the machine. ### Method Static Method ### Endpoint N/A (Python method) ### Parameters - nthreads (int, optional) - Number of threads to use for the cluster. - max_mem_size (str, optional) - Maximum memory size for the cluster (e.g., '2G'). ### Request Example ```python H2OLocalServer.start(nthreads=-1, max_mem_size='4G') ``` ### Response #### Success Response (N/A) - H2OLocalServer object - An object representing the running local H2O cluster. #### Response Example ```json ``` ``` -------------------------------- ### Get Variable Importances as Pandas DataFrame Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/model/model_base.html Retrieves variable importances and returns them as a pandas DataFrame. Requires pandas to be installed. ```python >>> var_imp_df = model.varimp(use_pandas=True) ``` -------------------------------- ### H2OAssembly Initialization and Usage Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/assembly.html Demonstrates how to initialize an H2OAssembly with a list of transformation steps and apply it to an H2OFrame. ```APIDOC ## H2OAssembly ### Description The H2OAssembly class can be used to specify multiple frame operations in one place. ### Parameters - **steps** (list): A list of tuples, where each tuple contains a name for the step and an H2OTransformer object. ### Returns - An H2OFrame with the transformations applied. ### Example ```python >>> iris = h2o.load_dataset("iris") >>> from h2o.assembly import * >>> from h2o.transforms.preprocessing import * >>> assembly = H2OAssembly(steps=[("col_select", H2OColSelect(["Sepal.Length", "Petal.Length", "Species"])), ... ("cos_Sepal.Length", H2OColOp(op=H2OFrame.cos, col="Sepal.Length", inplace=True)), ... ("str_cnt_Species", H2OColOp(op=H2OFrame.countmatches, col="Species", inplace=False, pattern="s"))]) >>> result = assembly.fit(iris) >>> print(result) ``` ``` -------------------------------- ### Access the Root Node of a Tree Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/tree.html Get the root node of the tree, which serves as the starting point for tree traversal. This property returns an H2ONode instance. ```python from h2o.tree import H2OTree from h2o.estimators import H2OGradientBoostingEstimator airlines = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/AirlinesTrain.csv") gbm = H2OGradientBoostingEstimator(ntrees=1) gbm.train(x=["Origin", "Dest"], y="IsDepDelayed", training_frame=airlines) tree = H2OTree(model = gbm, tree_number = 0 , tree_class = "NO") tree.root_node ``` -------------------------------- ### Export Checkpoints Directory Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/stackedensemble.html Configures the stacked ensemble estimator to automatically export generated models to a specified directory. This example also shows the setup for training base models. ```python >>> from h2o.estimators.random_forest import H2ORandomForestEstimator >>> from h2o.estimators.gbm import H2OGradientBoostingEstimator >>> from h2o.estimators.stackedensemble import H2OStackedEnsembleEstimator >>> import tempfile >>> from os import listdir >>> higgs = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/testng/higgs_train_5k.csv") >>> train, blend = higgs.split_frame(ratios = [.8], seed = 1234) >>> x = train.columns >>> y = "response" >>> x.remove(y) >>> train[y] = train[y].asfactor() >>> blend[y] = blend[y].asfactor() >>> nfolds = 3 >>> checkpoints_dir = tempfile.mkdtemp() >>> my_gbm = H2OGradientBoostingEstimator(distribution="bernoulli", ... ntrees=10, ... nfolds=nfolds, ... fold_assignment="Modulo", ... keep_cross_validation_predictions=True, ... seed=1) >>> my_gbm.train(x=x, y=y, training_frame=train) >>> my_rf = H2ORandomForestEstimator(ntrees=50, ... nfolds=nfolds, ... fold_assignment="Modulo", ``` -------------------------------- ### Initialize Deep Learning Model with Custom Weights and Biases Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/deeplearning.html This example demonstrates how to initialize a Deep Learning model with custom initial weights and biases. It sets up the model with specific hidden layer sizes and then trains it on a dataset. The initial biases can be accessed after training. ```python >>> w3 = dl1.weights(2) >>> b1 = dl1.biases(0) >>> b2 = dl1.biases(1) >>> b3 = dl1.biases(2) >>> dl2 = H2ODeepLearningEstimator(hidden=[10,10], ... initial_weights=[w1, w2, w3], ... initial_biases=[b1, b2, b3], ... epochs=0) >>> dl2.train(x=list(range(4)), y=4, training_frame=iris) >>> dl2.initial_biases ``` -------------------------------- ### Import Data and Setup Model Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/model_categories.html Imports the wine dataset and sets the response column for model training. Requires h2o and H2OGradientBoostingEstimator to be imported. ```python >>> import h2o >>> from h2o.estimators import H2OGradientBoostingEstimator >>> h2o.init() >>> >>> # Import the wine dataset into H2O: >>> f = "https://h2o-public-test-data.s3.amazonaws.com/smalldata/wine/winequality-redwhite-no-BOM.csv" >>> df = h2o.import_file(f) >>> >>> # Set the response >>> response = "quality" >>> ``` -------------------------------- ### Parse Raw Data with H2O Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/h2o.html Use `h2o.parse_raw` to parse a dataset after obtaining its setup using `h2o.parse_setup`. The example demonstrates parsing a CSV file and then generating a summary of the resulting H2OFrame. ```python >>> fraw = h2o.import_file(("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip"), ... parse=False) >>> fhex = h2o.parse_raw(h2o.parse_setup(fraw), ... id='prostate.csv', ... first_line_is_header=0) >>> fhex.summary() ``` -------------------------------- ### Train H2O Deep Learning Model Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/deeplearning.html Train a Deep Learning model using specified predictors, response, and training/validation frames. This example demonstrates basic model training setup. ```python >>> train, valid = cars.split_frame(ratios=[.8], seed=1234) >>> cars_dl = H2ODeepLearningEstimator(epsilon=1e-6, ... seed=1234) >>> cars_dl.train(x=predictors, ... y=response, ... training_frame=train, ... validation_frame=valid) >>> cars_dl.mse() ``` -------------------------------- ### H2OGridSearch.start() Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html Starts an asynchronous model build for grid search. ```APIDOC ## H2OGridSearch.start() ### Description Asynchronous model build by specifying the predictor columns, response column, and any additional frame-specific values. To block for results, call `join()`. ### Parameters * **x** (list) - A list of column names or indices indicating the predictor columns. * **y** (str or int) - An index or a column name indicating the response column. * **training_frame** (H2OFrame) - The H2OFrame having the columns indicated by x and y (as well as any additional columns specified by fold, offset, and weights). * **offset_column** (str or int) - The name or index of the column in training_frame that holds the offsets. * **fold_column** (str or int) - The name or index of the column in training_frame that holds the per-row fold assignments. * **weights_column** (str or int) - The name or index of the column in training_frame that holds the per-row weights. * **validation_frame** (H2OFrame) - H2OFrame with validation data to be scored on while training. * **params** (dict) - Additional parameters for the model. ### Example ```python from h2o.estimators import H2ODeepLearningEstimator from h2o.grid.grid_search import H2OGridSearch insurance = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/glm_test/insurance.csv") insurance["offset"] = insurance["Holders"].log() insurance["Group"] = insurance["Group"].asfactor() insurance["Age"] = insurance["Age"].asfactor() insurance["District"] = insurance["District"].asfactor() hyper_params = {'huber_alpha': [0.2,0.5], 'quantile_alpha': [0.2,0.6]} gs = H2OGridSearch(H2ODeepLearningEstimator(epochs=5), hyper_params) gs.start(x=list(range(3)),y="Claims", training_frame=insurance) gs.join() ``` ``` -------------------------------- ### Train Cox Proportional Hazards Model Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/metrics.html Example of training a Cox Proportional Hazards model using H2O. This snippet demonstrates setting up the estimator with start and stop columns and training on a dataset. ```python >>> from h2o.estimators.coxph import H2OCoxProportionalHazardsEstimator >>> heart = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/coxph_test/heart.csv") >>> coxph = H2OCoxProportionalHazardsEstimator(start_column="start", ... stop_column="stop", ... ties="breslow") >>> coxph.train(x="age", y="event", training_frame=heart) >>> coxph ``` -------------------------------- ### h2o.parse_setup Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/h2o.html Retrieves H2O's best guess as to the structure of a data file for parsing setup. ```APIDOC ## h2o.parse_setup ### Description Retrieves H2O's best guess as to the structure of a data file for parsing setup. ### Method POST ### Endpoint /99/ParseSetup ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **raw_frames** (list) - Required - A list of raw frames to set up for parsing. - **destination_frame** (str) - Optional - The name for the destination H2OFrame. - **header** (int) - Optional - Specifies if the first line is a header (0: no header, 1: header). Defaults to 0. - **separator** (str) - Optional - The character used to separate values in the file. - **column_names** (list[str]) - Optional - A list of names for the columns. - **column_types** (dict) - Optional - A dictionary specifying the data type for each column. - **na_strings** (list[str]) - Optional - A list of strings to be interpreted as NA values. - **skipped_columns** (list[int]) - Optional - A list of column indices to skip during parsing. - **force_col_types** (bool) - Optional - If True, forces the specified column types, potentially overriding H2O's inference. - **custom_non_data_line_markers** (list[str]) - Optional - Markers for lines that should not be considered data. - **partition_by** (list[str]) - Optional - Columns to partition the data by. - **quotechar** (str) - Optional - The character used for quoting values. - **escapechar** (str) - Optional - The character used for escaping special characters. - **tz_adjust_to_local** (bool) - Optional - If True, adjusts timezone to local time. ### Request Example ```json { "raw_frames": ["file.csv"], "destination_frame": "my_parsed_data", "separator": ",", "header": 1 } ``` ### Response #### Success Response (200) - **destination_frame** (str) - The name of the destination H2OFrame. - **column_names** (list[str]) - The inferred or provided column names. - **column_types** (dict) - The inferred or provided column types. #### Response Example ```json { "destination_frame": "my_parsed_data", "column_names": ["col1", "col2"], "column_types": {"col1": "numeric", "col2": "string"} } ``` ``` -------------------------------- ### Initialize and Train H2OGridSearch Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/grid/grid_search.html Demonstrates how to initialize H2OGridSearch with a model and hyper-parameters, and then train it on data. Requires importing necessary modules and data. ```python from h2o.grid.grid_search import H2OGridSearch from h2o.estimators.glm import H2OGeneralizedLinearEstimator hyper_parameters = {'alpha': [0.01,0.5], 'lambda': [1e-5,1e-6]} gs = H2OGridSearch(H2OGeneralizedLinearEstimator(family='binomial'), hyper_parameters) training_data = h2o.import_file("smalldata/logreg/benign.csv") gs.train(x=[3, 4-11], y=3, training_frame=training_data) gs.show() ``` -------------------------------- ### Specify GPU ID for H2OXGBoostEstimator Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html Assigns the model training to a specific GPU. This example trains the model using GPU ID 0. Ensure your environment has GPU support and the correct drivers installed. ```python boston = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/BostonHousing.csv") predictors = boston.columns[:-1] response = "medv" boston['chas'] = boston['chas'].asfactor() train, valid = boston.split_frame(ratios=[.8]) boston_xgb = H2OXGBoostEstimator(gpu_id=0, seed=1234) boston_xgb.train(x=predictors, y=response, training_frame=train, validation_frame=valid) boston_xgb.mse() ``` -------------------------------- ### H2OGridSearch Fallout Example Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/grid/metrics.html Get the Fallout (False Positive Rate) for each model in a grid search. You can specify thresholds or use the default maximizing threshold, and select data from training, validation, or cross-validation sets. ```python from h2o.grid.grid_search import H2OGridSearch from h2o.estimators.glm import H2OGeneralizedLinearEstimator training_data = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/logreg/benign.csv") hyper_parameters = {'alpha': [0.01,0.5], 'lambda': [1e-5,1e-6]} gs = H2OGridSearch(H2OGeneralizedLinearEstimator(family='binomial'), hyper_parameters) gs.train(x=[3, 4-11], y=3, training_frame=training_data) gs.fallout(train=True) ``` -------------------------------- ### Train and Plot H2OInfogram Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/infogram.html This example demonstrates how to initialize H2O, import data, split it, and then train an H2OInfogram model. It also shows how to access the `total_information_threshold` and plot the resulting infogram. Ensure H2O is initialized before running. ```python import h2o from h2o.estimators.infogram import H2OInfogram h2o.init() f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/admissibleml_test/taiwan_credit_card_uci.csv" col_types = {'SEX': "enum", 'MARRIAGE': "enum", 'default payment next month': "enum"} df = h2o.import_file(path=f, col_types=col_types) train = df.split_frame(seed=1)[0] y = "default payment next month" x = train.columns x.remove(y) pcols = ["SEX", "MARRIAGE", "AGE"] ig = H2OInfogram(protected_columns=pcols, total_information_threshold=0.5) ig.train(y=y, x=x, training_frame=train) print(ig.total_information_threshold) ig.plot() ``` -------------------------------- ### Get the number of observations from model performance Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/model_categories.html This example shows how to train a GBM model and then retrieve the number of observations used for its performance evaluation. It includes data loading, preprocessing, model training, obtaining model performance object, and calling the nobs() method. ```python >>> from h2o.estimators.gbm import H2OGradientBoostingEstimator >>> cars = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/junit/cars_20mpg.csv") >>> cars["economy_20mpg"] = cars["economy_20mpg"].asfactor() >>> predictors = ["displacement","power","weight","acceleration","year"] >>> response = "economy_20mpg" >>> train, valid = cars.split_frame(ratios = [.8], seed = 1234) >>> cars_gbm = H2OGradientBoostingEstimator(seed = 1234) >>> cars_gbm.train(x = predictors, ... y = response, ... training_frame = train, ... validation_frame = valid) >>> perf = cars_gbm.model_performance() >>> perf.nobs() ``` -------------------------------- ### H2O Local Server Initialization Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/backend/server.html Internal constructor for H2O local server. Use `H2OLocalServer.start()` for launching new servers. ```python def __init__(self): """[Internal] please use H2OLocalServer.start() to launch a new server.""" self._scheme = None # "http" or "https" self._ip = None self._port = None self._name = None self._process = None self._verbose = None self._jar_path = None self._extra_classpath = None self._ice_root = None self._stdout = None self._stderr = None self._tempdir = None ``` -------------------------------- ### Train H2O Cox Proportional Hazards Model Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/estimators/coxph.html This example demonstrates how to train a Cox Proportional Hazards model using H2O. It involves importing data, splitting it into training and validation sets, initializing the estimator with start and stop time columns, and then training the model with specified predictor and response columns. ```python heart = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/coxph_test/heart.csv") predictor = "age" response = "event" train, valid = heart.split_frame(ratios=[.8]) heart_coxph = H2OCoxProportionalHazardsEstimator(start_column="start", stop_column="stop") heart_coxph.train(x=predictor, y=response, training_frame=train, validation_frame=valid) heart_coxph.scoring_history() ``` -------------------------------- ### Initialize H2OInfogram with Data Fraction Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html This example shows how to initialize an H2OInfogram model with a specified data fraction for training. This can be useful for controlling the training process or for faster experimentation. ```python import h2o from h2o.estimators.infogram import H2OInfogram h2o.init() f = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/admissibleml_test/taiwan_credit_card_uci.csv" col_types = {'SEX': "enum", 'MARRIAGE": "enum", 'default payment next month': "enum"} df = h2o.import_file(path=f, col_types=col_types) train = df.split_frame(seed=1)[0] y = "default payment next month" x = train.columns x.remove(y) pcols = ["SEX", "MARRIAGE", "AGE"] ig = H2OInfogram(protected_columns=pcols, data_fraction=0.7) ``` -------------------------------- ### H2ONaiveBayesEstimator with eps_sdev parameter Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html This snippet shows the setup for training a Naive Bayes model on the Cars dataset, similar to the previous example, but focuses on the `eps_sdev` parameter. The code dynamically selects a response column and prepares the data. The actual training and evaluation are not shown in this specific snippet but are implied by the context of the `eps_sdev` property. ```python >>> cars = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/junit/cars_20mpg.csv") >>> problem = random.sample(["binomial","multinomial"],1) >>> predictors = ["displacement","power","weight","acceleration","year"] >>> if problem == "binomial": ... response_col = "economy_20mpg" ... else: ... response_col = "cylinders" >>> cars[response_col] = cars[response_col].asfactor() ``` -------------------------------- ### H2OPrincipalComponentAnalysisEstimator Initialization and Training Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html Demonstrates how to initialize the H2OPrincipalComponentAnalysisEstimator and train it on a dataset. It shows examples of setting parameters like `compute_metrics`, `ignore_const_cols`, and `impute_missing`. ```APIDOC ## H2OPrincipalComponentAnalysisEstimator ### Description Initializes and trains the Principal Component Analysis model. ### Parameters - **compute_metrics** (bool) - Optional - Whether to compute metrics on the training data. - **ignore_const_cols** (bool) - Optional - Ignore constant columns. - **impute_missing** (bool) - Optional - Whether to impute missing entries with the column mean. ### Request Example ```python >>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip") >>> prostate['CAPSULE'] = prostate['CAPSULE'].asfactor() >>> prostate['RACE'] = prostate['RACE'].asfactor() >>> prostate['DCAPS'] = prostate['DCAPS'].asfactor() >>> prostate['DPROS'] = prostate['DPROS'].asfactor() >>> pros_pca = H2OPrincipalComponentAnalysisEstimator(compute_metrics=False) >>> pros_pca.train(x=prostate.names, training_frame=prostate) >>> pros_pca.show() ``` ### Request Example 2 ```python >>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip") >>> prostate['CAPSULE'] = prostate['CAPSULE'].asfactor() >>> prostate['RACE'] = prostate['RACE'].asfactor() >>> prostate['DCAPS'] = prostate['DCAPS'].asfactor() >>> prostate['DPROS'] = prostate['DPROS'].asfactor() >>> pros_pca = H2OPrincipalComponentAnalysisEstimator(ignore_const_cols=False) >>> pros_pca.train(x=prostate.names, training_frame=prostate) >>> pros_pca.show() ``` ### Request Example 3 ```python >>> prostate = h2o.import_file("http://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv.zip") >>> prostate['CAPSULE'] = prostate['CAPSULE'].asfactor() >>> prostate['RACE'] = prostate['RACE'].asfactor() >>> prostate['DCAPS'] = prostate['DCAPS'].asfactor() >>> prostate['DPROS'] = prostate['DPROS'].asfactor() >>> pros_pca = H2OPrincipalComponentAnalysisEstimator(impute_missing=True) >>> pros_pca.train(x=prostate.names, training_frame=prostate) >>> pros_pca.show() ``` ``` -------------------------------- ### Install H2O Python Module Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_sources/intro.rst.txt Install the H2O Python module using pip. This command installs the specified version of the module. ```bash pip install h2o ``` -------------------------------- ### H2OHGLMEstimator Example Source: https://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/modeling.html Example of training an H2OHGLMEstimator model. ```APIDOC ## H2OHGLMEstimator Example ### Description Example of training an H2OHGLMEstimator model. ### Code ```python >>> import h2o >>> from h2o.estimators import H2OHGLMEstimator >>> h2o.init() >>> prostate_path <- system.file("extdata", "prostate.csv", package = "h2o") >>> prostate <- h2o.uploadFile(path = prostate_path) >>> prostate$CAPSULE <- as.factor(prostate$CAPSULE) >>> hglm_model =H2OHGLMEstimator(random_columns = ["AGE"], group_column = "RACE") >>> hglm_model.train(x=c("AGE","RACE","DPROS"), y="CAPSULE", training_frame=prostate) ``` ```