When the mind is disturbed by negative thoughts, opposite ones should be cultivated.
When the mind is assailed with thoughts contradictory to the principles of the Yamas and Niyamas, the Yogin should cultivate opposite thoughts that are aligned to the Yamas and Niyamas. This is called self-culture and going contrary to the grains of our undesirable habits and tendencies which have been in operation through repetition not only in this lifetime but also accumulated in previous ones.
What Rishi Patanjali is offering here is the Law of Substitution, replacing negative thoughts and habits by positive ones, their opposites. Repeated efforts must be made until the new habits (in line with the principles of the Yamas and Niyamas) are firmly in place. This is Yoga Psychology at its best and the pathway to self-mastery.
Remember that our habits, tendencies, thoughts and beliefs are conditioned by our programming whether that is appropriated (through culture, circumstances, environment) or by our own level of understanding. We can make a conscious choice to start moving towards more life giving and uplifting habits, tendencies, thoughts, beliefs and actions that will ultimately benefit us and others. It starts with awareness (of the negativity) and the genuine desire for self-cultivation (of positive thoughts and habits).