Instruction:
How would you explain what the following Python 3 function does
def stop(self):
        if self.pid > 0:
            print("Killing JVM with PID {}".format(self.pid))
            try:
                self.child.terminate()
                self.child.wait()
            except OSError:
                pass
            self.pid = -1

Response:
Normal node shutdown.
        Ignore failures for now.

        :return none


Instruction:
How would you code a function in Python 3 to
start the H2O cluster.

Response:
def start(self):
        """
        Start H2O cluster.
        The cluster is not up until wait_for_cloud_to_be_up() is called and returns.

        :return none
        """
        for node in self.nodes:
            node.start()

        for node in self.client_nodes:
            node.start()


Instruction:
Write a Python 3 script for
stopping all nodes and client nodes.

Response:
def stop(self):
        """
        Normal cluster shutdown.

        :return none
        """
        for node in self.nodes:
            node.stop()

        for node in self.client_nodes:
            node.stop()


Instruction:
Implement a function in Python 3 to
terminate a running cluster.

Response:
def terminate(self):
        """
        Terminate a running cluster.  (Due to a signal.)

        :return none
        """
        for node in self.client_nodes:
            node.terminate()

        for node in self.nodes:
            node.terminate()


Instruction:
Here you have a function in Python 3, explain what it does
def get_ip(self):
        if len(self.client_nodes) > 0:
            node = self.client_nodes[0]
        else:
            node = self.nodes[0]
        return node.get_ip()

Response: Return an ip to use to talk to this cluster.


Instruction:
Here you have a function in Python 3, explain what it does
def get_port(self):
        if len(self.client_nodes) > 0:
            node = self.client_nodes[0]
        else:
            node = self.nodes[0]
        return node.get_port()

Response: Return a port to use to talk to this cluster.


Instruction:
Explain what the following Python 3 code does
def main(argv):
    global g_log_base_dir
    global g_airline_java
    global g_milsongs_java
    global g_airline_python
    global g_milsongs_python
    global g_jenkins_url
    global g_airline_py_tail
    global g_milsongs_py_tail
    global g_airline_java_tail
    global g_milsongs_java_tail

    if len(argv) < 9:
        print "python grabGLRMrunLogs logsBaseDirectory airlineJavaFileNameWithPath milsongJavaFileNameWithPath " \
              "airlinePyunitWithPath airlinePyunitWithPath jenkinsJobURL startBuild# endBuild#.\n"
        sys.exit(1)
    else:   # we may be in business
#        g_script_name = os.path.basename(argv[0])   # get name of script being run.
            # base directory where all logs will be collected according to build #
        g_log_base_dir = argv[1]
        g_jenkins_url = argv[2]
        g_airline_java = argv[3]
        g_milsongs_java = argv[4]
        g_airline_python = argv[5]
        g_milsongs_python = argv[6]
        start_number = int(argv[7])
        end_number = int(argv[8])

        if (start_number > end_number):
            print "startBuild# must be <= end_number"
            sys.exit(1)
        else:
            for build_index in range(start_number, end_number+1):   # grab log info for all builds
                # copy the java jobs
                get_file_out(build_index, g_airline_java, g_airline_java_tail)
                get_file_out(build_index, g_milsongs_java, g_milsongs_java_tail)

                # copy the pyunit jobs
                get_file_out(build_index, g_airline_python, g_airline_py_tail)
                get_file_out(build_index, g_milsongs_python, g_milsongs_py_tail)

Response: Main function for the airline script.


Instruction:
Implement a function in Python 3 to
plot the training set and validation set for an H2OBinomialModel.

Response:
def plot(self, timestep="AUTO", metric="AUTO", server=False, **kwargs):
        """
        Plot training set (and validation set if available) scoring history for an H2OBinomialModel.

        The timestep and metric arguments are restricted to what is available in its scoring history.

        :param str timestep: A unit of measurement for the x-axis.
        :param str metric: A unit of measurement for the y-axis.
        :param bool server: if True, then generate the image inline (using matplotlib's "Agg" backend)
        """
        assert_is_type(metric, "AUTO", "logloss", "auc", "classification_error", "rmse")
        if self._model_json["algo"] in ("deeplearning", "deepwater", "xgboost", "drf", "gbm"):
            if metric == "AUTO":
                metric = "logloss"
        self._plot(timestep=timestep, metric=metric, server=server)


Instruction:
Write a Python 3 function for
returning the coordinates of the ROC curve for a given set of data.

Response:
def roc(self, train=False, valid=False, xval=False):
        """
        Return the coordinates of the ROC curve for a given set of data.

        The coordinates are two-tuples containing the false positive rates as a list and true positive rates as a list.
        If all are False (default), then return is the training data. If more than one ROC
        curve is requested, the data is returned as a dictionary of two-tuples.

        :param bool train: If True, return the ROC value for the training data.
        :param bool valid: If True, return the ROC value for the validation data.
        :param bool xval: If True, return the ROC value for each of the cross-validated splits.

        :returns: The ROC values for the specified key(s).
        """
        tm = ModelBase._get_metrics(self, train, valid, xval)
        m = {}
        for k, v in viewitems(tm):

            if v is not None:
                m[k] = (v.fprs, v.tprs)
        return list(m.values())[0] if len(m) == 1 else m


Instruction:
Write a Python 3 script for
retrieving the index in this metric s threshold list at which the given threshold is located.

Response:
def find_idx_by_threshold(self, threshold, train=False, valid=False, xval=False):
        """
        Retrieve the index in this metric's threshold list at which the given threshold is located.

        If all are False (default), then return the training metric value.
        If more than one options is set to True, then return a dictionary of metrics where the keys are "train",
        "valid", and "xval".

        :param float threshold: Threshold value to search for in the threshold list.
        :param bool train: If True, return the find idx by threshold value for the training data.
        :param bool valid: If True, return the find idx by threshold value for the validation data.
        :param bool xval: If True, return the find idx by threshold value for each of the cross-validated splits.

        :returns: The find idx by threshold values for the specified key(s).
        """
        tm = ModelBase._get_metrics(self, train, valid, xval)
        m = {}
        for k, v in viewitems(tm):
            m[k] = None if v is None else v.find_idx_by_threshold(threshold)
        return list(m.values())[0] if len(m) == 1 else m


Instruction:
How would you code a function in Python 3 to
create a new H2OWord2vecEstimator instance from an external model.

Response:
def from_external(external=H2OFrame):
        """
        Creates new H2OWord2vecEstimator based on an external model.
        :param external: H2OFrame with an external model
        :return: H2OWord2vecEstimator instance representing the external model
        """
        w2v_model = H2OWord2vecEstimator(pre_trained=external)
        w2v_model.train()
        return w2v_model


Instruction:
Given the following Python 3 function, write the documentation
def _determine_vec_size(self):
        first_column = self.pre_trained.types[self.pre_trained.columns[0]]

        if first_column != 'string':
            raise H2OValueError("First column of given pre_trained model %s is required to be a String",
                                self.pre_trained.frame_id)

        if list(self.pre_trained.types.values()).count('string') > 1:
            raise H2OValueError("There are multiple columns in given pre_trained model %s with a String type.",
                                self.pre_trained.frame_id)

        self.vec_size = self.pre_trained.dim[1] - 1;

Response: Determines the size of the vector for a pre - trained model after basic model verification.


Instruction:
Create a Python 3 function to
mean absolute error regression loss.

Response:
def h2o_mean_absolute_error(y_actual, y_predicted, weights=None):
    """
    Mean absolute error regression loss.

    :param y_actual: H2OFrame of actual response.
    :param y_predicted: H2OFrame of predicted response.
    :param weights: (Optional) sample weights
    :returns: mean absolute error loss (best is 0.0).
    """
    ModelBase._check_targets(y_actual, y_predicted)
    return _colmean((y_predicted - y_actual).abs())


Instruction:
Write a Python 3 script to
mean squared error regression loss.

Response:
def h2o_mean_squared_error(y_actual, y_predicted, weights=None):
    """
    Mean squared error regression loss

    :param y_actual: H2OFrame of actual response.
    :param y_predicted: H2OFrame of predicted response.
    :param weights: (Optional) sample weights
    :returns: mean squared error loss (best is 0.0).
    """
    ModelBase._check_targets(y_actual, y_predicted)
    return _colmean((y_predicted - y_actual) ** 2)


Instruction:
Can you tell what is the following Python 3 function doing
def h2o_explained_variance_score(y_actual, y_predicted, weights=None):
    ModelBase._check_targets(y_actual, y_predicted)

    _, numerator = _mean_var(y_actual - y_predicted, weights)
    _, denominator = _mean_var(y_actual, weights)
    if denominator == 0.0:
        return 1. if numerator == 0 else 0.  # 0/0 => 1, otherwise, 0
    return 1 - numerator / denominator

Response: Explained variance regression score function.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def h2o_r2_score(y_actual, y_predicted, weights=1.):
    ModelBase._check_targets(y_actual, y_predicted)
    numerator = (weights * (y_actual - y_predicted) ** 2).sum()
    denominator = (weights * (y_actual - _colmean(y_actual)) ** 2).sum()

    if denominator == 0.0:
        return 1. if numerator == 0. else 0.  # 0/0 => 1, else 0
    return 1 - numerator / denominator

Response: H2O R - squared regression score function


Instruction:
Create a Python 3 function to
plot the training set and validation set for an H2ORegressionModel.

Response:
def plot(self, timestep="AUTO", metric="AUTO", **kwargs):
        """
        Plots training set (and validation set if available) scoring history for an H2ORegressionModel. The timestep
        and metric arguments are restricted to what is available in its scoring history.

        :param timestep: A unit of measurement for the x-axis.
        :param metric: A unit of measurement for the y-axis.
        :returns: A scoring history plot.
        """

        if self._model_json["algo"] in ("deeplearning", "deepwater", "xgboost", "drf", "gbm"):
            if metric == "AUTO":
                metric = "rmse"
            elif metric not in ("rmse", "deviance", "mae"):
                raise ValueError("metric for H2ORegressionModel must be one of: AUTO, rmse, deviance, or mae")

        self._plot(timestep=timestep, metric=metric, **kwargs)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def assert_is_type(var, *types, **kwargs):
    assert types, "The list of expected types was not provided"
    expected_type = types[0] if len(types) == 1 else U(*types)
    if _check_type(var, expected_type): return

    # Type check failed => Create a nice error message
    assert set(kwargs).issubset({"message", "skip_frames"}), "Unexpected keyword arguments: %r" % kwargs
    message = kwargs.get("message", None)
    skip_frames = kwargs.get("skip_frames", 1)
    args = _retrieve_assert_arguments()
    vname = args[0]
    etn = _get_type_name(expected_type, dump=", ".join(args[1:]))
    vtn = _get_type_name(type(var))
    raise H2OTypeError(var_name=vname, var_value=var, var_type_name=vtn, exp_type_name=etn, message=message,
                       skip_frames=skip_frames)

