It's a simple probability calculation. SF has a smaller area which results in anyone having the possibility of getting targeted by dangerous people. Larger cities also have more gang violence because they are bigger and gangs try to control territory and cause a lot of crime in the process.
SF has made itself a target by thinking people don't have the capacity to do bad things on purpose... and bad people will absolutely take advantage of this (ironic?). There is more nuance here of course, but I'm trying to quickly explain why less than 10 years ago I was walking around at 2am and felt perfectly safe, but now I won't even go to SF during the day.