With the rapid expansion of computer networks, their security has become an attractive topic for researchers. Intrusion detection is known as one of the main techniques for retaining security in computer networks. Intrusion detection systems are divided into two categories: misuse detection and anomaly detection. Misuse detection techniques work based on the known attack patterns and have high detection rates. However, they are unable to detect new attacks. Anomaly detection techniques can detect new attacks, but they suffer from a high false alarm rate. An anomaly detection system often uses many features some of which may have no impact on detection. Some features may even adversely affect the detection rate. Feature selection can significantly improve the performance of an intrusion detection system. The main task of a feature selector is finding an optimal subset of features to improve the prediction accuracy. In this paper, a new hybrid evolutionary algorithm based on the imperialist competitive and genetic algorithm is proposed to select the optimal features for an anomaly detection system. The algorithm has been evaluated using the KDD99 dataset. The experimental results show that the proposed algorithm has a high detection rate and a low false alarm rate and outperforms the existing similar algorithms in terms of prediction accuracy. In addition, the proposed algorithm converges very fast and can quickly find optimal features which can make it suitable for being used in lightweight intrusion detection systems.