Response: Checks that the variable is of the specified type.


Instruction:
How would you explain what the following Python 3 function does
def assert_matches(v, regex):
    m = re.match(regex, v)
    if m is None:
        vn = _retrieve_assert_arguments()[0]
        message = "Argument `{var}` (= {val!r}) did not match /{regex}/".format(var=vn, regex=regex, val=v)
        raise H2OValueError(message, var_name=vn, skip_frames=1)
    return m

Response: Assert that string variable matches the provided regular expression.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def assert_satisfies(v, cond, message=None):
    if not cond:
        vname, vexpr = _retrieve_assert_arguments()
        if not message:
            message = "Argument `{var}` (= {val!r}) does not satisfy the condition {expr}" \
                      .format(var=vname, val=v, expr=vexpr)
        raise H2OValueError(message=message, var_name=vname, skip_frames=1)

Response: Assert that the variable v satisfies the condition cond.


Instruction:
Can you write a function in Python 3 where it
checks if the variable is of the specified type and returns True if the variable is of the specified type and False otherwise.

Response:
def _check_type(var, vtype):
    """
    Return True if the variable is of the specified type, and False otherwise.

    :param var: variable to check
    :param vtype: expected variable's type
    """
    if vtype is None:
        return var is None
    if isinstance(vtype, _primitive_type):
        return var == vtype
    if vtype is str:
        return isinstance(var, _str_type)
    if vtype is int:
        return isinstance(var, _int_type)
    if vtype is numeric:
        return isinstance(var, _num_type)
    if isinstance(vtype, MagicType):
        return vtype.check(var)
    if isinstance(vtype, type):
        # ``vtype`` is a name of the class, or a built-in type such as "list", "tuple", etc
        return isinstance(var, vtype)
    if isinstance(vtype, list):
        # ``vtype`` is a list literal
        elem_type = U(*vtype)
        return isinstance(var, list) and all(_check_type(item, elem_type) for item in var)
    if isinstance(vtype, set):
        # ``vtype`` is a set literal
        elem_type = U(*vtype)
        return isinstance(var, set) and all(_check_type(item, elem_type) for item in var)
    if isinstance(vtype, tuple):
        # ``vtype`` is a tuple literal
        return (isinstance(var, tuple) and len(vtype) == len(var) and
                all(_check_type(var[i], vtype[i]) for i in range(len(vtype))))
    if isinstance(vtype, dict):
        # ``vtype`` is a dict literal
        ttkv = U(*viewitems(vtype))
        return isinstance(var, dict) and all(_check_type(kv, ttkv) for kv in viewitems(var))
    if isinstance(vtype, (FunctionType, BuiltinFunctionType)):
        return vtype(var)
    raise RuntimeError("Ivalid type %r in _check_type()" % vtype)


Instruction:
Here you have a function in Python 3, explain what it does
def _get_type_name(vtype, dump=None):
    if vtype is None:
        return "None"
    if vtype is str:
        return "string"
    if vtype is int:
        return "integer"
    if vtype is numeric:
        return "numeric"
    if is_type(vtype, str):
        return '"%s"' % repr(vtype)[1:-1]
    if is_type(vtype, int):
        return str(vtype)
    if isinstance(vtype, MagicType):
        return vtype.name(dump)
    if isinstance(vtype, type):
        return vtype.__name__
    if isinstance(vtype, list):
        return "list(%s)" % _get_type_name(U(*vtype), dump)
    if isinstance(vtype, set):
        return "set(%s)" % _get_type_name(U(*vtype), dump)
    if isinstance(vtype, tuple):
        return "(%s)" % ", ".join(_get_type_name(item, dump) for item in vtype)
    if isinstance(vtype, dict):
        return "dict(%s)" % ", ".join("%s: %s" % (_get_type_name(tk, dump), _get_type_name(tv, dump))
                                      for tk, tv in viewitems(vtype))
    if isinstance(vtype, (FunctionType, BuiltinFunctionType)):
        if vtype.__name__ == "<lambda>":
            return _get_lambda_source_code(vtype, dump)
        else:
            return vtype.__name__
    raise RuntimeError("Unexpected `vtype`: %r" % vtype)

Response: Return the name of the provided type.


Instruction:
Write a Python 3 script for
attempting to find the source code of the lambda_fn within the string src.

Response:
def _get_lambda_source_code(lambda_fn, src):
    """Attempt to find the source code of the ``lambda_fn`` within the string ``src``."""
    def gen_lambdas():
        def gen():
            yield src + "\n"

        g = gen()
        step = 0
        tokens = []
        for tok in tokenize.generate_tokens(getattr(g, "next", getattr(g, "__next__", None))):
            if step == 0:
                if tok[0] == tokenize.NAME and tok[1] == "lambda":
                    step = 1
                    tokens = [tok]
                    level = 0
            elif step == 1:
                if tok[0] == tokenize.NAME:
                    tokens.append(tok)
                    step = 2
                else:
                    step = 0
            elif step == 2:
                if tok[0] == tokenize.OP and tok[1] == ":":
                    tokens.append(tok)
                    step = 3
                else:
                    step = 0
            elif step == 3:
                if level == 0 and (tok[0] == tokenize.OP and tok[1] in ",)" or tok[0] == tokenize.ENDMARKER):
                    yield tokenize.untokenize(tokens).strip()
                    step = 0
                else:
                    tokens.append(tok)
                    if tok[0] == tokenize.OP:
                        if tok[1] in "[({": level += 1
                        if tok[1] in "])}": level -= 1
        assert not tokens

    actual_code = lambda_fn.__code__.co_code
    for lambda_src in gen_lambdas():
        try:
            fn = eval(lambda_src, globals(), locals())
            if fn.__code__.co_code == actual_code:
                return lambda_src.split(":", 1)[1].strip()
        except Exception:
            pass
    return "<lambda>"


Instruction:
Can you generate the documentation for the following Python 3 function
def name(self, src=None):
        res = [_get_type_name(tt, src) for tt in self._types]
        if len(res) == 2 and "None" in res:
            res.remove("None")
            return "?" + res[0]
        else:
            return " | ".join(res)

Response: Return string representing the name of this type.


Instruction:
Explain what the following Python 3 code does
def check(self, var):
        return all(_check_type(var, tt) for tt in self._types)

Response: Return True if the variable matches this type and False otherwise.


Instruction:
Can you tell what is the following Python 3 function doing
def name(self, src=None):
        return " & ".join(_get_type_name(tt, src) for tt in self._types)

Response: Return string representing the name of this type.


Instruction:
Explain what the following Python 3 code does
def check(self, var):
        return not any(_check_type(var, tt) for tt in self._types)

Response: Return True if the variable matches any of the types and False otherwise.


Instruction:
Make a summary of the following Python 3 code
def name(self, src=None):
        if len(self._types) > 1:
            return "!(%s)" % str("|".join(_get_type_name(tt, src) for tt in self._types))
        else:
            return "!" + _get_type_name(self._types[0], src)

Response: Return string representing the name of this type.


Instruction:
Here you have a function in Python 3, explain what it does
def check(self, var):
        return isinstance(var, tuple) and all(_check_type(t, self._element_type) for t in var)

Response: Return True if the variable matches this type and False otherwise.


Instruction:
Create a Python 3 function for
returning True if the variable matches this type and False otherwise.

Response:
def check(self, var):
        """Return True if the variable matches this type, and False otherwise."""
        if not isinstance(var, dict): return False
        if any(key not in self._types for key in var): return False
        for key, ktype in viewitems(self._types):
            val = var.get(key, None)
            if not _check_type(val, ktype):
                return False
        return True


Instruction:
Can you create a Python 3 function that
returns string representing the name of this type.

Response:
def name(self, src=None):
        """Return string representing the name of this type."""
        return "{%s}" % ", ".join("%s: %s" % (key, _get_type_name(ktype, src))
                                  for key, ktype in viewitems(self._types))


Instruction:
Make a summary of the following Python 3 code
def check(self, var):
        return (isinstance(var, _int_type) and
                (self._lower_bound is None or var >= self._lower_bound) and
                (self._upper_bound is None or var <= self._upper_bound))

Response: Return True if the variable matches the specified type.


Instruction:
Create a Python 3 function to
return string representing the name of this type.

Response:
def name(self, src=None):
        """Return string representing the name of this type."""
        if self._upper_bound is None and self._lower_bound is None: return "int"
        if self._upper_bound is None:
            if self._lower_bound == 1: return "int>0"
            return "int≥%d" % self._lower_bound
        if self._lower_bound is None:
            return "int≤%d" % self._upper_bound
        return "int[%d…%d]" % (self._lower_bound, self._upper_bound)


Instruction:
Given the following Python 3 function, write the documentation
def check(self, var):
        return (isinstance(var, _num_type) and
                (self._lower_bound is None or var >= self._lower_bound) and
                (self._upper_bound is None or var <= self._upper_bound))

Response: Return True if the variable matches the specified type.


Instruction:
Can you write a function in Python 3 where it
returns True if the variable matches this type and False otherwise.

Response:
def check(self, var):
        """Return True if the variable matches this type, and False otherwise."""
        if self._class is None: self._init()
        return self._class and self._checker(var, self._class)


Instruction:
Given the following Python 3 function, write the documentation
def check(self, var):
        if not isinstance(var, _str_type): return False
        return _enum_mangle(var) in self._consts

Response: Check whether the provided value is a valid enum constant.


Instruction:
How would you implement a function in Python 3 that
retrieves the config as a dictionary of key - value pairs.

Response:
def get_config():
        """Retrieve the config as a dictionary of key-value pairs."""
        self = H2OConfigReader._get_instance()
        if not self._config_loaded:
            self._read_config()
        return self._config


Instruction:
Make a summary of the following Python 3 code
def _read_config(self):
        self._config_loaded = True
        conf = []
        for f in self._candidate_log_files():
            if os.path.isfile(f):
                self._logger.info("Reading config file %s" % f)
                section_rx = re.compile(r"^\[(\w+)\]$")
                keyvalue_rx = re.compile(r"^(\w+:)?([\w.]+)\s*=(.*)$")
                with io.open(f, "rt", encoding="utf-8") as config_file:
                    section_name = None
                    for lineno, line in enumerate(config_file):
                        line = line.strip()
                        if line == "" or line.startswith("#"): continue
                        m1 = section_rx.match(line)
                        if m1:
                            section_name = m1.group(1)
                            continue
                        m2 = keyvalue_rx.match(line)
                        if m2:
                            lng = m2.group(1)
                            key = m2.group(2)
                            val = m2.group(3).strip()
                            if lng and lng.lower() != "py:": continue
                            if section_name:
                                key = section_name + "." + key
                            if key in H2OConfigReader._allowed_config_keys:
                                conf.append((key, val))
                            else:
                                self._logger.error("Key %s is not a valid config key" % key)
                            continue
                        self._logger.error("Syntax error in config file line %d: %s" % (lineno, line))
                self._config = dict(conf)
                return

