Identifying the effect of selection (a form of homophily) and diffusion (a form of social contagion) in social networks is crucial to understand system dynamics and evolution. While it’s difficult to distinguish selection and diffusion in general without doing randomized experimentations, we build a mathematical model of opinion dynamics that the two effects are incorporated. Only using the opinion profiles of the individuals in the systems at two different times, without any intermediate information, we provide a machine-learning method to infer the global magnitude of selection and diffusion. Large-scale of simulated experiments are implemented, our algorithm gives good estimations of the two underlying parameters. Our models and methods could thus be essential to both our understanding of the mechanism and distinguishing the effect of selection and diffusion in network dynamics.