Preface:

Bayesian formula is used to guess the gender of the other person according to their Chinese name. Without further ado, let’s begin happily

The development tools

**Python version: **3.6.4

Related modules:

Pyqt5 module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

Let’s start with a brief introduction to Bayes’ formula, and then we’ll talk about code implementation. We all know that the probability of event A occurring when event B has already occurred is:

If A and B are two independent events, then:

Obviously, we can use the above formula to determine whether two events are independent. Let’s introduce the total probability formula (superscript C stands for complement) :

The formula above is easy to understand if you draw a Venn diagram:

Based on the above conclusions, we can easily derive Bayes’ formula:

When it comes to guessing our names and genders, we’re asking:

Obviously, we have:

Here we know how often each character appears in male and female names:

We can assume that they are independent, for example:

def genderprob(name, probs, type_='male') :
  assert type_ in ['male'.'female']
  if type_ == 'male':
    p = self.male_total / self.total
    for c in name:
      p *= probs.get(c, (0.0))0]
  else:
    p = self.female_total / self.total
    for c in name:
      p *= probs.get(c, (0.0))1]
  return p
Copy the code

Or take Liu Yifei as an example:

P (female) = the number of occurrences of female name/total occurrences P = liu (liu | female) in the women's name the number of occurrences of total number/female nameCopy the code

The denominator can cancel out when we divide, so we don’t have to calculate, namely:

male_prob = genderprob(name, self.name_probs, 'male')
female_prob = genderprob(name, self.name_probs, 'female')
result = {'male': male_prob / (male_prob + female_prob), 'female': female_prob / (male_prob + female_prob)}
Copy the code

Then use PyQt5 to create a simple visual interface for this small model of name predictability:

That concludes this article, thank you for watching, and follow me in my daily series of Python widgets. In my next article, I’ll share widgets that automatically generate art signatures

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ Complete source code + dry + Python novice learning exchange group: 594356095