Response: Find and parse config file and store all variables in self. _config.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _candidate_log_files():
        # Search for .h2oconfig in the current directory and all parent directories
        relpath = ".h2oconfig"
        prevpath = None
        while True:
            abspath = os.path.abspath(relpath)
            if abspath == prevpath: break
            prevpath = abspath
            relpath = "../" + relpath
            yield abspath
        # Also check if .h2oconfig exists in the user's directory
        yield os.path.expanduser("~/.h2oconfig")

Response: Return a generator that yields the absolute locations of the. h2oconfig file.


Instruction:
Can you generate the documentation for the following Python 3 function
def execute(self, progress_fn, print_verbose_info=None):
        assert_is_type(progress_fn, FunctionType, GeneratorType, MethodType)
        if isinstance(progress_fn, GeneratorType):
            # Convert generator to a regular function
            progress_fn = (lambda g: lambda: next(g))(progress_fn)

        # Initialize the execution context
        self._next_poll_time = 0
        self._t0 = time.time()
        self._x0 = 0
        self._v0 = 0.01  # corresponds to 100s completion time
        self._ve = 0.01

        progress = 0
        status = None  # Status message in case the job gets interrupted.
        try:
            while True:
                # We attempt to synchronize all helper functions, ensuring that each of them has the same idea
                # for what the current time moment is. Otherwise we could have some corner cases when one method
                # says that something must happen right now, while the other already sees that moment in the past.
                now = time.time()

                # Query the progress level, but only if it's time already
                if self._next_poll_time <= now:
                    res = progress_fn()  # may raise StopIteration
                    assert_is_type(res, (numeric, numeric), numeric)
                    if not isinstance(res, tuple):
                        res = (res, -1)
                    # Progress querying could have taken some time, so update the current time moment
                    now = time.time()
                    self._store_model_progress(res, now)
                    self._recalculate_model_parameters(now)

                # Render the widget regardless of whether it's too early or not
                progress = min(self._compute_progress_at_time(now)[0], 1)
                if progress == 1 and self._get_real_progress() >= 1:
                    # Do not exit until both the model and the actual progress reach 100% mark.
                    break
                result = self._widget.render(progress)
                assert_is_type(result, RenderResult)
                time0 = result.next_time
                time1 = self._get_time_at_progress(result.next_progress)
                next_render_time = min(time0, time1)
                self._draw(result.rendered)

                # Wait until the next rendering/querying cycle
                wait_time = min(next_render_time, self._next_poll_time) - now
                if wait_time > 0:
                    time.sleep(wait_time)
                    if print_verbose_info is not None:
                        print_verbose_info(progress)
        except KeyboardInterrupt:
            # If the user presses Ctrl+C, we interrupt the progress bar.
            status = "cancelled"
        except StopIteration as e:
            # If the generator raises StopIteration before reaching 100%, then the progress display will
            # reamin incomplete.
            status = str(e)

        # Do one final rendering before we exit
        result = self._widget.render(progress=progress, status=status)
        self._draw(result.rendered, final=True)

        if status == "cancelled":
            # Re-raise the exception, to inform the upstream caller that something unexpected happened.
            raise StopIteration(status)

Response: Execute a function in the progress bar.


Instruction:
Can you create a Python 3 function that
stores the current model progress into the internal list of items.

Response:
def _store_model_progress(self, res, now):
        """
        Save the current model progress into ``self._progress_data``, and update ``self._next_poll_time``.

        :param res: tuple (progress level, poll delay).
        :param now: current timestamp.
        """
        raw_progress, delay = res
        raw_progress = clamp(raw_progress, 0, self._maxval)
        self._progress_data.append((now, raw_progress))

        if delay < 0:
            # calculation of ``_guess_next_poll_interval()`` should be done only *after* we pushed the fresh data to
            # ``self._progress_data``.
            delay = self._guess_next_poll_interval()
        self._next_poll_time = now + clamp(delay, self.MIN_PROGRESS_CHECK_INTERVAL, self.MAX_PROGRESS_CHECK_INTERVAL)


Instruction:
Can you write a function in Python 3 where it
computes t0 x0 v0 ve.

Response:
def _recalculate_model_parameters(self, now):
        """Compute t0, x0, v0, ve."""
        time_until_end = self._estimate_progress_completion_time(now) - now
        assert time_until_end >= 0, "Estimated progress completion cannot be in the past."
        x_real = self._get_real_progress()
        if x_real == 1:
            t0, x0, v0, ve = now, 1, 0, 0
        else:
            x0, v0 = self._compute_progress_at_time(now)
            t0 = now
            if x0 >= 1:
                # On rare occasion, the model's progress may have reached 100% by ``now``. This can happen if
                # (1) the progress is close to 100% initially and has high speed, (2) on the previous call we
                # estimated that the process completion time will be right after the next poll time, and (3)
                # the polling itself took so much time that the process effectively "overshoot".
                # If this happens, then we adjust x0, v0 to the previous valid data checkpoint.
                t0, x0, v0 = self._t0, self._x0, self._v0
                time_until_end += now - t0
            z = self.BETA * time_until_end
            max_speed = (1 - x_real**2) / self.FINISH_DELAY
            ve = v0 + (self.BETA * (1 - x0) - v0 * z) / (z - 1 + math.exp(-z))
            if ve < 0:
                # Current speed is too high -- reduce v0 (violate non-smoothness of speed)
                v0 = self.BETA * (1 - x0) / (1 - math.exp(-z))
                ve = 0
            if ve > max_speed:
                # Current speed is too low: finish later, but do not allow ``ve`` to be higher than ``max_speed``
                ve = max_speed
        self._t0, self._x0, self._v0, self._ve = t0, x0, v0, ve


Instruction:
How would you implement a function in Python 3 that
estimates the time when the underlying process is expected to reach completion.

Response:
def _estimate_progress_completion_time(self, now):
        """
        Estimate the moment when the underlying process is expected to reach completion.

        This function should only return future times. Also this function is not allowed to return time moments less
        than self._next_poll_time if the actual progress is below 100% (this is because we won't know that the
        process have finished until we poll the external progress function).
        """
        assert self._next_poll_time >= now
        tlast, wlast = self._progress_data[-1]
        # If reached 100%, make sure that we finish as soon as possible, but maybe not immediately
        if wlast == self._maxval:
            current_completion_time = (1 - self._x0) / self._v0 + self._t0
            return clamp(current_completion_time, now, now + self.FINISH_DELAY)

        # Calculate the approximate speed of the raw progress based on recent data
        tacc, wacc = 0, 0
        factor = self.GAMMA
        for t, x in self._progress_data[-2::-1]:
            tacc += factor * (tlast - t)
            wacc += factor * (wlast - x)
            factor *= self.GAMMA
            if factor < 1e-2: break

        # If there was no progress at all, then just assume it's 5 minutes from now
        if wacc == 0: return now + 300

        # Estimate the completion time assuming linear progress
        t_estimate = tlast + tacc * (self._maxval - wlast) / wacc

        # Adjust the estimate if it looks like it may happen too soon
        if t_estimate <= self._next_poll_time:
            t_estimate = self._next_poll_time + self.FINISH_DELAY

        return t_estimate


Instruction:
Write a Python 3 script for
guessing the next poll interval for the next poll.

Response:
def _guess_next_poll_interval(self):
        """
        Determine when to query the progress status next.

        This function is used if the external progress function did not return time interval for when it should be
        queried next.
        """
        time_elapsed = self._progress_data[-1][0] - self._progress_data[0][0]
        real_progress = self._get_real_progress()
        return min(0.2 * time_elapsed, 0.5 + (1 - real_progress)**0.5)


Instruction:
Create a Python 3 function for
computing the modelled progress state at the given time moment.

Response:
def _compute_progress_at_time(self, t):
        """
        Calculate the modelled progress state for the given time moment.

        :returns: tuple (x, v) of the progress level and progress speed.
        """
        t0, x0, v0, ve = self._t0, self._x0, self._v0, self._ve
        z = (v0 - ve) * math.exp(-self.BETA * (t - t0))
        vt = ve + z
        xt = clamp(x0 + ve * (t - t0) + (v0 - ve - z) / self.BETA, 0, 1)
        return xt, vt


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_time_at_progress(self, x_target):
        t, x, v = self._t0, self._x0, self._v0
        # The convergence should be achieved in just few iterations, however in unlikely situation that it doesn't
        # we don't want to loop forever...
        for _ in range(20):
            if v == 0: return 1e20
            # make time prediction assuming the progress will continue at a linear speed ``v``
            t += (x_target - x) / v
            # calculate the actual progress at that time
            x, v = self._compute_progress_at_time(t)
            # iterate until convergence
            if abs(x - x_target) < 1e-3: return t
        return time.time() + 100

Response: Return the projected time at which the progress level x_target will be reached.


Instruction:
Can you create a Python 3 function that
prints the rendered string to the stdout.

Response:
def _draw(self, txt, final=False):
        """Print the rendered string to the stdout."""
        if not self._file_mode:
            # If the user presses Ctrl+C this ensures we still start writing from the beginning of the line
            sys.stdout.write("\r")
        sys.stdout.write(txt)
        if final and not isinstance(self._widget, _HiddenWidget):
            sys.stdout.write("\n")
        else:
            if not self._file_mode:
                sys.stdout.write("\r")
            sys.stdout.flush()


Instruction:
Explain what the following Python 3 code does
def _compute_widget_sizes(self):
        wl = [0] * len(self._widgets)
        flex_count = 0

        # First render all non-flexible widgets
        for i, widget in enumerate(self._widgets):
            if isinstance(widget, ProgressBarFlexibleWidget):
                flex_count += 1
            else:
                wl[i] = widget.render(1).length

        remaining_width = self._width - sum(wl)
        remaining_width -= len(self._widgets) - 1  # account for 1-space interval between widgets
        if remaining_width < 10 * flex_count:
            if self._file_mode:
                remaining_width = 10 * flex_count
            else:
                # The window is too small to accomodate the widget: try to split it into several lines, otherwise
                # switch to "file mode". If we don't do this, then rendering the widget will cause it to wrap, and
                # then when we use \r to go to the beginning of the line, only part of the widget will be overwritten,
                # which means we'll have many (possibly hundreds) of progress bar lines in the end.
                widget0 = self._widgets[0]
                if isinstance(widget0, PBWString) and remaining_width + widget0.render(0).length >= 10 * flex_count:
                    remaining_width += widget0.render(0).length + 1
                    self._to_render = widget0.render(0).rendered + "\n"
                    self._widgets = self._widgets[1:]
                if remaining_width < 10 * flex_count:
                    self._file_mode = True
                    remaining_width = 10 * flex_count

        remaining_width = max(remaining_width, 10 * flex_count)  # Ensure at least 10 chars per flexible widget

        for i, widget in enumerate(self._widgets):
            if isinstance(widget, ProgressBarFlexibleWidget):
                target_length = int(remaining_width / flex_count)
                result = widget.render(1, target_length)
                wl[i] = result.length
                remaining_width -= result.length
                flex_count -= 1

        return wl

