本文整理汇总了Python中sklearn.ensemble.VotingClassifier.transform方法的典型用法代码示例。如果您正苦于以下问题:Python VotingClassifier.transform方法的具体用法?Python VotingClassifier.transform怎么用?Python VotingClassifier.transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.VotingClassifier
的用法示例。
在下文中一共展示了VotingClassifier.transform方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_transform
# 需要导入模块: from sklearn.ensemble import VotingClassifier [as 别名]
# 或者: from sklearn.ensemble.VotingClassifier import transform [as 别名]
def test_transform():
"""Check transform method of VotingClassifier on toy dataset."""
clf1 = LogisticRegression(random_state=123)
clf2 = RandomForestClassifier(random_state=123)
clf3 = GaussianNB()
X = np.array([[-1.1, -1.5], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
y = np.array([1, 1, 2, 2])
eclf1 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft').fit(X, y)
eclf2 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft',
flatten_transform=True).fit(X, y)
eclf3 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft',
flatten_transform=False).fit(X, y)
warn_msg = ("'flatten_transform' default value will be "
"changed to True in 0.21. "
"To silence this warning you may"
" explicitly set flatten_transform=False.")
res = assert_warns_message(DeprecationWarning, warn_msg,
eclf1.transform, X)
assert_array_equal(res.shape, (3, 4, 2))
assert_array_equal(eclf2.transform(X).shape, (4, 6))
assert_array_equal(eclf3.transform(X).shape, (3, 4, 2))
assert_array_almost_equal(res.swapaxes(0, 1).reshape((4, 6)),
eclf2.transform(X))
assert_array_almost_equal(
eclf3.transform(X).swapaxes(0, 1).reshape((4, 6)),
eclf2.transform(X)
)
示例2: test_set_estimator_none
# 需要导入模块: from sklearn.ensemble import VotingClassifier [as 别名]
# 或者: from sklearn.ensemble.VotingClassifier import transform [as 别名]
def test_set_estimator_none():
"""VotingClassifier set_params should be able to set estimators as None"""
# Test predict
clf1 = LogisticRegression(random_state=123)
clf2 = RandomForestClassifier(random_state=123)
clf3 = GaussianNB()
eclf1 = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2),
('nb', clf3)],
voting='hard', weights=[1, 0, 0.5]).fit(X, y)
eclf2 = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2),
('nb', clf3)],
voting='hard', weights=[1, 1, 0.5])
eclf2.set_params(rf=None).fit(X, y)
assert_array_equal(eclf1.predict(X), eclf2.predict(X))
assert_true(dict(eclf2.estimators)["rf"] is None)
assert_true(len(eclf2.estimators_) == 2)
assert_true(all([not isinstance(est, RandomForestClassifier) for est in
eclf2.estimators_]))
assert_true(eclf2.get_params()["rf"] is None)
eclf1.set_params(voting='soft').fit(X, y)
eclf2.set_params(voting='soft').fit(X, y)
assert_array_equal(eclf1.predict(X), eclf2.predict(X))
assert_array_almost_equal(eclf1.predict_proba(X), eclf2.predict_proba(X))
msg = ('All estimators are None. At least one is required'
' to be a classifier!')
assert_raise_message(
ValueError, msg, eclf2.set_params(lr=None, rf=None, nb=None).fit, X, y)
# Test soft voting transform
X1 = np.array([[1], [2]])
y1 = np.array([1, 2])
eclf1 = VotingClassifier(estimators=[('rf', clf2), ('nb', clf3)],
voting='soft', weights=[0, 0.5],
flatten_transform=False).fit(X1, y1)
eclf2 = VotingClassifier(estimators=[('rf', clf2), ('nb', clf3)],
voting='soft', weights=[1, 0.5],
flatten_transform=False)
eclf2.set_params(rf=None).fit(X1, y1)
assert_array_almost_equal(eclf1.transform(X1),
np.array([[[0.7, 0.3], [0.3, 0.7]],
[[1., 0.], [0., 1.]]]))
assert_array_almost_equal(eclf2.transform(X1),
np.array([[[1., 0.],
[0., 1.]]]))
eclf1.set_params(voting='hard')
eclf2.set_params(voting='hard')
assert_array_equal(eclf1.transform(X1), np.array([[0, 0], [1, 1]]))
assert_array_equal(eclf2.transform(X1), np.array([[0], [1]]))
示例3: test_transform
# 需要导入模块: from sklearn.ensemble import VotingClassifier [as 别名]
# 或者: from sklearn.ensemble.VotingClassifier import transform [as 别名]
def test_transform():
"""Check transform method of VotingClassifier on toy dataset."""
clf1 = LogisticRegression(random_state=123)
clf2 = RandomForestClassifier(random_state=123)
clf3 = GaussianNB()
X = np.array([[-1.1, -1.5], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
y = np.array([1, 1, 2, 2])
eclf1 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft').fit(X, y)
eclf2 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft',
flatten_transform=True).fit(X, y)
eclf3 = VotingClassifier(estimators=[
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
voting='soft',
flatten_transform=False).fit(X, y)
assert_array_equal(eclf1.transform(X).shape, (4, 6))
assert_array_equal(eclf2.transform(X).shape, (4, 6))
assert_array_equal(eclf3.transform(X).shape, (3, 4, 2))
assert_array_almost_equal(eclf1.transform(X),
eclf2.transform(X))
assert_array_almost_equal(
eclf3.transform(X).swapaxes(0, 1).reshape((4, 6)),
eclf2.transform(X)
)
示例4: VotingWeightSearchCV
# 需要导入模块: from sklearn.ensemble import VotingClassifier [as 别名]
# 或者: from sklearn.ensemble.VotingClassifier import transform [as 别名]
#.........这里部分代码省略.........
self.verbose = verbose
self.random_state = random_state
self.refit = refit
if starting_weights is not None:
self.starting_weights = starting_weights
else:
self.starting_weights = [0.5] * len(estimators)
self.best_estimator_ = None
self.weights_ = None
self.peak_score_ = None
def _log(self, msg, verbosity=0):
if self.verbose >= verbosity:
print "{pre} {ind}{msg}".format(
pre = "(SW)",
ind = "".join([" "] * verbosity),
msg = msg
)
def fit(self, X, y):
"""Train and find the optimum weights.
https://www.kaggle.com/hsperr/otto-group-product-classification-challenge/finding-ensamble-weights/code
https://www.kaggle.com/sushanttripathy/otto-group-product-classification-challenge/wrapper-for-models-ensemble/code
"""
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size = self.test_size,
random_state = self.random_state,
stratify = y
)
fitted_estimators = []
predictions = []
def log_loss_func(weights):
final_prediction = 0
for weight, prediction in zip(weights, predictions):
final_prediction += weight * prediction
return log_loss(y_test, final_prediction)
# Fit on train set
self._log("Fitting on train subset...")
for label, clf in self.estimators:
self._log("fitting {0}...".format(label), 1)
fitted_clf = clone(clf).fit(X_train, y_train)
fitted_estimators.append((label, fitted_clf))
# Predict on test set
self._log("Predict on test subset...")
for label, clf in fitted_estimators:
self._log("predict using {0}...".format(label), 1)
predictions.append(clf.predict_proba(X_test))
# Search weights
self._log("Searching weights...")
cons = ({"type": "eq", "fun": lambda w: 1 - sum(w)})
bounds = [(0,1)]*len(predictions)
res = minimize(
log_loss_func,
self.starting_weights,
method = "SLSQP",
bounds = bounds,
constraints = cons
)
self.weights_ = list(res["x"])
self.peak_score_ = res["fun"]
self._log("Best weights: {0}".format(self.weights_), 1)
self._log("Peak score: {0}".format(self.peak_score_), 1)
# Build voting classifier
self.best_estimator_ = VotingClassifier(
estimators = self.estimators,
voting = "soft",
weights = self.weights_
)
if self.refit:
self._log("Refitting using best weights...")
self.best_estimator_.fit(X, y)
return self
def predict(self, X):
return self.best_estimator_.predict(X)
def predict_proba(self, X):
return self.best_estimator_.predict_proba(X)
def transform(self, X):
return self.best_estimator_.transform(X)