The high cost and time for determining water quality parameters justify the importance of application of mathematical models in discovering connection among them. This paper presents a data mining technique and its improved version in estimating water quality parameters. For this purpose, the surface and ground water quality data from Hamedan (Iran) between 2006 and 2015 were analyzed using M5 model tree and its modified version optimized with Excel Solver Platform (ESP). The values of electrical conductivity (EC), total dissolved solids (TDS), sodium adsorption ratio (SAR), and total hardness (TH) were considered as target variables, whereas pH, concentrations of sodium (Na), chlorine (Cl), bicarbonate (HCO3), sulfate (SO4), magnesium (Mg), calcium (Ca), and potassium (K) were as inputs. The results showed that in both the sources, pH was the least influential parameter on EC, TDS, SAR, and TH. It was found that among the objective parameters, the accuracy of models in estimating TH was higher than the other parameters, whereas SAR was a complex variable. The comparison of performances of the M5 and the M5-ESP models illustrated that the application of the ESP significantly decreased the normal root mean error (NRMSE) of the M5 model; the mean NRMSEs were decreased by 18.95% and 20.29% in estimating groundwater and surface water quality parameters, respectively. Moreover, ability of both the M5 and the M5-ESP models in computing objective parameters of the groundwater was found to be better than the surface water.