Response: Compute the widths of all widgets in the current window.


Instruction:
Here you have a function in Python 3, explain what it does
def _get_terminal_size():
        # If output is not terminal but a regular file, assume 100 chars width
        if not sys.stdout.isatty():
            return 80

        # Otherwise, first try getting the dimensions from shell command `stty`:
        try:
            import subprocess
            ret = subprocess.check_output(["stty", "size"]).strip().split(" ")
            if len(ret) == 2:
                return int(ret[1])
        except:
            pass

        # Otherwise try using ioctl
        try:
            from termios import TIOCGWINSZ
            from fcntl import ioctl
            from struct import unpack
            res = unpack("hh", ioctl(sys.stdout, TIOCGWINSZ, b"1234"))
            return int(res[1])
        except:
            pass

        # Finally check the COLUMNS environment variable
        return int(os.environ.get("COLUMNS", 80))

Response: Find current STDOUT s width in characters.


Instruction:
Can you generate the documentation for the following Python 3 function
def set_encoding(self, encoding):
        self._bar_ends = "[]"
        self._bar_symbols = "#"
        if not encoding: return
        s1 = "\u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"
        s2 = "\u258C\u2588"
        s3 = "\u2588"
        if self._file_mode:
            s1 = s2 = None
        assert len(s3) == 1
        for s in (s1, s2, s3):
            if s is None: continue
            try:
                s.encode(encoding)
                self._bar_ends = "||"
                self._bar_symbols = s
                return
            except UnicodeEncodeError:
                pass
            except LookupError:
                print("Warning: unknown encoding %s" % encoding)

Response: Inform the widget about the encoding of the underlying character stream.


Instruction:
Explain what the following Python 3 code does
def fit(self, frame = None):
        self._teColumns = list(map(lambda i: frame.names[i], self._teColumns)) if all(isinstance(n, int) for n in self._teColumns) else self._teColumns
        self._responseColumnName = frame.names[self._responseColumnName] if isinstance(self._responseColumnName, int) else self._responseColumnName
        self._foldColumnName = frame.names[self._foldColumnName] if isinstance(self._foldColumnName, int) else self._foldColumnName

        self._encodingMap = ExprNode("target.encoder.fit", frame, self._teColumns, self._responseColumnName,
                                     self._foldColumnName)._eager_map_frame()

        return self._encodingMap

Response: Returns an encoding map as an object that maps column_name -> frame_with_encoding_map_for_this_column_name


Instruction:
Create a Python 3 function to
apply transformation to the target encoding maps generated during training.

Response:
def transform(self, frame=None, holdout_type=None, noise=-1, seed=-1):
        """
        Apply transformation to `te_columns` based on the encoding maps generated during `TargetEncoder.fit()` call.
        You must not pass encodings manually from `.fit()` method because they are being stored internally
        after `.fit()' had been called.

        :param frame frame: to which frame we are applying target encoding transformations.
        :param str holdout_type: Supported options:

                1) "kfold" - encodings for a fold are generated based on out-of-fold data.
                2) "loo" - leave one out. Current row's response value is subtracted from the pre-calculated per-level frequencies.
                3) "none" - we do not holdout anything. Using whole frame for training

        :param float noise: the amount of random noise added to the target encoding.  This helps prevent overfitting. Defaults to 0.01 * range of y.
        :param int seed: a random seed used to generate draws from the uniform distribution for random noise. Defaults to -1.
        """
        assert_is_type(holdout_type, "kfold", "loo", "none")

        # We need to make sure that frames are being sent in the same order
        assert self._encodingMap.map_keys['string'] == self._teColumns
        encodingMapKeys = self._encodingMap.map_keys['string']
        encodingMapFramesKeys = list(map(lambda x: x['key']['name'], self._encodingMap.frames))
        return H2OFrame._expr(expr=ExprNode("target.encoder.transform", encodingMapKeys, encodingMapFramesKeys, frame, self._teColumns, holdout_type,
                                            self._responseColumnName, self._foldColumnName,
                                            self._blending, self._inflectionPoint, self._smoothing,
                                            noise, seed))


Instruction:
Here you have a function in Python 3, explain what it does
def generatePandaEnumCols(pandaFtrain, cname, nrows, domainL):
    import numpy as np
    import pandas as pd

    cmissingNames=[cname+".missing(NA)"]
    tempnp = np.zeros((nrows,1), dtype=np.int)
    # check for nan and assign it correct value
    colVals = pandaFtrain[cname]
    for ind in range(nrows):
        try:
            if not(colVals[ind] in domainL):
                tempnp[ind]=1
        except ValueError:
            pass
    zeroFrame = pd.DataFrame(tempnp)
    zeroFrame.columns=cmissingNames
    temp = pd.get_dummies(pandaFtrain[cname], prefix=cname, drop_first=False)
    tempNames = list(temp)  # get column names
    colLength = len(tempNames)
    newNames = ['a']*colLength

    for ind in range(0,colLength):
        newNames[ind]=cname+"_"+domainL[ind]
    ftemp = temp[newNames]
    ctemp = pd.concat([ftemp, zeroFrame], axis=1)
    return ctemp

Response: This function generates a panda frame with the necessary encoding for an H2O Enum column.


Instruction:
Here you have a function in Python 3, explain what it does
def get_frame(frame_id, rows=10, rows_offset=0, cols=-1, full_cols=-1, cols_offset=0, light=False):
        fr = H2OFrame()
        fr._ex._cache._id = frame_id
        try:
            fr._ex._cache.fill(rows=rows, rows_offset=rows_offset, cols=cols, full_cols=full_cols, cols_offset=cols_offset, light=light)
        except EnvironmentError:
            return None
        return fr

Response: Retrieve an existing H2OFrame from the H2O cluster using the frame s id.


Instruction:
Implement a Python 3 function for
reloading the frame information from the backend H2O server.

Response:
def refresh(self):
        """Reload frame information from the backend H2O server."""
        self._ex._cache.flush()
        self._frame(fill_cache=True)


Instruction:
Given the following Python 3 function, write the documentation
def names(self):
        if not self._ex._cache.names_valid():
            self._ex._cache.flush()
            self._frame(fill_cache=True)
        return list(self._ex._cache.names)

Response: The list of column names ( List [ str ] ).


Instruction:
Can you generate the documentation for the following Python 3 function
def nrows(self):
        if not self._ex._cache.nrows_valid():
            self._ex._cache.flush()
            self._frame(fill_cache=True)
        return self._ex._cache.nrows

Response: Number of rows in the dataframe ( int ).


Instruction:
Can you write a function in Python 3 where it
numbers of columns in the dataframe ( int ).

Response:
def ncols(self):
        """Number of columns in the dataframe (int)."""
        if not self._ex._cache.ncols_valid():
            self._ex._cache.flush()
            self._frame(fill_cache=True)
        return self._ex._cache.ncols


Instruction:
Here you have a function in Python 3, explain what it does
def type(self, col):
        assert_is_type(col, int, str)
        if not self._ex._cache.types_valid() or not self._ex._cache.names_valid():
            self._ex._cache.flush()
            self._frame(fill_cache=True)
        types = self._ex._cache.types
        if is_type(col, str):
            if col in types:
                return types[col]
        else:
            names = self._ex._cache.names
            if -len(names) <= col < len(names):
                return types[names[col]]
        raise H2OValueError("Column '%r' does not exist in the frame" % col)

Response: Returns the type of the given column.


Instruction:
Implement a function in Python 3 to
return a list of indices of columns of the specified type from the H2OFrame.

Response:
def columns_by_type(self, coltype="numeric"):
        """
        Extract columns of the specified type from the frame.

        :param str coltype: A character string indicating which column type to filter by. This must be
            one of the following:

            - ``"numeric"``      - Numeric, but not categorical or time
            - ``"categorical"``  - Integer, with a categorical/factor String mapping
            - ``"string"``       - String column
            - ``"time"``         - Long msec since the Unix Epoch - with a variety of display/parse options
            - ``"uuid"``         - UUID
            - ``"bad"``          - No none-NA rows (triple negative! all NAs or zero rows)

        :returns: list of indices of columns that have the requested type
        """
        assert_is_type(coltype, "numeric", "categorical", "string", "time", "uuid", "bad")
        assert_is_type(self, H2OFrame)
        return ExprNode("columnsByType", self, coltype)._eager_scalar()


Instruction:
Make a summary of the following Python 3 code
def show(self, use_pandas=False, rows=10, cols=200):
        if self._ex is None:
            print("This H2OFrame has been removed.")
            return
        if not self._has_content():
            print("This H2OFrame is empty and not initialized.")
            return
        if self.nrows == 0:
            print("This H2OFrame is empty.")
            return
        if not self._ex._cache.is_valid(): self._frame()._ex._cache.fill()
        if H2ODisplay._in_ipy():
            import IPython.display
            if use_pandas and can_use_pandas():
                IPython.display.display(self.head(rows=rows, cols=cols).as_data_frame(fill_cache=True))
            else:
                IPython.display.display_html(self._ex._cache._tabulate("html", False), raw=True)
        else:
            if use_pandas and can_use_pandas():
                print(self.head(rows=rows, cols=cols).as_data_frame(True))  # no keyword fill_cache
            else:
                s = self.__unicode__()
                stk = traceback.extract_stack()
                if "IPython" in stk[-3][0]:
                    s = "\n%s" % s
                try:
                    print(s)
                except UnicodeEncodeError:
                    print(s.encode("ascii", "replace"))

Response: Prints a string representation of the data frame.


Instruction:
Write a Python 3 script to
display summary information about the frame.

