本文整理汇总了Python中sklearn.ensemble.AdaBoostRegressor.predict方法的典型用法代码示例。如果您正苦于以下问题:Python AdaBoostRegressor.predict方法的具体用法?Python AdaBoostRegressor.predict怎么用?Python AdaBoostRegressor.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.AdaBoostRegressor
的用法示例。
在下文中一共展示了AdaBoostRegressor.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def main():
ab = AdaBoostRegressor(base_estimator=None, n_estimators=50,
learning_rate=1.0, loss='exponential',
random_state=None)
ab.fit(X_train, y_train)
#Evaluation in train set
#Evaluation in train set
pred_proba_train = ab.predict(X_train)
mse_train = mean_squared_error(y_train, pred_proba_train)
rmse_train = np.sqrt(mse_train)
logloss_train = log_loss(y_train, pred_proba_train)
#Evaluation in validation set
pred_proba_val = ab.predict(X_val)
mse_val = mean_squared_error(y_val, pred_proba_val)
rmse_val = np.sqrt(mse_val)
logloss_val = log_loss(y_val, pred_proba_val)
rmse_train
rmse_val
logloss_train
logloss_val
示例2: train_learning_model_decision_tree_ada_boost
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def train_learning_model_decision_tree_ada_boost(df):
#code taken from sklearn
X_all, y_all = preprocess_data(df)
X_train, X_test, y_train, y_test = split_data(X_all, y_all)
tree_regressor = DecisionTreeRegressor(max_depth = 6)
ada_regressor = AdaBoostRegressor(DecisionTreeRegressor(max_depth=6), n_estimators = 500, learning_rate = 0.01, random_state = 1)
tree_regressor.fit(X_train, y_train)
ada_regressor.fit(X_train, y_train)
y_pred_tree = tree_regressor.predict(X_test)
y_pred_ada = ada_regressor.predict(X_test)
mse_tree = mean_squared_error(y_test, y_pred_tree)
mse_ada = mean_squared_error(y_test, y_pred_ada)
mse_tree_train = mean_squared_error(y_train, tree_regressor.predict(X_train))
mse_ada_train = mean_squared_error(y_train, ada_regressor.predict(X_train))
print ("MSE tree: %.4f " %mse_tree)
print ("MSE ada: %.4f " %mse_ada)
print ("MSE tree train: %.4f " %mse_tree_train)
print ("MSE ada train: %.4f " %mse_ada_train)
示例3: round2
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def round2(X_df, featurelist):
# Set parameters
model = AdaBoostRegressor()
y_df = X_df['target']
n = len(y_df)
# Perform 5-fold cross validation
scores = []
kf = KFold(n, n_folds=5, shuffle=True)
# Calculate mean absolute deviation for train/test for each fold
for train_idx, test_idx in kf:
X_train, X_test = X_df.iloc[train_idx, :], X_df.iloc[test_idx, :]
# y_train, y_test = y_df[train_idx], y_df[test_idx]
X_train, X_test = applyFeatures(X_train, X_test, featurelist)
Xtrain_array, ytrain_array, Xtest_array, ytest_array = dfToArray(X_train, X_test)
model.fit(Xtrain_array, ytrain_array)
prediction = model.predict(Xtest_array)
rmse = np.sqrt(mean_squared_error(ytest_array, prediction))
scores.append(rmse)
print rmse
print "Finish fold"
return scores
示例4: Round2
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def Round2(X, y):
# Set parameters
min_score = {}
for loss in ['linear', 'square', 'exponential']:
model = AdaBoostRegressor(loss=loss)
n = len(y)
# Perform 5-fold cross validation
scores = []
kf = KFold(n, n_folds=5, shuffle=True)
# Calculate mean absolute deviation for train/test for each fold
for train_idx, test_idx in kf:
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
model.fit(X_train, y_train)
prediction = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, prediction))
# score = model.score(X_test, y_test)
scores.append(rmse)
if len(min_score) == 0:
min_score['loss'] = loss
min_score['scores'] = scores
else:
if np.mean(scores) < np.mean(min_score['scores']):
min_score['loss'] = loss
min_score['scores'] = scores
print "Loss:", loss
print scores
print np.mean(scores)
return min_score
示例5: predict
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def predict(tour_data):
vec = DictVectorizer()
tour_data = get_tour_data()
transformed = vec.fit_transform(tour_data).toarray()
categories = vec.get_feature_names()
y = transformed[:,[categories.index('rating')]]
X = transformed[:,np.arange(transformed.shape[1])!=categories.index('rating')]
reg_tree = DecisionTreeRegressor()
addboost_tree = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4),
n_estimators=300, random_state=rng)
red_tree.fit(X,y)
addboost_tree(X,y)
# Predict
y_1 = red_tree.predict(X)
y_2 = addboost_tree.predict(X)
return prediction
示例6: ada_boost_regressor
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def ada_boost_regressor(train_x, train_y, pred_x, review_id, v_curve=False, l_curve=False, get_model=True):
"""
:param train_x: train
:param train_y: text
:param pred_x: test set to predict
:param review_id: takes in a review id
:param v_curve: run the model for validation curve
:param l_curve: run the model for learning curve
:param get_model: run the model
:return: the predicted values,learning curve, validation curve
"""
ada = AdaBoostRegressor(n_estimators=5)
if get_model:
print "Fitting Ada..."
ada.fit(train_x, np.log(train_y+1))
ada_pred = np.exp(ada.predict(pred_x))-1
Votes = ada_pred[:,np.newaxis]
Id = np.array(review_id)[:,np.newaxis]
# create submission csv for Kaggle
submission_ada= np.concatenate((Id,Votes),axis=1)
np.savetxt("submission_ada.csv", submission_ada,header="Id,Votes", delimiter=',',fmt="%s, %0.2f", comments='')
# plot validation and learning curves
if l_curve:
print "Working on Learning Curves"
plot_learning_curve(AdaBoostRegressor(), "Learning curve: Adaboost", train_x, np.log(train_y+1.0))
if v_curve:
print "Working on Validation Curves"
plot_validation_curve(AdaBoostRegressor(), "Validation Curve: Adaboost", train_x, np.log(train_y+1.0),
param_name="n_estimators", param_range=[2, 5, 10, 15, 20, 25, 30])
示例7: fit
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def fit(self, start_date, end_date):
for ticker in self.tickers:
self.stocks[ticker] = Stock(ticker)
params_ada = [{
'n_estimators': [25, 50, 100],
'learning_rate': [0.01, 0.1, 1, 10],
'loss': ['linear', 'square', 'exponential']
}]
params = ParameterGrid(params_ada)
# Find the split for training and CV
mid_date = train_test_split(start_date, end_date)
for ticker, stock in self.stocks.items():
X_train, y_train = stock.get_data(start_date, mid_date, fit=True)
# X_train = self.pca.fit_transform(X_train.values)
X_train = X_train.values
# pdb.set_trace()
X_cv, y_cv = stock.get_data(mid_date, end_date)
# X_cv = self.pca.transform(X_cv.values)
X_cv = X_cv.values
lowest_mse = np.inf
for i, param in enumerate(params):
ada = AdaBoostRegressor(**param)
ada.fit(X_train, y_train.values)
mse = mean_squared_error(
y_cv, ada.predict(X_cv))
if mse <= lowest_mse:
self.models[ticker] = ada
return self
示例8: predict_volatility_1year_ahead
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def predict_volatility_1year_ahead(rows, day, num_days):
"""
SUMMARY: Predict volatility 1 year into the future
ALGORITHM:
a) The predictor will train on all data up to exactly 1 year (252 trading days) before `day`
b) The newest 10 days up to and including `day` will be used as the feature vector for the prediction
i.e. if day = 0, then the feature vector for prediction will consist of days (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
if day = 10, then the feature vector for predictor input will be days (10, 11, 12, 13, 14, 15, 16, 17, 19)
INPUT: minimum of (1 year + 10 days) of data before `day` (newest data is day=0)
"""
'''enforce that `day` is in the required range'''
assert len(rows) >= 252+num_days + day, 'You need to have AT LEAST 252+%d rows AFTER the day index. See predict_volatility_1year_ahead() for details.' % num_days
assert day >= 0
'''Compile features for fitting'''
feature_sets = []
value_sets = [];
for ii in range(day+num_days+252, len(rows) - num_days):
features = []
for jj in range(num_days):
day_index = ii + jj
features += [
float(rows[day_index][7]),
float(rows[day_index][8]),
float(rows[day_index][9]),
float(rows[day_index][10]),
float(rows[day_index][11]),
float(rows[day_index][12]),
float(rows[day_index][13]),
]
#print("issue here: " + str(rows[day_index][0]))
feature_sets += [features]
value_sets += [float(rows[ii-252][9])]
'''Create Regressor and fit'''
num_features = 16
rng = np.random.RandomState(1)
regr = AdaBoostRegressor(CustomClassifier(), n_estimators=3, random_state=rng)
regr.fit(feature_sets, value_sets)
'''Get prediction features'''
ii = day
features = []
for jj in range( num_days ):
day_index = ii + jj
features += [
float(rows[day_index][7]),
float(rows[day_index][8]),
float(rows[day_index][9]),
float(rows[day_index][10]),
float(rows[day_index][11]),
float(rows[day_index][12]),
float(rows[day_index][13]),
]
return float(regr.predict([features]))
示例9: Regressor
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
class Regressor(BaseEstimator):
def __init__(self):
self.clf = AdaBoostRegressor(RandomForestRegressor(n_estimators=500, max_depth=78, max_features=10), n_estimators=40)
def fit(self, X, y):
self.clf.fit(X, y)
def predict(self, X):
return self.clf.predict(X)
示例10: AdaBoost
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def AdaBoost(xTrain, yTrain, xTest, yTest, treeNum):
rms = dict()
for trees in treeNum:
ab = AdaBoostRegressor(n_estimators = trees)
ab.fit(xTrain, yTrain)
yPred = ab.predict(xTest)
rms[trees] = sqrt(mean_squared_error(yTest, yPred))
(bestRegressor, rmse) = sorted(rms.iteritems(), key = operator.itemgetter(1))[0]
return bestRegressor, rmse
示例11: Regressor
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
class Regressor(BaseEstimator):
def __init__(self):
cl = RandomForestRegressor(n_estimators=10, max_depth=10, max_features=10)
self.clf = AdaBoostRegressor(base_estimator = cl, n_estimators=100)
def fit(self, X, y):
self.clf.fit(X, y)
def predict(self, X):
return self.clf.predict(X)
#RandomForestClassifier
示例12: Regressor
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
class Regressor(BaseEstimator):
def __init__(self):
self.clf = AdaBoostRegressor(RandomForestRegressor(n_estimators=100, max_depth=40, max_features=25), n_estimators=100)
#self.clf_Boost = GradientBoostingRegressor( n_estimators = 500 , max_features = 20 )
#self.clf_Regression = LinearRegression()
def fit(self, X, y):
self.clf.fit(X,y)
def predict(self, X):
return self.clf.predict(X)
示例13: test_sparse_regression
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import 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])
示例14: performAdaBoostReg
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def performAdaBoostReg(train, test, features, output):
"""
Ada Boost Regression
"""
clf = AdaBoostRegressor()
clf.fit(train[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)
示例15: do_adaboost
# 需要导入模块: from sklearn.ensemble import AdaBoostRegressor [as 别名]
# 或者: from sklearn.ensemble.AdaBoostRegressor import predict [as 别名]
def do_adaboost(filename):
df, Y = create_merged_dataset(filename)
# Ideas:
# Create a feature for accelerations e deacceleration.
# Leave default base regressor for AdaBoost(decision tree). Extra trees were tried with catastrophic results.
#ada = AdaBoostRegressor(n_estimators=350, learning_rate=0.05)
ada = AdaBoostRegressor(n_estimators=500, learning_rate=1)
#X = df.drop(['driver', 'trip', 'prob_points', 'prob_speed', 'prob_distance', 'prob_acceleration'], 1)
X = df.drop(['driver', 'trip'], 1)
ada.fit(X, Y)
probs = ada.predict(X[:200])
return pd.DataFrame({'driver': df['driver'][:200], 'trip': df['trip'][:200], 'probs': probs})