Lung cancer is the most common malignant tumor in humans and the leading cause of cancer-related deaths worldwide. In this study, we focused on the immune cells in the microenvironment of lung cancer at the protein expression level by IHC as well as mIHC techniques to explore the spatial distribution characteristics of immune cells within the tumor. To predict the prognosis of NSCLC patients and their potential response to immunotherapy, a machine learning-based immune-related prognostic model for lung cancer was constructed by combining Cox regression analysis, random survival forest and XGBoost algorithm, and the effect of the prognostic model was verified on the relevant dataset. The results showed that there were some differences in the immune cells between lung adenocarcinoma and lung squamous carcinoma in the lung cancer microenvironment, and the spatial distribution heterogeneity of CD3+ T cells and MHC class II antigen-presenting cells was higher in lung adenocarcinoma (P<0.05).The overall survival of high-risk patients was lower than that of the low-risk group in both LUAD and LUSC (P<0.01), and the immuno-associated prognostic model of lung cancer had a stable performance in the AUC value in multiple independent cohorts with stable performance, and the IRS model maintained high accuracy and stable performance in the training set and test set, which indicates that IRS has great potential for clinical application.