Response:
def summary(self, return_data=False):
        """
        Display summary information about the frame.

        Summary includes min/mean/max/sigma and other rollup data.

        :param bool return_data: Return a dictionary of the summary output
        """
        if not self._has_content():
            print("This H2OFrame is empty and not initialized.")
            return self._ex._cache._data;
        if not self._ex._cache.is_valid(): self._frame()._ex._cache.fill()
        if not return_data:
            if self.nrows == 0:
                print("This H2OFrame is empty.")
            elif H2ODisplay._in_ipy():
                import IPython.display
                IPython.display.display_html(self._ex._cache._tabulate("html", True), raw=True)
            else:
                print(self._ex._cache._tabulate("simple", True))
        else:
            return self._ex._cache._data


Instruction:
How would you explain what the following Python 3 function does
def describe(self, chunk_summary=False):
        if self._has_content():
            res = h2o.api("GET /3/Frames/%s" % self.frame_id, data={"row_count": 10})["frames"][0]
            self._ex._cache._fill_data(res)

            print("Rows:{}".format(self.nrow))
            print("Cols:{}".format(self.ncol))

            #The chunk & distribution summaries are not cached, so must be pulled if chunk_summary=True.
            if chunk_summary:
                res["chunk_summary"].show()
                res["distribution_summary"].show()
            print("\n")
        self.summary()

Response: Generate an in - depth description of this H2OFrame.


Instruction:
Here you have a function in Python 3, explain what it does
def head(self, rows=10, cols=200):
        assert_is_type(rows, int)
        assert_is_type(cols, int)
        nrows = min(self.nrows, rows)
        ncols = min(self.ncols, cols)
        newdt = self[:nrows, :ncols]
        return newdt._frame(rows=nrows, cols=cols, fill_cache=True)

Response: Return the first rows and cols of the current H2OFrame.


Instruction:
Can you implement a function in Python 3 that
multiplies this H2OFrame by another matrix.

Response:
def mult(self, matrix):
        """
        Multiply this frame, viewed as a matrix, by another matrix.

        :param matrix: another frame that you want to multiply the current frame by; must be compatible with the
            current frame (i.e. its number of rows must be the same as number of columns in the current frame).
        :returns: new H2OFrame, which is the result of multiplying the current frame by ``matrix``.
        """
        if self.ncols != matrix.nrows:
            raise H2OValueError("Matrix is not compatible for multiplication with the current frame")
        return H2OFrame._expr(expr=ExprNode("x", self, matrix))


Instruction:
Write a Python 3 function that can
create a time column from individual components of a time column.

Response:
def moment(year=None, month=None, day=None, hour=None, minute=None, second=None, msec=None, date=None, time=None):
        """
        Create a time column from individual components.

        Each parameter should be either an integer, or a single-column H2OFrame
        containing the corresponding time parts for each row.

        The "date" part of the timestamp can be specified using either the tuple ``(year, month, day)``, or an
        explicit ``date`` parameter. The "time" part of the timestamp is optional, but can be specified either via
        the ``time`` parameter, or via the ``(hour, minute, second, msec)`` tuple.

        :param year: the year part of the constructed date
        :param month: the month part of the constructed date
        :param day: the day-of-the-month part of the constructed date
        :param hour: the hours part of the constructed date
        :param minute: the minutes part of the constructed date
        :param second: the seconds part of the constructed date
        :param msec: the milliseconds part of the constructed date
        :param date date: construct the timestamp from the Python's native ``datetime.date`` (or ``datetime.datetime``)
            object. If the object passed is of type ``date``, then you can specify the time part using either the
            ``time`` argument, or ``hour`` ... ``msec`` arguments (but not both). If the object passed is of type
            ``datetime``, then no other arguments can be provided.
        :param time time: construct the timestamp from this Python's native ``datetime.time`` object. This argument
            cannot be used alone, it should be supplemented with either ``date`` argument, or ``year`` ... ``day``
            tuple.

        :returns: H2OFrame with one column containing the date constructed from the provided arguments.
        """
        assert_is_type(date, None, datetime.date, numpy_datetime, pandas_timestamp)
        assert_is_type(time, None, datetime.time)
        assert_is_type(year, None, int, H2OFrame)
        assert_is_type(month, None, int, H2OFrame)
        assert_is_type(day, None, int, H2OFrame)
        assert_is_type(hour, None, int, H2OFrame)
        assert_is_type(minute, None, int, H2OFrame)
        assert_is_type(second, None, int, H2OFrame)
        assert_is_type(msec, None, int, H2OFrame)
        if time is not None:
            if hour is not None or minute is not None or second is not None or msec is not None:
                raise H2OValueError("Arguments hour, minute, second, msec cannot be used together with `time`.")
            hour = time.hour
            minute = time.minute
            second = time.second
            msec = time.microsecond // 1000
        if date is not None:
            if is_type(date, pandas_timestamp):
                date = date.to_pydatetime()
            if is_type(date, numpy_datetime):
                date = date.astype("M8[ms]").astype("O")
            if year is not None or month is not None or day is not None:
                raise H2OValueError("Arguments year, month and day cannot be used together with `date`.")
            year = date.year
            month = date.month
            day = date.day
            if isinstance(date, datetime.datetime):
                if time is not None:
                    raise H2OValueError("Argument `time` cannot be used together with `date` of datetime type.")
                if hour is not None or minute is not None or second is not None or msec is not None:
                    raise H2OValueError("Arguments hour, minute, second, msec cannot be used together with `date` "
                                        "of datetime type.")
                hour = date.hour
                minute = date.minute
                second = date.second
                msec = date.microsecond // 1000
        if year is None or month is None or day is None:
            raise H2OValueError("Either arguments (`year`, `month` and `day`) or the `date` are required.")
        if hour is None: hour = 0
        if minute is None: minute = 0
        if second is None: second = 0
        if msec is None: msec = 0

        local_vars = locals()
        res_nrows = None
        for n in ["year", "month", "day", "hour", "minute", "second", "msec"]:
            x = local_vars[n]
            if isinstance(x, H2OFrame):
                if x.ncols != 1:
                    raise H2OValueError("Argument `%s` is a frame with more than 1 column" % n)
                if x.type(0) not in {"int", "real"}:
                    raise H2OValueError("Column `%s` is not numeric (type = %s)" % (n, x.type(0)))
                if res_nrows is None:
                    res_nrows = x.nrows
                if x.nrows == 0 or x.nrows != res_nrows:
                    raise H2OValueError("Incompatible column `%s` having %d rows" % (n, x.nrows))
        if res_nrows is None:
            res_nrows = 1
        res = H2OFrame._expr(ExprNode("moment", year, month, day, hour, minute, second, msec))
        res._ex._cache._names = ["name"]
        res._ex._cache._types = {"name": "time"}
        res._ex._cache._nrows = res_nrows
        res._ex._cache._ncols = 1
        return res


Instruction:
Can you implement a function in Python 3 that
gets the factor levels.

Response:
def levels(self):
        """
        Get the factor levels.

        :returns: A list of lists, one list per column, of levels.
        """
        lol = H2OFrame._expr(expr=ExprNode("levels", self)).as_data_frame(False)
        lol.pop(0)  # Remove column headers
        lol = list(zip(*lol))
        return [[ll for ll in l if ll != ''] for l in lol]


Instruction:
Create a Python 3 function to
return the number of factor levels per categorical column.

Response:
def nlevels(self):
        """
        Get the number of factor levels for each categorical column.

        :returns: A list of the number of levels per column.
        """
        levels = self.levels()
        return [len(l) for l in levels] if levels else 0


Instruction:
How would you code a function in Python 3 to
return a new H2OFrame with the desired levels replaced.

Response:
def set_levels(self, levels):
        """
        Replace the levels of a categorical column.

        New levels must be aligned with the old domain. This call has copy-on-write semantics.

        :param List[str] levels: A list of strings specifying the new levels. The number of new
            levels must match the number of old levels.
        :returns: A single-column H2OFrame with the desired levels.
        """
        assert_is_type(levels, [str])
        return H2OFrame._expr(expr=ExprNode("setDomain", self, False, levels), cache=self._ex._cache)


Instruction:
Implement a function in Python 3 to
change names of columns in the frame.

Response:
def rename(self, columns=None):
        """
        Change names of columns in the frame.

        Dict key is an index or name of the column whose name is to be set.
        Dict value is the new name of the column.

        :param columns: dict-like transformations to apply to the column names
        """
        assert_is_type(columns, None, dict)
        new_names = self.names
        ncols = self.ncols

        for col, name in columns.items():
            col_index = None
            if is_type(col, int) and (-ncols <= col < ncols):
                col_index = (col + ncols) % ncols  # handle negative indices
            elif is_type(col, str) and col in self.names:
                col_index = self.names.index(col)  # lookup the name

            if col_index is not None:
                new_names[col_index] = name

        return self.set_names(new_names)


Instruction:
Can you write a function in Python 3 where it
sets the names of all columns in the current record set.

Response:
def set_names(self, names):
        """
        Change names of all columns in the frame.

        :param List[str] names: The list of new names for every column in the frame.
        """
        assert_is_type(names, [str])
        assert_satisfies(names, len(names) == self.ncol)
        self._ex = ExprNode("colnames=", self, range(self.ncol), names)  # Update-in-place, but still lazy
        return self


Instruction:
Create a Python 3 function to
set a new name for a column.

Response:
def set_name(self, col=None, name=None):
        """
        Set a new name for a column.

        :param col: index or name of the column whose name is to be set; may be skipped for 1-column frames
        :param name: the new name of the column
        """
        assert_is_type(col, None, int, str)
        assert_is_type(name, str)
        ncols = self.ncols

        col_index = None
        if is_type(col, int):
            if not(-ncols <= col < ncols):
                raise H2OValueError("Index %d is out of bounds for a frame with %d columns" % (col, ncols))
            col_index = (col + ncols) % ncols  # handle negative indices
        elif is_type(col, str):
            if col not in self.names:
                raise H2OValueError("Column %s doesn't exist in the frame." % col)
            col_index = self.names.index(col)  # lookup the name
        else:
            assert col is None
            if ncols != 1:
                raise H2OValueError("The frame has %d columns; please specify which one to rename" % ncols)
            col_index = 0
        if name != self.names[col_index] and name in self.types:
            raise H2OValueError("Column '%s' already exists in the frame" % name)

        oldname = self.names[col_index]
        old_cache = self._ex._cache
        self._ex = ExprNode("colnames=", self, col_index, name)  # Update-in-place, but still lazy
        self._ex._cache.fill_from(old_cache)
        if self.names is None:
            self._frame()._ex._cache.fill()
        else:
            self._ex._cache._names = self.names[:col_index] + [name] + self.names[col_index + 1:]
            self._ex._cache._types[name] = self._ex._cache._types.pop(oldname)
        return


Instruction:
Can you write a function in Python 3 where it
computes cumulative sum over rows / columns of the H2OFrame.

