Beispiel #1
0
    def fit_transform(self, X, y=None, **kwargs):
        """
        Fits the manifold on X and transforms the data to plot it on the axes.
        The optional y specified can be used to declare discrete colors. If
        the target is set to 'auto', this method also determines the target
        type, and therefore what colors will be used.

        Note also that fit records the amount of time it takes to fit the
        manifold and reports that information in the visualization.

        Parameters
        ----------
        X : array-like of shape (n, m)
            A matrix or data frame with n instances and m features

        y : array-like of shape (n,), optional
            A vector or series with target values for each instance in X. This
            vector is used to determine the color of the points in X.

        Returns
        -------
        Xprime : array-like of shape (n, 2)
            Returns the 2-dimensional embedding of the instances.

        """
        # Because some manifolds do not have transform, we cannot call individual
        # fit and transform methods, but must do it manually here.

        # Call super fit to compute features, classes, colors, etc.
        super(Manifold, self).fit(X, y)
        with Timer() as self.fit_time_:
            Xp = self.manifold.fit_transform(X)
        self.draw(Xp, y)
        return Xp
Beispiel #2
0
    def fit(self, X, y=None, **kwargs):
        """
        Fits the manifold on X and transforms the data to plot it on the axes.
        See fit_transform() for more details.

        Parameters
        ----------
        X : array-like of shape (n, m)
            A matrix or data frame with n instances and m features

        y : array-like of shape (n,), optional
            A vector or series with target values for each instance in X. This
            vector is used to determine the color of the points in X.

        Returns
        -------
        self : Manifold
            Returns the visualizer object.

        """
        if not hasattr(self.manifold, "transform"):
            name = self.manifold.__class__.__name__
            raise ModelError(
                ("{} requires data to be simultaneously fit and transformed, "
                 "use fit_transform instead").format(name))

        # Call super to compute features, classes, colors, etc.
        super(Manifold, self).fit(X, y)
        with Timer() as self.fit_time_:
            self.manifold.fit(X)
        return self
Beispiel #3
0
def time_model(X, y, estimator):
    """
    Test one estimator.
    """
    # Use Timer context manager to track fit time
    with Timer() as timer:

        # Instantiate the classification model and visualizer
        estimator.fit(X, y)
        _ = estimator.predict(X)

    return (estimator.__class__.__name__, timer.interval)
Beispiel #4
0
    def fit_transform(self, X, y=None):
        """
        Fits the manifold on X and transforms the data to plot it on the axes.
        The optional y specified can be used to declare discrete colors. If
        the target is set to 'auto', this method also determines the target
        type, and therefore what colors will be used.

        Note also that fit records the amount of time it takes to fit the
        manifold and reports that information in the visualization.

        Parameters
        ----------
        X : array-like of shape (n, m)
            A matrix or data frame with n instances and m features where m > 2.

        y : array-like of shape (n,), optional
            A vector or series with target values for each instance in X. This
            vector is used to determine the color of the points in X.

        Returns
        -------
        self : Manifold
            Returns the visualizer object.
        """
        # Determine target type
        self._determine_target_color_type(y)

        # Compute classes and colors if target type is discrete
        if self._target_color_type == DISCRETE:
            self.classes_ = np.unique(y)

            color_kwargs = {'n_colors': len(self.classes_)}

            if isinstance(self.colors, string_types):
                color_kwargs['colormap'] = self.colors
            else:
                color_kwargs['colors'] = self.colors

            self._colors = resolve_colors(**color_kwargs)

        # Compute target range if colors are continuous
        elif self._target_color_type == CONTINUOUS:
            y = np.asarray(y)
            self.range_ = (y.min(), y.max())

        with Timer() as self.fit_time_:
            Xp = self.manifold.fit_transform(X)

        self.draw(Xp, y)
        return Xp
Beispiel #5
0
def time_models(X, y, estimators):
    """
    Test various estimators.
    """
    fit_times = dict()
    for estimator in estimators:

        # Use Timer context manager to track fit time
        with Timer() as timer:

            # Instantiate the classification model and visualizer
            estimator.fit(X, y)
            _ = estimator.predict(X)

            # TODO: stop execution if the fit time is greater than 15min

        fit_times[estimator.__class__.__name__] = timer.interval

    return fit_times
Beispiel #6
0
    def fit(self, X, y=None):
        """
        Fit the clustering model, computing the centers then embeds the centers
        into 2D space using the embedding method specified.
        """
        with Timer() as self.fit_time_:
            if not check_fitted(self.estimator, is_fitted_by=self.is_fitted):
                # Fit the underlying estimator
                self.estimator.fit(X, y)

        # Get the centers
        # TODO: is this how sklearn stores all centers in the model?
        C = self.cluster_centers_

        # Embed the centers in 2D space and get the cluster scores
        self.embedded_centers_ = self.transformer.fit_transform(C)
        self.scores_ = self._score_clusters(X, y)

        # Draw the clusters and fit returns self
        self.draw()
        return self