Submit
Path:
~
/
/
proc
/
thread-self
/
root
/
proc
/
self
/
root
/
opt
/
alt
/
python35
/
lib64
/
python3.5
/
site-packages
/
sklearn
/
utils
/
File Content:
metaestimators.py
"""Utilities for meta-estimators""" # Author: Joel Nothman # Andreas Mueller # License: BSD from operator import attrgetter from functools import update_wrapper import numpy as np from ..utils import safe_indexing __all__ = ['if_delegate_has_method'] class _IffHasAttrDescriptor(object): """Implements a conditional property using the descriptor protocol. Using this class to create a decorator will raise an ``AttributeError`` if none of the delegates (specified in ``delegate_names``) is an attribute of the base object or the first found delegate does not have an attribute ``attribute_name``. This allows ducktyping of the decorated method based on ``delegate.attribute_name``. Here ``delegate`` is the first item in ``delegate_names`` for which ``hasattr(object, delegate) is True``. See https://docs.python.org/3/howto/descriptor.html for an explanation of descriptors. """ def __init__(self, fn, delegate_names, attribute_name): self.fn = fn self.delegate_names = delegate_names self.attribute_name = attribute_name # update the docstring of the descriptor update_wrapper(self, fn) def __get__(self, obj, type=None): # raise an AttributeError if the attribute is not present on the object if obj is not None: # delegate only on instances, not the classes. # this is to allow access to the docstrings. for delegate_name in self.delegate_names: try: delegate = attrgetter(delegate_name)(obj) except AttributeError: continue else: getattr(delegate, self.attribute_name) break else: attrgetter(self.delegate_names[-1])(obj) # lambda, but not partial, allows help() to work with update_wrapper out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) # update the docstring of the returned function update_wrapper(out, self.fn) return out def if_delegate_has_method(delegate): """Create a decorator for methods that are delegated to a sub-estimator This enables ducktyping by hasattr returning True according to the sub-estimator. Parameters ---------- delegate : string, list of strings or tuple of strings Name of the sub-estimator that can be accessed as an attribute of the base object. If a list or a tuple of names are provided, the first sub-estimator that is an attribute of the base object will be used. """ if isinstance(delegate, list): delegate = tuple(delegate) if not isinstance(delegate, tuple): delegate = (delegate,) return lambda fn: _IffHasAttrDescriptor(fn, delegate, attribute_name=fn.__name__) def _safe_split(estimator, X, y, indices, train_indices=None): """Create subset of dataset and properly handle kernels.""" from ..gaussian_process.kernels import Kernel as GPKernel if (hasattr(estimator, 'kernel') and callable(estimator.kernel) and not isinstance(estimator.kernel, GPKernel)): # cannot compute the kernel values with custom function raise ValueError("Cannot use a custom kernel function. " "Precompute the kernel matrix instead.") if not hasattr(X, "shape"): if getattr(estimator, "_pairwise", False): raise ValueError("Precomputed kernels or affinity matrices have " "to be passed as arrays or sparse matrices.") X_subset = [X[index] for index in indices] else: if getattr(estimator, "_pairwise", False): # X is a precomputed square kernel matrix if X.shape[0] != X.shape[1]: raise ValueError("X should be a square kernel matrix") if train_indices is None: X_subset = X[np.ix_(indices, indices)] else: X_subset = X[np.ix_(indices, train_indices)] else: X_subset = safe_indexing(X, indices) if y is not None: y_subset = safe_indexing(y, indices) else: y_subset = None return X_subset, y_subset
Edit
Rename
Chmod
Delete
FILE
FOLDER
Name
Size
Permission
Action
__pycache__
---
0755
sparsetools
---
0755
tests
---
0755
__init__.py
13522 bytes
0644
_logistic_sigmoid.cpython-35m-x86_64-linux-gnu.so
33688 bytes
0755
_random.cpython-35m-x86_64-linux-gnu.so
81096 bytes
0755
_scipy_sparse_lsqr_backport.py
18021 bytes
0644
arpack.py
73115 bytes
0644
arrayfuncs.cpython-35m-x86_64-linux-gnu.so
38104 bytes
0755
bench.py
370 bytes
0644
class_weight.py
7411 bytes
0644
deprecation.py
2417 bytes
0644
estimator_checks.py
60116 bytes
0644
extmath.py
27057 bytes
0644
fast_dict.cpython-35m-x86_64-linux-gnu.so
176248 bytes
0755
fixes.py
13923 bytes
0644
graph.py
6239 bytes
0644
graph_shortest_path.cpython-35m-x86_64-linux-gnu.so
86208 bytes
0755
lgamma.cpython-35m-x86_64-linux-gnu.so
20080 bytes
0755
linear_assignment_.py
9524 bytes
0644
metaestimators.py
4309 bytes
0644
mocking.py
2190 bytes
0644
multiclass.py
14732 bytes
0644
murmurhash.cpython-35m-x86_64-linux-gnu.so
77272 bytes
0755
optimize.py
5739 bytes
0644
random.py
10523 bytes
0644
seq_dataset.cpython-35m-x86_64-linux-gnu.so
77384 bytes
0755
setup.py
2993 bytes
0644
sparsefuncs.py
13505 bytes
0644
sparsefuncs_fast.cpython-35m-x86_64-linux-gnu.so
404296 bytes
0755
stats.py
1692 bytes
0644
testing.py
28245 bytes
0644
validation.py
26027 bytes
0644
weight_vector.cpython-35m-x86_64-linux-gnu.so
38080 bytes
0755
N4ST4R_ID | Naxtarrr