Response:
def cumsum(self,  axis=0):
        """
        Compute cumulative sum over rows / columns of the frame.

        :param int axis: 0 for column-wise, 1 for row-wise
        :returns: new H2OFrame with cumulative sums of the original frame.
        """
        return H2OFrame._expr(expr=ExprNode("cumsum", self, axis), cache=self._ex._cache)


Instruction:
Create a Python 3 function for
testing whether the elements of an H2OFrame are contained in the item.

Response:
def isin(self, item):
        """
        Test whether elements of an H2OFrame are contained in the ``item``.

        :param items: An item or a list of items to compare the H2OFrame against.

        :returns: An H2OFrame of 0s and 1s showing whether each element in the original H2OFrame is contained in item.
        """
        if is_type(item, list, tuple, set):
            if self.ncols == 1 and (self.type(0) == 'str' or self.type(0) == 'enum'):
                return self.match(item)
            else:
                return functools.reduce(H2OFrame.__or__, (self == i for i in item))
        else:
            return self == item


Instruction:
Write a Python 3 script to
build a fold assignments column for cross - validation.

Response:
def modulo_kfold_column(self, n_folds=3):
        """
        Build a fold assignments column for cross-validation.

        Rows are assigned a fold according to the current row number modulo ``n_folds``.

        :param int n_folds: An integer specifying the number of validation sets to split the training data into.
        :returns: A single-column H2OFrame with the fold assignments.
        """
        return H2OFrame._expr(expr=ExprNode("modulo_kfold_column", self, n_folds))._frame()


Instruction:
Explain what the following Python 3 code does
def stratified_kfold_column(self, n_folds=3, seed=-1):
        return H2OFrame._expr(
            expr=ExprNode("stratified_kfold_column", self, n_folds, seed))._frame()

Response:
Build a fold assignment column with the constraint that each fold has the same class
        distribution as the fold column.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def structure(self):
        df = self.as_data_frame(use_pandas=False)
        cn = df.pop(0)
        nr = self.nrow
        nc = self.ncol
        width = max([len(c) for c in cn])
        isfactor = self.isfactor()
        numlevels = self.nlevels()
        lvls = self.levels()
        print("H2OFrame: '{}' \nDimensions: {} obs. of {} variables".format(self.frame_id, nr, nc))
        for i in range(nc):
            print("$ {} {}: ".format(cn[i], ' ' * (width - max(0, len(cn[i])))), end=' ')
            if isfactor[i]:
                nl = numlevels[i]
                print("Factor w/ {} level(s) {} ".format(nl, '"' + '","'.join(lvls[i]) + '"'), end='\n')
            else:
                print("num {}".format(" ".join(it[0] if it else "nan" for it in h2o.as_list(self[:10, i], False)[1:])))

Response: Compactly display the internal structure of an H2OFrame.


Instruction:
Write a Python 3 script to
return the dataset as a python - local object.

Response:
def as_data_frame(self, use_pandas=True, header=True):
        """
        Obtain the dataset as a python-local object.

        :param bool use_pandas: If True (default) then return the H2OFrame as a pandas DataFrame (requires that the
            ``pandas`` library was installed). If False, then return the contents of the H2OFrame as plain nested
            list, in a row-wise order.
        :param bool header: If True (default), then column names will be appended as the first row in list

        :returns: A python object (a list of lists of strings, each list is a row, if use_pandas=False, otherwise
            a pandas DataFrame) containing this H2OFrame instance's data.
        """
        if can_use_pandas() and use_pandas:
            import pandas
            return pandas.read_csv(StringIO(self.get_frame_data()), low_memory=False, skip_blank_lines=False)
        from h2o.utils.csv.readers import reader
        frame = [row for row in reader(StringIO(self.get_frame_data()))]
        if not header:
            frame.pop(0)
        return frame


Instruction:
Write a Python 3 script for
dropping a single column or row from a H2OFrame.

Response:
def drop(self, index, axis=1):
        """
        Drop a single column or row or a set of columns or rows from a H2OFrame.

        Dropping a column or row is not in-place.
        Indices of rows and columns are zero-based.

        :param index: A list of column indices, column names, or row indices to drop; or
            a string to drop a single column by name; or an int to drop a single column by index.

        :param int axis: If 1 (default), then drop columns; if 0 then drop rows.

        :returns: a new H2OFrame with the respective dropped columns or rows. The original H2OFrame remains
            unchanged.
        """
        if axis == 1:
            if not isinstance(index, list):
                #If input is a string, i.e., "C1":
                if is_type(index, str):
                    #Check if index is an actual column(s) in the frame
                    if index not in self.names:
                        raise H2OValueError("Column(s) selected to drop are not in original frame: %r" % index)
                    index = self.names.index(index)
                #If input is an int indicating a column index, i.e., 3:
                elif is_type(index, int):
                    #Check if index is an actual column index in the frame
                    if index > self.ncol:
                        raise H2OValueError("Column index selected to drop is not part of the frame: %r" % index)
                    if index < 0:
                        raise H2OValueError("Column index selected to drop is not positive: %r" % index)

                fr = H2OFrame._expr(expr=ExprNode("cols", self, -(index + 1)), cache=self._ex._cache)
                fr._ex._cache.ncols -= 1
                fr._ex._cache.names = self.names[:index] + self.names[index + 1:]
                fr._ex._cache.types = {name: self.types[name] for name in fr._ex._cache.names}
                return fr

            elif isinstance(index, list):
                #If input is an int array indicating a column index, i.e., [3] or [1,2,3]:
                if is_type(index, [int]):
                    if max(index) > self.ncol:
                        raise H2OValueError("Column index selected to drop is not part of the frame: %r" % index)
                    if min(index) < 0:
                        raise H2OValueError("Column index selected to drop is not positive: %r" % index)
                    for i in range(len(index)):
                        index[i] = -(index[i] + 1)
                #If index is a string array, i.e., ["C1", "C2"]
                elif is_type(index, [str]):
                    #Check if index is an actual column(s) in the frame
                    if not set(index).issubset(self.names):
                        raise H2OValueError("Column(s) selected to drop are not in original frame: %r" % index)
                    for i in range(len(index)):
                        index[i] = -(self.names.index(index[i]) + 1)
                fr = H2OFrame._expr(expr=ExprNode("cols", self, index), cache=self._ex._cache)
                fr._ex._cache.ncols -= len(index)
                fr._ex._cache.names = [i for i in self.names
                                       if self.names.index(i) not in list(map(lambda x: abs(x) - 1, index))]
                fr._ex._cache.types = {name: fr.types[name] for name in fr._ex._cache.names}

            else:
                raise ValueError("Invalid column index types. Must either be a list of all int indexes, "
                                 "a string list of all column names, a single int index, or"
                                 "a single string for dropping columns.")
            return fr
        elif axis == 0:
            if is_type(index, [int]):
                #Check if index is an actual column index in the frame
                if max(index) > self.nrow:
                    raise H2OValueError("Row index selected to drop is not part of the frame: %r" % index)
                if min(index) < 0:
                    raise H2OValueError("Row index selected to drop is not positive: %r" % index)
                index = [-(x + 1) for x in index]
                fr = H2OFrame._expr(expr=ExprNode("rows", self, index), cache=self._ex._cache)
                fr._ex._cache.nrows -= len(index)
            else:
                raise ValueError("Invalid row indexes. Must be a list of int row indexes to drop from the H2OFrame.")
        return fr


Instruction:
Write a Python 3 function that can
pop a column from the current H2OFrame at index i.

Response:
def pop(self, i):
        """
        Pop a column from the H2OFrame at index i.

        :param i: The index (int) or name (str) of the column to pop.
        :returns: an H2OFrame containing the column dropped from the current frame; the current frame is modified
            in-place and loses the column.
        """
        if is_type(i, str): i = self.names.index(i)
        col = H2OFrame._expr(expr=ExprNode("cols", self, i))
        old_cache = self._ex._cache
        self._ex = ExprNode("cols", self, -(i + 1))
        self._ex._cache.ncols -= 1
        self._ex._cache.names = old_cache.names[:i] + old_cache.names[i + 1:]
        self._ex._cache.types = {name: old_cache.types[name] for name in self._ex._cache.names}
        self._ex._cache._data = None
        col._ex._cache.ncols = 1
        col._ex._cache.names = [old_cache.names[i]]
        return col


Instruction:
Can you generate a brief explanation for the following Python 3 code
def quantile(self, prob=None, combine_method="interpolate", weights_column=None):
        if len(self) == 0: return self
        if prob is None: prob = [0.01, 0.1, 0.25, 0.333, 0.5, 0.667, 0.75, 0.9, 0.99]
        if weights_column is None:
            weights_column = "_"
        else:
            assert_is_type(weights_column, str, I(H2OFrame, lambda wc: wc.ncol == 1 and wc.nrow == self.nrow))
            if isinstance(weights_column, H2OFrame):
                merged = self.cbind(weights_column)
                weights_column = merged.names[-1]
                return H2OFrame._expr(expr=ExprNode("quantile", merged, prob, combine_method, weights_column))
        return H2OFrame._expr(expr=ExprNode("quantile", self, prob, combine_method, weights_column))

Response: Compute quantiles for the current object.


Instruction:
How would you explain what the following Python 3 function does
def concat(self, frames, axis=1):
        if len(frames) == 0:
            raise ValueError("Input list of frames is empty! Nothing to concat.")

        if axis == 1:
            df = self.cbind(frames)
        else:
            df = self.rbind(frames)
        return df

Response: Concatenate multiple H2OFrames to this frame column - wise or row - wise.


Instruction:
How would you explain what the following Python 3 function does
def cbind(self, data):
        assert_is_type(data, H2OFrame, numeric, [H2OFrame, numeric])
        frames = [data] if not isinstance(data, list) else data
        new_cols = list(self.columns)
        new_types = dict(self.types)
        for frame in frames:
            if isinstance(frame, H2OFrame):
                if frame.nrow != self.nrow:
                    raise H2OValueError("Cannot bind a dataframe with %d rows to a data frame with %d rows: "
                                        "the number of rows should match" % (frame.nrow, self.nrow))
                new_cols += frame.columns
                new_types.update(frame.types)
            else:
                new_cols += [None]
        unique_cols = set(new_cols)
        fr = H2OFrame._expr(expr=ExprNode("cbind", self, *frames), cache=self._ex._cache)
        fr._ex._cache.ncols = len(new_cols)
        if len(new_cols) == len(unique_cols) and None not in unique_cols:
            fr._ex._cache.names = new_cols
            fr._ex._cache.types = new_types
        else:
            # Invalidate names and types since they contain duplicate / unknown names, and the server will choose those.
            fr._ex._cache.names = None
            fr._ex._cache.types = None
        return fr

