One thing I have to say here: nothing can prepare you for the fear that comes from a stranger pulling a weapon on you. If you've been in a fight with a stranger, you might understand this.
I agree completely with those people who say take up a martial arts, and also that mental preparation is a good idea. However, you must remember that your opponent will already have the intent to harm you and will have a weapon ready to use before you even know what's going on.
My advice is to be prepared to end the situation peacefully (if possible) or try to distract them so you can get the first hit in. Without that hit, you will most likely be left critically injured.