In online monitoring of critical systems, it is important to detect an abnormal behavior as early as possible. Signal temporal logic (STL) formulas are used to specify these undesired behaviors due to the expressivity and interpretability of the logic and the existence of efficient online monitoring algorithms. In this paper, we present a new method to synthesize formulas that belong to past time fragment of STL from a labeled dataset. In particular, we consider a dataset that includes signals and their labels marking the moment of occurrence of undesired behaviors, and propose a formula synthesis algorithm based on data mining algorithms. We first transform the dataset into a new dataset with attributes encoding basic temporal formulas, then learn a classifier from the transformed dataset and finally generate a ptSTL formula from the classifier. The proposed method requires much less computational time compared to similar algorithms and achieves competitive detection performance as shown in the case studies.