本文整理汇总了Python中sklearn.base.clone方法的典型用法代码示例。如果您正苦于以下问题:Python base.clone方法的具体用法?Python base.clone怎么用?Python base.clone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.base
的用法示例。
在下文中一共展示了base.clone方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _check_behavior_2d
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def _check_behavior_2d(clf):
# 1d case
X = np.array([[0], [0], [0], [0]]) # ignored
y = np.array([1, 2, 1, 1])
est = clone(clf)
est.fit(X, y)
y_pred = est.predict(X)
assert_equal(y.shape, y_pred.shape)
# 2d case
y = np.array([[1, 0],
[2, 0],
[1, 0],
[1, 3]])
est = clone(clf)
est.fit(X, y)
y_pred = est.predict(X)
assert_equal(y.shape, y_pred.shape)
示例2: _do_fit
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def _do_fit(n_jobs, verbose, pre_dispatch, base_estimator,
X, y, scorer, parameter_iterable, fit_params,
error_score, cv, **kwargs):
groups = kwargs.pop('groups')
# test_score, n_samples, parameters
out = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch)(
delayed(_fit_and_score)(
clone(base_estimator), X, y, scorer,
train, test, verbose, parameters,
fit_params=fit_params,
return_train_score=False,
return_n_test_samples=True,
return_times=False,
return_parameters=True,
error_score=error_score)
for parameters in parameter_iterable
for train, test in cv.split(X, y, groups))
# test_score, n_samples, _, parameters
return [(mod[0], mod[1], None, mod[2]) for mod in out]
示例3: _make_estimator
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def _make_estimator(self, append=True, random_state=None):
"""Make and configure a copy of the `base_estimator_` attribute.
sklearn/base.py
Warning: This method should be used to properly instantiate new
sub-estimators.
"""
# TODO: add a check for estimator_param
estimator = clone(self.base_estimator_)
estimator.set_params(**self.estimator_params)
if random_state is not None:
_set_random_states(estimator, random_state)
if append:
self.estimators_.append(estimator)
return estimator
示例4: check_cross_val_predict_binary
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def check_cross_val_predict_binary(est, X, y, method):
"""Helper for tests of cross_val_predict with binary classification"""
cv = KFold(n_splits=3, shuffle=False)
# Generate expected outputs
if y.ndim == 1:
exp_shape = (len(X),) if method == 'decision_function' else (len(X), 2)
else:
exp_shape = y.shape
expected_predictions = np.zeros(exp_shape)
for train, test in cv.split(X, y):
est = clone(est).fit(X[train], y[train])
expected_predictions[test] = getattr(est, method)(X[test])
# Check actual outputs for several representations of y
for tg in [y, y + 1, y - 2, y.astype('str')]:
assert_allclose(cross_val_predict(est, X, tg, method=method, cv=cv),
expected_predictions)
示例5: check_cross_val_predict_multiclass
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def check_cross_val_predict_multiclass(est, X, y, method):
"""Helper for tests of cross_val_predict with multiclass classification"""
cv = KFold(n_splits=3, shuffle=False)
# Generate expected outputs
float_min = np.finfo(np.float64).min
default_values = {'decision_function': float_min,
'predict_log_proba': float_min,
'predict_proba': 0}
expected_predictions = np.full((len(X), len(set(y))),
default_values[method],
dtype=np.float64)
_, y_enc = np.unique(y, return_inverse=True)
for train, test in cv.split(X, y_enc):
est = clone(est).fit(X[train], y_enc[train])
fold_preds = getattr(est, method)(X[test])
i_cols_fit = np.unique(y_enc[train])
expected_predictions[np.ix_(test, i_cols_fit)] = fold_preds
# Check actual outputs for several representations of y
for tg in [y, y + 1, y - 2, y.astype('str')]:
assert_allclose(cross_val_predict(est, X, tg, method=method, cv=cv),
expected_predictions)
示例6: test_transform_target_regressor_2d_transformer_multioutput
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_transform_target_regressor_2d_transformer_multioutput():
# Check consistency with transformer accepting only 2D array and a 2D y
# array.
X = friedman[0]
y = np.vstack((friedman[1], friedman[1] ** 2 + 1)).T
transformer = StandardScaler()
regr = TransformedTargetRegressor(regressor=LinearRegression(),
transformer=transformer)
y_pred = regr.fit(X, y).predict(X)
assert y.shape == y_pred.shape
# consistency forward transform
y_tran = regr.transformer_.transform(y)
_check_standard_scaled(y, y_tran)
assert y.shape == y_pred.shape
# consistency inverse transform
assert_allclose(y, regr.transformer_.inverse_transform(
y_tran).squeeze())
# consistency of the regressor
lr = LinearRegression()
transformer2 = clone(transformer)
lr.fit(X, transformer2.fit_transform(y))
y_lr_pred = lr.predict(X)
assert_allclose(y_pred, transformer2.inverse_transform(y_lr_pred))
assert_allclose(regr.regressor_.coef_, lr.coef_)
示例7: test_fit_predict_on_pipeline
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_fit_predict_on_pipeline():
# test that the fit_predict method is implemented on a pipeline
# test that the fit_predict on pipeline yields same results as applying
# transform and clustering steps separately
iris = load_iris()
scaler = StandardScaler()
km = KMeans(random_state=0)
# As pipeline doesn't clone estimators on construction,
# it must have its own estimators
scaler_for_pipeline = StandardScaler()
km_for_pipeline = KMeans(random_state=0)
# first compute the transform and clustering step separately
scaled = scaler.fit_transform(iris.data)
separate_pred = km.fit_predict(scaled)
# use a pipeline to do the transform and clustering in one step
pipe = Pipeline([
('scaler', scaler_for_pipeline),
('Kmeans', km_for_pipeline)
])
pipeline_pred = pipe.fit_predict(iris.data)
assert_array_almost_equal(pipeline_pred, separate_pred)
示例8: test_base_chain_random_order
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_base_chain_random_order():
# Fit base chain with random order
X, Y = generate_multilabel_dataset_with_correlations()
for chain in [ClassifierChain(LogisticRegression()),
RegressorChain(Ridge())]:
chain_random = clone(chain).set_params(order='random', random_state=42)
chain_random.fit(X, Y)
chain_fixed = clone(chain).set_params(order=chain_random.order_)
chain_fixed.fit(X, Y)
assert_array_equal(chain_fixed.order_, chain_random.order_)
assert_not_equal(list(chain_random.order), list(range(4)))
assert_equal(len(chain_random.order_), 4)
assert_equal(len(set(chain_random.order_)), 4)
# Randomly ordered chain should behave identically to a fixed order
# chain with the same order.
for est1, est2 in zip(chain_random.estimators_,
chain_fixed.estimators_):
assert_array_almost_equal(est1.coef_, est2.coef_)
示例9: test_base_chain_crossval_fit_and_predict
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_base_chain_crossval_fit_and_predict():
# Fit chain with cross_val_predict and verify predict
# performance
X, Y = generate_multilabel_dataset_with_correlations()
for chain in [ClassifierChain(LogisticRegression()),
RegressorChain(Ridge())]:
chain.fit(X, Y)
chain_cv = clone(chain).set_params(cv=3)
chain_cv.fit(X, Y)
Y_pred_cv = chain_cv.predict(X)
Y_pred = chain.predict(X)
assert Y_pred_cv.shape == Y_pred.shape
assert not np.all(Y_pred == Y_pred_cv)
if isinstance(chain, ClassifierChain):
assert jaccard_score(Y, Y_pred_cv, average='samples') > .4
else:
assert mean_squared_error(Y, Y_pred_cv) < .25
示例10: test_classifier_results
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_classifier_results():
"""tests if classifier results match target"""
alpha = .1
n_features = 20
n_samples = 10
tol = .01
max_iter = 200
rng = np.random.RandomState(0)
X = rng.normal(size=(n_samples, n_features))
w = rng.normal(size=n_features)
y = np.dot(X, w)
y = np.sign(y)
clf1 = LogisticRegression(solver='sag', C=1. / alpha / n_samples,
max_iter=max_iter, tol=tol, random_state=77)
clf2 = clone(clf1)
clf1.fit(X, y)
clf2.fit(sp.csr_matrix(X), y)
pred1 = clf1.predict(X)
pred2 = clf2.predict(X)
assert_almost_equal(pred1, y, decimal=12)
assert_almost_equal(pred2, y, decimal=12)
示例11: test_weighted_vs_repeated
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_weighted_vs_repeated():
# a sample weight of N should yield the same result as an N-fold
# repetition of the sample
rng = np.random.RandomState(0)
sample_weight = rng.randint(1, 5, size=n_samples)
X_repeat = np.repeat(X, sample_weight, axis=0)
estimators = [KMeans(init="k-means++", n_clusters=n_clusters,
random_state=42),
KMeans(init="random", n_clusters=n_clusters,
random_state=42),
KMeans(init=centers.copy(), n_clusters=n_clusters,
random_state=42),
MiniBatchKMeans(n_clusters=n_clusters, batch_size=10,
random_state=42)]
for estimator in estimators:
est_weighted = clone(estimator).fit(X, sample_weight=sample_weight)
est_repeated = clone(estimator).fit(X_repeat)
repeated_labels = np.repeat(est_weighted.labels_, sample_weight)
assert_almost_equal(v_measure_score(est_repeated.labels_,
repeated_labels), 1.0)
if not isinstance(estimator, MiniBatchKMeans):
assert_almost_equal(_sort_centers(est_weighted.cluster_centers_),
_sort_centers(est_repeated.cluster_centers_))
示例12: test_nmf_sparse_input
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def test_nmf_sparse_input():
# Test that sparse matrices are accepted as input
from scipy.sparse import csc_matrix
rng = np.random.mtrand.RandomState(42)
A = np.abs(rng.randn(10, 10))
A[:, 2 * np.arange(5)] = 0
A_sparse = csc_matrix(A)
for solver in ('cd', 'mu'):
est1 = NMF(solver=solver, n_components=5, init='random',
random_state=0, tol=1e-2)
est2 = clone(est1)
W1 = est1.fit_transform(A)
W2 = est2.fit_transform(A_sparse)
H1 = est1.components_
H2 = est2.components_
assert_array_almost_equal(W1, W2)
assert_array_almost_equal(H1, H2)
示例13: apply_gridsearch
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def apply_gridsearch(self,model):
"""
apply grid search on ml algorithm to specified parameters
returns updated best score and parameters
"""
# check if custom evalution function is specified
if callable(self.params_cv['scoring']):
scoring = make_scorer(self.params_cv['scoring'],greater_is_better=self._greater_is_better)
else:
scoring = self.params_cv['scoring']
gsearch = GridSearchCV(estimator=model,param_grid=self.get_params_tune(),scoring=scoring,
iid=self.params_cv['iid'],cv=self.params_cv['cv_folds'],n_jobs=self.params_cv['n_jobs'])
gsearch.fit(self.X,self.y)
# update best model if best_score is improved
if (gsearch.best_score_ * self._score_mult) > (self.best_score * self._score_mult):
self.best_model = clone(gsearch.best_estimator_)
self.best_score = gsearch.best_score_
# update tuned parameters with optimal values
for key,value in gsearch.best_params_.items():
self._params[key] = value
self._temp_score = gsearch.best_score_
return self
示例14: _clone_and_score_clusterer
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def _clone_and_score_clusterer(clf, X, n_clusters):
"""Clones and scores clusterer instance.
Args:
clf: Clusterer instance that implements ``fit``,``fit_predict``, and
``score`` methods, and an ``n_clusters`` hyperparameter.
e.g. :class:`sklearn.cluster.KMeans` instance
X (array-like, shape (n_samples, n_features)):
Data to cluster, where n_samples is the number of samples and
n_features is the number of features.
n_clusters (int): Number of clusters
Returns:
score: Score of clusters
time: Number of seconds it took to fit cluster
"""
start = time.time()
clf = clone(clf)
setattr(clf, 'n_clusters', n_clusters)
return clf.fit(X).score(X), time.time() - start
示例15: fit
# 需要导入模块: from sklearn import base [as 别名]
# 或者: from sklearn.base import clone [as 别名]
def fit(self, X, y=None, **fit_params):
if not isinstance(X, pd.DataFrame):
raise ValueError('X is not a pandas.DataFrame')
self.models_ = {}
columns = self._get_fit_columns(X)
for key in X[self.by].unique():
# Copy the model
model = clone(self.base_model)
# Select the rows that will be fitted
mask = (X[self.by] == key).tolist()
rows = X.index[mask]
# Fit the model
model.fit(X.loc[rows, columns], y[mask], **fit_params)
# Save the model
self.models_[key] = model
return self