Creating Gender Detector (Male / Female) in Python

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn Python
  • You will learn Decision Tree
  • You will learn IF-THEN logic

Requirements

  • numpy
  • scipy
  • scikit-learn

Difficulty

  • Intermediate

Tutorial Contents

Hai Everyone, I spent part of an early semester holiday in 2018 to learn Python programming language interpreters. Initially interested in making a simple machine learning that can be trained, many search results recommend to use Python programming language.

After learning the basics and how Python works finally to practice the first case I take is the gender detection of men or women of each character with the help of tutorial here and there. This detector uses the Decision Tree method .

Decision Tree

For those who are not familiar with the Decision Tree method , simply this method is a classification method to get the conclusion of a data. The process works similar to IF-THEN logic . As an illustration of how the Decision Tree works , an illustration of Dr. The following Saed Sayad can help:

Decision_rules.png

Decision Tree logic can be trained by teaching certain patterns from a number of datasets to make new decisions, new inputs then analyzed to look for specific patterns according to previous training to generate a knowledge that can determine conclusions for new input data

Male or Female Detection Cases

In this case, the training data we use is high character data, weight and size of shoes from men and women. With a number of training datasets , Decision Tree will create an analysis of the character patterns of each gender so that when a new character data input application can determine the input data is the character of a man or woman.

How to make

In Python, there is a package that has provided the decision tree method to use. This package is stored in a bit-learn package . To be able to use scious-learn there are 2 other dependency package that is needed, that is numpy and scipy . These packages can be installed by using the #pip install [package] command via terminal / cmd. In general the installation package as follows:

  • $ pip install numpy
  • $ pip install scipy
  • $ pip install -U scikit-learn

Coding

Simply coding for this application can be done on one file only, create a file with the name demo.py, with the contents:

from sklearn import tree # import scikit-learn untuk decision tree

# sample data format [height, weight, size-shoe]
# data size of 11 people
data = [[181,80,44], [177,70,43], [160,60,38], [154,54,37], 
  [166,65,40], [190,90,47], [175,64,39], [165,49,40],
  [171,75,42], [157,55,39], [181,85,43],]

# data gender,berurut sesuai dengan datanya (merujuk variabel data)
gender = ['male', 'male', 'female', 'female', 
   'female', 'male', 'male', 'female', 
   'male', 'female', 'male',]

# calling the DecisionTreeClassifier () method from the tree side
klasifikasi = tree.DecisionTreeClassifier()
# training data, calling methods fit (param), param = data and gender
klasifikasi = klasifikasi.fit(data,gender)

# inserting new data to predict
# invokes the predict method ([data])
databaru = [170,59,41]
prediksi = klasifikasi.predict([databaru])

# print predicted results
print(data)
# print(type(data))
print(prediksi)

There are several important parts of the code above:

  • First we call the package to be used which is scikit-learn with
from sklearn import tree
  • Contains gender character data on variable
data
  • contains the gender character pair in the
gender

variable

  • Conduct training data with:
# calling the DecisionTreeClassifier () method from the tree sideklasifikasi = tree.DecisionTreeClassifier()
# training data, calling methods fit (param), param = data and gender
klasifikasi = klasifikasi.fit(data,gender)
  • Catch new data for prediction:
# inserting new data to predict
# invokes the predict method ([data])
databaru = [170,59,41]
prediksi = klasifikasi.predict([databaru])
  • then print the result with
print

command

Results

image.png

The above is one way to create a simple male-female detector with each character input.
So with the above tools we are able to predict a person with character traits such as height, weight, size-shoes. Okay, thanks to everyone who has seen the tutorial "Creating Gender Detector (Male / Female) in Python"



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

You simply used Google translate to translate this tutorial, and that is considered plagiarism.

You can contact us on Discord.
[utopian-moderator]

Hey @amosbastian, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!