Can I punish myself for a sin without being exposed in society?
It is not permissible for someone to implement punishments (such as Hudood and Tazeer) upon oneself. However, there is no problem in making fasting a few days, Nafl Salah, charity etc. incumbent upon oneself as a form of self-admonition. In fact, this will make it easy for one to stop committing sins.
کما فی الصحیح للمسلم: عن ابی ہریرۃؓ قال قال رسول اللہ ﷺ من قتل نفسہ بحدیدۃ فحدیدتہ فی یدہ یتوجہ بہا فی بطنہ فی نار جہنم خالدًا فیہا۔ الخ (ص۱۰۹) واللہ اعلم بالصواب