ABSTRACT We present a scheme based on artificial neural networks (ANNs) to estimate the line-of-sight velocities of individual galaxies from an observed redshift–space galaxy distribution. We find an estimate of the peculiar velocity at a galaxy based on galaxy counts and barycentres in shells around it. By training the network with environmental characteristics, such as the total mass and mass centre within each shell surrounding every galaxy in redshift space, our ANN model can accurately predict the line-of-sight velocity of each individual galaxy. When this velocity is used to eliminate the RSD effect, the two-point correlation function (TPCF) in real space can be recovered with an accuracy better than 1 per cent at s > 8 $\, h^{-1}\, \mathrm{Mpc}$, and 4 per cent on all scales compared to ground truth. The real-space power spectrum can be recovered within 3 per cent on k< 0.5 $\, \mathrm{Mpc}^{-1}\, h$, and less than 5 per cent for all k modes. The quadrupole moment of the TPCF or power spectrum is almost zero down to s = 10 $\, h^{-1}\, \mathrm{Mpc}$ or all k modes, indicating an effective correction of the spatial anisotropy caused by the RSD effect. We demonstrate that on large scales, without additional training with new data, our network is adaptable to different galaxy formation models, different cosmological models, and mock galaxy samples at high-redshifts and high biases, achieving less than 10 per cent error for scales greater than 15 $\, h^{-1}\, \mathrm{Mpc}$. As it is sensitive to large-scale densities, it does not manage to remove Fingers of God in large clusters, but works remarkably well at recovering real-space galaxy positions elsewhere. Our scheme provides a novel way to predict the peculiar velocity of individual galaxies, to eliminate the RSD effect directly in future large galaxy surveys, and to reconstruct the three-dimensional cosmic velocity field accurately.