本文整理汇总了Python中sklearn.utils.validation.check_is_fitted函数的典型用法代码示例。如果您正苦于以下问题:Python check_is_fitted函数的具体用法?Python check_is_fitted怎么用?Python check_is_fitted使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_is_fitted函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict
def predict(self, ys, paradigm, use_beta=True):
"""
"""
check_is_fitted(self, "hrf_")
names, onsets, durations, modulation = check_paradigm(paradigm)
frame_times = np.arange(0, onsets.max() + self.time_offset, self.t_r)
f_hrf = interp1d(self.hx_, self.hrf_)
dm = make_design_matrix_hrf(frame_times, paradigm,
hrf_length=self.hrf_length,
t_r=self.t_r, time_offset=self.time_offset,
drift_model=self.drift_model,
period_cut=self.period_cut,
drift_order=self.drift_order,
f_hrf=f_hrf)
# Least squares estimation
if use_beta:
beta_values = self.beta
else:
beta_values = np.linalg.pinv(dm.values).dot(ys)
#print dm.shape
#print beta_values.shape
ys_fit = dm.values[:, :len(beta_values)].dot(beta_values)
#ys -= drifts.dot(np.linalg.pinv(drifts).dot(ys))
ress = ys - ys_fit
return ys_fit, dm, beta_values, ress
示例2: _check_vocabulary
def _check_vocabulary(self):
"""Check if vocabulary is empty or missing (not fit-ed)"""
msg = "%(name)s - Vocabulary wasn't fitted."
check_is_fitted(self, 'vocabulary_', msg=msg),
if len(self.vocabulary_) == 0:
raise ValueError("Vocabulary is empty")
示例3: transform
def transform(self, X):
"""Transform a dataframe given the fit imputer.
Parameters
----------
X : Pandas ``DataFrame``, shape=(n_samples, n_features)
The Pandas frame to transform.
Returns
-------
X : pd.DataFrame or np.ndarray
The imputed matrix
"""
check_is_fitted(self, 'fills_')
# check on state of X and cols
X, _ = validate_is_pd(X, self.cols)
cols = self.cols if self.cols is not None else X.columns.values
# get the fills
modes = self.fills_
# if it's a single int, easy:
if isinstance(modes, int):
X[cols] = X[cols].fillna(modes)
else:
# it's a dict
for nm in cols:
X[nm] = X[nm].fillna(modes[nm])
return X if self.as_df else X.as_matrix()
示例4: predict
def predict(self, X):
"""Predict if a particular sample is an outlier or not.
Calling xgboost `predict` function.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
Returns
-------
outlier_labels : numpy array of shape (n_samples,)
For each observation, tells whether or not
it should be considered as an outlier according to the
fitted model. 0 stands for inliers and 1 for outliers.
"""
check_is_fitted(self, ['clf_', 'decision_scores_',
'labels_', '_scalar'])
X = check_array(X)
# construct the new feature space
X_add = self._generate_new_features(X)
X_new = np.concatenate((X, X_add), axis=1)
pred_scores = self.clf_.predict(X_new)
return pred_scores.ravel()
示例5: score
def score(self, X, y=None):
"""Return the average log-likelihood of all samples.
This calls sklearn.decomposition.PCA's score method
on the specified columns [1].
Parameters
----------
X: Pandas ``DataFrame``, shape=(n_samples, n_features)
The data to score.
y: None
Passthrough for pipeline/gridsearch
Returns
-------
ll: float
Average log-likelihood of the samples under the fit
PCA model (`self.pca_`)
References
----------
.. [1] Bishop, C. "Pattern Recognition and Machine Learning"
12.2.1 p. 574 http://www.miketipping.com/papers/met-mppca.pdf
"""
check_is_fitted(self, 'pca_')
X, _ = validate_is_pd(X, self.cols)
cols = X.columns if not self.cols else self.cols
ll = self.pca_.score(X[cols].as_matrix(), _as_numpy(y))
return ll
示例6: _predict_rank
def _predict_rank(self, X, normalized=False):
"""Predict the outlyingness rank of a sample by a fitted model. The
method is for outlier detector score combination.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
normalized : bool, optional (default=False)
If set to True, all ranks are normalized to [0,1].
Returns
-------
ranks : array, shape (n_samples,)
Outlying rank of a sample according to the training data.
"""
check_is_fitted(self, ['decision_scores_'])
test_scores = self.decision_function(X)
train_scores = self.decision_scores_
sorted_train_scores = np.sort(train_scores)
ranks = np.searchsorted(sorted_train_scores, test_scores)
if normalized:
# return normalized ranks
ranks = ranks / ranks.max()
return ranks
示例7: transform
def transform(self, X):
"""Transform a test matrix given the already-fit transformer.
Parameters
----------
X : Pandas ``DataFrame``
The Pandas frame to transform. The operation will
be applied to a copy of the input data, and the result
will be returned.
Returns
-------
X : Pandas ``DataFrame``
The operation is applied to a copy of ``X``,
and the result set is returned.
"""
check_is_fitted(self, 'lambda_')
# check on state of X and cols
X, cols = validate_is_pd(X, self.cols, assert_all_finite=True) # creates a copy -- we need all to be finite
cols = _cols_if_none(X, self.cols)
lambdas_ = self.lambda_
# do transformations
for nm in cols:
X[nm] = _yj_transform_y(X[nm], lambdas_[nm])
return X if self.as_df else X.as_matrix()
示例8: decision_function
def decision_function(self,X):
'''
Computes distance to separating hyperplane between classes. The larger
is the absolute value of the decision function further data point is
from the decision boundary.
Parameters
----------
X: array-like of size (n_samples_test,n_features)
Matrix of explanatory variables
Returns
-------
decision: numpy array of size (n_samples_test,)
Distance to decision boundary
'''
check_is_fitted(self, 'coef_')
X = check_array(X, accept_sparse=None, dtype = np.float64)
n_features = self.relevant_vectors_[0].shape[1]
if X.shape[1] != n_features:
raise ValueError("X has %d features per sample; expecting %d"
% (X.shape[1], n_features))
kernel = lambda rvs : get_kernel(X,rvs,self.gamma, self.degree,
self.coef0, self.kernel, self.kernel_params)
decision = []
for rv,cf,act,b in zip(self.relevant_vectors_,self.coef_,self.active_,
self.intercept_):
# if there are no relevant vectors => use intercept only
if rv.shape[0] == 0:
decision.append( np.ones(X.shape[0])*b )
else:
decision.append(self._decision_function_active(kernel(rv),cf,act,b))
decision = np.asarray(decision).squeeze().T
return decision
示例9: decision_function
def decision_function(self, X):
"""Predict raw anomaly score of X using the fitted detector.
The anomaly score of an input sample is computed based on different
detector algorithms. For consistency, outliers are assigned with
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
# Invert outlier scores. Outliers comes with higher outlier scores
# noinspection PyProtectedMember
if _sklearn_version_20():
return invert_order(self.detector_._score_samples(X))
else:
return invert_order(self.detector_._decision_function(X))
示例10: predict
def predict(self, X):
""" 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.
"""
check_is_fitted(self, 'estimators_')
if self.voting == 'soft':
maj = np.argmax(self.predict_proba(X), axis=1)
else: # 'hard' voting
predictions = self._predict(X)
maj = np.apply_along_axis(lambda x:
np.argmax(np.bincount(x,
weights=self.weights)),
axis=1,
arr=predictions.astype('int'))
maj = self.le_.inverse_transform(maj)
return maj
示例11: sample
def sample(self, X, y):
"""Resample the dataset.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Matrix containing the data which have to be sampled.
y : array-like, shape (n_samples,)
Corresponding label for each sample in X.
Returns
-------
X_resampled : {ndarray, sparse matrix}, shape \
(n_samples_new, n_features)
The array containing the resampled data.
y_resampled : ndarray, shape (n_samples_new)
The corresponding label of `X_resampled`
"""
# Check the consistency of X and y
X, y = check_X_y(X, y, accept_sparse=['csr', 'csc'])
check_is_fitted(self, 'ratio_')
self._check_X_y(X, y)
return self._sample(X, y)
示例12: decision_function
def decision_function(self, X):
"""Compute the decision function of ``X``.
Parameters
----------
X : array-like of shape = [n_samples, n_features]
The input samples.
Returns
-------
score : array, shape = [n_samples, k]
The decision function of the input samples. The order of
outputs is the same of that of the `classes_` attribute.
Binary classification is a special cases with ``k == 1``,
otherwise ``k==n_classes``. For binary classification,
values closer to -1 or 1 mean more like the first or second
class in ``classes_``, respectively.
"""
check_is_fitted(self, "n_classes_")
X = np.asarray(X)
pred = None
for estimator in self.estimators_:
# The weights are all 1. for LogitBoost
current_pred = estimator.predict(X)
if pred is None:
pred = current_pred
else:
pred += current_pred
return pred
示例13: decision_function
def decision_function(self, X):
"""Predict raw anomaly score of X using the fitted detector.
The anomaly score of an input sample is computed based on different
detector algorithms. For consistency, outliers are assigned with
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['model_', 'history_'])
X = check_array(X)
if self.preprocessing:
X_norm = self.scaler_.transform(X)
else:
X_norm = np.copy(X)
# Predict on X and return the reconstruction errors
pred_scores = self.model_.predict(X_norm)
return pairwise_distances_no_broadcast(X_norm, pred_scores)
示例14: predict_proba
def predict_proba(self, X):
"""Predict probability for each possible outcome.
Compute the probability estimates for each single sample in X
and each possible outcome seen during training (categorical
distribution).
Parameters
----------
X : array_like, shape = [n_samples, n_features]
Returns
-------
probabilities : array, shape = [n_samples, n_classes]
Normalized probability distributions across
class labels
"""
check_is_fitted(self, 'X_')
X_2d = check_array(X, accept_sparse = ['csc', 'csr', 'coo', 'dok',
'bsr', 'lil', 'dia'])
weight_matrices = self._get_kernel(self.X_, X_2d)
if self.kernel == 'knn':
probabilities = []
for weight_matrix in weight_matrices:
ine = np.sum(self.label_distributions_[weight_matrix], axis=0)
probabilities.append(ine)
probabilities = np.array(probabilities)
else:
weight_matrices = weight_matrices.T
probabilities = np.dot(weight_matrices, self.label_distributions_)
normalizer = np.atleast_2d(np.sum(probabilities, axis=1)).T
probabilities /= normalizer
return probabilities
示例15: _kernel_decision_function
def _kernel_decision_function(self,X):
''' Computes kernel and decision function based on kernel'''
check_is_fitted(self,'coef_')
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
K = get_kernel( X, self.relevant_vectors_, self.gamma, self.degree,
self.coef0, self.kernel, self.kernel_params)
return K , np.dot(K,self.coef_[self.active_]) + self.intercept_