Last active
December 26, 2017 05:32
-
-
Save nicholaskajoh/3ae130a7e7df91141c3efdbc7a989304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# data | |
heights = np.array([6.3, 5.9, 5.1, 5.6, 5.1]) | |
weights = np.array([50.2, 79.7, 61.4, 47.1, 59.8]) | |
classes = np.array(["Male", "Female", "Female" , "Male", "Female"]) | |
# P(Class) | |
males_count = 0 | |
females_count = 0 | |
sample_size = len(classes) | |
for x in classes: | |
if x == "Male": | |
males_count += 1 | |
else: | |
females_count += 1 | |
p_male = males_count / sample_size | |
p_female = females_count / sample_size | |
heights_of_males = [] | |
weights_of_males = [] | |
heights_of_females = [] | |
weights_of_females = [] | |
for i in range(sample_size): | |
if classes[i] == "Male": | |
heights_of_males.append(heights[i]) | |
weights_of_males.append(weights[i]) | |
else: | |
heights_of_females.append(heights[i]) | |
weights_of_females.append(weights[i]) | |
mean_height_males = np.mean(heights_of_males) | |
mean_weight_males = np.mean(weights_of_males) | |
mean_height_females = np.mean(heights_of_females) | |
mean_weight_females = np.mean(weights_of_females) | |
var_height_males = np.var(heights_of_males) | |
var_weight_males = np.var(weights_of_males) | |
var_height_females = np.var(heights_of_females) | |
var_weight_females = np.var(weights_of_females) | |
def the_pdf(x, mean, variance): | |
pd = 1 / (np.sqrt(2 * np.pi * variance)) * np.exp((-(x - mean)**2) / (2 * variance)) | |
return pd | |
# Predict | |
x = [5.8, 82.1] # [height, weight] | |
p_height_male = the_pdf(x[0], mean_height_males, var_height_males) | |
p_weight_male = the_pdf(x[1], mean_weight_males, var_weight_males) | |
p_height_female = the_pdf(x[0], mean_height_females, var_height_females) | |
p_weight_female = the_pdf(x[1], mean_weight_females, var_weight_females) | |
# Get class probabilities | |
p_male_h_and_w = p_male * p_height_male * p_weight_male | |
p_female_h_and_w = p_female * p_height_female * p_weight_female | |
print("P(Male | height & weight) =", p_male_h_and_w) | |
print("P(Female | height & weight) =", p_female_h_and_w) | |
# Return prediction | |
if p_male_h_and_w > p_female_h_and_w: | |
print("class = Male") | |
else: | |
print("class = Female") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment