The results obtained for the modeling and optimization of photovoltaic self-consumption facilities are presented. The study has been carried out for three Spanish cities with different climatic conditions. The self-consumption and self-sufficiency curves for different hourly consumption profiles have been obtained based on the installed peak power and the size of the battery. Different models of machine learning are proposed to predict these parameters. The input variables of these models are related to the configuration of the installation, its location and the type of consumption profile. The model with best predictions of self-sufficiency is Random Forest, which in cross-validation has a relative error of 5%. For the prediction of self-consumption, the model that performs best is the multilayer perceptron, with an average absolute error of 0.55 and an absolute relative error of 3%.