A person on average tend to be more comfortable and confident in decisions if its mimics behavior of people that look/are similar to themselves.
The teaching profession in Sweden actually start out with almost 50/50 during the first year at university. What the Swedish study found was that as students had setbacks, the students that decided to quit was mostly men. By graduation time a large portion of the male students has quit. Same thing occurs when people start seeking jobs, after the first year, second year, third year and fourth year. At the end the wast majority of men are gone from the profession, most citing "culture mismatch" and not feeling that they belong.
I find this theory much more plausible explanation than hormones, and it also explains why removing gender roles might increase gender segregation. If a person is uncertain where they fit, making a similar decision to other people in the same situation will on average feel safer. It also explain why mentor programs are one of the most effective tactics to combat gender segregation.
My own anecdotal observation for Norway matches this. Norway is the top of charts regarding gender equality. Yet for example, family lawyers, a highly paid profession, here are mostly women, a complete reversal of situation that was 50 years ago.
In software development the gender inequality increased during the last 20 years especially if one accounts that many women in IT here are immigrants. They find it is easier to get a job without good knowledge of Norwegian (in IT in Norway English is typically enough) even if such jobs are not something that they prefer all other things considered equal.
This is an interesting low-blame description of a phenomenon that might otherwise be described as discrimination because of a hostile work environment.
I actually think the real issue is that men are not always professional when working with women that they find attractive. Time for personal anecdote: Every woman I know in tech has been hit on by coworkers, sometimes multiple times even though turning down the man. Depending on the guy, it can get real nasty. In addition, they all hear the same thing at least once "Wow you know programming/hacking/linux, pretty cool for being a girl". Imaging hearing that shit over and over again, of course you will quit.
My point is, there are many factors at play here. Sadly, I don't think there is a good solution to this problem.
Note the average here does not translate into an ability to predict performance of a particular person based on biological sex as the variation is too high for that, but statistically the effect is rather significant.
So I suspect if a profession require a lot of social interactions, the observed discrimination is simple reflection that men may perform worse on average in those professions.