Docsity

Automating Information Gain in ML: Python Program for Education & Marital Status, Exercises of Machine Learning

Partial preview of the text

Download Automating Information Gain in ML: Python Program for Education & Marital Status and more Exercises Machine Learning in PDF only on Docsity!

Name: Muhammad Luqman Ayub

Reg No.: 2020 - AG- 6459

Course: IT- 505

Course Title: Machine Learning

Submitted to: Dr. Hassan Tariq

Topic:

PROGRAM TO AUTOMATE INFORMATION GAIN ON GIVEN DATASET

PROGRAM TO AUTOMATE INFORMATION GAIN ON GIVEN DATASET

import pandas as pd import numpy as np import math #Information_Gain play_data = pd.read_csv('data.csv') print(play_data) print(play_data.Emp_Sal.value_counts()) p = - (9/14)np.log2(9/14) q = - (5/14)np.log2(5/14) ovall_uncen = p + q print("Overall_Uncertainity: ",ovall_uncen) print("Emp_Sal <=50K") print(play_data.Emp_Sal == "<=50K") print("Emp_Sal >50K") print(play_data.Emp_Sal == ">50K") play_data[play_data.Edu_of_Emp == 'Bachelors'] Entropy_Play_Edu_Bachelors =-(4/6)np.log2(4/6) - (2/6)np.log2(2/6) print("Entropy_Play_Edu_Bachelors: ",Entropy_Play_Edu_Bachelors) play_data[play_data.Edu_of_Emp == 'Masters'] Entropy_Play_Edu_Masters =-(1/2)np.log2(1/2) - (1/2)np.log2(1/2) print("Entropy_Play_Edu_Masters: ",Entropy_Play_Edu_Masters) play_data[play_data.Edu_of_Emp == 'HS-grad'] Entropy_Play_Edu_HS_grad =-(1/2)np.log2(1/2) - (1/2)np.log2(1/2) print("Entropy_Play_Edu_HS_grad: ",Entropy_Play_Edu_HS_grad)

Entropy(Emp_Sal|Edu_of_Emp=11th)

Since, it's a homogenous data entropy will be 0

Entropy(Emp_Sal|Edu_of_Emp=9th)

print("overall_uncertainity_on_Emp_race_type: ",overall_uncertainity_on_Emp_race_type) Gain_on_Emp_race_type = ovall_uncen - (8/14)Entropy_Play_Emp_race_type_White - (5/14)Entropy_Play_Emp_race_type_Black print("Reduction in Uncertainity Emp_race_type: ",Gain_on_Emp_race_type) Entropy_Play_sex_of_emp_Male =-(5/9)math.log2(5/9) - (4/9)math.log2(4/9) print("Entropy_Play_sex_of_emp_Male: ",Entropy_Play_sex_of_emp_Male) Entropy_Play_sex_of_emp_Female =-(4/5)np.log2(4/5) - (1/5)np.log2(1/5) print("Entropy_Play_sex_of_emp_Female: ",Entropy_Play_sex_of_emp_Female) overall_uncertainity_emp_sex = (9/14)Entropy_Play_sex_of_emp_Male + (5/14)Entropy_Play_sex_of_emp_Female print("overall_uncertainity_emp_sex: ",overall_uncertainity_emp_sex) Gain_on_sex_of_emp = ovall_uncen - (9/14)Entropy_Play_sex_of_emp_Male - (5/14)Entropy_Play_sex_of_emp_Female print("Reduction in Uncertainity sex_of_emp: ",Gain_on_sex_of_emp) #So Education Reduces the uncertainity the most OUTPUT