Response: Return a new H2OFrame with all frames in data appended column - wise.


Instruction:
Can you tell what is the following Python 3 function doing
def rbind(self, data):
        assert_is_type(data, H2OFrame, [H2OFrame])
        frames = [data] if not isinstance(data, list) else data
        for frame in frames:
            if frame.ncol != self.ncol:
                raise H2OValueError("Cannot row-bind a dataframe with %d columns to a data frame with %d columns: "
                                    "the columns must match" % (frame.ncol, self.ncol))
            if frame.columns != self.columns or frame.types != self.types:
                raise H2OValueError("Column names and types must match for rbind() to work")
        fr = H2OFrame._expr(expr=ExprNode("rbind", self, *frames), cache=self._ex._cache)
        fr._ex._cache.nrows = self.nrow + sum(frame.nrow for frame in frames)
        return fr

Response: Append data to this H2OFrame with all frames in data appended row - wise.


Instruction:
How would you explain what the following Python 3 function does
def split_frame(self, ratios=None, destination_frames=None, seed=None):
        assert_is_type(ratios, [numeric], None)
        assert_is_type(destination_frames, [str], None)
        assert_is_type(seed, int, None)

        if ratios is None:
            ratios = [0.75]
        if not ratios:
            raise ValueError("Ratios array may not be empty")

        if destination_frames is not None:
            if len(ratios) + 1 != len(destination_frames):
                raise ValueError("The number of provided destination_frames must be one more "
                                 "than the number of provided ratios")

        num_slices = len(ratios) + 1
        boundaries = []

        last_boundary = 0
        i = 0
        while i < num_slices - 1:
            ratio = ratios[i]
            if ratio < 0:
                raise ValueError("Ratio must be greater than 0")
            boundary = last_boundary + ratio
            if boundary >= 1.0:
                raise ValueError("Ratios must add up to less than 1.0")
            boundaries.append(boundary)
            last_boundary = boundary
            i += 1

        splits = []
        tmp_runif = self.runif(seed)
        tmp_runif.frame_id = "%s_splitter" % _py_tmp_key(h2o.connection().session_id)

        i = 0
        while i < num_slices:
            if i == 0:
                # lower_boundary is 0.0
                upper_boundary = boundaries[i]
                tmp_slice = self[(tmp_runif <= upper_boundary), :]
            elif i == num_slices - 1:
                lower_boundary = boundaries[i - 1]
                # upper_boundary is 1.0
                tmp_slice = self[(tmp_runif > lower_boundary), :]
            else:
                lower_boundary = boundaries[i - 1]
                upper_boundary = boundaries[i]
                tmp_slice = self[((tmp_runif > lower_boundary) & (tmp_runif <= upper_boundary)), :]

            if destination_frames is None:
                splits.append(tmp_slice)
            else:
                destination_frame_id = destination_frames[i]
                tmp_slice.frame_id = destination_frame_id
                splits.append(tmp_slice)

            i += 1

        del tmp_runif
        return splits

Response: Splits a single frame into distinct subsets of size determined by the given ratios.


Instruction:
Explain what the following Python 3 code does
def group_by(self, by):
        assert_is_type(by, str, int, [str, int])
        return GroupBy(self, by)

Response: Return a new GroupBy object using this frame and the desired grouping columns.


Instruction:
Write a Python 3 script for
sorting the Frame by a list of column names and indices.

Response:
def sort(self, by, ascending=[]):
        """
        Return a new Frame that is sorted by column(s) in ascending order. A fully distributed and parallel sort.
        However, the original frame can contain String columns but sorting cannot be done on String columns.
        Default sorting direction is ascending.

        :param by: The column to sort by (either a single column name, or a list of column names, or
            a list of column indices)
        :param ascending: Boolean array to denote sorting direction for each sorting column.  True for ascending
            sort and False for descending sort.

        :return:  a new sorted Frame
        """
        assert_is_type(by, str, int, [str, int])
        if type(by) != list: by = [by]
        if type(ascending) != list: ascending = [ascending]   # convert to list
        ascendingI=[1]*len(by)  # intitalize sorting direction to ascending by default
        for c in by:
            if self.type(c) not in ["enum","time","int","real","string"]:
                raise H2OValueError("Sort by column: " + str(c) + " not of enum, time, int, real, or string type")
        if len(ascending)>0:  # user did not specify sort direction, assume all columns ascending
            assert len(ascending)==len(by), "Sorting direction must be specified for each sorted column."
            for index in range(len(by)):
                ascendingI[index]=1 if ascending[index] else -1
        return H2OFrame._expr(expr=ExprNode("sort",self,by,ascendingI))


Instruction:
Can you write a function in Python 3 where it
returns a new Frame that fills NA along a given axis with a maximum fill length.

Response:
def fillna(self,method="forward",axis=0,maxlen=1):
        """
        Return a new Frame that fills NA along a given axis and along a given direction with a maximum fill length

        :param method: ``"forward"`` or ``"backward"``
        :param axis:  0 for columnar-wise or 1 for row-wise fill
        :param maxlen: Max number of consecutive NA's to fill

        :return:
        """
        assert_is_type(axis, 0, 1)
        assert_is_type(method,str)
        assert_is_type(maxlen, int)
        return H2OFrame._expr(expr=ExprNode("h2o.fillna",self,method,axis,maxlen))


Instruction:
Write a Python 3 script for
imputing missing values into the frame.

Response:
def impute(self, column=-1, method="mean", combine_method="interpolate", by=None, group_by_frame=None, values=None):
        """
        Impute missing values into the frame, modifying it in-place.

        :param int column: Index of the column to impute, or -1 to impute the entire frame.
        :param str method: The method of imputation: ``"mean"``, ``"median"``, or ``"mode"``.
        :param str combine_method: When the method is ``"median"``, this setting dictates how to combine quantiles
            for even samples. One of ``"interpolate"``, ``"average"``, ``"low"``, ``"high"``.
        :param by: The list of columns to group on.
        :param H2OFrame group_by_frame: Impute the values with this pre-computed grouped frame.
        :param List values: The list of impute values, one per column. None indicates to skip the column.

        :returns: A list of values used in the imputation or the group-by result used in imputation.
        """
        if is_type(column, str): column = self.names.index(column)
        if is_type(by, str):     by = self.names.index(by)

        if values is None:
            values = "_"
        else:
            assert len(values) == len(self.columns), "Length of values does not match length of columns"
            # convert string values to categorical num values
            values2 = []
            for i in range(0,len(values)):
                if self.type(i) == "enum":
                    try:
                        values2.append(self.levels()[i].index(values[i]))
                    except:
                        raise H2OValueError("Impute value of: " + values[i] + " not found in existing levels of"
                                            " column: " + self.col_names[i])
                else:
                    values2.append(values[i])
            values = values2
        if group_by_frame is None: group_by_frame = "_"

        # This code below is needed to ensure the frame (self) exists on the server. Without it, self._ex._cache.fill()
        # fails with an assertion that ._id is None.
        # This code should be removed / reworked once we have a more consistent strategy of dealing with frames.
        self._ex._eager_frame()

        if by is not None or group_by_frame is not "_":
            res = H2OFrame._expr(
                expr=ExprNode("h2o.impute", self, column, method, combine_method, by, group_by_frame, values))._frame()
        else:
            res = ExprNode("h2o.impute", self, column, method, combine_method, by, group_by_frame,
                           values)._eager_scalar()

        self._ex._cache.flush()
        self._ex._cache.fill(10)
        return res


Instruction:
Can you tell what is the following Python 3 function doing
def merge(self, other, all_x=False, all_y=False, by_x=None, by_y=None, method="auto"):

        if by_x is None and by_y is None:
            common_names = list(set(self.names) & set(other.names))
            if not common_names:
                raise H2OValueError("No columns in common to merge on!")

        if by_x is None:
            by_x = [self.names.index(c) for c in common_names]
        else:
            by_x = _getValidCols(by_x,self)

        if by_y is None:
            by_y = [other.names.index(c) for c in common_names]
        else:
            by_y = _getValidCols(by_y,other)

        return H2OFrame._expr(expr=ExprNode("merge", self, other, all_x, all_y, by_x, by_y, method))

Response:
Merge two datasets based on common column names.  We do not support all_x=True and all_y=True.
        Only one can be True or none is True.  The default merge method is auto and it will default to the
        radix method.  The radix method will return the correct merge result regardless of duplicated rows
         in the right frame.  In addition, the radix method can perform merge even if you have string columns
         in your frames.  If there are duplicated rows in your rite frame, they will not be included if you use
        the hash method.  The hash method cannot perform merge if you have string columns in your left frame.
        Hence, we consider the radix method superior to the hash method and is the default method to use.

        :param H2OFrame other: The frame to merge to the current one. By default, must have at least one column in common with
            this frame, and all columns in common are used as the merge key.  If you want to use only a subset of the
            columns in common, rename the other columns so the columns are unique in the merged result.
        :param bool all_x: If True, include all rows from the left/self frame
        :param bool all_y: If True, include all rows from the right/other frame
        :param by_x: list of columns in the current frame to use as a merge key.
        :param by_y: list of columns in the ``other`` frame to use as a merge key. Should have the same number of
            columns as in the ``by_x`` list.
        :param method: string representing the merge method, one of auto(default), radix or hash.

        :returns: New H2OFrame with the result of merging the current frame with the ``other`` frame.


Instruction:
How would you code a function in Python 3 to
reorder the levels of a single column of a H2O factor for one single column of a H2O frame.

Response:
def relevel(self, y):
        """
        Reorder levels of an H2O factor for one single column of a H2O frame

        The levels of a factor are reordered such that the reference level is at level 0, all remaining levels are
        moved down as needed.

        :param str y: The reference level
        :returns: New reordered factor column
        """
        return H2OFrame._expr(expr=ExprNode("relevel", self, quote(y)))


Instruction:
Write a Python 3 function for
inserting missing values into the current frame.

Response:
def insert_missing_values(self, fraction=0.1, seed=None):
        """
        Insert missing values into the current frame, modifying it in-place.

        Randomly replaces a user-specified fraction of entries in a H2O dataset with missing
        values.

        :param float fraction: A number between 0 and 1 indicating the fraction of entries to replace with missing.
        :param int seed: The seed for the random number generator used to determine which values to make missing.

        :returns: the original H2OFrame with missing values inserted.
        """
        kwargs = {}
        kwargs['dataset'] = self.frame_id  # Eager; forces eval now for following REST call
        kwargs['fraction'] = fraction
        if seed is not None: kwargs['seed'] = seed
        job = {}
        job['job'] = h2o.api("POST /3/MissingInserter", data=kwargs)
        H2OJob(job, job_type=("Insert Missing Values")).poll()
        self._ex._cache.flush()
        return self


