# Returns new dataset with out-of-fold predictions
pipeline = ModelsPipeline(model_rf, model_lr)
stack_ds = pipeline.blend(proportion=0.2, seed=111)

# Train LinearRegression on stacked data (second stage)
stacker = Regressor(dataset=stack_ds, estimator=LinearRegression)
results = stacker.predict()
# Validate results using 10 fold cross-validation
results = stacker.validate(k=10, scorer=mean_absolute_error)

#Weighted
dataset = Dataset(preprocessor=boston_dataset)

model_rf = Regressor(dataset=dataset,
                     estimator=RandomForestRegressor,
                     parameters={'n_estimators': 151},
                     name='rf')
model_lr = Regressor(dataset=dataset,
                     estimator=LinearRegression,
                     parameters={'normalize': True},
                     name='lr')
model_knn = Regressor(dataset=dataset,
                      estimator=KNeighborsRegressor,
                      parameters={'n_neighbors': 15},
                      name='knn')

pipeline = ModelsPipeline(model_rf, model_lr, model_knn)

weights = pipeline.find_weights(mean_absolute_error)
result = pipeline.weight(weights)
 pipeline = ModelsPipeline(model_rf1,model_knn,model_rcv)
 #stack_ds = pipeline.stack(k=5,seed=111)
 #blending = pipeline.blend(proportion=0.3,seed=111)
 params_las = {'alpha':1.7}
 params_rcv2 = {'cv':5,'normalize':True,'gcv_mode':'auto','scoring':'neg_mean_absolute_error'}
 #stacker = Regressor(dataset=stack_ds,estimator=rcv, parameters=params_rcv2)
 #y_pre = stacker.predict()
 #print(y_pre)
 #y_pre = pipeline.blend()
 #print(y_pre)
 ###
 #loss_stack = Evaluation([y_pre],[y_test])
 #stacking_pre.append(y_pre)
 weights = pipeline.find_weights(mean_squared_error)
 #print(weights)
 result = pipeline.weight(weights).execute()
 #print(result)
 weights_pre.append(result)
 Y_test.append(y_test)
 #loss_gbrt = Evaluation([y_pre_gbrt],[y_test])
 '''
 if loss_stack>0.065:
     output(fw_rf,i+1,y_pre)
     fw_rf.write(str(i+1)+',stacking,'+str(loss_stack)+'\n')
 '''
 '''
 if loss_gbrt>0.015:
     output(fw_gbrt,i+1,y_pre_rf)
     fw_gbrt.write(str(i+1)+',gbrt,'+str(loss_gbrt)+'\n')
     '''
 '''
dataset = Dataset(X_train,y_train,X_test)

# initialize RandomForest & LinearRegression
model_rf = Regressor(dataset=dataset, estimator=RandomForestRegressor, parameters={'n_estimators': 50},name='rf')
model_lr = Regressor(dataset=dataset, estimator=LinearRegression, parameters={'normalize': True},name='lr')

# Stack two models
# Returns new dataset with out-of-fold predictions
pipeline = ModelsPipeline(model_rf,model_lr)
stack_ds = pipeline.blend(proportion=0.2,seed=111)

# Train LinearRegression on stacked data (second stage)
stacker = Regressor(dataset=stack_ds, estimator=LinearRegression)
results = stacker.predict()
# Validate results using 10 fold cross-validation
results = stacker.validate(k=10,scorer=mean_absolute_error)



#Weighted
dataset = Dataset(preprocessor=boston_dataset)

model_rf = Regressor(dataset=dataset, estimator=RandomForestRegressor, parameters={'n_estimators': 151},name='rf')
model_lr = Regressor(dataset=dataset, estimator=LinearRegression, parameters={'normalize': True},name='lr')
model_knn = Regressor(dataset=dataset, estimator=KNeighborsRegressor, parameters={'n_neighbors': 15},name='knn')

pipeline = ModelsPipeline(model_rf,model_lr,model_knn)

weights = pipeline.find_weights(mean_absolute_error)
result = pipeline.weight(weights)