def transform(self, values): """Transform values and return the resulting feature matrix Parameters ---------- values : array-like, [n_samples] Returns ------- X : sparse matrix, shape [n_samples, n_features] Raises ------ NotFittedError If the vectorizer has not yet been fitted. """ if not hasattr(self, 'feature_names_'): raise utils.NotFittedError('Vectorizer has not yet been fitted') if self._lowercase: values = [value.lower() for value in values] values = np.asarray(values).reshape(-1, 1) return self._vectorizer.transform(values)
def transform(self, values): """Transform values and return the resulting feature matrix Parameters ---------- values : array-like, [n_samples] Returns ------- X : sparse matrix, [n_samples, n_features] Raises ------ NotFittedError If the vectorizer has not yet been fitted. """ if not hasattr(self, 'feature_names_'): raise utils.NotFittedError('Vectorizer has not yet been fitted') values = np.asarray(values) indices = np.digitize(values, self._bin_edges) ones = np.ones(indices.shape[0], dtype=bool) return sp.coo_matrix( (ones, (np.arange(indices.shape[0]), indices)), shape=(indices.shape[0], len(self.feature_names_)), dtype=bool)
def transform(self, values): """Transform values to feature matrix Parameters ---------- values : array-like, [n_samples] Strings for transforming. Returns ------- X : sparse matrix, shape [n_samples, n_features] Feature matrix. Raises ------ NotFittedError If the vectorizer has not yet been fitted. ValueError If `values` is not a one-dimensional array. """ if not hasattr(self, 'feature_names_'): raise utils.NotFittedError('Vectorizer has be yet been fitted') if self._categorical: values = [value.lower() for value in values] return self._vectorizer.transform(values).astype(bool) else: return self._vectorizer.transform(values)
def transform(self, values): """Transform booleans to feature matrix Parameters ---------- values : array-like, [n_samples] Booleans for transforming. Returns ------- X : ndarray, [n_samples, 1] Feature matrix. Raises ------ NotFittedError If the vectorizer has not yet been fitted. ValueError If `values` is not a one-dimensional array. """ if not hasattr(self, 'feature_names_'): raise utils.NotFittedError('Vectorizer has be yet been fitted') values = np.asarray(values, dtype=bool) if values.ndim != 1: raise ValueError( 'values must be a one dimensional array, not with shape {}'. format(values.shape)) return np.expand_dims(values, 1)
def transform(self, values): """Transform numbers to feature matrix Parameters ---------- values : array-like, [n_samples] Numbers for transforming. Returns ------- X : sparse matrix, [n_samples, n_features] Feature matrix. Raises ------ NotFittedError If the vectorizer has not yet been fitted. ValueError If `values` is not a one-dimensional array. """ if not hasattr(self, 'feature_names_'): raise utils.NotFittedError('Vectorizer has be yet been fitted') values = np.asarray(values) if values.ndim != 1: raise ValueError( 'values must be a one dimensional array, not with shape {}'. format(values.shape)) indices = np.digitize(values, self._bin_edges) ones = np.ones(indices.shape[0], dtype=bool) return sp.coo_matrix( (ones, (np.arange(indices.shape[0]), indices)), shape=(indices.shape[0], len(self.feature_names_)), dtype=bool)