desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Compute decision function of ``X`` for each iteration. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is ...
def staged_decision_function(self, X):
for dec in self._staged_decision_function(X): (yield dec)
'Predict class for X. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns y : array of shape = [n_samples] The predicted values.'
def predict(self, X):
score = self.decision_function(X) decisions = self.loss_._score_to_decision(score) return self.classes_.take(decisions, axis=0)
'Predict class at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse...
def staged_predict(self, X):
for score in self._staged_decision_function(X): decisions = self.loss_._score_to_decision(score) (yield self.classes_.take(decisions, axis=0))
'Predict class probabilities for X. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Raises AttributeError If the ``loss`` does not support probabilities....
def predict_proba(self, X):
score = self.decision_function(X) try: return self.loss_._score_to_proba(score) except NotFittedError: raise except AttributeError: raise AttributeError(('loss=%r does not support predict_proba' % self.loss))
'Predict class log-probabilities for X. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Raises AttributeError If the ``loss`` does not support probabilit...
def predict_log_proba(self, X):
proba = self.predict_proba(X) return np.log(proba)
'Predict class probabilities at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provid...
def staged_predict_proba(self, X):
try: for score in self._staged_decision_function(X): (yield self.loss_._score_to_proba(score)) except NotFittedError: raise except AttributeError: raise AttributeError(('loss=%r does not support predict_proba' % self.loss))
'Predict regression target for X. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns y : array of shape = [n_samples] The predicted values.'
def predict(self, X):
X = check_array(X, dtype=DTYPE, order='C', accept_sparse='csr') return self._decision_function(X).ravel()
'Predict regression target at each stage for X. This method allows monitoring (i.e. determine error on testing set) after each stage. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided...
def staged_predict(self, X):
for y in self._staged_decision_function(X): (yield y.ravel())
'Apply trees in the ensemble to X, return leaf indices. .. versionadded:: 0.17 Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted to a sparse ``csr_matrix``. ...
def apply(self, X):
leaves = super(GradientBoostingRegressor, self).apply(X) leaves = leaves.reshape(X.shape[0], self.estimators_.shape[0]) return leaves
'Fit the estimators. Parameters X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. sample_weight : array-like, shape = [n_samples] or None Sample weights....
def fit(self, X, y, sample_weight=None):
if (isinstance(y, np.ndarray) and (len(y.shape) > 1) and (y.shape[1] > 1)): raise NotImplementedError('Multilabel and multi-output classification is not supported.') if (self.voting not in ('soft', 'hard')): raise ValueError(("Voting must be 'soft' or 'hard'; ...
'Get the weights of not `None` estimators'
@property def _weights_not_none(self):
if (self.weights is None): return None return [w for (est, w) in zip(self.estimators, self.weights) if (est[1] is not None)]
'Predict class labels for X. Parameters X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. Returns maj : array-like, shape = [n_samples] Predicted class labels.'
def predict(self, X):
check_is_fitted(self, 'estimators_') if (self.voting == 'soft'): maj = np.argmax(self.predict_proba(X), axis=1) else: predictions = self._predict(X) maj = np.apply_along_axis((lambda x: np.argmax(np.bincount(x, weights=self._weights_not_none))), axis=1, arr=predictions) maj = sel...
'Collect results from clf.predict calls.'
def _collect_probas(self, X):
return np.asarray([clf.predict_proba(X) for clf in self.estimators_])
'Predict class probabilities for X in \'soft\' voting'
def _predict_proba(self, X):
if (self.voting == 'hard'): raise AttributeError(('predict_proba is not available when voting=%r' % self.voting)) check_is_fitted(self, 'estimators_') avg = np.average(self._collect_probas(X), axis=0, weights=self._weights_not_none) return avg
'Compute probabilities of possible outcomes for samples in X. Parameters X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. Returns avg : array-like, shape = [n_samples, n_classes] Weighted average probabi...
@property def predict_proba(self):
return self._predict_proba
'Return class labels or probabilities for X for each estimator. Parameters X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. Returns If `voting=\'soft\'` and `flatten_transform=True`: array-like = (n_clas...
def transform(self, X):
check_is_fitted(self, 'estimators_') if (self.voting == 'soft'): probas = self._collect_probas(X) if (self.flatten_transform is None): warnings.warn("'flatten_transform' default value will be changed to True in 0.21.To silence this warning you ...
'Setting the parameters for the voting classifier Valid parameter keys can be listed with get_params(). Parameters params: keyword arguments Specific parameters using e.g. set_params(parameter_name=new_value) In addition, to setting the parameters of the ``VotingClassifier``, the individual classifiers of the ``VotingC...
def set_params(self, **params):
super(VotingClassifier, self)._set_params('estimators', **params) return self
'Get the parameters of the VotingClassifier Parameters deep: bool Setting it to True gets the various classifiers and the parameters of the classifiers as well'
def get_params(self, deep=True):
return super(VotingClassifier, self)._get_params('estimators', deep=deep)
'Collect results from clf.predict calls.'
def _predict(self, X):
return np.asarray([clf.predict(X) for clf in self.estimators_]).T
'Build a boosted classifier/regressor from the training set (X, y). Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. COO, DOK, and LIL are converted to CSR. The dtype is forced to DTYPE from tree._tree if the base ...
def fit(self, X, y, sample_weight=None):
if (self.learning_rate <= 0): raise ValueError('learning_rate must be greater than zero') if ((self.base_estimator is None) or isinstance(self.base_estimator, (BaseDecisionTree, BaseForest))): dtype = DTYPE accept_sparse = 'csc' else: dtype = None accep...
'Implement a single boost. Warning: This method needs to be overridden by subclasses. Parameters iboost : int The index of the current boost iteration. X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. COO, DOK, and LIL are co...
@abstractmethod def _boost(self, iboost, X, y, sample_weight, random_state):
pass
'Return staged scores for X, y. This generator method yields the ensemble score after each iteration of boosting and therefore allows monitoring, such as to determine the score on a test set after each boost. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Spars...
def staged_score(self, X, y, sample_weight=None):
for y_pred in self.staged_predict(X): if is_classifier(self): (yield accuracy_score(y, y_pred, sample_weight=sample_weight)) else: (yield r2_score(y, y_pred, sample_weight=sample_weight))
'Return the feature importances (the higher, the more important the feature). Returns feature_importances_ : array, shape = [n_features]'
@property def feature_importances_(self):
if ((self.estimators_ is None) or (len(self.estimators_) == 0)): raise ValueError('Estimator not fitted, call `fit` before `feature_importances_`.') try: norm = self.estimator_weights_.sum() return (sum(((weight * clf.feature_importances_) for (weight, clf) in zip(self....
'Ensure that X is in the proper format'
def _validate_X_predict(self, X):
if ((self.base_estimator is None) or isinstance(self.base_estimator, (BaseDecisionTree, BaseForest))): X = check_array(X, accept_sparse='csr', dtype=DTYPE) else: X = check_array(X, accept_sparse=['csr', 'csc', 'coo']) return X
'Build a boosted classifier from the training set (X, y). Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. DOK and LIL are converted to CSR. y : array-like of shape = [n_samples] The target values (class labels). s...
def fit(self, X, y, sample_weight=None):
if (self.algorithm not in ('SAMME', 'SAMME.R')): raise ValueError(('algorithm %s is not supported' % self.algorithm)) return super(AdaBoostClassifier, self).fit(X, y, sample_weight)
'Check the estimator and set the base_estimator_ attribute.'
def _validate_estimator(self):
super(AdaBoostClassifier, self)._validate_estimator(default=DecisionTreeClassifier(max_depth=1)) if (self.algorithm == 'SAMME.R'): if (not hasattr(self.base_estimator_, 'predict_proba')): raise TypeError("AdaBoostClassifier with algorithm='SAMME.R' requires that the weak ...
'Implement a single boost. Perform a single boost according to the real multi-class SAMME.R algorithm or to the discrete SAMME algorithm and return the updated sample weights. Parameters iboost : int The index of the current boost iteration. X : {array-like, sparse matrix} of shape = [n_samples, n_features] The trainin...
def _boost(self, iboost, X, y, sample_weight, random_state):
if (self.algorithm == 'SAMME.R'): return self._boost_real(iboost, X, y, sample_weight, random_state) else: return self._boost_discrete(iboost, X, y, sample_weight, random_state)
'Implement a single boost using the SAMME.R real algorithm.'
def _boost_real(self, iboost, X, y, sample_weight, random_state):
estimator = self._make_estimator(random_state=random_state) estimator.fit(X, y, sample_weight=sample_weight) y_predict_proba = estimator.predict_proba(X) if (iboost == 0): self.classes_ = getattr(estimator, 'classes_', None) self.n_classes_ = len(self.classes_) y_predict = self.class...
'Implement a single boost using the SAMME discrete algorithm.'
def _boost_discrete(self, iboost, X, y, sample_weight, random_state):
estimator = self._make_estimator(random_state=random_state) estimator.fit(X, y, sample_weight=sample_weight) y_predict = estimator.predict(X) if (iboost == 0): self.classes_ = getattr(estimator, 'classes_', None) self.n_classes_ = len(self.classes_) incorrect = (y_predict != y) e...
'Predict classes for X. The predicted class of an input sample is computed as the weighted mean prediction of the classifiers in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. DOK and LIL are conver...
def predict(self, X):
pred = self.decision_function(X) if (self.n_classes_ == 2): return self.classes_.take((pred > 0), axis=0) return self.classes_.take(np.argmax(pred, axis=1), axis=0)
'Return staged predictions for X. The predicted class of an input sample is computed as the weighted mean prediction of the classifiers in the ensemble. This generator method yields the ensemble prediction after each iteration of boosting and therefore allows monitoring, such as to determine the prediction on a test se...
def staged_predict(self, X):
n_classes = self.n_classes_ classes = self.classes_ if (n_classes == 2): for pred in self.staged_decision_function(X): (yield np.array(classes.take((pred > 0), axis=0))) else: for pred in self.staged_decision_function(X): (yield np.array(classes.take(np.argmax(pre...
'Compute the decision function of ``X``. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. DOK and LIL are converted to CSR. Returns score : array, shape = [n_samples, k] The decision function of the input samples. ...
def decision_function(self, X):
check_is_fitted(self, 'n_classes_') X = self._validate_X_predict(X) n_classes = self.n_classes_ classes = self.classes_[:, np.newaxis] pred = None if (self.algorithm == 'SAMME.R'): pred = sum((_samme_proba(estimator, n_classes, X) for estimator in self.estimators_)) else: pre...
'Compute decision function of ``X`` for each boosting iteration. This method allows monitoring (i.e. determine error on testing set) after each boosting iteration. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. D...
def staged_decision_function(self, X):
check_is_fitted(self, 'n_classes_') X = self._validate_X_predict(X) n_classes = self.n_classes_ classes = self.classes_[:, np.newaxis] pred = None norm = 0.0 for (weight, estimator) in zip(self.estimator_weights_, self.estimators_): norm += weight if (self.algorithm == 'SAMME...
'Predict class probabilities for X. The predicted class probabilities of an input sample is computed as the weighted mean predicted class probabilities of the classifiers in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, ...
def predict_proba(self, X):
check_is_fitted(self, 'n_classes_') n_classes = self.n_classes_ X = self._validate_X_predict(X) if (n_classes == 1): return np.ones((X.shape[0], 1)) if (self.algorithm == 'SAMME.R'): proba = sum((_samme_proba(estimator, n_classes, X) for estimator in self.estimators_)) else: ...
'Predict class probabilities for X. The predicted class probabilities of an input sample is computed as the weighted mean predicted class probabilities of the classifiers in the ensemble. This generator method yields the ensemble predicted class probabilities after each iteration of boosting and therefore allows monito...
def staged_predict_proba(self, X):
X = self._validate_X_predict(X) n_classes = self.n_classes_ proba = None norm = 0.0 for (weight, estimator) in zip(self.estimator_weights_, self.estimators_): norm += weight if (self.algorithm == 'SAMME.R'): current_proba = _samme_proba(estimator, n_classes, X) el...
'Predict class log-probabilities for X. The predicted class log-probabilities of an input sample is computed as the weighted mean predicted class log-probabilities of the classifiers in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix ...
def predict_log_proba(self, X):
return np.log(self.predict_proba(X))
'Build a boosted regressor from the training set (X, y). Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. DOK and LIL are converted to CSR. y : array-like of shape = [n_samples] The target values (real numbers). sa...
def fit(self, X, y, sample_weight=None):
if (self.loss not in ('linear', 'square', 'exponential')): raise ValueError("loss must be 'linear', 'square', or 'exponential'") return super(AdaBoostRegressor, self).fit(X, y, sample_weight)
'Check the estimator and set the base_estimator_ attribute.'
def _validate_estimator(self):
super(AdaBoostRegressor, self)._validate_estimator(default=DecisionTreeRegressor(max_depth=3))
'Implement a single boost for regression Perform a single boost according to the AdaBoost.R2 algorithm and return the updated sample weights. Parameters iboost : int The index of the current boost iteration. X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can...
def _boost(self, iboost, X, y, sample_weight, random_state):
estimator = self._make_estimator(random_state=random_state) cdf = stable_cumsum(sample_weight) cdf /= cdf[(-1)] uniform_samples = random_state.random_sample(X.shape[0]) bootstrap_idx = cdf.searchsorted(uniform_samples, side='right') bootstrap_idx = np.array(bootstrap_idx, copy=False) estimat...
'Predict regression value for X. The predicted regression value of an input sample is computed as the weighted median prediction of the classifiers in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. ...
def predict(self, X):
check_is_fitted(self, 'estimator_weights_') X = self._validate_X_predict(X) return self._get_median_predict(X, len(self.estimators_))
'Return staged predictions for X. The predicted regression value of an input sample is computed as the weighted median prediction of the classifiers in the ensemble. This generator method yields the ensemble prediction after each iteration of boosting and therefore allows monitoring, such as to determine the prediction...
def staged_predict(self, X):
check_is_fitted(self, 'estimator_weights_') X = self._validate_X_predict(X) for (i, _) in enumerate(self.estimators_, 1): (yield self._get_median_predict(X, limit=i))
'Apply trees in the forest to X, return leaf indices. Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns X_leaves : arra...
def apply(self, X):
X = self._validate_X_predict(X) results = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend='threading')((delayed(parallel_helper)(tree, 'apply', X, check_input=False) for tree in self.estimators_)) return np.array(results).T
'Return the decision path in the forest .. versionadded:: 0.18 Parameters X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csr_matrix``. Returns indica...
def decision_path(self, X):
X = self._validate_X_predict(X) indicators = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend='threading')((delayed(parallel_helper)(tree, 'decision_path', X, check_input=False) for tree in self.estimators_)) n_nodes = [0] n_nodes.extend([i.shape[1] for i in indicators]) n_nodes_ptr = np.a...
'Build a forest of trees from the training set (X, y). Parameters X : array-like or sparse matrix of shape = [n_samples, n_features] The training input samples. Internally, its dtype will be converted to ``dtype=np.float32``. If a sparse matrix is provided, it will be converted into a sparse ``csc_matrix``. y : array-l...
def fit(self, X, y, sample_weight=None):
X = check_array(X, accept_sparse='csc', dtype=DTYPE) y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None) if (sample_weight is not None): sample_weight = check_array(sample_weight, ensure_2d=False) if issparse(X): X.sort_indices() (n_samples, self.n_features_) = X.sha...
'Validate X whenever one tries to predict, apply, predict_proba'
def _validate_X_predict(self, X):
if ((self.estimators_ is None) or (len(self.estimators_) == 0)): raise NotFittedError('Estimator not fitted, call `fit` before exploiting the model.') return self.estimators_[0]._validate_X_predict(X, check_input=True)
'Return the feature importances (the higher, the more important the feature). Returns feature_importances_ : array, shape = [n_features]'
@property def feature_importances_(self):
check_is_fitted(self, 'estimators_') all_importances = Parallel(n_jobs=self.n_jobs, backend='threading')((delayed(getattr)(tree, 'feature_importances_') for tree in self.estimators_)) return (sum(all_importances) / len(self.estimators_))
'Compute out-of-bag score'
def _set_oob_score(self, X, y):
X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_classes_ = self.n_classes_ n_samples = y.shape[0] oob_decision_function = [] oob_score = 0.0 predictions = [] for k in range(self.n_outputs_): predictions.append(np.zeros((n_samples, n_classes_[k]))) for estimator in self.est...
'Predict class for X. The predicted class of an input sample is a vote by the trees in the forest, weighted by their probability estimates. That is, the predicted class is the one with highest mean probability estimate across the trees. Parameters X : array-like or sparse matrix of shape = [n_samples, n_features] The i...
def predict(self, X):
proba = self.predict_proba(X) if (self.n_outputs_ == 1): return self.classes_.take(np.argmax(proba, axis=1), axis=0) else: n_samples = proba[0].shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) for k in range(self.n_outputs_): predictions[:, k] = self....
'Predict class probabilities for X. The predicted class probabilities of an input sample are computed as the mean predicted class probabilities of the trees in the forest. The class probability of a single tree is the fraction of samples of the same class in a leaf. Parameters X : array-like or sparse matrix of shape =...
def predict_proba(self, X):
check_is_fitted(self, 'estimators_') X = self._validate_X_predict(X) (n_jobs, _, _) = _partition_estimators(self.n_estimators, self.n_jobs) all_proba = [np.zeros((X.shape[0], j), dtype=np.float64) for j in np.atleast_1d(self.n_classes_)] Parallel(n_jobs=n_jobs, verbose=self.verbose, backend='threadi...
'Predict class log-probabilities for X. The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the trees in the forest. Parameters X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be con...
def predict_log_proba(self, X):
proba = self.predict_proba(X) if (self.n_outputs_ == 1): return np.log(proba) else: for k in range(self.n_outputs_): proba[k] = np.log(proba[k]) return proba
'Predict regression target for X. The predicted regression target of an input sample is computed as the mean predicted regression targets of the trees in the forest. Parameters X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, its dtype will be converted to ``dtype=np.flo...
def predict(self, X):
check_is_fitted(self, 'estimators_') X = self._validate_X_predict(X) (n_jobs, _, _) = _partition_estimators(self.n_estimators, self.n_jobs) if (self.n_outputs_ > 1): y_hat = np.zeros((X.shape[0], self.n_outputs_), dtype=np.float64) else: y_hat = np.zeros(X.shape[0], dtype=np.float64)...
'Compute out-of-bag scores'
def _set_oob_score(self, X, y):
X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_samples = y.shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) n_predictions = np.zeros((n_samples, self.n_outputs_)) for estimator in self.estimators_: unsampled_indices = _generate_unsampled_indices(estimator.random_state, n...
'Fit estimator. Parameters X : array-like or sparse matrix, shape=(n_samples, n_features) The input samples. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csc_matrix`` for maximum efficiency. sample_weight : array-like, shape = [n_samples] or None Sample weights. If N...
def fit(self, X, y=None, sample_weight=None):
self.fit_transform(X, y, sample_weight=sample_weight) return self
'Fit estimator and transform dataset. Parameters X : array-like or sparse matrix, shape=(n_samples, n_features) Input data used to build forests. Use ``dtype=np.float32`` for maximum efficiency. sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Splits th...
def fit_transform(self, X, y=None, sample_weight=None):
X = check_array(X, accept_sparse=['csc']) if issparse(X): X.sort_indices() rnd = check_random_state(self.random_state) y = rnd.uniform(size=X.shape[0]) super(RandomTreesEmbedding, self).fit(X, y, sample_weight=sample_weight) self.one_hot_encoder_ = OneHotEncoder(sparse=self.sparse_output...
'Transform dataset. Parameters X : array-like or sparse matrix, shape=(n_samples, n_features) Input data to be transformed. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csr_matrix`` for maximum efficiency. Returns X_transformed : sparse matrix, shape=(n_samples, n_ou...
def transform(self, X):
return self.one_hot_encoder_.transform(self.apply(X))
'Fit estimator. Parameters X : array-like or sparse matrix, shape (n_samples, n_features) The input samples. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csc_matrix`` for maximum efficiency. sample_weight : array-like, shape = [n_samples] or None Sample weights. If N...
def fit(self, X, y=None, sample_weight=None):
X = check_array(X, accept_sparse=['csc']) if issparse(X): X.sort_indices() rnd = check_random_state(self.random_state) y = rnd.uniform(size=X.shape[0]) n_samples = X.shape[0] if isinstance(self.max_samples, six.string_types): if (self.max_samples == 'auto'): max_sampl...
'Predict if a particular sample is an outlier or not. Parameters X : array-like or sparse matrix, shape (n_samples, n_features) The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns is_inlier : array, shape (n_samples,) For eac...
def predict(self, X):
X = check_array(X, accept_sparse='csr') is_inlier = np.ones(X.shape[0], dtype=int) is_inlier[(self.decision_function(X) <= self.threshold_)] = (-1) return is_inlier
'Average anomaly score of X of the base classifiers. The anomaly score of an input sample is computed as the mean anomaly score of the trees in the forest. The measure of normality of an observation given a tree is the depth of the leaf containing this observation, which is equivalent to the number of splittings requir...
def decision_function(self, X):
X = check_array(X, accept_sparse='csr') n_samples = X.shape[0] n_samples_leaf = np.zeros((n_samples, self.n_estimators), order='f') depths = np.zeros((n_samples, self.n_estimators), order='f') if (self._max_features == X.shape[1]): subsample_features = False else: subsample_featu...
'Build a Bagging ensemble of estimators from the training set (X, y). Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrices are accepted only if they are supported by the base estimator. y : array-like, shape = [n_samples] The target values (class labe...
def fit(self, X, y, sample_weight=None):
return self._fit(X, y, self.max_samples, sample_weight=sample_weight)
'Build a Bagging ensemble of estimators from the training set (X, y). Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrices are accepted only if they are supported by the base estimator. y : array-like, shape = [n_samples] The target values (class labe...
def _fit(self, X, y, max_samples=None, max_depth=None, sample_weight=None):
random_state = check_random_state(self.random_state) (X, y) = check_X_y(X, y, ['csr', 'csc']) if (sample_weight is not None): sample_weight = check_array(sample_weight, ensure_2d=False) check_consistent_length(y, sample_weight) (n_samples, self.n_features_) = X.shape self._n_samples ...
'The subset of drawn samples for each base estimator. Returns a dynamically generated list of boolean masks identifying the samples used for fitting each member of the ensemble, i.e., the in-bag samples. Note: the list is re-created at each call to the property in order to reduce the object memory footprint by not stor...
@property def estimators_samples_(self):
sample_masks = [] for (_, sample_indices) in self._get_estimators_indices(): mask = indices_to_mask(sample_indices, self._n_samples) sample_masks.append(mask) return sample_masks
'Check the estimator and set the base_estimator_ attribute.'
def _validate_estimator(self):
super(BaggingClassifier, self)._validate_estimator(default=DecisionTreeClassifier())
'Predict class for X. The predicted class of an input sample is computed as the class with the highest mean predicted probability. If base estimators do not implement a ``predict_proba`` method, then it resorts to voting. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input s...
def predict(self, X):
predicted_probabilitiy = self.predict_proba(X) return self.classes_.take(np.argmax(predicted_probabilitiy, axis=1), axis=0)
'Predict class probabilities for X. The predicted class probabilities of an input sample is computed as the mean predicted class probabilities of the base estimators in the ensemble. If base estimators do not implement a ``predict_proba`` method, then it resorts to voting and the predicted class probabilities of an inp...
def predict_proba(self, X):
check_is_fitted(self, 'classes_') X = check_array(X, accept_sparse=['csr', 'csc']) if (self.n_features_ != X.shape[1]): raise ValueError('Number of features of the model must match the input. Model n_features is {0} and input n_features is {1}.'....
'Predict class log-probabilities for X. The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the base estimators in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matri...
def predict_log_proba(self, X):
check_is_fitted(self, 'classes_') if hasattr(self.base_estimator_, 'predict_log_proba'): X = check_array(X, accept_sparse=['csr', 'csc']) if (self.n_features_ != X.shape[1]): raise ValueError('Number of features of the model must match the input. Model ...
'Average of the decision functions of the base classifiers. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrices are accepted only if they are supported by the base estimator. Returns score : array, shape = [n_samples, k] The decision function of the ...
@if_delegate_has_method(delegate='base_estimator') def decision_function(self, X):
check_is_fitted(self, 'classes_') X = check_array(X, accept_sparse=['csr', 'csc']) if (self.n_features_ != X.shape[1]): raise ValueError('Number of features of the model must match the input. Model n_features is {0} and input n_features is {1} ...
'Predict regression target for X. The predicted regression target of an input sample is computed as the mean predicted regression targets of the estimators in the ensemble. Parameters X : {array-like, sparse matrix} of shape = [n_samples, n_features] The training input samples. Sparse matrices are accepted only if they...
def predict(self, X):
check_is_fitted(self, 'estimators_features_') X = check_array(X, accept_sparse=['csr', 'csc']) (n_jobs, n_estimators, starts) = _partition_estimators(self.n_estimators, self.n_jobs) all_y_hat = Parallel(n_jobs=n_jobs, verbose=self.verbose)((delayed(_parallel_predict_regression)(self.estimators_[starts[i...
'Check the estimator and set the base_estimator_ attribute.'
def _validate_estimator(self):
super(BaggingRegressor, self)._validate_estimator(default=DecisionTreeRegressor())
'Get parameters for this estimator. Parameters deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns params : mapping of string to any Parameter names mapped to their values.'
def get_params(self, deep=True):
return self._get_params('steps', deep=deep)
'Set the parameters of this estimator. Valid parameter keys can be listed with ``get_params()``. Returns self'
def set_params(self, **kwargs):
self._set_params('steps', **kwargs) return self
'Fit the model Fit all the transforms one after the other and transform the data, then fit the transformed data using the final estimator. Parameters X : iterable Training data. Must fulfill input requirements of first step of the pipeline. y : iterable, default=None Training targets. Must fulfill label requirements fo...
def fit(self, X, y=None, **fit_params):
(Xt, fit_params) = self._fit(X, y, **fit_params) if (self._final_estimator is not None): self._final_estimator.fit(Xt, y, **fit_params) return self
'Fit the model and transform with the final estimator Fits all the transforms one after the other and transforms the data, then uses fit_transform on transformed data with the final estimator. Parameters X : iterable Training data. Must fulfill input requirements of first step of the pipeline. y : iterable, default=Non...
def fit_transform(self, X, y=None, **fit_params):
last_step = self._final_estimator (Xt, fit_params) = self._fit(X, y, **fit_params) if hasattr(last_step, 'fit_transform'): return last_step.fit_transform(Xt, y, **fit_params) elif (last_step is None): return Xt else: return last_step.fit(Xt, y, **fit_params).transform(Xt)
'Apply transforms to the data, and predict with the final estimator Parameters X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline. Returns y_pred : array-like'
@if_delegate_has_method(delegate='_final_estimator') def predict(self, X):
Xt = X for (name, transform) in self.steps[:(-1)]: if (transform is not None): Xt = transform.transform(Xt) return self.steps[(-1)][(-1)].predict(Xt)
'Applies fit_predict of last step in pipeline after transforms. Applies fit_transforms of a pipeline to the data, followed by the fit_predict method of the final estimator in the pipeline. Valid only if the final estimator implements fit_predict. Parameters X : iterable Training data. Must fulfill input requirements of...
@if_delegate_has_method(delegate='_final_estimator') def fit_predict(self, X, y=None, **fit_params):
(Xt, fit_params) = self._fit(X, y, **fit_params) return self.steps[(-1)][(-1)].fit_predict(Xt, y, **fit_params)
'Apply transforms, and predict_proba of the final estimator Parameters X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline. Returns y_proba : array-like, shape = [n_samples, n_classes]'
@if_delegate_has_method(delegate='_final_estimator') def predict_proba(self, X):
Xt = X for (name, transform) in self.steps[:(-1)]: if (transform is not None): Xt = transform.transform(Xt) return self.steps[(-1)][(-1)].predict_proba(Xt)
'Apply transforms, and decision_function of the final estimator Parameters X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline. Returns y_score : array-like, shape = [n_samples, n_classes]'
@if_delegate_has_method(delegate='_final_estimator') def decision_function(self, X):
Xt = X for (name, transform) in self.steps[:(-1)]: if (transform is not None): Xt = transform.transform(Xt) return self.steps[(-1)][(-1)].decision_function(Xt)
'Apply transforms, and predict_log_proba of the final estimator Parameters X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline. Returns y_score : array-like, shape = [n_samples, n_classes]'
@if_delegate_has_method(delegate='_final_estimator') def predict_log_proba(self, X):
Xt = X for (name, transform) in self.steps[:(-1)]: if (transform is not None): Xt = transform.transform(Xt) return self.steps[(-1)][(-1)].predict_log_proba(Xt)
'Apply transforms, and transform with the final estimator This also works where final estimator is ``None``: all prior transformations are applied. Parameters X : iterable Data to transform. Must fulfill input requirements of first step of the pipeline. Returns Xt : array-like, shape = [n_samples, n_transformed_feature...
@property def transform(self):
if (self._final_estimator is not None): self._final_estimator.transform return self._transform
'Apply inverse transformations in reverse order All estimators in the pipeline must support ``inverse_transform``. Parameters Xt : array-like, shape = [n_samples, n_transformed_features] Data samples, where ``n_samples`` is the number of samples and ``n_features`` is the number of features. Must fulfill input requireme...
@property def inverse_transform(self):
for (name, transform) in self.steps: if (transform is not None): transform.inverse_transform return self._inverse_transform
'Apply transforms, and score with the final estimator Parameters X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline. y : iterable, default=None Targets used for scoring. Must fulfill label requirements for all steps of the pipeline. sample_weight : array-like, default=None If...
@if_delegate_has_method(delegate='_final_estimator') def score(self, X, y=None, sample_weight=None):
Xt = X for (name, transform) in self.steps[:(-1)]: if (transform is not None): Xt = transform.transform(Xt) score_params = {} if (sample_weight is not None): score_params['sample_weight'] = sample_weight return self.steps[(-1)][(-1)].score(Xt, y, **score_params)
'Get parameters for this estimator. Parameters deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns params : mapping of string to any Parameter names mapped to their values.'
def get_params(self, deep=True):
return self._get_params('transformer_list', deep=deep)
'Set the parameters of this estimator. Valid parameter keys can be listed with ``get_params()``. Returns self'
def set_params(self, **kwargs):
self._set_params('transformer_list', **kwargs) return self
'Generate (name, est, weight) tuples excluding None transformers'
def _iter(self):
get_weight = (self.transformer_weights or {}).get return ((name, trans, get_weight(name)) for (name, trans) in self.transformer_list if (trans is not None))
'Get feature names from all transformers. Returns feature_names : list of strings Names of the features produced by transform.'
def get_feature_names(self):
feature_names = [] for (name, trans, weight) in self._iter(): if (not hasattr(trans, 'get_feature_names')): raise AttributeError(('Transformer %s (type %s) does not provide get_feature_names.' % (str(name), type(trans).__name__))) feature_names.extend([((name + '...
'Fit all transformers using X. Parameters X : iterable or array-like, depending on transformers Input data, used to fit transformers. y : array-like, shape (n_samples, ...), optional Targets for supervised learning. Returns self : FeatureUnion This estimator'
def fit(self, X, y=None):
self._validate_transformers() transformers = Parallel(n_jobs=self.n_jobs)((delayed(_fit_one_transformer)(trans, X, y) for (_, trans, _) in self._iter())) self._update_transformer_list(transformers) return self
'Fit all transformers, transform the data and concatenate results. Parameters X : iterable or array-like, depending on transformers Input data to be transformed. y : array-like, shape (n_samples, ...), optional Targets for supervised learning. Returns X_t : array-like or sparse matrix, shape (n_samples, sum_n_component...
def fit_transform(self, X, y=None, **fit_params):
self._validate_transformers() result = Parallel(n_jobs=self.n_jobs)((delayed(_fit_transform_one)(trans, weight, X, y, **fit_params) for (name, trans, weight) in self._iter())) if (not result): return np.zeros((X.shape[0], 0)) (Xs, transformers) = zip(*result) self._update_transformer_list(tr...
'Transform X separately by each transformer, concatenate results. Parameters X : iterable or array-like, depending on transformers Input data to be transformed. Returns X_t : array-like or sparse matrix, shape (n_samples, sum_n_components) hstack of results of transformers. sum_n_components is the sum of n_components (...
def transform(self, X):
Xs = Parallel(n_jobs=self.n_jobs)((delayed(_transform_one)(trans, weight, X) for (name, trans, weight) in self._iter())) if (not Xs): return np.zeros((X.shape[0], 0)) if any((sparse.issparse(f) for f in Xs)): Xs = sparse.hstack(Xs).tocsr() else: Xs = np.hstack(Xs) return Xs
'Fit the model with X. Samples random projection according to n_features. Parameters X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data, where n_samples in the number of samples and n_features is the number of features. Returns self : object Returns the transformer.'
def fit(self, X, y=None):
X = check_array(X, accept_sparse='csr') random_state = check_random_state(self.random_state) n_features = X.shape[1] self.random_weights_ = (np.sqrt((2 * self.gamma)) * random_state.normal(size=(n_features, self.n_components))) self.random_offset_ = random_state.uniform(0, (2 * np.pi), size=self.n_c...
'Apply the approximate feature map to X. Parameters X : {array-like, sparse matrix}, shape (n_samples, n_features) New data, where n_samples in the number of samples and n_features is the number of features. Returns X_new : array-like, shape (n_samples, n_components)'
def transform(self, X):
check_is_fitted(self, 'random_weights_') X = check_array(X, accept_sparse='csr') projection = safe_sparse_dot(X, self.random_weights_) projection += self.random_offset_ np.cos(projection, projection) projection *= (np.sqrt(2.0) / np.sqrt(self.n_components)) return projection
'Fit the model with X. Samples random projection according to n_features. Parameters X : array-like, shape (n_samples, n_features) Training data, where n_samples in the number of samples and n_features is the number of features. Returns self : object Returns the transformer.'
def fit(self, X, y=None):
X = check_array(X) random_state = check_random_state(self.random_state) n_features = X.shape[1] uniform = random_state.uniform(size=(n_features, self.n_components)) self.random_weights_ = ((1.0 / np.pi) * np.log(np.tan(((np.pi / 2.0) * uniform)))) self.random_offset_ = random_state.uniform(0, (2...
'Apply the approximate feature map to X. Parameters X : array-like, shape (n_samples, n_features) New data, where n_samples in the number of samples and n_features is the number of features. All values of X must be strictly greater than "-skewedness". Returns X_new : array-like, shape (n_samples, n_components)'
def transform(self, X):
check_is_fitted(self, 'random_weights_') X = as_float_array(X, copy=True) X = check_array(X, copy=False) if (X <= (- self.skewedness)).any(): raise ValueError('X may not contain entries smaller than -skewedness.') X += self.skewedness np.log(X, X) projection = sa...
'Set parameters.'
def fit(self, X, y=None):
X = check_array(X, accept_sparse='csr') if (self.sample_interval is None): if (self.sample_steps == 1): self.sample_interval_ = 0.8 elif (self.sample_steps == 2): self.sample_interval_ = 0.5 elif (self.sample_steps == 3): self.sample_interval_ = 0.4 ...
'Apply approximate feature map to X. Parameters X : {array-like, sparse matrix}, shape = (n_samples, n_features) Returns X_new : {array, sparse matrix}, shape = (n_samples, n_features * (2*sample_steps + 1)) Whether the return value is an array of sparse matrix depends on the type of the input X.'
def transform(self, X):
msg = '%(name)s is not fitted. Call fit to set the parameters before calling transform' check_is_fitted(self, 'sample_interval_', msg=msg) X = check_array(X, accept_sparse='csr') sparse = sp.issparse(X) if ((X.data if sparse else X) < 0).any(): raise Value...
'Fit estimator to data. Samples a subset of training points, computes kernel on these and computes normalization matrix. Parameters X : array-like, shape=(n_samples, n_feature) Training data.'
def fit(self, X, y=None):
X = check_array(X, accept_sparse='csr') rnd = check_random_state(self.random_state) n_samples = X.shape[0] if (self.n_components > n_samples): n_components = n_samples warnings.warn('n_components > n_samples. This is not possible.\nn_components was set to n_...
'Apply feature map to X. Computes an approximate feature map using the kernel between some training points and X. Parameters X : array-like, shape=(n_samples, n_features) Data to transform. Returns X_transformed : array, shape=(n_samples, n_components) Transformed data.'
def transform(self, X):
check_is_fitted(self, 'components_') X = check_array(X, accept_sparse='csr') kernel_params = self._get_kernel_params() embedded = pairwise_kernels(X, self.components_, metric=self.kernel, filter_params=True, **kernel_params) return np.dot(embedded, self.normalization_.T)
'Fits a Minimum Covariance Determinant with the FastMCD algorithm. Parameters X : array-like, shape = [n_samples, n_features] Training data, where n_samples is the number of samples and n_features is the number of features. y : not used, present for API consistence purpose. Returns self : object Returns self.'
def fit(self, X, y=None):
X = check_array(X, ensure_min_samples=2, estimator='MinCovDet') random_state = check_random_state(self.random_state) (n_samples, n_features) = X.shape if ((linalg.svdvals(np.dot(X.T, X)) > 1e-08).sum() != n_features): warnings.warn('The covariance matrix associated to your data...
'Apply a correction to raw Minimum Covariance Determinant estimates. Correction using the empirical correction factor suggested by Rousseeuw and Van Driessen in [RVD]_. Parameters data : array-like, shape (n_samples, n_features) The data matrix, with p features and n samples. The data set must be the one which was used...
def correct_covariance(self, data):
correction = (np.median(self.dist_) / chi2(data.shape[1]).isf(0.5)) covariance_corrected = (self.raw_covariance_ * correction) self.dist_ /= correction return covariance_corrected
'Re-weight raw Minimum Covariance Determinant estimates. Re-weight observations using Rousseeuw\'s method (equivalent to deleting outlying observations from the data set before computing location and covariance estimates) described in [RVDriessen]_. Parameters data : array-like, shape (n_samples, n_features) The data m...
def reweight_covariance(self, data):
(n_samples, n_features) = data.shape mask = (self.dist_ < chi2(n_features).isf(0.025)) if self.assume_centered: location_reweighted = np.zeros(n_features) else: location_reweighted = data[mask].mean(0) covariance_reweighted = self._nonrobust_covariance(data[mask], assume_centered=sel...
'Fit the EllipticEnvelope model with X. Parameters X : numpy array or sparse matrix of shape [n_samples, n_features] Training data y : (ignored)'
def fit(self, X, y=None):
super(EllipticEnvelope, self).fit(X) self.threshold_ = sp.stats.scoreatpercentile(self.dist_, (100.0 * (1.0 - self.contamination))) return self
'Compute the decision function of the given observations. Parameters X : array-like, shape (n_samples, n_features) raw_values : bool Whether or not to consider raw Mahalanobis distances as the decision function. Must be False (default) for compatibility with the others outlier detection tools. Returns decision : array-...
def decision_function(self, X, raw_values=False):
check_is_fitted(self, 'threshold_') X = check_array(X) mahal_dist = self.mahalanobis(X) if raw_values: decision = mahal_dist else: transformed_mahal_dist = (mahal_dist ** 0.33) decision = ((self.threshold_ ** 0.33) - transformed_mahal_dist) return decision