Example #1
0
    def manifold(self, transformer):
        """
        Creates the manifold estimator if a string value is passed in,
        validates other objects passed in.
        """
        if not is_estimator(transformer):
            if transformer not in self.ALGORITHMS:
                raise YellowbrickValueError(
                    "could not create manifold for '%s'".format(
                        str(transformer)))

            # Create a new transformer with the specified params
            self._name = MANIFOLD_NAMES[transformer]
            transformer = clone(self.ALGORITHMS[transformer])
            params = {
                "n_components": 2,
                "n_neighbors": self.n_neighbors,
                "random_state": self.random_state,
            }

            for param in list(params.keys()):
                if param not in transformer.get_params():
                    del params[param]

            transformer.set_params(**params)

        self._manifold = transformer
        if self._name is None:
            self._name = self._manifold.__class__.__name__
Example #2
0
    def test_manifold_construction(self):
        """
        Should be able to construct a manifold estimator from a string
        """
        # TODO: parametrize this once unittest.TestCase dependency removed.
        algorithms = [
            "lle",
            "ltsa",
            "hessian",
            "modified",
            "isomap",
            "mds",
            "spectral",
            "tsne",
        ]

        for algorithm in algorithms:
            message = "case failed for {}".format(algorithm)
            params = {
                "n_neighbors": 18,
                "random_state": 53,
            }
            oz = Manifold(manifold=algorithm, **params)
            assert is_estimator(oz.manifold), message
            assert oz.manifold.get_params()["n_components"] == 2, message

            manifold_params = oz.manifold.get_params()
            for param, value in params.items():
                if param in manifold_params:
                    assert value == manifold_params[param], message
Example #3
0
    def manifold(self, transformer):
        """
        Creates the manifold estimator if a string value is passed in,
        validates other objects passed in.
        """
        if not is_estimator(transformer):
            if transformer not in self.ALGORITHMS:
                raise YellowbrickValueError(
                    "could not create manifold for '%s'".format(str(transformer))
                )

            # Create a new transformer with the specified params
            self._name = MANIFOLD_NAMES[transformer]
            transformer = clone(self.ALGORITHMS[transformer])
            params = {
                "n_components": 2,
                "n_neighbors": self.n_neighbors,
                "random_state": self.random_state,
            }

            for param in list(params.keys()):
                if param not in transformer.get_params():
                    del params[param]

            transformer.set_params(**params)

        self._manifold = transformer
        if self._name is None:
            self._name = self._manifold.__class__.__name__
Example #4
0
def get_model_name(model):
    """
    Detects the model name for a Scikit-Learn model or pipeline.

    Parameters
    ----------
    model: class or instance
        The object to determine the name for. If the model is an estimator it
        returns the class name; if it is a Pipeline it returns the class name
        of the final transformer or estimator in the Pipeline.

    Returns
    -------
    name : string
        The name of the model or pipeline.
    """
    if not is_estimator(model):
        raise YellowbrickTypeError(
            "Cannot detect the model name for non estimator: '{}'".format(
                type(model)))

    if isinstance(model, Pipeline):
        return get_model_name(model.steps[-1][-1])
    elif isinstance(model, ContribEstimator):
        return model.estimator.__class__.__name__
    else:
        return model.__class__.__name__
Example #5
0
    def manifold(self, transformer):
        """
        Creates the manifold estimator if a string value is passed in,
        validates other objects passed in.
        """
        if not is_estimator(transformer):
            if transformer not in self.ALGORITHMS:
                raise YellowbrickValueError(
                    "could not create manifold for '{}'".format(
                        str(transformer)))

            # 2 components is required for 2D plots
            n_components = self.projection
            requires_default_neighbors = {
                "lle",
                "ltsa",
                "isomap",
                "hessian",
                "spectral",
                "modified",
            }

            # Check if the n_neighbors attribute needs to be set.
            if self.n_neighbors is None and transformer in requires_default_neighbors:
                if transformer == "hessian":
                    self.n_neighbors = int(1 + (n_components *
                                                (1 + (n_components + 1) / 2)))
                else:
                    self.n_neighbors = 5

                # Issue a warning that the n_neighbors was set to a default.
                warnmsg = (
                    "using n_neighbors={};"
                    " please explicitly specify for the '{}' manifold").format(
                        self.n_neighbors, str(transformer))
                warnings.warn(warnmsg, YellowbrickWarning)

            # Create a new transformer with the specified params
            self._name = MANIFOLD_NAMES[transformer]
            transformer = clone(self.ALGORITHMS[transformer])
            params = {
                "n_components": n_components,
                "n_neighbors": self.n_neighbors,
                "random_state": self.random_state,
            }

            for param in list(params.keys()):
                if param not in transformer.get_params():
                    del params[param]

            transformer.set_params(**params)

        self._manifold = transformer
        if self._name is None:
            self._name = self._manifold.__class__.__name__
Example #6
0
    def test_manifold_construction(self, algorithm):
        """
        Should be able to construct a manifold estimator from a string
        """
        message = "case failed for {}".format(algorithm)
        params = {"n_neighbors": 18, "random_state": 53}
        oz = Manifold(manifold=algorithm, **params)
        assert is_estimator(oz.manifold), message
        assert oz.manifold.get_params()["n_components"] == 2, message

        manifold_params = oz.manifold.get_params()
        for param, value in params.items():
            if param in manifold_params:
                assert value == manifold_params[param], message
    def test_manifold_construction(self):
        """
        Should be able to construct a manifold estimator from a string
        """
        # TODO: parametrize this once unittest.TestCase dependency removed.
        algorithms = [
            "lle", "ltsa", "hessian", "modified",
            "isomap", "mds", "spectral", "tsne",
        ]

        for algorithm in algorithms:
            message = "case failed for {}".format(algorithm)
            params = {
                "n_neighbors": 18,
                "random_state": 53,
            }
            oz = Manifold(manifold=algorithm, **params)
            assert is_estimator(oz.manifold), message
            assert oz.manifold.get_params()["n_components"] == 2, message

            manifold_params = oz.manifold.get_params()
            for param, value in params.items():
                if param in manifold_params:
                    assert value == manifold_params[param], message
Example #8
0
 def test_wraps_is_estimator(self):
     """
     Assert a wrapped estimator passes is_estimator check
     """
     tpe = wrap(ThirdPartyEstimator())
     assert is_estimator(tpe)