The accurate estimation of gas viscosity remains a pivotal concern for petroleum engineers, exerting substantial influence on the modeling efficacy of natural gas operations. Due to their time-consuming and costly nature, experimental measurements of gas viscosity are challenging. Data-based machine learning (ML) techniques afford a resourceful and less exhausting substitution, aiding research and industry at gas modeling that is incredible to reach in the laboratory. Statistical approaches were used to analyze the experimental data before applying machine learning. Seven machine learning techniques specifically Linear Regression, random forest (RF), decision trees, gradient boosting, K-nearest neighbors, Nu support vector regression (NuSVR), and artificial neural network (ANN) were applied for the prediction of methane (CH4), nitrogen (N2), and natural gas mixture viscosities. More than 4304 datasets from real experimental data utilizing pressure, temperature, and gas density were employed for developing ML models. Furthermore, three novel correlations have developed for the viscosity of CH4, N2, and composite gas using ANN. Results revealed that models and anticipated correlations predicted methane, nitrogen, and natural gas mixture viscosities with high precision. Results designated that the ANN, RF, and gradient Boosting models have performed better with a coefficient of determination (R2) of 0.99 for testing data sets of methane, nitrogen, and natural gas mixture viscosities. However, linear regression and NuSVR have performed poorly with a coefficient of determination (R2) of 0.07 and − 0.01 respectively for testing data sets of nitrogen viscosity. Such machine learning models offer the industry and research a cost-effective and fast tool for accurately approximating the viscosities of methane, nitrogen, and gas mixture under normal and harsh conditions.