Heart disease is among the leading causes of mortality globally. Predicting cardiovascular disease is a major difficulty in clinical data analysis. AI has been demonstrated to be powerful in deciding and anticipating an enormous measure of information created by the health domain. We provide a unique method for finding essential traits employing machine learning approaches in this paper, which enhances the effectiveness of identifying heart diseases. Decision tree (DT), support vector machine (SVM), artificial neural network (ANN), and K-nearest neighbor (KNN) are the classification techniques used to create the proposed system. Ensemble stacking integrates the four classification models to create a single best-fit predictive model using logistic regression. Many explorations have been directed at the identification of cardiac infection; however, the exactness of the outcomes is poor. Accordingly, to further enhance the efficiency, Moth-Flame Optimization (MFO) algorithm is proposed. The feature selection strategies are used to improve the classification accuracy while shortening the execution time of the classification system. Medical data are used to assess the probability of heart disease based on BP, age, gender, chest ache, cholesterol, blood sugar, and other variables. Results revealed that the proposed system excelled other existing models, obtaining 99% accuracy in the Cleveland dataset.