Aiming at the problem that the single model compensation method has poor adaptability to the temperature error of fiber optic gyroscope(FOG), a sectional compensation method for the temperature error of FOG based on random forest (RF)algorithm is proposed based on the idea of sectional modeling compensation. During modeling, factors affecting temperature and temperature changes were added, and three hyperparameters were optimized to obtain the optimal compensation model. In order to verify the compensation effect of this method, temperature experiments of $-60\ ^{\circ}\ \mathbf{C} \sim 40\ ^{\circ}\ \mathbf{C}$ FOG were designed, and the temperature errors were compensated using the proposed method, least squares method, and BOA-GBDT, respectively. The comparison results indicate that the BO-RF model can greatly reduce temperature errors. Compared with traditional algorithms, the average bias value of the gyroscope is reduced by an order of magnitude, and the BO-RF segmented model has the best compensation effect.