Ejemplo n.º 1
0
    def transform(self, X, y=None):
        """Transforms input data by selecting features. If the component_obj does not have a transform method, will raise an MethodPropertyNotFoundError exception.

        Arguments:
            X (ww.DataTable, pd.DataFrame): Data to transform.
            y (ww.DataColumn, pd.Series, optional): Target data. Ignored.

        Returns:
            ww.DataTable: Transformed X
        """
        X = _convert_to_woodwork_structure(X)
        X = _convert_woodwork_types_wrapper(X.to_dataframe())
        self.input_feature_names = list(X.columns.values)

        try:
            X_t = self._component_obj.transform(X)
        except AttributeError:
            raise MethodPropertyNotFoundError(
                "Feature selector requires a transform method or a component_obj that implements transform"
            )

        X_dtypes = X.dtypes.to_dict()
        selected_col_names = self.get_names()
        col_types = {key: X_dtypes[key] for key in selected_col_names}
        features = pd.DataFrame(X_t, columns=selected_col_names,
                                index=X.index).astype(col_types)
        return _convert_to_woodwork_structure(features)
Ejemplo n.º 2
0
    def feature_importance(self):
        """Returns importance associated with each feature.

        Returns:
            np.ndarray: Importance associated with each feature
        """
        try:
            return self._component_obj.feature_importances_
        except AttributeError:
            raise MethodPropertyNotFoundError("Estimator requires a feature_importance property or a component_obj that implements feature_importances_")
Ejemplo n.º 3
0
    def transform(self, X, y=None):
        """Transforms data X

        Arguments:
            X (pd.DataFrame): Data to transform
            y (pd.Series, optional): Target data
        Returns:
            pd.DataFrame: Transformed X
        """
        try:
            X_t = self._component_obj.transform(X)
        except AttributeError:
            raise MethodPropertyNotFoundError("Transformer requires a transform method or a component_obj that implements transform")
        if not isinstance(X_t, pd.DataFrame) and isinstance(X, pd.DataFrame):
            return pd.DataFrame(X_t, columns=X.columns, index=X.index)
        return pd.DataFrame(X_t)
Ejemplo n.º 4
0
    def predict_proba(self, X):
        """Make probability estimates for labels.

        Arguments:
            X (ww.DataTable, pd.DataFrame, or np.ndarray): Features

        Returns:
            ww.DataTable: Probability estimates
        """
        try:
            X = infer_feature_types(X)
            X = _convert_woodwork_types_wrapper(X.to_dataframe())
            pred_proba = self._component_obj.predict_proba(X)
        except AttributeError:
            raise MethodPropertyNotFoundError("Estimator requires a predict_proba method or a component_obj that implements predict_proba")
        return infer_feature_types(pred_proba)
Ejemplo n.º 5
0
    def predict(self, X):
        """Make predictions using selected features.

        Arguments:
            X (ww.DataTable, pd.DataFrame, or np.ndarray): Data of shape [n_samples, n_features]

        Returns:
            ww.DataColumn: Predicted values
        """
        try:
            X = infer_feature_types(X)
            X = _convert_woodwork_types_wrapper(X.to_dataframe())
            predictions = self._component_obj.predict(X)
        except AttributeError:
            raise MethodPropertyNotFoundError("Estimator requires a predict method or a component_obj that implements predict")
        return infer_feature_types(predictions)
Ejemplo n.º 6
0
    def fit(self, X, y=None):
        """Fits component to data

        Arguments:
            X (list, ww.DataTable, pd.DataFrame or np.ndarray): The input training data of shape [n_samples, n_features]
            y (list, ww.DataColumn, pd.Series, np.ndarray, optional): The target training data of length [n_samples]

        Returns:
            self
        """
        X = infer_feature_types(X)
        X = _convert_woodwork_types_wrapper(X.to_dataframe())
        if y is not None:
            y = infer_feature_types(y)
            y = _convert_woodwork_types_wrapper(y.to_series())
        try:
            self._component_obj.fit(X, y)
            return self
        except AttributeError:
            raise MethodPropertyNotFoundError("Component requires a fit method or a component_obj that implements fit")
Ejemplo n.º 7
0
    def transform(self, X, y=None):
        """Transforms data X.

        Arguments:
            X (ww.DataTable, pd.DataFrame): Data to transform.
            y (ww.DataColumn, pd.Series, optional): Target data.

        Returns:
            ww.DataTable: Transformed X
        """
        try:
            X = _convert_to_woodwork_structure(X)
            X = _convert_woodwork_types_wrapper(X.to_dataframe())
            if y is not None:
                y = _convert_to_woodwork_structure(y)
                y = _convert_woodwork_types_wrapper(y.to_series())
            X_t = self._component_obj.transform(X, y)
        except AttributeError:
            raise MethodPropertyNotFoundError(
                "Transformer requires a transform method or a component_obj that implements transform"
            )
        X_t_df = pd.DataFrame(X_t, columns=X.columns, index=X.index)
        return _convert_to_woodwork_structure(X_t_df)