Making a Gender Classifier With Python (PyMachine Learning Series)

What does this program do?
This program will be given a list of three inputs (Height, Weight and Shoe size) and will predict whether the given attributes belong to a male or a female, Pretty neat!

What do we need?
Well, Since we are making this program in Python we need Python 3 installed on our computer. You can download python3 from the link given below(Make sure you download the correct BIT version I.E 32-bit or 64-bit depending on your computer), run the installer and once python is installed we can proceed to install other dependencies.

https://www.python.org/downloads/release/python-352/

Now press the WINDOWS KEY +R to open Run and type “CMD”, this will open the command prompt and type in the following commands individually and let them install.

pip install numpy
pip install scikit-learn

And if for some reason CMD does not recognize the above commands refer to this link below on how to install PIP(Python Package Manager) and once you have installed it type the above commands again.

YouTube Tutorial: https://www.youtube.com/watch?v=zPMr0lEMqpo

And now we are ready to rumble! (Don’t forget to write the program in IDLE)
Let’s write our program (Basic python knowledge needed)
First, we import our modules

from sklearn import tree

Then we make a parent list variable which contains other small lists which have three inputs (Height, Weight and Shoe size of various men and women)If you are curious you can google the average height, weight and shoe size of men and women over the years) And the reason we need this raw data is that our program will make decisions based on this data and decide whether the input WE provide it is of a female or male by using something called a decision tree.

X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],
[190, 90, 47], [175, 64, 39],
[177, 70, 40], [159, 55, 37], [171, 75, 42], [181, 85, 43]]

Now we need another list to tell our program which list above belong to women or men.
Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
'female', 'male', 'male']

Now we make our decision tree object to predict whether a random input we give to our program is of a male or female, here we provide our own input.

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
prediction = clf.predict([[190, 70, 43]])

Now Lastly we print our prediction
print(prediction)

And now save the code and on running we shall see the following output
male

I don’t intend to trigger feminists 🙂

Voila! we have successfully trained our program to classify male or female gender using just three inputs
P.S: The more data you give, the accurate the prediction will be, do play around with the code. And for Linux and Mac users write your code in any text editor of your choice and run it in the terminal using

python .py

Source: Video

This is Harvinder and I will see you next time!

You might also like More from author