Most of the time, no real way to know, especially with shadowbans. For regular bans and restrictions, unless a moderator explicitly tells you what happened or the platform gives you a specific reason, you're mostly left guessing from whatever generic explanation they provide. Even then, there's a lot of asterisks involved.
The fact that you made 20 comments then banned a few hours later could have done it on it's own, could have been any number of them or just one. Maybe it's spam, a false-positive, what have you.
So you can sometimes make an educated guess, but you generally can't know for certain unless the platform actually tells you.