Instruction:
Here you have a function in Python 3, explain what it does
def sum(self, skipna=True, axis=0, **kwargs):
        assert_is_type(skipna, bool)
        assert_is_type(axis, 0, 1)
        # Deprecated since 2016-10-14,
        if "na_rm" in kwargs:
            warnings.warn("Parameter na_rm is deprecated; use skipna instead", category=DeprecationWarning)
            na_rm = kwargs.pop("na_rm")
            assert_is_type(na_rm, bool)
            skipna = na_rm  # don't assign to skipna directly, to help with error reporting
        # Determine whether to return a frame or a list
        return_frame = get_config_value("general.allow_breaking_changes", False)
        if "return_frame" in kwargs:
            return_frame = kwargs.pop("return_frame")
            assert_is_type(return_frame, bool)
        if kwargs:
            raise H2OValueError("Unknown parameters %r" % list(kwargs))

        if return_frame:
            return H2OFrame._expr(ExprNode("sumaxis", self, skipna, axis))
        else:
            return ExprNode("sumNA" if skipna else "sum", self)._eager_scalar()

Response: Compute the sum of the presence of the entries in the current H2O frame.


Instruction:
Can you generate the documentation for the following Python 3 function
def var(self, y=None, na_rm=False, use=None):
        symmetric = False
        if y is None:
            y = self
            symmetric = True
        if use is None: use = "complete.obs" if na_rm else "everything"
        if self.nrow == 1 or (self.ncol == 1 and y.ncol == 1):
            return ExprNode("var", self, y, use, symmetric)._eager_scalar()
        return H2OFrame._expr(expr=ExprNode("var", self, y, use, symmetric))._frame()

Response: Compute the variance - covariance matrix of one or two H2OFrames.


Instruction:
Can you generate the documentation for the following Python 3 function
def cor(self, y=None, na_rm=False, use=None):
        assert_is_type(y, H2OFrame, None)
        assert_is_type(na_rm, bool)
        assert_is_type(use, None, "everything", "all.obs", "complete.obs")
        if y is None:
            y = self
        if use is None: use = "complete.obs" if na_rm else "everything"
        if self.nrow == 1 or (self.ncol == 1 and y.ncol == 1): return ExprNode("cor", self, y, use)._eager_scalar()
        return H2OFrame._expr(expr=ExprNode("cor", self, y, use))._frame()

Response: Compute the correlation matrix between two columns of the current frame and the columns of y.


Instruction:
Implement a Python 3 function for
computing a pairwise distance measure between all rows of this H2OFrame and y.

Response:
def distance(self, y, measure=None):
        """
        Compute a pairwise distance measure between all rows of two numeric H2OFrames.

        :param H2OFrame y: Frame containing queries (small)
        :param str use: A string indicating what distance measure to use. Must be one of:

            - ``"l1"``:        Absolute distance (L1-norm, >=0)
            - ``"l2"``:        Euclidean distance (L2-norm, >=0)
            - ``"cosine"``:    Cosine similarity (-1...1)
            - ``"cosine_sq"``: Squared Cosine similarity (0...1)

        :examples:
          >>>
          >>> iris_h2o = h2o.import_file(path=pyunit_utils.locate("smalldata/iris/iris.csv"))
          >>> references = iris_h2o[10:150,0:4
          >>> queries    = iris_h2o[0:10,0:4]
          >>> A = references.distance(queries, "l1")
          >>> B = references.distance(queries, "l2")
          >>> C = references.distance(queries, "cosine")
          >>> D = references.distance(queries, "cosine_sq")
          >>> E = queries.distance(references, "l1")
          >>> (E.transpose() == A).all()

        :returns: An H2OFrame of the matrix containing pairwise distance / similarity between the
            rows of this frame (N x p) and ``y`` (M x p), with dimensions (N x M).
        """
        assert_is_type(y, H2OFrame)
        if measure is None: measure = "l2"
        return H2OFrame._expr(expr=ExprNode("distance", self, y, measure))._frame()


Instruction:
Explain what the following Python 3 code does
def strdistance(self, y, measure=None, compare_empty=True):
        assert_is_type(y, H2OFrame)
        assert_is_type(measure, Enum('lv', 'lcs', 'qgram', 'jaccard', 'jw', 'soundex'))
        assert_is_type(compare_empty, bool)
        return H2OFrame._expr(expr=ExprNode("strDistance", self, y, measure, compare_empty))._frame()

Response: Compute element - wise string distances between two H2OFrames.


Instruction:
Explain what the following Python 3 code does
def asfactor(self):
        for colname in self.names:
            t = self.types[colname]
            if t not in {"bool", "int", "string", "enum"}:
                raise H2OValueError("Only 'int' or 'string' are allowed for "
                                    "asfactor(), got %s:%s " % (colname, t))
        fr = H2OFrame._expr(expr=ExprNode("as.factor", self), cache=self._ex._cache)
        if fr._ex._cache.types_valid():
            fr._ex._cache.types = {name: "enum" for name in self.types}
        else:
            raise H2OTypeError("Types are not available in result")

        return fr

Response: Convert columns in the current frame to categoricals.


Instruction:
Given the following Python 3 function, write the documentation
def categories(self):
        if self.ncols != 1:
            raise H2OValueError("This operation only applies to a single factor column")
        if self.types[self.names[0]] != "enum":
            raise H2OValueError("Input is not a factor. This operation only applies to a single factor column")
        return self.levels()[0]

Response: Return the list of categories for an enum categorical column. This function can only be applied to single - column categorical frame.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def strsplit(self, pattern):
        fr = H2OFrame._expr(expr=ExprNode("strsplit", self, pattern))
        fr._ex._cache.nrows = self.nrow
        return fr

Response: Splits the strings in the target column on the given regular expression pattern.


Instruction:
Can you generate the documentation for the following Python 3 function
def countmatches(self, pattern):
        assert_is_type(pattern, str, [str])
        fr = H2OFrame._expr(expr=ExprNode("countmatches", self, pattern))
        fr._ex._cache.nrows = self.nrow
        fr._ex._cache.ncols = self.ncol
        return fr

Response: Count the number of occurrences of the pattern in each string in the frame.


Instruction:
Here you have a function in Python 3, explain what it does
def substring(self, start_index, end_index=None):
        fr = H2OFrame._expr(expr=ExprNode("substring", self, start_index, end_index))
        fr._ex._cache.nrows = self.nrow
        fr._ex._cache.ncol = self.ncol
        return fr

Response: Returns a new string that is a substring of the original string.


Instruction:
Can you implement a function in Python 3 that
returns a new H2OFrame with all leading characters removed from the left of the column.

Response:
def lstrip(self, set=" "):
        """
        Return a copy of the column with leading characters removed.

        The set argument is a string specifying the set of characters to be removed.
        If omitted, the set argument defaults to removing whitespace.

        :param character set: The set of characters to lstrip from strings in column.
        :returns: a new H2OFrame with the same shape as the original frame and having all its values
            trimmed from the left (equivalent of Python's ``str.lstrip()``).
        """
        # work w/ None; parity with python lstrip
        if set is None: set = " "

        fr = H2OFrame._expr(expr=ExprNode("lstrip", self, set))
        fr._ex._cache.nrows = self.nrow
        fr._ex._cache.ncol = self.ncol
        return fr


Instruction:
Can you generate the documentation for the following Python 3 function
def entropy(self):
        fr = H2OFrame._expr(expr=ExprNode("entropy", self))
        fr._ex._cache.nrows = self.nrow
        fr._ex._cache.ncol = self.ncol
        return fr

Response: Compute the Shannon entropy for each string.


Instruction:
Make a summary of the following Python 3 code
def num_valid_substrings(self, path_to_words):
        assert_is_type(path_to_words, str)
        fr = H2OFrame._expr(expr=ExprNode("num_valid_substrings", self, path_to_words))
        fr._ex._cache.nrows = self.nrow
        fr._ex._cache.ncol = self.ncol
        return fr

Response: Returns the number of substrings that are contained in the given line - separated text file.


Instruction:
Explain what the following Python 3 code does
def table(self, data2=None, dense=True):
        return H2OFrame._expr(expr=ExprNode("table", self, data2, dense)) if data2 is not None else H2OFrame._expr(
            expr=ExprNode("table", self, dense))

Response: Compute the counts of values appearing in a column or co - occupurence counts between two columns.


Instruction:
Can you write a function in Python 3 where it
computes a histogram over a numeric column.

Response:
def hist(self, breaks="sturges", plot=True, **kwargs):
        """
        Compute a histogram over a numeric column.

        :param breaks: Can be one of ``"sturges"``, ``"rice"``, ``"sqrt"``, ``"doane"``, ``"fd"``, ``"scott"``;
            or a single number for the number of breaks; or a list containing the split points, e.g:
            ``[-50, 213.2123, 9324834]``. If breaks is "fd", the MAD is used over the IQR in computing bin width.
        :param bool plot: If True (default), then a plot will be generated using ``matplotlib``.

        :returns: If ``plot`` is False, return H2OFrame with these columns: breaks, counts, mids_true,
            mids, and density; otherwise this method draws a plot and returns nothing.
        """
        server = kwargs.pop("server") if "server" in kwargs else False
        assert_is_type(breaks, int, [numeric], Enum("sturges", "rice", "sqrt", "doane", "fd", "scott"))
        assert_is_type(plot, bool)
        assert_is_type(server, bool)
        if kwargs:
            raise H2OValueError("Unknown parameters to hist(): %r" % kwargs)
        hist = H2OFrame._expr(expr=ExprNode("hist", self, breaks))._frame()

        if plot:
            try:
                import matplotlib
                if server:
                    matplotlib.use("Agg", warn=False)
                import matplotlib.pyplot as plt
            except ImportError:
                print("ERROR: matplotlib is required to make the histogram plot. "
                      "Set `plot` to False, if a plot is not desired.")
                return

            hist["widths"] = hist["breaks"].difflag1()
            # [2:] because we're removing the title and the first row (which consists of NaNs)
            lefts = [float(c[0]) for c in h2o.as_list(hist["breaks"], use_pandas=False)[2:]]
            widths = [float(c[0]) for c in h2o.as_list(hist["widths"], use_pandas=False)[2:]]
            counts = [float(c[0]) for c in h2o.as_list(hist["counts"], use_pandas=False)[2:]]

            plt.xlabel(self.names[0])
            plt.ylabel("Frequency")
            plt.title("Histogram of %s" % self.names[0])
            plt.bar(left=lefts, width=widths, height=counts, bottom=0)
            if not server:
                plt.show()
        else:
            hist["density"] = hist["counts"] / (hist["breaks"].difflag1() * hist["counts"].sum())
            return hist