Progress Estimator

    Jakarta Commons Cookbook这本书中介绍了一个使用回归分析来估计某个工作需要多长时间的例子。 笔者看过之后感觉有些问题(也可能是笔者数学功底不够)。以下是经过笔者修改后的代码。

 

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.math.stat.regression.SimpleRegression;

public class ProgressEstimator {
	//
	private StopWatch stopWatch;
	private SimpleRegression regression;
	
	/**
	 * 
	 */
	public ProgressEstimator() {
		this.stopWatch = new StopWatch();
		this.regression = new SimpleRegression();
	}
	
	/**
	 * 
	 */
	public void start() {
		//
		regression.clear();
		
		//
		stopWatch.reset();
		stopWatch.start();
	}
	
	public void stop() {
		//
		stopWatch.stop();
	}
	
	/**
	 * 
	 */
	public void step(int progress) {
		regression.addData(progress, stopWatch.getTime());
	}
	
	public long getElapsedTime() {
		return stopWatch.getTime();
	}
	
	public long getRemainingTime(long total) {
		long r = (long)(regression.getSlope() * total + regression.getIntercept()) - getElapsedTime();
		return r >= 0 ? r : 0;
	}
	
	
	/**
	 * 
	 */
	public static void main(String args[]) {
		//
		ProgressEstimator pe = new ProgressEstimator();
		pe.start();
		
		//
		int count = 1000;
		for(int i = 0; i < count; i++) {
			//
			try {
				Thread.sleep(RandomUtils.nextInt(30));
			} catch(Exception e) {
				break;
			}
			
			//
			if(i % 10 == 9) {
				//
				pe.step(i);
				
				//
				long elapsed = pe.getElapsedTime();
				long estimatedRemaining = pe.getRemainingTime(count);
				long estimatedTotal = elapsed + estimatedRemaining;
				System.out.println("elapsed time: " + elapsed + ", estimated remaining time: " + estimatedRemaining + ", estimated total: " + estimatedTotal);
			}
		}
		pe.stop();
		System.out.println("total elapsed time: " + pe.getElapsedTime());
	}
}
# 对元学习器进行调参 meta_grid_search = GridSearchCV(estimator=meta_estimator, param_grid=meta_param_grid, cv=3, scoring='r2') # 使用基学习器的预测作为元学习器的输入特征 base_predictions_train = np.column_stack( [estimator.predict(X_train_scaled) for _, estimator in best_base_estimators]) meta_grid_search.fit(base_predictions_train, y_train) best_meta_estimator = meta_grid_search.best_estimator_ # 自定义堆叠模型,因为 StackingRegressor 不支持直接对元学习器调参后的使用 class CustomStackingModel: def __init__(self, base_estimators, meta_estimator): self.base_estimators = base_estimators self.meta_estimator = meta_estimator def fit(self, X, y): base_predictions_train = np.column_stack( [estimator.predict(X) for _, estimator in self.base_estimators]) self.meta_estimator.fit(base_predictions_train, y) def predict(self, X): base_predictions_test = np.column_stack([estimator.predict(X) for _, estimator in self.base_estimators]) return self.meta_estimator.predict(base_predictions_test) stacking_model = CustomStackingModel(best_base_estimators, best_meta_estimator) stacking_model.fit(X_train_scaled, y_train) # 评估堆叠模型的表现 train_pred_stacking = stacking_model.predict(X_train_scaled) test_pred_stacking = stacking_model.predict(X_test_scaled) r2_train_stack = r2_score(y_train, train_pred_stacking) mse_train_stack = mean_squared_error(y_train, train_pred_stacking) mae_train_stack = mean_absolute_error(y_train, train_pred_stacking) r2_test_stack = r2_score(y_test, test_pred_stacking) mse_test_stack = mean_squared_error(y_test, test_pred_stacking) mae_test_stack = mean_absolute_error(y_test, test_pred_stacking) return r2_train_stack, r2_test_stack, mse_train_stack, mse_test_stack, mae_train_stack, mae_test_stack # 主程序 results = [] for i in range(100): random_seed = random.randint(1, 10000) data = pd.read_csv('Ecorr-7特征 +2.csv') # 确保文件名和路径正确 r2_train, r2_test, mse_train, mse_test, mae_train, mae_test = stacking_with_tuning(random_seed, data) results.append({ 'Random Seed': random_seed, 'Train R^2': r2_train, 'Test R^2': r2_test, 'Train MSE': mse_train, 'Test MSE': mse_test, 'MAE Train': mae_train, 'MAE Test': mae_test }) print(f'Progress: {i + 1}%') # 进度显示 # 创建 DataFrame df = pd.DataFrame(results) # 写入 CSV 文件 df.to_csv('XGB——stacking_results_with_tuning_xgb_meta.csv', index=False) print("Data saved to XGB——stacking_results_with_tuning_xgb_meta.csv")我注意到代码中写元学习器3折交叉验证调参,一般来说元学习器不用交叉验证
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值