This study proposes a deep Q-network (DQN) algorithm based on the K-nearest neighbor (KNN) algorithm (K-DQN) for the energy consumption prediction of buildings. When using the Markov decision process to model the energy consumption of buildings, the K-DQN algorithm shrinks the original action space to improve the prediction accuracy and convergence rate considering large-scale action space problems. Firstly, the original action space is evenly divided into multiple sub-action spaces, and the corresponding state of each sub-action space is regarded as a class to construct the KNN algorithm. Secondly, actions of the same sequence in different classes are denoted by the KNN algorithm to shrink the original action space. Finally, state class probabilities and original states are combined by K-DQN to construct new states and help determine the meaning of each action in the shrunken action space, which can ensure the convergence of the K-DQN algorithm. The experimental results indicate that the proposed K-DQN algorithm can achieve higher prediction accuracy than deep deterministic policy gradient (DDPG) and DQN algorithms and take less network training time.