Software Defined Networking (SDN) is a new architectural paradigm that enables programmable control of a network to make it more flexible and easier to manage. SDN architectures decouple control and forwarding functionalities, and enable switches and routers to be remotely configurable/programmable in run-time by a controller. Modeling and optimization of such modern heterogeneous network infrastructures are key factors to achieve better performance, e.g. in terms of traffic flow improvement while reducing bandwidth allocation. Identifying an accurate model of a network device in SDNs (e.g., a switch or a router) is crucial in order to apply advanced techniques such as Model Predictive Control (MPC). However, such a problem is very challenging due to non-linearities and unavailability of internal variables measurements in real devices. To this aim, a promising direction is given by an appropriate integration of System Identification and Machine Learning techniques to obtain predictive models using historical data collected from the network thanks to the SDN paradigm. In this paper we apply a novel data-driven methodology to learn accurate models of the dynamical input-output behavior of a network's switch device by appropriately combining AutoRegressive eXogenous (ARX) model identification with Regression Trees (RTs) and Random Forests (RFs). The advantage of such model is that it can be directly used to apply MPC (which just requires Quadratic Programming to be solved) to optimally control the queues' bandwidth of the switch ports within the SDN paradigm. We validate our approach on an experimental emulation setup using the Mininet network emulator environment and a real dataset obtained from measurements of an Italian Internet Service Provider (Sonicatel S.r.l.). To this aim, we first develop a model of a real network switch, then implement MPC using the RYU controller, and finally demonstrate the benefits of the proposed dynamic queueing control methodology in terms of packet losses reduction and bandwidth saving, i.e. in terms of improvement of the Quality of Service.