本文整理汇总了Python中sklearn.ensemble.GradientBoostingRegressor.n_estimators方法的典型用法代码示例。如果您正苦于以下问题:Python GradientBoostingRegressor.n_estimators方法的具体用法?Python GradientBoostingRegressor.n_estimators怎么用?Python GradientBoostingRegressor.n_estimators使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.GradientBoostingRegressor
的用法示例。
在下文中一共展示了GradientBoostingRegressor.n_estimators方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train_gbt
# 需要导入模块: from sklearn.ensemble import GradientBoostingRegressor [as 别名]
# 或者: from sklearn.ensemble.GradientBoostingRegressor import n_estimators [as 别名]
def train_gbt(args):
X, y, question, depth, feature_labels, ntrees = args
# first find optimal number of trees
if ntrees is None:
ntrees = 1000
if verbose:
print 'Training gradient boosted tree for question', question, 'using', ntrees, 'trees.'
if question == 'Class1.1':
verbosity = 1
else:
verbosity = 0
gbt = GradientBoostingRegressor(n_estimators=ntrees, subsample=0.5, max_depth=depth, verbose=verbosity)
gbt.fit(X, y)
oob_score = gbt.oob_improvement_.cumsum()
if verbose:
print 'Optimal number of trees is', oob_score.argmax() + 1, 'for question', question
plt.clf()
plt.plot(oob_score)
plt.ylabel('OOB Score')
plt.xlabel('Number of Trees')
plt.title(question)
plt.savefig(plot_dir + 'OOB_Score_GBT_' + question + '.png')
if doshow:
plt.show()
plt.close()
ntrees = oob_score.argmax() + 1
gbt.n_estimators = ntrees
gbt.fit(X, y)
if verbose:
print 'Pickling best GBT object...'
cPickle.dump(gbt, open(gbt_dir + 'GBT_' + question + '_ntrees' + str(ntrees) + '_depth' + str(depth) +
'.pickle', 'wb'))
# make feature importance plot
fimp = gbt.feature_importances_
fimp /= fimp.max()
sidx = np.argsort(fimp)
feature_labels = np.asarray(feature_labels)
pos = np.arange(50) + 0.5
plt.clf()
plt.barh(pos, fimp[sidx[-50:]], align='center')
plt.yticks(pos, feature_labels[sidx[-50:]])
plt.xlabel("Relative Importance: Top 50")
plt.savefig(plot_dir + 'feature_importance_GBT_' + question + '.png')
if doshow:
plt.show()
return gbt
示例2: boost_residuals
# 需要导入模块: from sklearn.ensemble import GradientBoostingRegressor [as 别名]
# 或者: from sklearn.ensemble.GradientBoostingRegressor import n_estimators [as 别名]
def boost_residuals(args):
resid, stock_idx = args
X, Xpredict = build_design_matrix(stock_idx)
gbr = GradientBoostingRegressor(loss='lad', max_depth=2, subsample=0.5, learning_rate=0.001,
n_estimators=400)
gbr.fit(X, resid)
oob_error = -np.cumsum(gbr.oob_improvement_)
#plt.plot(oob_error)
#plt.show()
ntrees = np.max(np.array([np.argmin(oob_error) + 1, 5]))
print "Using ", ntrees, " trees for stock ", cnames[stock_idx]
gbr.n_estimators = ntrees
# get cross-validation accuracy
print "Getting CV error..."
cv_error = cross_validation.cross_val_score(gbr, X, y=resid, score_func=mean_absolute_error,
cv=10)
gbr.fit(X, resid)
fimportance = gbr.feature_importances_
fimportance /= fimportance.max()
pfile_name = base_dir + 'data/GBR_O' + str(stock_idx+1) + '.p'
pfile = open(pfile_name, 'wb')
cPickle.dump(gbr, pfile)
pfile.close()
return gbr.predict(Xpredict), cv_error, fimportance
示例3: model
# 需要导入模块: from sklearn.ensemble import GradientBoostingRegressor [as 别名]
# 或者: from sklearn.ensemble.GradientBoostingRegressor import n_estimators [as 别名]
plt.title("Best model (55 trees)", fontsize=14)
plt.show()
gbrt = GradientBoostingRegressor(
max_depth=2,
n_estimators=1,
learning_rate=0.1,
random_state=42,
warm_start=True)
min_val_error = float("inf")
error_going_up = 0
for n_estimators in range(1, 120):
gbrt.n_estimators = n_estimators
gbrt.fit(X_train, y_train)
y_pred = gbrt.predict(X_val)
val_error = mean_squared_error(y_val, y_pred)
if val_error < min_val_error:
min_val_error = val_error
error_going_up = 0
else:
error_going_up += 1
if error_going_up == 5:
break
print(gbrt.n_estimators)