In this article, we propose a model to optimize the detection of attacks in IoT. IoT network is a promising technology that connects living and non-living things around the world. Despite the increased development of these technologies, cyber-attacks remains a weakness, making it vulnerable to numerous cyber-attacks. Of course, automatic computer intrusion detection systems are deployed. However, it does not make it possible to mobilize the full potential of Machine Learning. Our approach in this maneuver consists of offering a means to select the least expensive ML method in terms of learning in order to optimize the prediction of threats to introduce IoT objects. To do this, we make modular design based on two layers. The first module is a canvas containing the different methods most used in ML such as supervised learning method, unsupervised learning method and reinforcement learning method. The second module introduces a mechanism to measure the learning cost linked to each of these methods in order to choose the least expensive one in order to quickly and efficiently detect intrusions in IoT objects. To prove the validity of the proposed model, we simulated it using the Weka tool. The results obtained illustrate the following behaviors: The classification quality rate is 93.66%. This last result is supported by a classification consistency rate of 0.882 (close to unity 1) demonstrating a trend towards convergence between observation and prediction.