class H2OConnection(backwards_compatible()): """ Connection handle to an H2O cluster. In a typical scenario you don't need to access this class directly. Instead use :func:`h2o.connect` to establish a connection, and :func:`h2o.api` to make requests to the backend H2O server. However if your use-case is not typical, then read on. Instances of this class may only be created through a static method :meth:`open`:: hc = H2OConnection.open(...) Once opened, the connection remains active until the script exits (or until you explicitly :meth:`close` it). If the script exits with an exception, then the connection will fail to close, and the backend server will keep all the temporary frames and the open session. Alternatively you can use this class as a context manager, which will ensure that the connection gets closed at the end of the ``with ...`` block even if an exception occurs:: with H2OConnection.open() as hc: hc.info().pprint() Once the connection is established, you can send REST API requests to the server using :meth:`request`. """ @staticmethod def open(server=None, url=None, ip=None, port=None, https=None, auth=None, verify_ssl_certificates=True, proxy=None, cluster_name=None, verbose=True): r""" Establish connection to an existing H2O server. The connection is not kept alive, so what this method actually does is it attempts to connect to the specified server, and checks that the server is healthy and responds to REST API requests. If the H2O server cannot be reached, an :class:`H2OConnectionError` will be raised. On success this method returns a new :class:`H2OConnection` object, and it is the only "official" way to create instances of this class. There are 3 ways to specify the target to connect to (these settings are mutually exclusive): * pass a ``server`` option, * pass the full ``url`` for the connection, * provide a triple of parameters ``ip``, ``port``, ``https``. :param H2OLocalServer server: connect to the specified local server instance. There is a slight difference between connecting to a local server by specifying its ip and address, and connecting through an H2OLocalServer instance: if the server becomes unresponsive, then having access to its process handle will allow us to query the server status through OS, and potentially provide snapshot of the server's error log in the exception information. :param url: full url of the server to connect to. :param ip: target server's IP address or hostname (default "localhost"). :param port: H2O server's port (default 54321). :param https: if True then connect using https instead of http (default False). :param verify_ssl_certificates: if False then SSL certificate checking will be disabled (default True). This setting should rarely be disabled, as it makes your connection vulnerable to man-in-the-middle attacks. When used, it will generate a warning from the requests library. Has no effect when ``https`` is False. :param auth: authentication token for connecting to the remote server. This can be either a (username, password) tuple, or an authenticator (AuthBase) object. Please refer to the documentation in the ``requests.auth`` module. :param proxy: url address of a proxy server. If you do not specify the proxy, then the requests module will attempt to use a proxy specified in the environment (in HTTP_PROXY / HTTPS_PROXY variables). We check for the presence of these variables and issue a warning if they are found. In order to suppress that warning and use proxy from the environment, pass ``proxy="(default)"``. :param cluster_name: name of the H2O cluster to connect to. This option is used from Steam only. :param verbose: if True, then connection progress info will be printed to the stdout. :returns: A new :class:`H2OConnection` instance. :raises H2OConnectionError: if the server cannot be reached. :raises H2OServerError: if the server is in an unhealthy state (although this might be a recoverable error, the client itself should decide whether it wants to retry or not). """ if server is not None: assert_is_type(server, H2OLocalServer) assert_is_type( ip, None, "`ip` should be None when `server` parameter is supplied") assert_is_type( url, None, "`ip` should be None when `server` parameter is supplied") if not server.is_running(): raise H2OConnectionError( "Unable to connect to server because it is not running") ip = server.ip port = server.port scheme = server.scheme elif url is not None: assert_is_type(url, str) assert_is_type( ip, None, "`ip` should be None when `url` parameter is supplied") # We don't allow any Unicode characters in the URL. Maybe some day we will... match = assert_matches( url, r"^(https?)://((?:[\w-]+\.)*[\w-]+):(\d+)/?$") scheme = match.group(1) ip = match.group(2) port = int(match.group(3)) else: if ip is None: ip = str("localhost") if port is None: port = 54321 if https is None: https = False if is_str(port) and port.isdigit(): port = int(port) assert_is_type(ip, str) assert_is_type(port, int) assert_is_type(https, bool) assert_matches(ip, r"(?:[\w-]+\.)*[\w-]+") assert_satisfies(port, 1 <= port <= 65535) scheme = "https" if https else "http" if verify_ssl_certificates is None: verify_ssl_certificates = True assert_is_type(verify_ssl_certificates, bool) assert_is_type(proxy, str, None) assert_is_type(auth, AuthBase, (str, str), None) assert_is_type(cluster_name, str, None) conn = H2OConnection() conn._verbose = bool(verbose) conn._local_server = server conn._base_url = "%s://%s:%d" % (scheme, ip, port) conn._verify_ssl_cert = bool(verify_ssl_certificates) conn._auth = auth conn._cluster_name = cluster_name conn._proxies = None if proxy and proxy != "(default)": conn._proxies = {scheme: proxy} elif not proxy: # Give user a warning if there are any "*_proxy" variables in the environment. [PUBDEV-2504] # To suppress the warning pass proxy = "(default)". for name in os.environ: if name.lower() == scheme + "_proxy": warn("Proxy is defined in the environment: %s. " "This may interfere with your H2O Connection." % os.environ[name]) try: # Make a fake _session_id, otherwise .request() will complain that the connection is not initialized retries = 20 if server else 5 conn._stage = 1 conn._timeout = 3.0 conn._cluster_info = conn._test_connection(retries) # If a server is unable to respond within 1s, it should be considered a bug. However we disable this # setting for now, for no good reason other than to ignore all those bugs :( conn._timeout = None atexit.register(lambda: conn.close()) except: # Reset _session_id so that we know the connection was not initialized properly. conn._stage = 0 raise return conn def request(self, endpoint, data=None, json=None, filename=None): """ Perform a REST API request to the backend H2O server. :param endpoint: (str) The endpoint's URL, for example "GET /4/schemas/KeyV4" :param data: data payload for POST (and sometimes GET) requests. This should be a dictionary of simple key/value pairs (values can also be arrays), which will be sent over in x-www-form-encoded format. :param json: also data payload, but it will be sent as a JSON body. Cannot be used together with `data`. :param filename: file to upload to the server. Cannot be used with `data` or `json`. :returns: an H2OResponse object representing the server's response :raises H2OConnectionError: if the H2O server cannot be reached (or connection is not initialized) :raises H2OServerError: if there was a server error (http 500), or server returned malformed JSON :raises H2OResponseError: if the server returned an H2OErrorV3 response (e.g. if the parameters were invalid) """ if self._stage == 0: raise H2OConnectionError( "Connection not initialized; run .connect() first.") if self._stage == -1: raise H2OConnectionError( "Connection was closed, and can no longer be used.") # Prepare URL assert_is_type(endpoint, str) match = assert_matches(str(endpoint), r"^(GET|POST|PUT|DELETE|PATCH|HEAD) (/.*)$") method = match.group(1) urltail = match.group(2) url = self._base_url + urltail # Prepare data if filename is not None: assert_is_type(filename, str) assert_is_type( json, None, "Argument `json` should be None when `filename` is used.") assert_is_type( data, None, "Argument `data` should be None when `filename` is used.") assert_satisfies( method, method == "POST", "File uploads can only be done via POST method, got %s" % method) elif data is not None: assert_is_type(data, dict) assert_is_type( json, None, "Argument `json` should be None when `data` is used.") elif json is not None: assert_is_type(json, dict) data = self._prepare_data_payload(data) files = self._prepare_file_payload(filename) params = None if method == "GET" and data: params = data data = None # Make the request start_time = time.time() try: self._log_start_transaction(endpoint, data, json, files, params) headers = { "User-Agent": "H2O Python client/" + sys.version.replace("\n", ""), "X-Cluster": self._cluster_name } resp = requests.request(method=method, url=url, data=data, json=json, files=files, params=params, headers=headers, timeout=self._timeout, auth=self._auth, verify=self._verify_ssl_cert, proxies=self._proxies) self._log_end_transaction(start_time, resp) return self._process_response(resp) except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: if self._local_server and not self._local_server.is_running(): self._log_end_exception("Local server has died.") raise H2OConnectionError( "Local server has died unexpectedly. RIP.") else: self._log_end_exception(e) raise H2OConnectionError("Unexpected HTTP error: %s" % e) except requests.exceptions.Timeout as e: self._log_end_exception(e) elapsed_time = time.time() - start_time raise H2OConnectionError("Timeout after %.3fs" % elapsed_time) except H2OResponseError as e: err = e.args[0] err.endpoint = endpoint err.payload = (data, json, files, params) raise def info(self, refresh=False): """ Information about the current state of the connection, or None if it has not been initialized properly. :param refresh: If False, then retrieve the latest known info; if True then fetch the newest info from the server. Usually you want `refresh` to be True, except right after establishing a connection when it is still fresh. :return: H2OCluster object. """ if self._stage == 0: return None if refresh: self._cluster_info = self.request("GET /3/Cloud") self._cluster_info.connection = self return self._cluster_info def close(self): """ Close an existing connection; once closed it cannot be used again. Strictly speaking it is not necessary to close all connection that you opened -- we have several mechanisms in place that will do so automatically (__del__(), __exit__() and atexit() handlers), however there is also no good reason to make this method private. """ if self._session_id: try: self.request("DELETE /4/sessions/%s" % self._session_id) self._print("H2O session %s closed." % self._session_id) except: pass self._session_id = None self._stage = -1 @property def session_id(self): """ Return the session id of the current connection. The session id is issued (through an API request) the first time it is requested, but no sooner. This is because generating a session id puts it into the DKV on the server, which effectively locks the cloud. Once issued, the session id will stay the same until the connection is closed. """ if self._session_id is None: self._session_id = self.request("POST /4/sessions")["session_key"] return CallableString(self._session_id) @property def base_url(self): """Base URL of the server, without trailing ``"/"``. For example: ``"https://example.com:54321"``.""" return self._base_url @property def proxy(self): """URL of the proxy server used for the connection (or None if there is no proxy).""" if self._proxies is None: return None return self._proxies.values()[0] @property def requests_count(self): """Total number of request requests made since the connection was opened (used for debug purposes).""" return self._requests_counter @property def timeout_interval(self): """Timeout length for each request, in seconds.""" return self._timeout @timeout_interval.setter def timeout_interval(self, v): assert_maybe_numeric(v) self._timeout = v def shutdown_server(self, prompt): """ Shut down the specified server. This method checks if H2O is running at the specified IP address and port, and if it is, shuts down that H2O instance. All data will be lost. :param prompt: A logical value indicating whether to prompt the user before shutting down the H2O server. """ if not self.cluster_is_up(): return assert_is_type(prompt, bool) if prompt: question = "Are you sure you want to shutdown the H2O instance running at %s (Y/N)? " % self._base_url response = input( question ) # works in Py2 & Py3 because redefined in h2o.utils.compatibility module else: response = "Y" if response.lower() in {"y", "yes"}: self.request("POST /3/Shutdown") self.close() def cluster_is_up(self): """ Determine if an H2O cluster is running or not. :returns: True if the cluster is up; False otherwise """ try: if self._local_server and not self._local_server.is_running(): return False self.request("GET /") return True except (H2OConnectionError, H2OServerError): return False def start_logging(self, dest=None): """ Start logging all API requests to the provided destination. :param dest: Where to write the log: either a filename (str), or an open file handle (file). If not given, then a new temporary file will be created. """ assert_is_type(dest, None, str, type(sys.stdout)) if dest is None: dest = os.path.join(tempfile.mkdtemp(), "h2o-connection.log") self._print("Now logging all API requests to file %r" % dest) self._is_logging = True self._logging_dest = dest def stop_logging(self): """Stop logging API requests.""" if self._is_logging: self._print("Logging stopped.") self._is_logging = False #------------------------------------------------------------------------------------------------------------------- # PRIVATE #------------------------------------------------------------------------------------------------------------------- def __init__(self): """[Private] Please use H2OConnection.connect() to create H2OConnection objects.""" super(H2OConnection, self).__init__() globals( )["__H2OCONN__"] = self # for backward-compatibility: __H2OCONN__ is the latest instantiated object self._stage = 0 # 0 = not connected, 1 = connected, -1 = disconnected self._session_id = None # Rapids session id; issued upon request only self._base_url = None # "{scheme}://{ip}:{port}" self._verify_ssl_cert = None self._auth = None # Authentication token self._proxies = None # `proxies` dictionary in the format required by the requests module self._cluster_name = None self._cluster_info = None # Latest result of "GET /3/Cloud" request self._verbose = None # Print detailed information about connection status self._requests_counter = 0 # how many API requests were made self._timeout = None # timeout for a single request (in seconds) self._is_logging = False # when True, log every request self._logging_dest = None # where the log messages will be written, either filename or open file handle self._local_server = None # H2OLocalServer instance to which we are connected (if known) # self.start_logging(sys.stdout) def _test_connection(self, max_retries=5): """ Test that the H2O cluster can be reached, and retrieve basic cloud status info. :param max_retries: Number of times to try to connect to the cloud (with 0.2s intervals) :return Cloud information (an H2OCluster object) :raise H2OConnectionError, H2OServerError """ self._print("Connecting to H2O server at " + self._base_url, end="..") cld = None errors = [] for _ in range(max_retries): self._print(".", end="", flush=True) if self._local_server and not self._local_server.is_running(): raise H2OServerError("Local server was unable to start") try: cld = self.request("GET /3/Cloud") if cld.consensus and cld.cloud_healthy: self._print(" successful!") return cld else: if cld.consensus and not cld.cloud_healthy: msg = "in consensus but not healthy" elif not cld.consensus and cld.cloud_healthy: msg = "not in consensus but healthy" else: msg = "not in consensus and not healthy" errors.append( "Cloud is in a bad shape: %s (size = %d, bad nodes = %d)" % (msg, cld.cloud_size, cld.bad_nodes)) except (H2OConnectionError, H2OServerError) as e: message = str(e) if "\n" in message: message = message[:message.index("\n")] errors.append("[%s.%02d] %s: %s" % (time.strftime("%M:%S"), int(time.time() * 100) % 100, e.__class__.__name__, message)) # Cloud too small, or voting in progress, or server is not up yet; sleep then try again time.sleep(0.2) self._print(" failed.") if cld and not cld.cloud_healthy: raise H2OServerError("Cluster reports unhealthy status") if cld and not cld.consensus: raise H2OServerError("Cluster cannot reach consensus") else: raise H2OConnectionError( "Could not establish link to the H2O cloud %s after %d retries\n%s" % (self._base_url, max_retries, "\n".join(errors))) @staticmethod def _prepare_data_payload(data): """ Make a copy of the `data` object, preparing it to be sent to the server. The data will be sent via x-www-form-urlencoded or multipart/form-data mechanisms. Both of them work with plain lists of key/value pairs, so this method converts the data into such format. """ if not data: return None res = {} for key, value in viewitems(data): if value is None: continue # don't send args set to None so backend defaults take precedence if isinstance(value, list): value = stringify_list(value) elif isinstance(value, dict) and "__meta" in value and value[ "__meta"]["schema_name"].endswith("KeyV3"): value = value["name"] else: value = str(value) # Some hackery here... It appears that requests library cannot stomach "upgraded" strings if they contain # certain characters such as '/'. Therefore we explicitly cast them to their native representation. # Reproduction steps: # >>> import requests # >>> from future.types import newstr as str # >>> requests.get("http://www.google.com/search", params={"q": str("/foo/bar")}) # (throws a "KeyError 47" exception). # if PY2 and hasattr(value, "__native__"): value = value.__native__() # if PY2 and hasattr(key, "__native__"): key = key.__native__() res[key] = value return res @staticmethod def _prepare_file_payload(filename): """ Prepare `filename` to be sent to the server. The "preparation" consists of creating a data structure suitable for passing to requests.request(). """ if not filename: return None absfilename = os.path.abspath(filename) if not os.path.exists(absfilename): raise H2OValueError("File %s does not exist" % filename, skip_frames=1) return {os.path.basename(absfilename): open(absfilename, "rb")} def _log_start_transaction(self, endpoint, data, json, files, params): """Log the beginning of an API request.""" # TODO: add information about the caller, i.e. which module + line of code called the .request() method # This can be done by fetching current traceback and then traversing it until we find the request function self._requests_counter += 1 if not self._is_logging: return msg = "\n---- %d --------------------------------------------------------\n" % self._requests_counter msg += "[%s] %s\n" % (time.strftime("%H:%M:%S"), endpoint) if params is not None: msg += " params: {%s}\n" % ", ".join( "%s:%s" % item for item in viewitems(params)) if data is not None: msg += " body: {%s}\n" % ", ".join("%s:%s" % item for item in viewitems(data)) if json is not None: msg += " json: %s\n" % json.dumps(json) if files is not None: msg += " file: %s\n" % ", ".join(f.name for f in viewvalues(files)) self._log_message(msg + "\n") def _log_end_transaction(self, start_time, response): """Log response from an API request.""" if not self._is_logging: return elapsed_time = int((time.time() - start_time) * 1000) msg = "<<< HTTP %d %s (%d ms)\n" % (response.status_code, response.reason, elapsed_time) if "Content-Type" in response.headers: msg += " Content-Type: %s\n" % response.headers["Content-Type"] msg += response.text self._log_message(msg + "\n\n") def _log_end_exception(self, exception): """Log API request that resulted in an exception.""" if not self._is_logging: return self._log_message(">>> %s\n\n" % str(exception)) def _log_message(self, msg): """ Log the message `msg` to the destination `self._logging_dest`. If this destination is a file name, then we append the message to the file and then close the file immediately. If the destination is an open file handle, then we simply write the message there and do not attempt to close it. """ if is_str(self._logging_dest): with open(self._logging_dest, "at", encoding="utf-8") as f: f.write(msg) else: self._logging_dest.write(msg) @staticmethod def _process_response(response): """ Given a response object, prepare it to be handed over to the external caller. Preparation steps include: * detect if the response has error status, and convert it to an appropriate exception; * detect Content-Type, and based on that either parse the response as JSON or return as plain text. """ content_type = response.headers[ "Content-Type"] if "Content-Type" in response.headers else "" if ";" in content_type: # Remove a ";charset=..." part content_type = content_type[:content_type.index(";")] status_code = response.status_code # Auto-detect response type by its content-type. Decode JSON, all other responses pass as-is. if content_type == "application/json": try: data = response.json(object_pairs_hook=H2OResponse) except (JSONDecodeError, requests.exceptions.ContentDecodingError) as e: raise H2OServerError("Malformed JSON from server (%s):\n%s" % (str(e), response.text)) else: data = response.text # Success (200 = "Ok", 201 = "Created", 202 = "Accepted", 204 = "No Content") if status_code in {200, 201, 202, 204}: return data # Client errors (400 = "Bad Request", 404 = "Not Found", 412 = "Precondition Failed") if status_code in {400, 404, 412} and isinstance( data, (H2OErrorV3, H2OModelBuilderErrorV3)): raise H2OResponseError(data) # Server errors (notably 500 = "Server Error") # Note that it is possible to receive valid H2OErrorV3 object in this case, however it merely means the server # did not provide the correct status code. raise H2OServerError("HTTP %d %s:\n%r" % (status_code, response.reason, data)) def _print(self, msg, flush=False, end="\n"): """Helper function to print connection status messages when in verbose mode.""" if self._verbose: print2(msg, end=end, flush=flush) def __repr__(self): if self._stage == 0: return "<H2OConnection uninitialized>" elif self._stage == 1: sess = "session %s" % self._session_id if self._session_id else "no session" return "<H2OConnection to %s, %s>" % (self._base_url, sess) else: return "<H2OConnection closed>" def __enter__(self): """Called when an H2OConnection object is created within the ``with ...`` statement.""" return self def __exit__(self, *args): """Called at the end of the ``with ...`` statement.""" self.close() assert len(args) == 3 # Avoid warning about unused args... return False # ensure that any exception will be re-raised #------------------------------------------------------------------------------------------------------------------- # DEPRECATED # # Access to any of these vars / methods will produce deprecation warnings. # Consult backwards_compatible.py for the description of these vars. # # These methods are deprecated since July 2016. Please remove them if it's 2017 already... #------------------------------------------------------------------------------------------------------------------- _bcsv = {"__ENCODING__": "utf-8", "__ENCODING_ERROR__": "replace"} _bcsm = { "default": lambda: _deprecated_default(), "jar_paths": lambda: list(getattr(H2OLocalServer, "_jar_paths")()), "rest_version": lambda: 3, "https": lambda: __H2OCONN__.base_url.split(":")[0] == "https", "ip": lambda: __H2OCONN__.base_url.split(":")[1][2:], "port": lambda: __H2OCONN__.base_url.split(":")[2], "username": lambda: _deprecated_username(), "password": lambda: _deprecated_password(), "insecure": lambda: not getattr(__H2OCONN__, "_verify_ssl_cert"), "current_connection": lambda: __H2OCONN__, "check_conn": lambda: _deprecated_check_conn(), # "cluster_is_up" used to be static (required `conn` parameter) -> now it's non-static w/o any patching needed "make_url": lambda url_suffix, _rest_version=3: __H2OCONN__.make_url( url_suffix, _rest_version), "get": lambda url_suffix, **kwargs: _deprecated_get(__H2OCONN__, url_suffix, **kwargs), "post": lambda url_suffix, file_upload_info=None, **kwa: _deprecated_post( __H2OCONN__, url_suffix, file_upload_info=file_upload_info, **kwa), "delete": lambda url_suffix, **kwargs: _deprecated_delete( __H2OCONN__, url_suffix, **kwargs), "get_json": lambda url_suffix, **kwargs: _deprecated_get(__H2OCONN__, url_suffix, **kwargs), "post_json": lambda url_suffix, file_upload_info=None, **kwa: _deprecated_post( __H2OCONN__, url_suffix, file_upload_info=file_upload_info, **kwa), "rest_ctr": lambda: __H2OCONN__.requests_count, } _bcim = { "make_url": lambda self, url_suffix, _rest_version=3: "/".join( [self._base_url, str(_rest_version), url_suffix]), "get": lambda *args, **kwargs: _deprecated_get(*args, **kwargs), "post": lambda *args, **kwargs: _deprecated_post(*args, **kwargs), "delete": lambda *args, **kwargs: _deprecated_delete(*args, **kwargs), "get_json": lambda *args, **kwargs: _deprecated_get(*args, **kwargs), "post_json": lambda *args, **kwargs: _deprecated_post(*args, **kwargs), }
class ModelBase(backwards_compatible()): """Base class for all models.""" def __init__(self): """Construct a new model instance.""" super(ModelBase, self).__init__() self._id = None self._model_json = None self._metrics_class = None self._is_xvalidated = False self._xval_keys = None self._parms = {} # internal, for object recycle self.parms = {} # external self._estimator_type = "unsupervised" self._future = False # used by __repr__/show to query job state self._job = None # used when _future is True @property def model_id(self): """Model identifier.""" return self._id @model_id.setter def model_id(self, newid): oldid = self._id self._id = newid h2o.rapids('(rename "%s" "%s")' % (oldid, newid)) @property def params(self): """ Get the parameters and the actual/default values only. :returns: A dictionary of parameters used to build this model. """ params = {} for p in self.parms: params[p] = {"default": self.parms[p]["default_value"], "actual": self.parms[p]["actual_value"]} return params @property def default_params(self): """ Get default parameters of a model :return: A dictionary of default parameters for the model """ params = {} for p in self.parms: params[p] = self.parms[p]["default_value"] return params @property def actual_params(self): """ Get actual parameters of a model :return: A dictionary of actual parameters for the model """ params_to_select = {'model_id':'name', \ 'response_column':'column_name', \ 'training_frame': 'name', \ 'validation_frame':'name'} params = {} for p in self.parms: if p in params_to_select.keys(): params[p] = self.parms[p]["actual_value"].get(params_to_select[p], None) else: params[p] = self.parms[p]["actual_value"] return params @property def full_parameters(self): """ Get the full specification of all parameters. :returns: a dictionary of parameters used to build this model. """ return self.parms @property def type(self): """Get the type of model built as a string. :returns: "classifier" or "regressor" or "unsupervised" """ return self._estimator_type def __repr__(self): # PUBDEV-2278: using <method>? from IPython caused everything to dump stk = traceback.extract_stack() if not ("IPython" in stk[-2][0] and "info" == stk[-2][2]): self.show() return "" def predict_leaf_node_assignment(self, test_data): """ Predict on a dataset and return the leaf node assignment (only for tree-based models). :param H2OFrame test_data: Data on which to make predictions. :returns: A new H2OFrame of predictions. """ if not isinstance(test_data, h2o.H2OFrame): raise ValueError("test_data must be an instance of H2OFrame") j = h2o.api("POST /3/Predictions/models/%s/frames/%s" % (self.model_id, test_data.frame_id), data={"leaf_node_assignment": True}) return h2o.get_frame(j["predictions_frame"]["name"]) def predict(self, test_data): """ Predict on a dataset. :param H2OFrame test_data: Data on which to make predictions. :returns: A new H2OFrame of predictions. """ if not isinstance(test_data, h2o.H2OFrame): raise ValueError("test_data must be an instance of H2OFrame") j = H2OJob(h2o.api("POST /4/Predictions/models/%s/frames/%s" % (self.model_id, test_data.frame_id)), self._model_json['algo'] + " prediction") j.poll() return h2o.get_frame(j.dest_key) def is_cross_validated(self): """Return True if the model was cross-validated.""" return self._is_xvalidated def xval_keys(self): """Return model keys for the cross-validated model.""" return self._xval_keys def get_xval_models(self, key=None): """ Return a Model object. :param key: If None, return all cross-validated models; otherwise return the model that key points to. :returns: A model or list of models. """ return h2o.get_model(key) if key is not None else [h2o.get_model(k) for k in self._xval_keys] @property def xvals(self): """ Return a list of the cross-validated models. :returns: A list of models """ return self.get_xval_models() def deepfeatures(self, test_data, layer): """ Return hidden layer details. :param test_data: Data to create a feature space on :param layer: 0 index hidden layer """ if test_data is None: raise ValueError("Must specify test data") j = H2OJob(h2o.api("POST /4/Predictions/models/%s/frames/%s" % (self._id, test_data.frame_id), data={"deep_features_hidden_layer": layer}), "deepfeatures") j.poll() return h2o.get_frame(j.dest_key) def weights(self, matrix_id=0): """ Return the frame for the respective weight matrix. :param: matrix_id: an integer, ranging from 0 to number of layers, that specifies the weight matrix to return. :returns: an H2OFrame which represents the weight matrix identified by matrix_id """ num_weight_matrices = len(self._model_json['output']['weights']) if matrix_id not in list(range(num_weight_matrices)): raise ValueError( "Weight matrix does not exist. Model has {0} weight matrices (0-based indexing), but matrix {1} " "was requested.".format(num_weight_matrices, matrix_id)) return h2o.get_frame(self._model_json['output']['weights'][matrix_id]['URL'].split('/')[3]) def biases(self, vector_id=0): """ Return the frame for the respective bias vector. :param: vector_id: an integer, ranging from 0 to number of layers, that specifies the bias vector to return. :returns: an H2OFrame which represents the bias vector identified by vector_id """ num_bias_vectors = len(self._model_json['output']['biases']) if vector_id not in list(range(num_bias_vectors)): raise ValueError( "Bias vector does not exist. Model has {0} bias vectors (0-based indexing), but vector {1} " "was requested.".format(num_bias_vectors, vector_id)) return h2o.get_frame(self._model_json['output']['biases'][vector_id]['URL'].split('/')[3]) def normmul(self): """Normalization/Standardization multipliers for numeric predictors.""" return self._model_json['output']['normmul'] def normsub(self): """Normalization/Standardization offsets for numeric predictors.""" return self._model_json['output']['normsub'] def respmul(self): """Normalization/Standardization multipliers for numeric response.""" return self._model_json['output']['normrespmul'] def respsub(self): """Normalization/Standardization offsets for numeric response.""" return self._model_json['output']['normrespsub'] def catoffsets(self): """Categorical offsets for one-hot encoding.""" return self._model_json['output']['catoffsets'] def model_performance(self, test_data=None, train=False, valid=False, xval=False): """ Generate model metrics for this model on test_data. Parameters ---------- test_data: H2OFrame, optional Data set for which model metrics shall be computed against. All three of train, valid and xval arguments are ignored if test_data is not None. train: boolean, optional Report the training metrics for the model. valid: boolean, optional Report the validation metrics for the model. xval: boolean, optional Report the cross-validation metrics for the model. If train and valid are True, then it defaults to True. :returns: An object of class H2OModelMetrics. """ if test_data is None: if not train and not valid and not xval: train = True # default to train if train: return self._model_json["output"]["training_metrics"] if valid: return self._model_json["output"]["validation_metrics"] if xval: return self._model_json["output"]["cross_validation_metrics"] else: # cases dealing with test_data not None if not isinstance(test_data, h2o.H2OFrame): raise ValueError("`test_data` must be of type H2OFrame. Got: " + type(test_data)) res = h2o.api("POST /3/ModelMetrics/models/%s/frames/%s" % (self.model_id, test_data.frame_id)) # FIXME need to do the client-side filtering... (PUBDEV-874) raw_metrics = None for mm in res["model_metrics"]: if mm["frame"] is not None and mm["frame"]["name"] == test_data.frame_id: raw_metrics = mm break return self._metrics_class(raw_metrics, algo=self._model_json["algo"]) def scoring_history(self): """ Retrieve Model Score History. :returns: The score history as an H2OTwoDimTable or a Pandas DataFrame. """ model = self._model_json["output"] if "scoring_history" in model and model["scoring_history"] is not None: return model["scoring_history"].as_data_frame() print("No score history for this model") def cross_validation_metrics_summary(self): """ Retrieve Cross-Validation Metrics Summary. :returns: The cross-validation metrics summary as an H2OTwoDimTable """ model = self._model_json["output"] if "cross_validation_metrics_summary" in model and model["cross_validation_metrics_summary"] is not None: return model["cross_validation_metrics_summary"] print("No cross-validation metrics summary for this model") def summary(self): """Print a detailed summary of the model.""" model = self._model_json["output"] if model["model_summary"]: model["model_summary"].show() # H2OTwoDimTable object def show(self): """Print innards of model, without regards to type.""" if self._future: self._job.poll_once() return if self._model_json is None: print("No model trained yet") return if self.model_id is None: print("This H2OEstimator has been removed.") return model = self._model_json["output"] print("Model Details") print("=============") print(self.__class__.__name__, ": ", self._model_json["algo_full_name"]) print("Model Key: ", self._id) self.summary() print() # training metrics tm = model["training_metrics"] if tm: tm.show() vm = model["validation_metrics"] if vm: vm.show() xm = model["cross_validation_metrics"] if xm: xm.show() xms = model["cross_validation_metrics_summary"] if xms: xms.show() if "scoring_history" in model and model["scoring_history"]: model["scoring_history"].show() if "variable_importances" in model and model["variable_importances"]: model["variable_importances"].show() def varimp(self, use_pandas=False): """ Pretty print the variable importances, or return them in a list. :param use_pandas: If True, then the variable importances will be returned as a pandas data frame. :returns: A list or Pandas DataFrame. """ model = self._model_json["output"] if "variable_importances" in list(model.keys()) and model["variable_importances"]: vals = model["variable_importances"].cell_values header = model["variable_importances"].col_header if use_pandas and can_use_pandas(): import pandas return pandas.DataFrame(vals, columns=header) else: return vals else: print("Warning: This model doesn't have variable importances") def residual_deviance(self, train=False, valid=False, xval=None): """ Retreive the residual deviance if this model has the attribute, or None otherwise. :param train: Get the residual deviance for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the residual deviance for the validation set. If both train and valid are True, then train is selected by default. :param xval: not implemented :returns: Return the residual deviance, or None if it is not present. """ if xval: raise H2OValueError("Cross-validation metrics are not available.") if not train and not valid: train = True if train and valid: train = True if train: return self._model_json["output"]["training_metrics"].residual_deviance() else: return self._model_json["output"]["validation_metrics"].residual_deviance() def residual_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the residual degress of freedom if this model has the attribute, or None otherwise. :param train: Get the residual dof for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the residual dof for the validation set. If both train and valid are True, then train is selected by default. :param xval: not implemented :returns: Return the residual dof, or None if it is not present. """ if xval: raise H2OValueError("Cross-validation metrics are not available.") if not train and not valid: train = True if train and valid: train = True if train: return self._model_json["output"]["training_metrics"].residual_degrees_of_freedom() else: return self._model_json["output"]["validation_metrics"].residual_degrees_of_freedom() def null_deviance(self, train=False, valid=False, xval=False): """ Retreive the null deviance if this model has the attribute, or None otherwise. :param train: Get the null deviance for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the null deviance for the validation set. If both train and valid are True, then train is selected by default. :param xval: not implemented :returns: Return the null deviance, or None if it is not present. """ if xval: raise H2OValueError("Cross-validation metrics are not available.") if not train and not valid: train = True if train and valid: train = True if train: return self._model_json["output"]["training_metrics"].null_deviance() else: return self._model_json["output"]["validation_metrics"].null_deviance() def null_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the null degress of freedom if this model has the attribute, or None otherwise. :param train: Get the null dof for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the null dof for the validation set. If both train and valid are True, then train is selected by default. :param xval: not implemented :returns: Return the null dof, or None if it is not present. """ if xval: raise H2OValueError("Cross-validation metrics are not available.") if not train and not valid: train = True if train and valid: train = True if train: return self._model_json["output"]["training_metrics"].null_degrees_of_freedom() else: return self._model_json["output"]["validation_metrics"].null_degrees_of_freedom() def pprint_coef(self): """Pretty print the coefficents table (includes normalized coefficients).""" print(self._model_json["output"]["coefficients_table"]) # will return None if no coefs! def coef(self): """Return the coefficients for this model.""" tbl = self._model_json["output"]["coefficients_table"] if tbl is None: return None tbl = tbl.cell_values return {a[0]: a[1] for a in tbl} def coef_norm(self): """Return the normalized coefficients.""" tbl = self._model_json["output"]["coefficients_table"] if tbl is None: return None tbl = tbl.cell_values return {a[0]: a[2] for a in tbl} def r2(self, train=False, valid=False, xval=False): """ Return the R^2 for this regression model. Will return R^2 for GLM Models and will return NaN otherwise. The R^2 value is defined to be 1 - MSE/var, where var is computed as sigma*sigma. 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 train: If train is True, then return the R^2 value for the training data. :param valid: If valid is True, then return the R^2 value for the validation data. :param xval: If xval is True, then return the R^2 value for the cross validation data. :returns: The R^2 for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.r2() return list(m.values())[0] if len(m) == 1 else m def mse(self, train=False, valid=False, xval=False): """ Get the MSE. 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". Parameters ---------- train : bool, default=True If train is True, then return the MSE value for the training data. valid : bool, default=True If valid is True, then return the MSE value for the validation data. xval : bool, default=True If xval is True, then return the MSE value for the cross validation data. :returns: The MSE for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.mse() return list(m.values())[0] if len(m) == 1 else m def rmse(self, train=False, valid=False, xval=False): """ Get the RMSE. 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". Parameters ---------- train : bool, default=True If train is True, then return the RMSE value for the training data. valid : bool, default=True If valid is True, then return the RMSE value for the validation data. xval : bool, default=True If xval is True, then return the RMSE value for the cross validation data. Returns ------- The RMSE for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.rmse() return list(m.values())[0] if len(m) == 1 else m def mae(self, train=False, valid=False, xval=False): """ Get the MAE. 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". Parameters ---------- train : bool, default=True If train is True, then return the MAE value for the training data. valid : bool, default=True If valid is True, then return the MAE value for the validation data. xval : bool, default=True If xval is True, then return the MAE value for the cross validation data. Returns ------- The MAE for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.mae() return list(m.values())[0] if len(m) == 1 else m def rmsle(self, train=False, valid=False, xval=False): """ Get the rmsle. 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". Parameters ---------- train : bool, default=True If train is True, then return the rmsle value for the training data. valid : bool, default=True If valid is True, then return the rmsle value for the validation data. xval : bool, default=True If xval is True, then return the rmsle value for the cross validation data. Returns ------- The rmsle for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.rmsle() return list(m.values())[0] if len(m) == 1 else m def logloss(self, train=False, valid=False, xval=False): """ Get the Log Loss. 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 train: If train is True, then return the Log Loss value for the training data. :param valid: If valid is True, then return the Log Loss value for the validation data. :param xval: If xval is True, then return the Log Loss value for the cross validation data. :returns: The Log Loss for this binomial model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.logloss() return list(m.values())[0] if len(m) == 1 else m def mean_residual_deviance(self, train=False, valid=False, xval=False): """ Get the Mean Residual Deviances. 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 train: If train is True, then return the Mean Residual Deviance value for the training data. :param valid: If valid is True, then return the Mean Residual Deviance value for the validation data. :param xval: If xval is True, then return the Mean Residual Deviance value for the cross validation data. :returns: The Mean Residual Deviance for this regression model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.mean_residual_deviance() return list(m.values())[0] if len(m) == 1 else m def auc(self, train=False, valid=False, xval=False): """ Get the AUC. 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 train: If train is True, then return the AUC value for the training data. :param valid: If valid is True, then return the AUC value for the validation data. :param xval: If xval is True, then return the AUC value for the validation data. :returns: The AUC. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.auc() return list(m.values())[0] if len(m) == 1 else m def aic(self, train=False, valid=False, xval=False): """ Get the AIC(s). 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 train: If train is True, then return the AIC value for the training data. :param valid: If valid is True, then return the AIC value for the validation data. :param xval: If xval is True, then return the AIC value for the validation data. :returns: The AIC. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.aic() return list(m.values())[0] if len(m) == 1 else m def gini(self, train=False, valid=False, xval=False): """ Get the Gini coefficient. 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 train: If train is True, then return the Gini Coefficient value for the training data. :param valid: If valid is True, then return the Gini Coefficient value for the validation data. :param xval: If xval is True, then return the Gini Coefficient value for the cross validation data. :returns: The Gini Coefficient for this binomial model. """ tm = ModelBase._get_metrics(self, train, valid, xval) m = {} for k, v in viewitems(tm): m[k] = None if v is None else v.gini() return list(m.values())[0] if len(m) == 1 else m def download_pojo(self, path="", get_genmodel_jar=False): """ Download the POJO for this model to the directory specified by path. If path is "", then dump to screen. :param path: An absolute path to the directory where POJO should be saved. :returns: name of the POJO file written. """ assert_is_type(path, str) assert_is_type(get_genmodel_jar, bool) path = path.rstrip("/") return h2o.download_pojo(self, path, get_jar=get_genmodel_jar) def download_mojo(self, path=".", get_genmodel_jar=False): """ Download the model in MOJO format. :param path: the path where MOJO file should be saved. :param get_genmodel_jar: if True, then also download h2o-genmodel.jar and store it in folder ``path``. :returns: name of the MOJO file written. """ assert_is_type(path, str) assert_is_type(get_genmodel_jar, bool) if self.algo not in {"drf", "gbm", "deepwater", "glrm"}: raise H2OValueError("MOJOs are currently supported for Distributed Random Forest, " "Gradient Boosting Machine, Deep Water and GLRM models only.") if get_genmodel_jar: h2o.api("GET /3/h2o-genmodel.jar", save_to=os.path.join(path, "h2o-genmodel.jar")) return h2o.api("GET /3/Models/%s/mojo" % self.model_id, save_to=path) @staticmethod def _get_metrics(o, train, valid, xval): # noinspection PyProtectedMember output = o._model_json["output"] metrics = {} if train: metrics["train"] = output["training_metrics"] if valid: metrics["valid"] = output["validation_metrics"] if xval: metrics["xval"] = output["cross_validation_metrics"] if len(metrics) == 0: metrics["train"] = output["training_metrics"] return metrics # Delete from cluster as model goes out of scope # def __del__(self): # h2o.remove(self._id) def _plot(self, timestep, metric, server=False): plt = _get_matplotlib_pyplot(server) if not plt: return scoring_history = self.scoring_history() # Separate functionality for GLM since its output is different from other algos if self._model_json["algo"] == "glm": # GLM has only one timestep option, which is `iteration` timestep = "iteration" if metric == "AUTO": metric = "log_likelihood" elif metric not in ("log_likelihood", "objective"): raise H2OValueError("for GLM, metric must be one of: log_likelihood, objective") plt.xlabel(timestep) plt.ylabel(metric) plt.title("Validation Scoring History") plt.plot(scoring_history[timestep], scoring_history[metric]) elif self._model_json["algo"] in ("deeplearning", "deepwater", "drf", "gbm"): # Set timestep if self._model_json["algo"] in ("gbm", "drf"): assert_is_type(timestep, "AUTO", "duration", "number_of_trees") if timestep == "AUTO": timestep = "number_of_trees" else: # self._model_json["algo"] == "deeplearning": # Delete first row of DL scoring history since it contains NAs & NaNs if scoring_history["samples"][0] == 0: scoring_history = scoring_history[1:] assert_is_type(timestep, "AUTO", "epochs", "samples", "duration") if timestep == "AUTO": timestep = "epochs" training_metric = "training_{}".format(metric) validation_metric = "validation_{}".format(metric) if timestep == "duration": dur_colname = "duration_{}".format(scoring_history["duration"][1].split()[1]) scoring_history[dur_colname] = [str(x).split()[0] for x in scoring_history["duration"]] timestep = dur_colname if can_use_pandas(): valid = validation_metric in list(scoring_history) ylim = (scoring_history[[training_metric, validation_metric]].min().min(), scoring_history[[training_metric, validation_metric]].max().max()) if valid \ else (scoring_history[training_metric].min(), scoring_history[training_metric].max()) else: valid = validation_metric in scoring_history.col_header ylim = (min(min(scoring_history[[training_metric, validation_metric]])), max(max(scoring_history[[training_metric, validation_metric]]))) if valid \ else (min(scoring_history[training_metric]), max(scoring_history[training_metric])) if ylim[0] == ylim[1]: ylim = (0, 1) if valid: # Training and validation scoring history plt.xlabel(timestep) plt.ylabel(metric) plt.title("Scoring History") plt.ylim(ylim) plt.plot(scoring_history[timestep], scoring_history[training_metric], label="Training") plt.plot(scoring_history[timestep], scoring_history[validation_metric], color="orange", label="Validation") plt.legend() else: # Training scoring history only plt.xlabel(timestep) plt.ylabel(training_metric) plt.title("Training Scoring History") plt.ylim(ylim) plt.plot(scoring_history[timestep], scoring_history[training_metric]) else: # algo is not glm, deeplearning, drf, gbm raise H2OValueError("Plotting not implemented for this type of model") if not server: plt.show() def partial_plot(self, data, cols, destination_key=None, nbins=20, plot=True, figsize=(7,10), server=False): """ Create partial dependence plot which gives a graphical depiction of the marginal effect of a variable on the response. The effect of a variable is measured in change in the mean response. :param H2OFrame data: An H2OFrame object used for scoring and constructing the plot. :param cols: Feature(s) for which partial dependence will be calculated. :param destination_key: An key reference to the created partial dependence tables in H2O. :param nbins: Number of bins used. :param plot: A boolean specifying whether to plot partial dependence table. :param figsize: Dimension/size of the returning plots, adjust to fit your output cells. :param server: ? :return: Plot and list of calculated mean response tables for each feature requested. """ if not isinstance(data, h2o.H2OFrame): raise ValueError("data must be an instance of H2OFrame") assert_is_type(cols, [str]) assert_is_type(destination_key, None, str) assert_is_type(nbins, int) assert_is_type(plot, bool) assert_is_type(figsize, (int,int)) ## Check cols specified exist in frame data for xi in cols: if not xi in data.names: raise H2OValueError("Column %s does not exist in the training frame" % xi) kwargs = {} kwargs['cols'] = cols kwargs['model_id'] = self.model_id kwargs['frame_id'] = data.frame_id kwargs['nbins'] = nbins kwargs['destination_key'] = destination_key json = H2OJob(h2o.api("POST /3/PartialDependence/", data=kwargs), job_type="PartialDependencePlot").poll() json = h2o.api("GET /3/PartialDependence/%s" % json.dest_key) # Extract partial dependence data from json response # pps = json pps = json['partial_dependence_data'] ## Plot partial dependence plots using matplotlib if plot: plt = _get_matplotlib_pyplot(server) if not plt: return fig, axs = plt.subplots(len(cols), squeeze=False, figsize=figsize) for i, pp in enumerate(pps): ## Check weather column was categorical or numeric col=cols[i] cat=data[col].isfactor()[0] if cat: labels = pp[0] x = range(len(labels)) y = pp[1] axs[i,0].plot(x, y, 'o') axs[i,0].set_xticks(x) axs[i,0].set_xticklabels(labels) axs[i,0].margins(0.2) else: axs[i,0].plot(pp[0], pp[1]) axs[i,0].set_xlim(min(pp[0]), max(pp[0])) axs[i,0].set_title('Partial Dependence Plot For {}'.format(col)) axs[i,0].set_xlabel(pp.col_header[0]) axs[i,0].set_ylabel(pp.col_header[1]) axs[i,0].xaxis.grid() axs[i,0].yaxis.grid() if len(col) >1: fig.tight_layout(pad = 0.4,w_pad=0.5, h_pad=1.0) return pps def varimp_plot(self, num_of_features=None, server=False): """ Plot the variable importance for a trained model. :param num_of_features: the number of features shown in the plot. :param server: ? :returns: None. """ assert_is_type(num_of_features, None, int) assert_is_type(server, bool) plt = _get_matplotlib_pyplot(server) if not plt: return # check if the model is a glm if self._model_json["algo"] == "glm": # print statement to used std_coef_plot(), and use std_coef_plt instead print("Variable importance does not apply to GLM. Will use std_coef_plot() instead.") self.std_coef_plot(num_of_features) return # get the variable importances as a list of tuples, do not use pandas dataframe importances = self.varimp(use_pandas=False) # features labels correspond to the first value of each tuple in the importances list feature_labels = [tup[0] for tup in importances] # relative importances correspond to the first value of each tuple in the importances list scaled_importances = [tup[2] for tup in importances] # specify bar centers on the y axis, but flip the order so largest bar appears at top pos = range(len(feature_labels))[::-1] # specify the bar lengths val = scaled_importances # check that num_of_features is an integer if num_of_features is None: num_of_features = len(val) fig, ax = plt.subplots(1, 1, figsize=(14, 10)) # create separate plot for the case where num_of_features == 1 if num_of_features == 1: plt.barh(pos[0:num_of_features], val[0:num_of_features], align="center", height=0.8, color="#1F77B4", edgecolor="none") # Hide the right and top spines, color others grey ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_color("#7B7B7B") ax.spines["left"].set_color("#7B7B7B") # Only show ticks on the left and bottom spines ax.yaxis.set_ticks_position("left") ax.xaxis.set_ticks_position("bottom") plt.yticks(pos[0:num_of_features], feature_labels[0:num_of_features]) ax.margins(y=0.5) else: plt.barh(pos[0:num_of_features], val[0:num_of_features], align="center", height=0.8, color="#1F77B4", edgecolor="none") # Hide the right and top spines, color others grey ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_color("#7B7B7B") ax.spines["left"].set_color("#7B7B7B") # Only show ticks on the left and bottom spines ax.yaxis.set_ticks_position("left") ax.xaxis.set_ticks_position("bottom") plt.yticks(pos[0:num_of_features], feature_labels[0:num_of_features]) ax.margins(y=0.5) # check which algorithm was used to select right plot title if self._model_json["algo"] == "gbm": plt.title("Variable Importance: H2O GBM", fontsize=20) if not server: plt.show() elif self._model_json["algo"] == "drf": plt.title("Variable Importance: H2O DRF", fontsize=20) if not server: plt.show() # if H2ODeepLearningEstimator has variable_importances == True elif self._model_json["algo"] == "deeplearning": plt.title("Variable Importance: H2O Deep Learning", fontsize=20) if not server: plt.show() else: raise H2OValueError("A variable importances plot is not implemented for this type of model") def std_coef_plot(self, num_of_features=None, server=False): """ Plot a GLM model's standardized coefficient magnitudes. :param num_of_features: the number of features shown in the plot. :param server: ? :returns: None. """ assert_is_type(num_of_features, None, I(int, lambda x: x > 0)) # check that model is a glm if self._model_json["algo"] != "glm": raise H2OValueError("This function is available for GLM models only") plt = _get_matplotlib_pyplot(server) if not plt: return # get unsorted tuple of labels and coefficients unsorted_norm_coef = self.coef_norm().items() # drop intercept value then sort tuples by the coefficient's absolute value drop_intercept = [tup for tup in unsorted_norm_coef if tup[0] != "Intercept"] norm_coef = sorted(drop_intercept, key=lambda x: abs(x[1]), reverse=True) signage = [] for element in norm_coef: # if positive including zero, color blue, else color orange (use same colors as Flow) if element[1] >= 0: signage.append("#1F77B4") # blue else: signage.append("#FF7F0E") # dark orange # get feature labels and their corresponding magnitudes feature_labels = [tup[0] for tup in norm_coef] norm_coef_magn = [abs(tup[1]) for tup in norm_coef] # specify bar centers on the y axis, but flip the order so largest bar appears at top pos = range(len(feature_labels))[::-1] # specify the bar lengths val = norm_coef_magn # check number of features, default is all the features if num_of_features is None: num_of_features = len(val) # plot horizontal plot fig, ax = plt.subplots(1, 1, figsize=(14, 10)) # create separate plot for the case where num_of_features = 1 if num_of_features == 1: plt.barh(pos[0], val[0], align="center", height=0.8, color=signage[0], edgecolor="none") # Hide the right and top spines, color others grey ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_color("#7B7B7B") ax.spines["left"].set_color("#7B7B7B") # Only show ticks on the left and bottom spines ax.yaxis.set_ticks_position("left") ax.xaxis.set_ticks_position("bottom") plt.yticks([0], feature_labels[0]) ax.margins(y=0.5) else: plt.barh(pos[0:num_of_features], val[0:num_of_features], align="center", height=0.8, color=signage[0:num_of_features], edgecolor="none") # Hide the right and top spines, color others grey ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_color("#7B7B7B") ax.spines["left"].set_color("#7B7B7B") # Only show ticks on the left and bottom spines ax.yaxis.set_ticks_position("left") ax.xaxis.set_ticks_position("bottom") plt.yticks(pos[0:num_of_features], feature_labels[0:num_of_features]) ax.margins(y=0.05) # generate custom fake lines that will be used as legend entries: # check if positive and negative values exist # if positive create positive legend if "#1F77B4" in signage[0:num_of_features] and "#FF7F0E" not in signage[0:num_of_features]: color_ids = {"Positive": "#1F77B4"} markers = [plt.Line2D([0, 0], [0, 0], color=color, marker="s", linestyle="") for color in signage[0:num_of_features]] lgnd = plt.legend(markers, color_ids, numpoints=1, loc="best", frameon=False, fontsize=13) lgnd.legendHandles[0]._legmarker.set_markersize(10) # if neg create neg legend elif "#FF7F0E" in signage[0:num_of_features] and "#1F77B4" not in signage[0:num_of_features]: color_ids = {"Negative": "#FF7F0E"} markers = [plt.Line2D([0, 0], [0, 0], color=color, marker="s", linestyle="") for color in set(signage[0:num_of_features])] lgnd = plt.legend(markers, color_ids, numpoints=1, loc="best", frameon=False, fontsize=13) lgnd.legendHandles[0]._legmarker.set_markersize(10) # if both provide both colors in legend else: color_ids = {"Positive": "#1F77B4", "Negative": "#FF7F0E"} markers = [plt.Line2D([0, 0], [0, 0], color=color, marker="s", linestyle="") for color in set(signage[0:num_of_features])] lgnd = plt.legend(markers, color_ids, numpoints=1, loc="best", frameon=False, fontsize=13) lgnd.legendHandles[0]._legmarker.set_markersize(10) lgnd.legendHandles[1]._legmarker.set_markersize(10) # Hide the right and top spines, color others grey ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_color("#7B7B7B") ax.spines["left"].set_color("#7B7B7B") # Only show ticks on the left and bottom spines # ax.yaxis.set_ticks_position("left") # ax.xaxis.set_ticks_position("bottom") plt.yticks(pos[0:num_of_features], feature_labels[0:num_of_features]) plt.tick_params(axis="x", which="minor", bottom="off", top="off", labelbottom="off") plt.title("Standardized Coef. Magnitudes: H2O GLM", fontsize=20) # plt.axis("tight") # show plot if not server: plt.show() @staticmethod def _check_targets(y_actual, y_predicted): """Check that y_actual and y_predicted have the same length. :param H2OFrame y_actual: :param H2OFrame y_predicted: :returns: None """ if len(y_actual) != len(y_predicted): raise ValueError("Row mismatch: [{},{}]".format(len(y_actual), len(y_predicted))) def cross_validation_models(self): """ Obtain a list of cross-validation models. :returns: list of H2OModel objects """ cvmodels = self._model_json["output"]["cross_validation_models"] if cvmodels is None: return None m = [] for p in cvmodels: m.append(h2o.get_model(p["name"])) return m def cross_validation_predictions(self): """ Obtain the (out-of-sample) holdout predictions of all cross-validation models on their holdout data. Note that the predictions are expanded to the full number of rows of the training data, with 0 fill-in. :returns: list of H2OFrame objects """ preds = self._model_json["output"]["cross_validation_predictions"] if preds is None: return None m = [] for p in preds: m.append(h2o.get_frame(p["name"])) return m def cross_validation_holdout_predictions(self): """ Obtain the (out-of-sample) holdout predictions of all cross-validation models on the training data. This is equivalent to summing up all H2OFrames returned by cross_validation_predictions. :returns: H2OFrame """ preds = self._model_json["output"]["cross_validation_holdout_predictions_frame_id"] if preds is None: return None return h2o.get_frame(preds["name"]) def cross_validation_fold_assignment(self): """ Obtain the cross-validation fold assignment for all rows in the training data. :returns: H2OFrame """ fid = self._model_json["output"]["cross_validation_fold_assignment_frame_id"] if fid is None: return None return h2o.get_frame(fid["name"]) def score_history(self): """[DEPRECATED].""" warnings.warn("`score_history` is deprecated. Use `scoring_history`", category=DeprecationWarning, stacklevel=2) return self.scoring_history() # Deprecated functions; left here for backward compatibility _bcim = { "giniCoef": lambda self, *args, **kwargs: self.gini(*args, **kwargs) }
class H2OGridSearch(backwards_compatible()): """ Grid Search of a Hyper-Parameter Space for a Model Parameters ---------- model : H2OEstimator, type The type of model to be explored initialized with optional parameters that will be unchanged across explored models. hyper_params: dict A dictionary of string parameters (keys) and a list of values to be explored by grid search (values). grid_id : str, optional The unique id assigned to the resulting grid object. If none is given, an id will automatically be generated. search_criteria: dict, optional A dictionary of directives which control the search of the hyperparameter space. The default strategy 'Cartesian' covers the entire space of hyperparameter combinations. Specify the 'RandomDiscrete' strategy to get random search of all the combinations of your hyperparameters. RandomDiscrete should usually be combined with at least one early stopping criterion, max_models and/or max_runtime_secs, e.g. search_criteria = {strategy: 'RandomDiscrete', max_models: 42, max_runtime_secs: 28800, seed = 1234} search_criteria = {strategy: 'RandomDiscrete', stopping_metric: 'AUTO', stopping_tolerance: 0.001,stopping_rounds: 10, seed = 1234, seed = 1234} search_criteria = {strategy: 'RandomDiscrete', stopping_metric: 'misclassification', stopping_tolerance: 0.00001, stopping_rounds: 5, seed = 1234}. Returns ------- A new H2OGridSearch instance. Examples -------- >>> from h2o.grid.grid_search import H2OGridSearch >>> from h2o.estimators.glm import H2OGeneralizedLinearEstimator >>> hyper_parameters = {'alpha': [0.01,0.5], 'lambda': [1e-5,1e-6]} >>> gs = H2OGridSearch(H2OGeneralizedLinearEstimator(family='binomial'), hyper_parameters) >>> training_data = h2o.import_file("smalldata/logreg/benign.csv") >>> gs.train(x=range(3) + range(4,11),y=3, training_frame=training_data) >>> gs.show() """ def __init__(self, model, hyper_params, grid_id=None, search_criteria=None): super(H2OGridSearch, self).__init__() assert_is_type(model, None, H2OEstimator, lambda mdl: issubclass(mdl, H2OEstimator)) assert_is_type(hyper_params, dict) assert_is_type(grid_id, None, str) assert_is_type(search_criteria, None, dict) if not (model is None or is_type(model, H2OEstimator)): model = model() self._id = grid_id self.model = model self.hyper_params = dict(hyper_params) self.search_criteria = None if search_criteria is None else dict(search_criteria) self._grid_json = None self.models = None # list of H2O Estimator instances self._parms = {} # internal, for object recycle # self.parms = {} # external# self._future = False # used by __repr__/show to query job state# self._job = None # used when _future is True# @property def grid_id(self): """A key that identifies this grid search object in H2O.""" return self._id @grid_id.setter def grid_id(self, value): oldname = self.grid_id self._id = value h2o.rapids('(rename "{}" "{}")'.format(oldname, value)) @property def model_ids(self): return [i['name'] for i in self._grid_json["model_ids"]] @property def hyper_names(self): return self._grid_json["hyper_names"] @property def failed_params(self): return self._grid_json.get("failed_params", None) @property def failure_details(self): return self._grid_json.get("failure_details", None) @property def failure_stack_traces(self): return self._grid_json.get("failure_stack_traces", None) @property def failed_raw_params(self): return self._grid_json.get("failed_raw_params", None) def start(self, x, y=None, training_frame=None, offset_column=None, fold_column=None, weights_column=None, validation_frame=None, **params): """Asynchronous model build by specifying the predictor columns, response column, and any additional frame-specific values. To block for results, call join. Parameters ---------- x : list A list of column names or indices indicating the predictor columns. y : str An index or a column name indicating the response column. training_frame : H2OFrame The H2OFrame having the columns indicated by x and y (as well as any additional columns specified by fold, offset, and weights). offset_column : str, optional The name or index of the column in training_frame that holds the offsets. fold_column : str, optional The name or index of the column in training_frame that holds the per-row fold assignments. weights_column : str, optional The name or index of the column in training_frame that holds the per-row weights. validation_frame : H2OFrame, optional H2OFrame with validation data to be scored on while training. """ self._future = True self.train(x=x, y=y, training_frame=training_frame, offset_column=offset_column, fold_column=fold_column, weights_column=weights_column, validation_frame=validation_frame, **params) def join(self): self._future = False self._job.poll() self._job = None def train(self, x, y=None, training_frame=None, offset_column=None, fold_column=None, weights_column=None, validation_frame=None, **params): # same api as estimator_base train algo_params = locals() parms = self._parms.copy() parms.update({k: v for k, v in algo_params.items() if k not in ["self", "params", "algo_params", "parms"]}) parms["search_criteria"] = self.search_criteria parms["hyper_parameters"] = self.hyper_params # unique to grid search parms.update({k: v for k, v in list(self.model._parms.items()) if v is not None}) # unique to grid search parms.update(params) if '__class__' in parms: # FIXME: hackt for PY3 del parms['__class__'] y = algo_params["y"] tframe = algo_params["training_frame"] if tframe is None: raise ValueError("Missing training_frame") if y is not None: if is_type(y, list, tuple): if len(y) == 1: parms["y"] = y[0] else: raise ValueError('y must be a single column reference') self._estimator_type = "classifier" if tframe[y].isfactor() else "regressor" self.build_model(parms) def build_model(self, algo_params): if algo_params["training_frame"] is None: raise ValueError("Missing training_frame") x = algo_params.pop("x") y = algo_params.pop("y", None) training_frame = algo_params.pop("training_frame") validation_frame = algo_params.pop("validation_frame", None) is_auto_encoder = (algo_params is not None) and ("autoencoder" in algo_params and algo_params["autoencoder"]) algo = self.model._compute_algo() # unique to grid search is_unsupervised = is_auto_encoder or algo == "pca" or algo == "svd" or algo == "kmeans" or algo == "glrm" if is_auto_encoder and y is not None: raise ValueError("y should not be specified for autoencoder.") if not is_unsupervised and y is None: raise ValueError("Missing response") self._model_build(x, y, training_frame, validation_frame, algo_params) def _model_build(self, x, y, tframe, vframe, kwargs): kwargs['training_frame'] = tframe if vframe is not None: kwargs["validation_frame"] = vframe if is_type(y, int): y = tframe.names[y] if y is not None: kwargs['response_column'] = y if not is_type(x, list, tuple): x = [x] if is_type(x[0], int): x = [tframe.names[i] for i in x] offset = kwargs["offset_column"] folds = kwargs["fold_column"] weights = kwargs["weights_column"] ignored_columns = list(set(tframe.names) - set(x + [y, offset, folds, weights])) kwargs["ignored_columns"] = None if not ignored_columns else [quoted(col) for col in ignored_columns] kwargs = dict([(k, kwargs[k].frame_id if isinstance(kwargs[k], H2OFrame) else kwargs[k]) for k in kwargs if kwargs[k] is not None]) # gruesome one-liner algo = self.model._compute_algo() # unique to grid search if self.grid_id is not None: kwargs["grid_id"] = self.grid_id rest_ver = kwargs.pop("_rest_version") if "_rest_version" in kwargs else None grid = H2OJob(h2o.api("POST /99/Grid/%s" % algo, data=kwargs), job_type=(algo + " Grid Build")) if self._future: self._job = grid return grid.poll() if rest_ver is not None: grid_json = h2o.api("GET /99/Grids/%s" % (grid.dest_key)) error_index = 0 if len(grid_json["failure_details"]) > 0: print("Errors/Warnings building gridsearch model\n") for error_message in grid_json["failure_details"]: if isinstance(grid_json["failed_params"][error_index], dict): for h_name in grid_json['hyper_names']: print("Hyper-parameter: {0}, {1}".format(h_name, grid_json['failed_params'][error_index][h_name])) if len(grid_json["failure_stack_traces"]) > error_index: print("failure_details: {0}\nfailure_stack_traces: " "{1}\n".format(error_message, grid_json['failure_stack_traces'][error_index])) error_index += 1 else: grid_json = h2o.api("GET /99/Grids/%s" % grid.dest_key) self.models = [h2o.get_model(key['name']) for key in grid_json['model_ids']] # get first model returned in list of models from grid search to get model class (binomial, multinomial, etc) # sometimes no model is returned due to bad parameter values provided by the user. if len(grid_json['model_ids']) > 0: first_model_json = h2o.api("GET /%d/Models/%s" % (rest_ver or 3, grid_json['model_ids'][0]['name']))['models'][0] self._resolve_grid(grid.dest_key, grid_json, first_model_json) else: raise ValueError("Gridsearch returns no model due to bad parameter values or other reasons....") def _resolve_grid(self, grid_id, grid_json, first_model_json): model_class = H2OGridSearch._metrics_class(first_model_json) m = model_class() m._id = grid_id m._grid_json = grid_json # m._metrics_class = metrics_class m._parms = self._parms H2OEstimator.mixin(self, model_class) self.__dict__.update(m.__dict__.copy()) def __getitem__(self, item): return self.models[item] def __iter__(self): nmodels = len(self.models) return (self[i] for i in range(nmodels)) def __len__(self): return len(self.models) def __repr__(self): self.show() return "" def predict(self, test_data): """Predict on a dataset. Parameters ---------- test_data : H2OFrame Data to be predicted on. Returns ------- H2OFrame filled with predictions. """ return {model.model_id: model.predict(test_data) for model in self.models} def is_cross_validated(self): """ Returns ------- True if the model was cross-validated. """ return {model.model_id: model.is_cross_validated() for model in self.models} def xval_keys(self): """ Returns ------- The model keys for the cross-validated model. """ return {model.model_id: model.xval_keys() for model in self.models} def get_xval_models(self, key=None): """Return a Model object. Parameters ---------- key : str If None, return all cross-validated models; otherwise return the model that key points. Returns ------- A model or list of models. """ return {model.model_id: model.get_xval_models(key) for model in self.models} def xvals(self): """ Returns ------- A list of cross-validated models. """ return {model.model_id: model.xvals for model in self.models} def deepfeatures(self, test_data, layer): """Obtain a hidden layer's details on a dataset. Parameters ---------- test_data: H2OFrame Data to create a feature space on layer: int index of the hidden layer Returns ------- A dictionary of hidden layer details for each model. """ return {model.model_id: model.deepfeatures(test_data, layer) for model in self.models} def weights(self, matrix_id=0): """ Return the frame for the respective weight matrix :param: matrix_id: an integer, ranging from 0 to number of layers, that specifies the weight matrix to return. :return: an H2OFrame which represents the weight matrix identified by matrix_id """ return {model.model_id: model.weights(matrix_id) for model in self.models} def biases(self, vector_id=0): """ Return the frame for the respective bias vector :param: vector_id: an integer, ranging from 0 to number of layers, that specifies the bias vector to return. :return: an H2OFrame which represents the bias vector identified by vector_id """ return {model.model_id: model.biases(vector_id) for model in self.models} def normmul(self): """ Normalization/Standardization multipliers for numeric predictors """ return {model.model_id: model.normmul() for model in self.models} def normsub(self): """ Normalization/Standardization offsets for numeric predictors """ return {model.model_id: model.normsub() for model in self.models} def respmul(self): """ Normalization/Standardization multipliers for numeric response """ return {model.model_id: model.respmul() for model in self.models} def respsub(self): """ Normalization/Standardization offsets for numeric response """ return {model.model_id: model.respsub() for model in self.models} def catoffsets(self): """ Categorical offsets for one-hot encoding """ return {model.model_id: model.catoffsets() for model in self.models} def model_performance(self, test_data=None, train=False, valid=False, xval=False): """Generate model metrics for this model on test_data. :param test_data: Data set for which model metrics shall be computed against. All three of train, valid and xval arguments are ignored if test_data is not None. :param train: Report the training metrics for the model. :param valid: Report the validation metrics for the model. :param xval: Report the validation metrics for the model. :return: An object of class H2OModelMetrics. """ return {model.model_id: model.model_performance(test_data, train, valid, xval) for model in self.models} def scoring_history(self): """Retrieve Model Score History Returns ------- Score history (H2OTwoDimTable) """ return {model.model_id: model.scoring_history() for model in self.models} def summary(self, header=True): """Print a detailed summary of the explored models.""" table = [] for model in self.models: model_summary = model._model_json["output"]["model_summary"] r_values = list(model_summary.cell_values[0]) r_values[0] = model.model_id table.append(r_values) # if h2o.can_use_pandas(): # import pandas # pandas.options.display.max_rows = 20 # print pandas.DataFrame(table,columns=self.col_header) # return print() if header: print('Grid Summary:') print() H2ODisplay(table, ['Model Id'] + model_summary.col_header[1:], numalign="left", stralign="left") def show(self): """Print models sorted by metric""" hyper_combos = itertools.product(*list(self.hyper_params.values())) if not self.models: c_values = [[idx + 1, list(val)] for idx, val in enumerate(hyper_combos)] print(H2OTwoDimTable( col_header=['Model', 'Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']'], table_header='Grid Search of Model ' + self.model.__class__.__name__, cell_values=c_values)) else: print(self.sorted_metric_table()) def varimp(self, use_pandas=False): """Pretty print the variable importances, or return them in a list/pandas DataFrame Parameters ---------- use_pandas: boolean, optional If True, then the variable importances will be returned as a pandas data frame. Returns ------- A dictionary of lists or Pandas DataFrame instances. """ return {model.model_id: model.varimp(use_pandas) for model in self.models} def residual_deviance(self, train=False, valid=False, xval=False): """Retreive the residual deviance if this model has the attribute, or None otherwise. Parameters ---------- train : boolean, optional, default=True Get the residual deviance for the training set. If both train and valid are False, then train is selected by default. valid: boolean, optional Get the residual deviance for the validation set. If both train and valid are True, then train is selected by default. xval : boolean, optional Get the residual deviance for the cross-validated models. Returns ------- Return the residual deviance, or None if it is not present. """ return {model.model_id: model.residual_deviance(train, valid, xval) for model in self.models} def residual_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the residual degress of freedom if this model has the attribute, or None otherwise. :param train: Get the residual dof for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the residual dof for the validation set. If both train and valid are True, then train is selected by default. :return: Return the residual dof, or None if it is not present. """ return {model.model_id: model.residual_degrees_of_freedom(train, valid, xval) for model in self.models} def null_deviance(self, train=False, valid=False, xval=False): """ Retreive the null deviance if this model has the attribute, or None otherwise. :param: train Get the null deviance for the training set. If both train and valid are False, then train is selected by default. :param: valid Get the null deviance for the validation set. If both train and valid are True, then train is selected by default. :return: Return the null deviance, or None if it is not present. """ return {model.model_id: model.null_deviance(train, valid, xval) for model in self.models} def null_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the null degress of freedom if this model has the attribute, or None otherwise. :param train: Get the null dof for the training set. If both train and valid are False, then train is selected by default. :param valid: Get the null dof for the validation set. If both train and valid are True, then train is selected by default. :return: Return the null dof, or None if it is not present. """ return {model.model_id: model.null_degrees_of_freedom(train, valid, xval) for model in self.models} def pprint_coef(self): """ Pretty print the coefficents table (includes normalized coefficients) :return: None """ for i, model in enumerate(self.models): print('Model', i) model.pprint_coef() print() def coef(self): """ :return: Return the coefficients for this model. """ return {model.model_id: model.coef() for model in self.models} def coef_norm(self): """ :return: Return the normalized coefficients """ return {model.model_id: model.coef_norm() for model in self.models} def r2(self, train=False, valid=False, xval=False): """ Return the R^2 for this regression model. The R^2 value is defined to be 1 - MSE/var, where var is computed as sigma*sigma. 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 train: If train is True, then return the R^2 value for the training data. :param valid: If valid is True, then return the R^2 value for the validation data. :param xval: If xval is True, then return the R^2 value for the cross validation data. :return: The R^2 for this regression model. """ return {model.model_id: model.r2(train, valid, xval) for model in self.models} def mse(self, train=False, valid=False, xval=False): """ Get the MSE(s). 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 train: If train is True, then return the MSE value for the training data. :param valid: If valid is True, then return the MSE value for the validation data. :param xval: If xval is True, then return the MSE value for the cross validation data. :return: The MSE for this regression model. """ return {model.model_id: model.mse(train, valid, xval) for model in self.models} def logloss(self, train=False, valid=False, xval=False): """ Get the Log Loss(s). 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 train: If train is True, then return the Log Loss value for the training data. :param valid: If valid is True, then return the Log Loss value for the validation data. :param xval: If xval is True, then return the Log Loss value for the cross validation data. :return: The Log Loss for this binomial model. """ return {model.model_id: model.logloss(train, valid, xval) for model in self.models} def mean_residual_deviance(self, train=False, valid=False, xval=False): """ Get the Mean Residual Deviances(s). 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 train: If train is True, then return the Mean Residual Deviance value for the training data. :param valid: If valid is True, then return the Mean Residual Deviance value for the validation data. :param xval: If xval is True, then return the Mean Residual Deviance value for the cross validation data. :return: The Mean Residual Deviance for this regression model. """ return {model.model_id: model.mean_residual_deviance(train, valid, xval) for model in self.models} def auc(self, train=False, valid=False, xval=False): """ Get the AUC(s). 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 train: If train is True, then return the AUC value for the training data. :param valid: If valid is True, then return the AUC value for the validation data. :param xval: If xval is True, then return the AUC value for the validation data. :return: The AUC. """ return {model.model_id: model.auc(train, valid, xval) for model in self.models} def aic(self, train=False, valid=False, xval=False): """ Get the AIC(s). 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 train: If train is True, then return the AIC value for the training data. :param valid: If valid is True, then return the AIC value for the validation data. :param xval: If xval is True, then return the AIC value for the validation data. :return: The AIC. """ return {model.model_id: model.aic(train, valid, xval) for model in self.models} def gini(self, train=False, valid=False, xval=False): """ Get the Gini Coefficient(s). 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 train: If train is True, then return the Gini Coefficient value for the training data. :param valid: If valid is True, then return the Gini Coefficient value for the validation data. :param xval: If xval is True, then return the Gini Coefficient value for the cross validation data. :return: The Gini Coefficient for this binomial model. """ return {model.model_id: model.gini(train, valid, xval) for model in self.models} def sort_by(self, metric, increasing=True): """ Sort the models in the grid space by a metric. Parameters ---------- metric: str A metric ('logloss', 'auc', 'r2') by which to sort the models. If addtional arguments are desired, they can be passed to the metric, for example 'logloss(valid=True)' increasing: boolean, optional Sort the metric in increasing (True) (default) or decreasing (False) order. Returns ------- An H2OTwoDimTable of the sorted models showing model id, hyperparameters, and metric value. The best model can be selected and used for prediction. Examples -------- >>> grid_search_results = gs.sort_by('F1', False) >>> best_model_id = grid_search_results['Model Id'][0] >>> best_model = h2o.get_model(best_model_id) >>> best_model.predict(test_data) """ if metric[-1] != ')': metric += '()' c_values = [list(x) for x in zip(*sorted(eval('self.' + metric + '.items()'), key=lambda k_v: k_v[1]))] c_values.insert(1, [self.get_hyperparams(model_id, display=False) for model_id in c_values[0]]) if not increasing: for col in c_values: col.reverse() if metric[-2] == '(': metric = metric[:-2] return H2OTwoDimTable( col_header=['Model Id', 'Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']', metric], table_header='Grid Search Results for ' + self.model.__class__.__name__, cell_values=[list(x) for x in zip(*c_values)]) def get_hyperparams(self, id, display=True): """ Get the hyperparameters of a model explored by grid search. Parameters ---------- id: str The model id of the model with hyperparameters of interest. display: boolean Flag to indicate whether to display the hyperparameter names. Returns ------- A list of the hyperparameters for the specified model. """ idx = id if is_type(id, int) else self.model_ids.index(id) model = self[idx] # if cross-validation is turned on, parameters in one of the fold model actuall contains the max_runtime_secs # parameter and not the main model that is returned. if model._is_xvalidated: model = h2o.get_model(model._xval_keys[0]) res = [model.params[h]['actual'][0] if isinstance(model.params[h]['actual'], list) else model.params[h]['actual'] for h in self.hyper_params] if display: print('Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']') return res def get_hyperparams_dict(self, id, display=True): """ Derived and returned the model parameters used to train the particular grid search model. Parameters ---------- id: str The model id of the model with hyperparameters of interest. display: boolean Flag to indicate whether to display the hyperparameter names. Returns ------- A dict of model pararmeters derived from the hyper-parameters used to train this particular model. """ idx = id if is_type(id, int) else self.model_ids.index(id) model = self[idx] model_params = dict() # if cross-validation is turned on, parameters in one of the fold model actual contains the max_runtime_secs # parameter and not the main model that is returned. if model._is_xvalidated: model = h2o.get_model(model._xval_keys[0]) for param_name in self.hyper_names: model_params[param_name] = model.params[param_name]['actual'][0] if \ isinstance(model.params[param_name]['actual'], list) else model.params[param_name]['actual'] if display: print('Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']') return model_params def sorted_metric_table(self): """ Retrieve Summary Table of an H2O Grid Search Returns ------- The summary table as an H2OTwoDimTable or a Pandas DataFrame. """ summary = self._grid_json["summary_table"] if summary is not None: return summary.as_data_frame() print("No sorted metric table for this grid search") @staticmethod def _metrics_class(model_json): model_type = model_json["output"]["model_category"] if model_type == "Binomial": model_class = H2OBinomialGridSearch elif model_type == "Clustering": model_class = H2OClusteringGridSearch elif model_type == "Regression": model_class = H2ORegressionGridSearch elif model_type == "Multinomial": model_class = H2OMultinomialGridSearch elif model_type == "AutoEncoder": model_class = H2OAutoEncoderGridSearch elif model_type == "DimReduction": model_class = H2ODimReductionGridSearch else: raise NotImplementedError(model_type) return model_class def get_grid(self, sort_by=None, decreasing=None): """ Retrieve an H2OGridSearch instance. Optionally specify a metric by which to sort models and a sort order. Parameters ---------- sort_by : str, optional A metric by which to sort the models in the grid space. Choices are "logloss", "residual_deviance", "mse", "auc", "r2", "accuracy", "precision", "recall", "f1", etc. decreasing : bool, optional Sort the models in decreasing order of metric if true, otherwise sort in increasing order (default). Returns ------- A new H2OGridSearch instance optionally sorted on the specified metric. """ if sort_by is None and decreasing is None: return self grid_json = h2o.api("GET /99/Grids/%s" % self._id, data={"sort_by": sort_by, "decreasing": decreasing}) grid = H2OGridSearch(self.model, self.hyper_params, self._id) grid.models = [h2o.get_model(key['name']) for key in grid_json['model_ids']] # reordered first_model_json = h2o.api("GET /99/Models/%s" % grid_json['model_ids'][0]['name'])['models'][0] model_class = H2OGridSearch._metrics_class(first_model_json) m = model_class() m._id = self._id m._grid_json = grid_json # m._metrics_class = metrics_class m._parms = grid._parms H2OEstimator.mixin(grid, model_class) grid.__dict__.update(m.__dict__.copy()) return grid # Deprecated functions; left here for backward compatibility _bcim = { "giniCoef": lambda self, *args, **kwargs: self.gini(*args, **kwargs) }
class MetricsBase(backwards_compatible()): """ A parent class to house common metrics available for the various Metrics types. The methods here are available across different model categories. """ def __init__(self, metric_json, on=None, algo=""): super(MetricsBase, self).__init__() # Yep, it's messed up... if isinstance(metric_json, MetricsBase): metric_json = metric_json._metric_json self._metric_json = metric_json # train and valid and xval are not mutually exclusive -- could have a test. train and # valid only make sense at model build time. self._on_train = False self._on_valid = False self._on_xval = False self._algo = algo if on == "training_metrics": self._on_train = True elif on == "validation_metrics": self._on_valid = True elif on == "cross_validation_metrics": self._on_xval = True elif on is None: pass else: raise ValueError("on expected to be train,valid,or xval. Got: " + str(on)) @classmethod def make(cls, kvs): """Factory method to instantiate a MetricsBase object from the list of key-value pairs.""" return cls(metric_json=dict(kvs)) def __repr__(self): # FIXME !!! __repr__ should never print anything, but return a string self.show() return "" # TODO: convert to actual fields list def __getitem__(self, key): return self._metric_json.get(key) @staticmethod def _has(dictionary, key): return key in dictionary and dictionary[key] is not None def show(self): """Display a short summary of the metrics.""" metric_type = self._metric_json['__meta']['schema_type'] types_w_glm = ['ModelMetricsRegressionGLM', 'ModelMetricsBinomialGLM'] types_w_clustering = ['ModelMetricsClustering'] types_w_mult = ['ModelMetricsMultinomial'] types_w_ord = ['ModelMetricsOrdinal'] types_w_bin = ['ModelMetricsBinomial', 'ModelMetricsBinomialGLM'] types_w_r2 = ['ModelMetricsRegressionGLM'] types_w_mean_residual_deviance = ['ModelMetricsRegressionGLM', 'ModelMetricsRegression'] types_w_mean_absolute_error = ['ModelMetricsRegressionGLM', 'ModelMetricsRegression'] types_w_logloss = types_w_bin + types_w_mult+types_w_ord types_w_dim = ["ModelMetricsGLRM"] print() print(metric_type + ": " + self._algo) reported_on = "** Reported on {} data. **" if self._on_train: print(reported_on.format("train")) elif self._on_valid: print(reported_on.format("validation")) elif self._on_xval: print(reported_on.format("cross-validation")) else: print(reported_on.format("test")) print() print("MSE: " + str(self.mse())) print("RMSE: " + str(self.rmse())) if metric_type in types_w_mean_absolute_error: print("MAE: " + str(self.mae())) print("RMSLE: " + str(self.rmsle())) if metric_type in types_w_r2: print("R^2: " + str(self.r2())) if metric_type in types_w_mean_residual_deviance: print("Mean Residual Deviance: " + str(self.mean_residual_deviance())) if metric_type in types_w_logloss: print("LogLoss: " + str(self.logloss())) if metric_type == 'ModelMetricsBinomial': # second element for first threshold is the actual mean per class error print("Mean Per-Class Error: %s" % self.mean_per_class_error()[0][1]) if metric_type == 'ModelMetricsMultinomial' or metric_type == 'ModelMetricsOrdinal': print("Mean Per-Class Error: " + str(self.mean_per_class_error())) if metric_type in types_w_glm: print("Null degrees of freedom: " + str(self.null_degrees_of_freedom())) print("Residual degrees of freedom: " + str(self.residual_degrees_of_freedom())) print("Null deviance: " + str(self.null_deviance())) print("Residual deviance: " + str(self.residual_deviance())) print("AIC: " + str(self.aic())) if metric_type in types_w_bin: print("AUC: " + str(self.auc())) print("Gini: " + str(self.gini())) self.confusion_matrix().show() self._metric_json["max_criteria_and_metric_scores"].show() if self.gains_lift(): print(self.gains_lift()) if (metric_type in types_w_mult) or (metric_type in types_w_ord): self.confusion_matrix().show() self.hit_ratio_table().show() if metric_type in types_w_clustering: print("Total Within Cluster Sum of Square Error: " + str(self.tot_withinss())) print("Total Sum of Square Error to Grand Mean: " + str(self.totss())) print("Between Cluster Sum of Square Error: " + str(self.betweenss())) self._metric_json['centroid_stats'].show() if metric_type in types_w_dim: print("Sum of Squared Error (Numeric): " + str(self.num_err())) print("Misclassification Error (Categorical): " + str(self.cat_err())) if self.custom_metric_name(): print("{}: {}".format(self.custom_metric_name(), self.custom_metric_value())) def r2(self): """The R squared coefficient.""" return self._metric_json["r2"] def logloss(self): """Log loss.""" return self._metric_json["logloss"] def nobs(self): """The number of observations.""" return self._metric_json["nobs"] def mean_residual_deviance(self): """The mean residual deviance for this set of metrics.""" return self._metric_json["mean_residual_deviance"] def auc(self): """The AUC for this set of metrics.""" return self._metric_json['AUC'] def aic(self): """The AIC for this set of metrics.""" return self._metric_json['AIC'] def gini(self): """Gini coefficient.""" return self._metric_json['Gini'] def mse(self): """The MSE for this set of metrics.""" return self._metric_json['MSE'] def rmse(self): """The RMSE for this set of metrics.""" return self._metric_json['RMSE'] def mae(self): """The MAE for this set of metrics.""" return self._metric_json['mae'] def rmsle(self): """The RMSLE for this set of metrics.""" return self._metric_json['rmsle'] def residual_deviance(self): """The residual deviance if the model has it, otherwise None.""" if MetricsBase._has(self._metric_json, "residual_deviance"): return self._metric_json["residual_deviance"] return None def residual_degrees_of_freedom(self): """The residual DoF if the model has residual deviance, otherwise None.""" if MetricsBase._has(self._metric_json, "residual_degrees_of_freedom"): return self._metric_json["residual_degrees_of_freedom"] return None def null_deviance(self): """The null deviance if the model has residual deviance, otherwise None.""" if MetricsBase._has(self._metric_json, "null_deviance"): return self._metric_json["null_deviance"] return None def null_degrees_of_freedom(self): """The null DoF if the model has residual deviance, otherwise None.""" if MetricsBase._has(self._metric_json, "null_degrees_of_freedom"): return self._metric_json["null_degrees_of_freedom"] return None def mean_per_class_error(self): """The mean per class error.""" return self._metric_json['mean_per_class_error'] def custom_metric_name(self): """Name of custom metric or None.""" if MetricsBase._has(self._metric_json, "custom_metric_name"): return self._metric_json['custom_metric_name'] else: return None def custom_metric_value(self): """Value of custom metric or None.""" if MetricsBase._has(self._metric_json, "custom_metric_value"): return self._metric_json['custom_metric_value'] else: return None # Deprecated functions; left here for backward compatibility _bcim = { "giniCoef": lambda self, *args, **kwargs: self.gini(*args, **kwargs) }
class H2OGridSearch(backwards_compatible()): """ Grid Search of a Hyper-Parameter Space for a Model :param model: The type of model to be explored initialized with optional parameters that will be unchanged across explored models. :param hyper_params: A dictionary of string parameters (keys) and a list of values to be explored by grid search (values). :param str grid_id: The unique id assigned to the resulting grid object. If none is given, an id will automatically be generated. :param search_criteria: The optional dictionary of directives which control the search of the hyperparameter space. The dictionary can include values for: ``strategy``, ``max_models``, ``max_runtime_secs``, ``stopping_metric``, ``stopping_tolerance``, ``stopping_rounds`` and ``seed``. The default strategy, "Cartesian", covers the entire space of hyperparameter combinations. If you want to use cartesian grid search, you can leave the search_criteria argument unspecified. Specify the "RandomDiscrete" strategy to get random search of all the combinations of your hyperparameters with three ways of specifying when to stop the search: max number of models, max time, and metric-based early stopping (e.g., stop if MSE hasn’t improved by 0.0001 over the 5 best models). Examples below:: >>> criteria = {"strategy": "RandomDiscrete", "max_runtime_secs": 600, ... "max_models": 100, "stopping_metric": "AUTO", ... "stopping_tolerance": 0.00001, "stopping_rounds": 5, ... "seed": 123456} >>> criteria = {"strategy": "RandomDiscrete", "max_models": 42, ... "max_runtime_secs": 28800, "seed": 1234} >>> criteria = {"strategy": "RandomDiscrete", "stopping_metric": "AUTO", ... "stopping_tolerance": 0.001, "stopping_rounds": 10} >>> criteria = {"strategy": "RandomDiscrete", "stopping_rounds": 5, ... "stopping_metric": "misclassification", ... "stopping_tolerance": 0.00001} :param parallelism Level of parallelism during grid model building. 1 = sequential building (default). Use the value of 0 for adaptive parallelism - decided by H2O. Any number > 1 sets the exact number of models built in parallel. :returns: a new H2OGridSearch instance Examples -------- >>> from h2o.grid.grid_search import H2OGridSearch >>> from h2o.estimators.glm import H2OGeneralizedLinearEstimator >>> hyper_parameters = {'alpha': [0.01,0.5], 'lambda': [1e-5,1e-6]} >>> gs = H2OGridSearch(H2OGeneralizedLinearEstimator(family='binomial'), hyper_parameters) >>> training_data = h2o.import_file("smalldata/logreg/benign.csv") >>> gs.train(x=range(3) + range(4,11),y=3, training_frame=training_data) >>> gs.show() """ def __init__(self, model, hyper_params, grid_id=None, search_criteria=None, export_checkpoints_dir=None, parallelism=1): super(H2OGridSearch, self).__init__() assert_is_type(model, None, H2OEstimator, lambda mdl: issubclass(mdl, H2OEstimator)) assert_is_type(hyper_params, dict) assert_is_type(grid_id, None, str) assert_is_type(search_criteria, None, dict) if not (model is None or is_type(model, H2OEstimator)): model = model() self._id = grid_id self.model = model self.hyper_params = dict(hyper_params) self.search_criteria = None if search_criteria is None else dict(search_criteria) self.export_checkpoints_dir = export_checkpoints_dir self._parallelism = parallelism # Degree of parallelism during model building self._grid_json = None self.models = None # list of H2O Estimator instances self._parms = {} # internal, for object recycle # self.parms = {} # external# self._future = False # used by __repr__/show to query job state# self._job = None # used when _future is True# @property def grid_id(self): """A key that identifies this grid search object in H2O.""" return self._id @grid_id.setter def grid_id(self, value): oldname = self.grid_id self._id = value h2o.rapids('(rename "{}" "{}")'.format(oldname, value)) @property def model_ids(self): return [i['name'] for i in self._grid_json["model_ids"]] @property def hyper_names(self): return self._grid_json["hyper_names"] @property def failed_params(self): return self._grid_json.get("failed_params", None) @property def failure_details(self): return self._grid_json.get("failure_details", None) @property def failure_stack_traces(self): return self._grid_json.get("failure_stack_traces", None) @property def failed_raw_params(self): return self._grid_json.get("failed_raw_params", None) def start(self, x, y=None, training_frame=None, offset_column=None, fold_column=None, weights_column=None, validation_frame=None, **params): """ Asynchronous model build by specifying the predictor columns, response column, and any additional frame-specific values. To block for results, call :meth:`join`. :param x: A list of column names or indices indicating the predictor columns. :param y: An index or a column name indicating the response column. :param training_frame: The H2OFrame having the columns indicated by x and y (as well as any additional columns specified by fold, offset, and weights). :param offset_column: The name or index of the column in training_frame that holds the offsets. :param fold_column: The name or index of the column in training_frame that holds the per-row fold assignments. :param weights_column: The name or index of the column in training_frame that holds the per-row weights. :param validation_frame: H2OFrame with validation data to be scored on while training. """ self._future = True self.train(x=x, y=y, training_frame=training_frame, offset_column=offset_column, fold_column=fold_column, weights_column=weights_column, validation_frame=validation_frame, **params) def join(self): """Wait until grid finishes computing.""" self._future = False self._job.poll() self._job = None def train(self, x=None, y=None, training_frame=None, offset_column=None, fold_column=None, weights_column=None, validation_frame=None, **params): """ Train the model synchronously (i.e. do not return until the model finishes training). To train asynchronously call :meth:`start`. :param x: A list of column names or indices indicating the predictor columns. :param y: An index or a column name indicating the response column. :param training_frame: The H2OFrame having the columns indicated by x and y (as well as any additional columns specified by fold, offset, and weights). :param offset_column: The name or index of the column in training_frame that holds the offsets. :param fold_column: The name or index of the column in training_frame that holds the per-row fold assignments. :param weights_column: The name or index of the column in training_frame that holds the per-row weights. :param validation_frame: H2OFrame with validation data to be scored on while training. """ algo_params = locals() parms = self._parms.copy() parms.update({k: v for k, v in algo_params.items() if k not in ["self", "params", "algo_params", "parms"]}) # dictionaries have special handling in grid search, avoid the implicit conversion parms["search_criteria"] = None if self.search_criteria is None else str(self.search_criteria) parms["export_checkpoints_dir"] = self.export_checkpoints_dir parms["parallelism"] = self._parallelism parms["hyper_parameters"] = None if self.hyper_params is None else str(self.hyper_params) # unique to grid search parms.update({k: v for k, v in list(self.model._parms.items()) if v is not None}) # unique to grid search parms.update(params) if '__class__' in parms: # FIXME: hackt for PY3 del parms['__class__'] y = algo_params["y"] tframe = algo_params["training_frame"] if tframe is None: raise ValueError("Missing training_frame") if y is not None: if is_type(y, list, tuple): if len(y) == 1: parms["y"] = y[0] else: raise ValueError('y must be a single column reference') if x is None: if(isinstance(y, int)): xset = set(range(training_frame.ncols)) - {y} else: xset = set(training_frame.names) - {y} else: xset = set() if is_type(x, int, str): x = [x] for xi in x: if is_type(xi, int): if not (-training_frame.ncols <= xi < training_frame.ncols): raise H2OValueError("Column %d does not exist in the training frame" % xi) xset.add(training_frame.names[xi]) else: if xi not in training_frame.names: raise H2OValueError("Column %s not in the training frame" % xi) xset.add(xi) x = list(xset) parms["x"] = x self.build_model(parms) def build_model(self, algo_params): """(internal)""" if algo_params["training_frame"] is None: raise ValueError("Missing training_frame") x = algo_params.pop("x") y = algo_params.pop("y", None) training_frame = algo_params.pop("training_frame") validation_frame = algo_params.pop("validation_frame", None) is_auto_encoder = (algo_params is not None) and ("autoencoder" in algo_params and algo_params["autoencoder"]) algo = self.model._compute_algo() # unique to grid search is_unsupervised = is_auto_encoder or algo == "pca" or algo == "svd" or algo == "kmeans" or algo == "glrm" if is_auto_encoder and y is not None: raise ValueError("y should not be specified for autoencoder.") if not is_unsupervised and y is None: raise ValueError("Missing response") if not is_unsupervised: y = y if y in training_frame.names else training_frame.names[y] self.model._estimator_type = "classifier" if training_frame.types[y] == "enum" else "regressor" self._model_build(x, y, training_frame, validation_frame, algo_params) def _model_build(self, x, y, tframe, vframe, kwargs): kwargs['training_frame'] = tframe if vframe is not None: kwargs["validation_frame"] = vframe if is_type(y, int): y = tframe.names[y] if y is not None: kwargs['response_column'] = y if not is_type(x, list, tuple): x = [x] if is_type(x[0], int): x = [tframe.names[i] for i in x] offset = kwargs["offset_column"] folds = kwargs["fold_column"] weights = kwargs["weights_column"] ignored_columns = list(set(tframe.names) - set(x + [y, offset, folds, weights])) kwargs["ignored_columns"] = None if not ignored_columns else [quoted(col) for col in ignored_columns] kwargs = dict([(k, kwargs[k].frame_id if isinstance(kwargs[k], H2OFrame) else kwargs[k]) for k in kwargs if kwargs[k] is not None]) # gruesome one-liner algo = self.model._compute_algo() # unique to grid search if self.grid_id is not None: kwargs["grid_id"] = self.grid_id rest_ver = kwargs.pop("_rest_version") if "_rest_version" in kwargs else None grid = H2OJob(h2o.api("POST /99/Grid/%s" % algo, data=kwargs), job_type=(algo + " Grid Build")) if self._future: self._job = grid return grid.poll() grid_json = h2o.api("GET /99/Grids/%s" % (grid.dest_key)) failure_messages_stacks = "" error_index = 0 if len(grid_json["failure_details"]) > 0: print("Errors/Warnings building gridsearch model\n") # will raise error if no grid model is returned, store error messages here for error_message in grid_json["failure_details"]: if isinstance(grid_json["failed_params"][error_index], dict): for h_name in grid_json['hyper_names']: print("Hyper-parameter: {0}, {1}".format(h_name, grid_json['failed_params'][error_index][h_name])) if len(grid_json["failure_stack_traces"]) > error_index: print("failure_details: {0}\nfailure_stack_traces: " "{1}\n".format(error_message, grid_json['failure_stack_traces'][error_index])) failure_messages_stacks += error_message+'\n' error_index += 1 self.models = [h2o.get_model(key['name']) for key in grid_json['model_ids']] for model in self.models: model._estimator_type = self.model._estimator_type # get first model returned in list of models from grid search to get model class (binomial, multinomial, etc) # sometimes no model is returned due to bad parameter values provided by the user. if len(grid_json['model_ids']) > 0: first_model_json = h2o.api("GET /%d/Models/%s" % (rest_ver or 3, grid_json['model_ids'][0]['name']))['models'][0] self._resolve_grid(grid.dest_key, grid_json, first_model_json) else: if len(failure_messages_stacks)>0: raise ValueError(failure_messages_stacks) else: raise ValueError("Gridsearch returns no model due to bad parameter values or other reasons....") def _resolve_grid(self, grid_id, grid_json, first_model_json): model_class = H2OGridSearch._metrics_class(first_model_json) m = model_class() m._id = grid_id m._grid_json = grid_json # m._metrics_class = metrics_class m._parms = self._parms self.export_checkpoints_dir = m._grid_json["export_checkpoints_dir"] H2OEstimator.mixin(self, model_class) self.__dict__.update(m.__dict__.copy()) def __getitem__(self, item): return self.models[item] def __iter__(self): nmodels = len(self.models) return (self[i] for i in range(nmodels)) def __len__(self): return len(self.models) def __repr__(self): self.show() return "" def predict(self, test_data): """ Predict on a dataset. :param H2OFrame test_data: Data to be predicted on. :returns: H2OFrame filled with predictions. """ return {model.model_id: model.predict(test_data) for model in self.models} def is_cross_validated(self): """Return True if the model was cross-validated.""" return {model.model_id: model.is_cross_validated() for model in self.models} def xval_keys(self): """Model keys for the cross-validated model.""" return {model.model_id: model.xval_keys() for model in self.models} def get_xval_models(self, key=None): """ Return a Model object. :param str key: If None, return all cross-validated models; otherwise return the model specified by the key. :returns: A model or a list of models. """ return {model.model_id: model.get_xval_models(key) for model in self.models} def xvals(self): """Return the list of cross-validated models.""" return {model.model_id: model.xvals for model in self.models} def deepfeatures(self, test_data, layer): """ Obtain a hidden layer's details on a dataset. :param test_data: Data to create a feature space on. :param int layer: Index of the hidden layer. :returns: A dictionary of hidden layer details for each model. """ return {model.model_id: model.deepfeatures(test_data, layer) for model in self.models} def weights(self, matrix_id=0): """ Return the frame for the respective weight matrix. :param: matrix_id: an integer, ranging from 0 to number of layers, that specifies the weight matrix to return. :returns: an H2OFrame which represents the weight matrix identified by matrix_id """ return {model.model_id: model.weights(matrix_id) for model in self.models} def biases(self, vector_id=0): """ Return the frame for the respective bias vector. :param: vector_id: an integer, ranging from 0 to number of layers, that specifies the bias vector to return. :returns: an H2OFrame which represents the bias vector identified by vector_id """ return {model.model_id: model.biases(vector_id) for model in self.models} def normmul(self): """Normalization/Standardization multipliers for numeric predictors.""" return {model.model_id: model.normmul() for model in self.models} def normsub(self): """Normalization/Standardization offsets for numeric predictors.""" return {model.model_id: model.normsub() for model in self.models} def respmul(self): """Normalization/Standardization multipliers for numeric response.""" return {model.model_id: model.respmul() for model in self.models} def respsub(self): """Normalization/Standardization offsets for numeric response.""" return {model.model_id: model.respsub() for model in self.models} def catoffsets(self): """ Categorical offsets for one-hot encoding """ return {model.model_id: model.catoffsets() for model in self.models} def model_performance(self, test_data=None, train=False, valid=False, xval=False): """ Generate model metrics for this model on test_data. :param test_data: Data set for which model metrics shall be computed against. All three of train, valid and xval arguments are ignored if test_data is not None. :param train: Report the training metrics for the model. :param valid: Report the validation metrics for the model. :param xval: Report the validation metrics for the model. :return: An object of class H2OModelMetrics. """ return {model.model_id: model.model_performance(test_data, train, valid, xval) for model in self.models} def scoring_history(self): """ Retrieve model scoring history. :returns: Score history (H2OTwoDimTable) """ return {model.model_id: model.scoring_history() for model in self.models} def summary(self, header=True): """Print a detailed summary of the explored models.""" table = [] for model in self.models: model_summary = model._model_json["output"]["model_summary"] r_values = list(model_summary.cell_values[0]) r_values[0] = model.model_id table.append(r_values) # if h2o.can_use_pandas(): # import pandas # pandas.options.display.max_rows = 20 # print pandas.DataFrame(table,columns=self.col_header) # return print() if header: print('Grid Summary:') print() H2ODisplay(table, header=['Model Id'] + model_summary.col_header[1:], numalign="left", stralign="left") def show(self): """Print models sorted by metric.""" hyper_combos = itertools.product(*list(self.hyper_params.values())) if not self.models: c_values = [[idx + 1, list(val)] for idx, val in enumerate(hyper_combos)] print(H2OTwoDimTable( col_header=['Model', 'Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']'], table_header='Grid Search of Model ' + self.model.__class__.__name__, cell_values=c_values)) else: print(self.sorted_metric_table()) def varimp(self, use_pandas=False): """ Pretty print the variable importances, or return them in a list/pandas DataFrame. :param bool use_pandas: If True, then the variable importances will be returned as a pandas data frame. :returns: A dictionary of lists or Pandas DataFrame instances. """ return {model.model_id: model.varimp(use_pandas) for model in self.models} def residual_deviance(self, train=False, valid=False, xval=False): """ Retreive the residual deviance if this model has the attribute, or None otherwise. :param bool train: Get the residual deviance for the training set. If both train and valid are False, then train is selected by default. :param bool valid: Get the residual deviance for the validation set. If both train and valid are True, then train is selected by default. :param bool xval: Get the residual deviance for the cross-validated models. :returns: the residual deviance, or None if it is not present. """ return {model.model_id: model.residual_deviance(train, valid, xval) for model in self.models} def residual_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the residual degress of freedom if this model has the attribute, or None otherwise. :param bool train: Get the residual dof for the training set. If both train and valid are False, then train is selected by default. :param bool valid: Get the residual dof for the validation set. If both train and valid are True, then train is selected by default. :param bool xval: Get the residual dof for the cross-validated models. :returns: the residual degrees of freedom, or None if they are not present. """ return {model.model_id: model.residual_degrees_of_freedom(train, valid, xval) for model in self.models} def null_deviance(self, train=False, valid=False, xval=False): """ Retreive the null deviance if this model has the attribute, or None otherwise. :param bool train: Get the null deviance for the training set. If both train and valid are False, then train is selected by default. :param bool valid: Get the null deviance for the validation set. If both train and valid are True, then train is selected by default. :param bool xval: Get the null deviance for the cross-validated models. :returns: the null deviance, or None if it is not present. """ return {model.model_id: model.null_deviance(train, valid, xval) for model in self.models} def null_degrees_of_freedom(self, train=False, valid=False, xval=False): """ Retreive the null degress of freedom if this model has the attribute, or None otherwise. :param bool train: Get the null dof for the training set. If both train and valid are False, then train is selected by default. :param bool valid: Get the null dof for the validation set. If both train and valid are True, then train is selected by default. :param bool xval: Get the null dof for the cross-validated models. :returns: the null dof, or None if it is not present. """ return {model.model_id: model.null_degrees_of_freedom(train, valid, xval) for model in self.models} def pprint_coef(self): """Pretty print the coefficents table (includes normalized coefficients).""" for i, model in enumerate(self.models): print('Model', i) model.pprint_coef() print() def coef(self): """Return the coefficients that can be applied to the non-standardized data. Note: standardize = True by default. If set to False, then coef() returns the coefficients that are fit directly. """ return {model.model_id: model.coef() for model in self.models} def coef_norm(self): """Return coefficients fitted on the standardized data (requires standardize = True, which is on by default). These coefficients can be used to evaluate variable importance. """ return {model.model_id: model.coef_norm() for model in self.models} def r2(self, train=False, valid=False, xval=False): """ Return the R^2 for this regression model. The R^2 value is defined to be ``1 - MSE/var``, where ``var`` is computed as ``sigma^2``. 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 bool train: If train is True, then return the R^2 value for the training data. :param bool valid: If valid is True, then return the R^2 value for the validation data. :param bool xval: If xval is True, then return the R^2 value for the cross validation data. :returns: The R^2 for this regression model. """ return {model.model_id: model.r2(train, valid, xval) for model in self.models} def mse(self, train=False, valid=False, xval=False): """ Get the MSE(s). 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 bool train: If train is True, then return the MSE value for the training data. :param bool valid: If valid is True, then return the MSE value for the validation data. :param bool xval: If xval is True, then return the MSE value for the cross validation data. :returns: The MSE for this regression model. """ return {model.model_id: model.mse(train, valid, xval) for model in self.models} def logloss(self, train=False, valid=False, xval=False): """ Get the Log Loss(s). 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 bool train: If train is True, then return the Log Loss value for the training data. :param bool valid: If valid is True, then return the Log Loss value for the validation data. :param bool xval: If xval is True, then return the Log Loss value for the cross validation data. :returns: The Log Loss for this binomial model. """ return {model.model_id: model.logloss(train, valid, xval) for model in self.models} def mean_residual_deviance(self, train=False, valid=False, xval=False): """ Get the Mean Residual Deviances(s). 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 bool train: If train is True, then return the Mean Residual Deviance value for the training data. :param bool valid: If valid is True, then return the Mean Residual Deviance value for the validation data. :param bool xval: If xval is True, then return the Mean Residual Deviance value for the cross validation data. :returns: The Mean Residual Deviance for this regression model. """ return {model.model_id: model.mean_residual_deviance(train, valid, xval) for model in self.models} def auc(self, train=False, valid=False, xval=False): """ Get the AUC(s). 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 bool train: If train is True, then return the AUC value for the training data. :param bool valid: If valid is True, then return the AUC value for the validation data. :param bool xval: If xval is True, then return the AUC value for the validation data. :returns: The AUC. """ return {model.model_id: model.auc(train, valid, xval) for model in self.models} def aic(self, train=False, valid=False, xval=False): """ Get the AIC(s). 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 bool train: If train is True, then return the AIC value for the training data. :param bool valid: If valid is True, then return the AIC value for the validation data. :param bool xval: If xval is True, then return the AIC value for the validation data. :returns: The AIC. """ return {model.model_id: model.aic(train, valid, xval) for model in self.models} def gini(self, train=False, valid=False, xval=False): """ Get the Gini Coefficient(s). 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 bool train: If train is True, then return the Gini Coefficient value for the training data. :param bool valid: If valid is True, then return the Gini Coefficient value for the validation data. :param bool xval: If xval is True, then return the Gini Coefficient value for the cross validation data. :returns: The Gini Coefficient for this binomial model. """ return {model.model_id: model.gini(train, valid, xval) for model in self.models} def get_hyperparams(self, id, display=True): """ Get the hyperparameters of a model explored by grid search. :param str id: The model id of the model with hyperparameters of interest. :param bool display: Flag to indicate whether to display the hyperparameter names. :returns: A list of the hyperparameters for the specified model. """ idx = id if is_type(id, int) else self.model_ids.index(id) model = self[idx] # if cross-validation is turned on, parameters in one of the fold model actuall contains the max_runtime_secs # parameter and not the main model that is returned. if model._is_xvalidated: model = h2o.get_model(model._xval_keys[0]) res = [model.params[h]['actual'][0] if isinstance(model.params[h]['actual'], list) else model.params[h]['actual'] for h in self.hyper_params] if display: print('Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']') return res def get_hyperparams_dict(self, id, display=True): """ Derived and returned the model parameters used to train the particular grid search model. :param str id: The model id of the model with hyperparameters of interest. :param bool display: Flag to indicate whether to display the hyperparameter names. :returns: A dict of model pararmeters derived from the hyper-parameters used to train this particular model. """ idx = id if is_type(id, int) else self.model_ids.index(id) model = self[idx] model_params = dict() # if cross-validation is turned on, parameters in one of the fold model actual contains the max_runtime_secs # parameter and not the main model that is returned. if model._is_xvalidated: model = h2o.get_model(model._xval_keys[0]) for param_name in self.hyper_names: model_params[param_name] = model.params[param_name]['actual'][0] if \ isinstance(model.params[param_name]['actual'], list) else model.params[param_name]['actual'] if display: print('Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']') return model_params def sorted_metric_table(self): """ Retrieve summary table of an H2O Grid Search. :returns: The summary table as an H2OTwoDimTable or a Pandas DataFrame. """ summary = self._grid_json["summary_table"] if summary is not None: return summary.as_data_frame() print("No sorted metric table for this grid search") @staticmethod def _metrics_class(model_json): model_type = model_json["output"]["model_category"] if model_type == "Binomial": model_class = H2OBinomialGridSearch elif model_type == "Clustering": model_class = H2OClusteringGridSearch elif model_type == "Regression": model_class = H2ORegressionGridSearch elif model_type == "Multinomial": model_class = H2OMultinomialGridSearch elif model_type == "Ordinal": model_class = H2OOrdinalGridSearch elif model_type == "AutoEncoder": model_class = H2OAutoEncoderGridSearch elif model_type == "DimReduction": model_class = H2ODimReductionGridSearch else: raise NotImplementedError(model_type) return model_class def get_grid(self, sort_by=None, decreasing=None): """ Retrieve an H2OGridSearch instance. Optionally specify a metric by which to sort models and a sort order. Note that if neither cross-validation nor a validation frame is used in the grid search, then the training metrics will display in the "get grid" output. If a validation frame is passed to the grid, and ``nfolds = 0``, then the validation metrics will display. However, if ``nfolds`` > 1, then cross-validation metrics will display even if a validation frame is provided. :param str sort_by: A metric by which to sort the models in the grid space. Choices are: ``"logloss"``, ``"residual_deviance"``, ``"mse"``, ``"auc"``, ``"r2"``, ``"accuracy"``, ``"precision"``, ``"recall"``, ``"f1"``, etc. :param bool decreasing: Sort the models in decreasing order of metric if true, otherwise sort in increasing order (default). :returns: A new H2OGridSearch instance optionally sorted on the specified metric. """ if sort_by is None and decreasing is None: return self grid_json = h2o.api("GET /99/Grids/%s" % self._id, data={"sort_by": sort_by, "decreasing": decreasing}) grid = H2OGridSearch(self.model, self.hyper_params, self._id) grid.models = [h2o.get_model(key['name']) for key in grid_json['model_ids']] # reordered first_model_json = h2o.api("GET /99/Models/%s" % grid_json['model_ids'][0]['name'])['models'][0] model_class = H2OGridSearch._metrics_class(first_model_json) m = model_class() m._id = self._id m._grid_json = grid_json # m._metrics_class = metrics_class m._parms = grid._parms H2OEstimator.mixin(grid, model_class) grid.__dict__.update(m.__dict__.copy()) return grid # Deprecated functions; left here for backward compatibility _bcim = { "giniCoef": lambda self, *args, **kwargs: self.gini(*args, **kwargs) } @deprecated("grid.sort_by() is deprecated; use grid.get_grid() instead") def sort_by(self, metric, increasing=True): """Deprecated since 2016-12-12, use grid.get_grid() instead.""" if metric[-1] != ')': metric += '()' c_values = [list(x) for x in zip(*sorted(eval('self.' + metric + '.items()'), key=lambda k_v: k_v[1]))] c_values.insert(1, [self.get_hyperparams(model_id, display=False) for model_id in c_values[0]]) if not increasing: for col in c_values: col.reverse() if metric[-2] == '(': metric = metric[:-2] return H2OTwoDimTable( col_header=['Model Id', 'Hyperparameters: [' + ', '.join(list(self.hyper_params.keys())) + ']', metric], table_header='Grid Search Results for ' + self.model.__class__.__name__, cell_values=[list(x) for x in zip(*c_values)])