Arsenic contamination in groundwater is a global issue. This study examines arsenic removal using continuous flow electrocoagulation with iron and aluminum electrodes. Various parameters, such as electrode material, number, and applied voltage, were tested. Arsenic removal efficiencies ranged from 52 % to 89 % with aluminum electrodes and 46 % to 96 % with iron electrodes, depending on conditions. Surface pretreatment of electrodes significantly enhanced performance. Iron electrodes provided a faster and more cost-effective solution compared to aluminum. Statistical methods, including Pearson's correlation and Gradient Boosting Machine (GBM) modeling, were used to optimize operational parameters like current density, coagulant dose, pH, and conductivity. The GBM model achieved high predictive accuracy (R2 > 0.97), capturing complex relationships. Experiment duration was the most critical factor, contributing 70.2 % to arsenic removal efficiency, with a strong correlation (r = 0.75). Coagulant dose was also crucial, followed by current density and voltage. Cost analysis combined with GBM modeling optimized cost-efficiency for achieving arsenic levels below 10 μg/L. This research offers valuable insights for optimizing electrocoagulation processes, demonstrating the potential of machine learning in enhancing water treatment technologies.