本文整理汇总了Python中sklearn.ensemble.AdaBoostRegressor.staged_predict方法的典型用法代码示例。如果您正苦于以下问题:Python AdaBoostRegressor.staged_predict方法的具体用法?Python AdaBoostRegressor.staged_predict怎么用?Python AdaBoostRegressor.staged_predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.AdaBoostRegressor
的用法示例。
在下文中一共展示了AdaBoostRegressor.staged_predict方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train_predict
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import staged_predict [as 别名]
def train_predict(train_id, test_id):
# load libsvm files for training dataset
Xs_train = []
ys_train = []
n_train = load_libsvm_files(train_id, Xs_train, ys_train)
# load libsvm files for testing dataset
Xs_test = []
ys_test = []
n_test = load_libsvm_files(test_id, Xs_test, ys_test)
# models
model = []
# ans
ans_train = []
ans_test = []
# generate predictions for training dataset
ps_train = []
for i in range(0, n_train):
ps_train.append([0.0 for j in range(10)])
# generate predictions for testing dataset
ps_test = []
for i in range(0, n_test):
ps_test.append([0.0 for j in range(10)])
# fit models
for i in range(10):
l = np.array([ys_train[j][i] for j in range(n_train)])
clf = AdaBoostRegressor(DecisionTreeRegressor(max_depth=params['max_depth']), n_estimators=params['n_estimators'], learning_rate=params['learning_rate'])
clf.fit(Xs_train[i].toarray(), l)
print "[%s] [INFO] %d model training done" % (t_now(), i)
preds_train = clf.staged_predict(Xs_train[i].toarray())
ans_train.append([item for item in preds_train])
# print "len(ans_train[%d]) = %d" % (i, len(ans_train[i]))
print "[%s] [INFO] %d model predict for training data set done" % (t_now(), i)
preds_test = clf.staged_predict(Xs_test[i].toarray())
ans_test.append([item for item in preds_test])
print "[%s] [INFO] %d model predict for testing data set done" % (t_now(), i)
#print "len_ans_train=%d" % len(ans_train[0])
# predict for testing data set
for i in range(params['n_estimators']):
for j in range(10):
tmp = min(i, len(ans_train[j]) - 1)
for k in range(n_train):
ps_train[k][j] = ans_train[j][tmp][k]
tmp = min(i, len(ans_test[j]) - 1)
for k in range(n_test):
ps_test[k][j] = ans_test[j][tmp][k]
print "%s,%d,%f,%f" % (t_now(), i + 1, mean_cos_similarity(ys_train, ps_train, n_train), mean_cos_similarity(ys_test, ps_test, n_test))
return 0
示例2: test_sparse_regression
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import staged_predict [as 别名]
def test_sparse_regression():
"""Check regression with sparse input."""
class CustomSVR(SVR):
"""SVR variant that records the nature of the training set."""
def fit(self, X, y, sample_weight=None):
"""Modification on fit caries data type for later verification."""
super(CustomSVR, self).fit(X, y, sample_weight=sample_weight)
self.data_type_ = type(X)
return self
X, y = datasets.make_regression(n_samples=100, n_features=50, n_targets=1,
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
for sparse_format in [csc_matrix, csr_matrix, lil_matrix, coo_matrix,
dok_matrix]:
X_train_sparse = sparse_format(X_train)
X_test_sparse = sparse_format(X_test)
# Trained on sparse format
sparse_classifier = AdaBoostRegressor(
base_estimator=CustomSVR(probability=True),
random_state=1
).fit(X_train_sparse, y_train)
# Trained on dense format
dense_classifier = dense_results = AdaBoostRegressor(
base_estimator=CustomSVR(probability=True),
random_state=1
).fit(X_train, y_train)
# predict
sparse_results = sparse_classifier.predict(X_test_sparse)
dense_results = dense_classifier.predict(X_test)
assert_array_equal(sparse_results, dense_results)
# staged_predict
sparse_results = sparse_classifier.staged_predict(X_test_sparse)
dense_results = dense_classifier.staged_predict(X_test)
for sprase_res, dense_res in zip(sparse_results, dense_results):
assert_array_equal(sprase_res, dense_res)
sparse_type = type(X_train_sparse)
types = [i.data_type_ for i in sparse_classifier.estimators_]
assert all([(t == csc_matrix or t == csr_matrix)
for t in types])