{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":472,"sourceType":"datasetVersion","datasetId":222}],"dockerImageVersionId":30839,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as srs\nimport matplotlib.pyplot as plt\nimport sklearn\nfrom sklearn.metrics import classification_report","metadata":{"_uuid":"ff579627-10ad-4e18-9d9a-d60311ea2b7e","_cell_guid":"01945f8f-a409-462a-969d-200c4691cbd5","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:09.494589Z","iopub.execute_input":"2025-03-31T11:17:09.494967Z","iopub.status.idle":"2025-03-31T11:17:09.499348Z","shell.execute_reply.started":"2025-03-31T11:17:09.494940Z","shell.execute_reply":"2025-03-31T11:17:09.498269Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/adult-income-dataset/adult.csv\")","metadata":{"_uuid":"6dde2c6e-1c9b-4a26-b692-0c3a2eb50a2f","_cell_guid":"33c8fa3d-795e-43d1-a8bd-6da78f20517e","collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:09.500514Z","iopub.execute_input":"2025-03-31T11:17:09.500883Z","iopub.status.idle":"2025-03-31T11:17:09.645209Z","shell.execute_reply.started":"2025-03-31T11:17:09.500840Z","shell.execute_reply":"2025-03-31T11:17:09.644294Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data","metadata":{"_uuid":"0fd6e99e-6392-461c-8e3f-c085d541fac8","_cell_guid":"cfb50929-5e8a-4220-914b-65d0f3f4f2ff","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:09.985009Z","iopub.execute_input":"2025-03-31T11:17:09.985329Z","iopub.status.idle":"2025-03-31T11:17:10.001213Z","shell.execute_reply.started":"2025-03-31T11:17:09.985304Z","shell.execute_reply":"2025-03-31T11:17:10.000093Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.isna().sum()","metadata":{"_uuid":"5611b0c7-dcc7-403b-8384-785bc976b835","_cell_guid":"85e51ea2-120d-4574-a383-07ec3f920c6a","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.002493Z","iopub.execute_input":"2025-03-31T11:17:10.002832Z","iopub.status.idle":"2025-03-31T11:17:10.042358Z","shell.execute_reply.started":"2025-03-31T11:17:10.002805Z","shell.execute_reply":"2025-03-31T11:17:10.041386Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.columns","metadata":{"_uuid":"e6580ef9-d611-4ade-b01d-edef468e9faa","_cell_guid":"18e7712a-a5e6-4ff9-9c6f-3ad91810c976","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.043997Z","iopub.execute_input":"2025-03-31T11:17:10.044295Z","iopub.status.idle":"2025-03-31T11:17:10.059506Z","shell.execute_reply.started":"2025-03-31T11:17:10.044272Z","shell.execute_reply":"2025-03-31T11:17:10.058645Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for single_column in data.columns:\n    print(\"unique value in {} are: {}\".format(single_column,data[single_column].unique()))","metadata":{"_uuid":"f79e0080-66e4-4978-9fe2-da91b3412e69","_cell_guid":"78648f27-eb16-4706-9825-af9e614ac275","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.060583Z","iopub.execute_input":"2025-03-31T11:17:10.060926Z","iopub.status.idle":"2025-03-31T11:17:10.112114Z","shell.execute_reply.started":"2025-03-31T11:17:10.060901Z","shell.execute_reply":"2025-03-31T11:17:10.111219Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.replace(to_replace=\"?\" ,value=np.nan ,inplace= True)","metadata":{"_uuid":"21f61319-80c9-4741-840d-bde8b2ffe7c6","_cell_guid":"981aa0df-6a8f-42b2-81e2-0cfe9e9315f8","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.113188Z","iopub.execute_input":"2025-03-31T11:17:10.113542Z","iopub.status.idle":"2025-03-31T11:17:10.151241Z","shell.execute_reply.started":"2025-03-31T11:17:10.113508Z","shell.execute_reply":"2025-03-31T11:17:10.150133Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for single_column in data.columns:\n    print(\"unique value in {} are: {}\".format(single_column,data[single_column].unique()))","metadata":{"_uuid":"e2b85c95-af64-4ca5-8f43-c2188042ca52","_cell_guid":"fa9387d1-11a6-450b-a7cf-2aa9de7af84d","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.197550Z","iopub.execute_input":"2025-03-31T11:17:10.198088Z","iopub.status.idle":"2025-03-31T11:17:10.231537Z","shell.execute_reply.started":"2025-03-31T11:17:10.198052Z","shell.execute_reply":"2025-03-31T11:17:10.230740Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mising_value_df=data.isna().sum()\nmising_value_df","metadata":{"_uuid":"6bbed7c6-7663-4d23-a3b2-86010510d685","_cell_guid":"893ded59-79e6-4fcb-8a57-833c115ee3d9","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.232578Z","iopub.execute_input":"2025-03-31T11:17:10.232908Z","iopub.status.idle":"2025-03-31T11:17:10.262424Z","shell.execute_reply.started":"2025-03-31T11:17:10.232883Z","shell.execute_reply":"2025-03-31T11:17:10.261393Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mod_df = data.mode()\nmod_df","metadata":{"_uuid":"152a8e9e-f206-4840-8143-93c8d61b4fa6","_cell_guid":"28d970c8-7019-4994-8ebb-8ff607049869","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.264202Z","iopub.execute_input":"2025-03-31T11:17:10.264476Z","iopub.status.idle":"2025-03-31T11:17:10.314230Z","shell.execute_reply.started":"2025-03-31T11:17:10.264449Z","shell.execute_reply":"2025-03-31T11:17:10.313275Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for single_column in mising_value_df[mising_value_df>0].index:\n    data[single_column].fillna(value=mod_df[single_column][0], inplace= True)","metadata":{"_uuid":"71f3f32e-2a0f-4cbb-9adf-c73018b34386","_cell_guid":"d2c737c9-b0e9-4a80-a178-235a51ad29e2","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.315579Z","iopub.execute_input":"2025-03-31T11:17:10.315885Z","iopub.status.idle":"2025-03-31T11:17:10.330669Z","shell.execute_reply.started":"2025-03-31T11:17:10.315862Z","shell.execute_reply":"2025-03-31T11:17:10.329723Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.isna().sum()","metadata":{"_uuid":"02a017c9-9feb-42be-898a-9e5d8c8c5dbb","_cell_guid":"1c8d2bbf-351f-4fb9-9232-25c5777d259c","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.331556Z","iopub.execute_input":"2025-03-31T11:17:10.331837Z","iopub.status.idle":"2025-03-31T11:17:10.371577Z","shell.execute_reply.started":"2025-03-31T11:17:10.331810Z","shell.execute_reply":"2025-03-31T11:17:10.370625Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.duplicated().sum()","metadata":{"_uuid":"3b812e00-f112-4d05-8759-57cf2a5e523b","_cell_guid":"d376ebbd-d5f1-46fe-afb3-d54fdf6beffa","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.372642Z","iopub.execute_input":"2025-03-31T11:17:10.372980Z","iopub.status.idle":"2025-03-31T11:17:10.426044Z","shell.execute_reply.started":"2025-03-31T11:17:10.372941Z","shell.execute_reply":"2025-03-31T11:17:10.425245Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.drop_duplicates(inplace=True,ignore_index=True)","metadata":{"_uuid":"9647e836-1ce8-43dd-9a76-0452b28e2594","_cell_guid":"302b2db6-ed64-468e-a2ae-92f37a5883cb","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.426983Z","iopub.execute_input":"2025-03-31T11:17:10.427296Z","iopub.status.idle":"2025-03-31T11:17:10.471747Z","shell.execute_reply.started":"2025-03-31T11:17:10.427271Z","shell.execute_reply":"2025-03-31T11:17:10.470923Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"income\"].unique()","metadata":{"_uuid":"c29d871d-952c-40d2-b783-bcf113e2639e","_cell_guid":"d797b38a-9cd3-4a12-929b-ca29b187ebb6","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.472587Z","iopub.execute_input":"2025-03-31T11:17:10.472878Z","iopub.status.idle":"2025-03-31T11:17:10.480608Z","shell.execute_reply.started":"2025-03-31T11:17:10.472855Z","shell.execute_reply":"2025-03-31T11:17:10.479745Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"less_then_50k_data = data[data[\"income\"] == data[\"income\"].unique()[0]]\ngreater_then_50k_data = data[data[\"income\"] == data[\"income\"].unique()[1]]","metadata":{"_uuid":"e5b2bbb9-780a-44e0-9808-ea8a821f7402","_cell_guid":"24a6b0ed-fce3-40ad-a0b1-41dbac9af82b","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.483415Z","iopub.execute_input":"2025-03-31T11:17:10.483732Z","iopub.status.idle":"2025-03-31T11:17:10.511155Z","shell.execute_reply.started":"2025-03-31T11:17:10.483708Z","shell.execute_reply":"2025-03-31T11:17:10.510351Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"less_then_50k_data.shape","metadata":{"_uuid":"473954c4-6924-4ed3-a6d7-6476099d0f5c","_cell_guid":"8a9581f6-f0f4-4b4b-96cb-b42028257a30","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.512710Z","iopub.execute_input":"2025-03-31T11:17:10.512997Z","iopub.status.idle":"2025-03-31T11:17:10.517903Z","shell.execute_reply.started":"2025-03-31T11:17:10.512973Z","shell.execute_reply":"2025-03-31T11:17:10.517027Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"greater_then_50k_data.shape","metadata":{"_uuid":"846a309b-97d8-4e59-a911-df75f2d8e48f","_cell_guid":"4bcc450b-a46b-4bf4-b3f7-8b934d48b469","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.518898Z","iopub.execute_input":"2025-03-31T11:17:10.519504Z","iopub.status.idle":"2025-03-31T11:17:10.535612Z","shell.execute_reply.started":"2025-03-31T11:17:10.519479Z","shell.execute_reply":"2025-03-31T11:17:10.534788Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data_fraction = 0.6\ncv_data_fraction = 0.4","metadata":{"_uuid":"6a76d964-5e87-4446-9ddf-4051be06b943","_cell_guid":"5106f1ed-b3ff-4812-ab38-2db5ca713dd1","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.536587Z","iopub.execute_input":"2025-03-31T11:17:10.536899Z","iopub.status.idle":"2025-03-31T11:17:10.554424Z","shell.execute_reply.started":"2025-03-31T11:17:10.536876Z","shell.execute_reply":"2025-03-31T11:17:10.553427Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data_num_rows = int(training_data_fraction * data.shape[0])\ncv_data_num_rows = int(cv_data_fraction * data.shape[0])","metadata":{"_uuid":"cb5f05ff-1eaf-4434-945e-dd88d1a4848e","_cell_guid":"7a2e3523-40ad-465f-89fe-f93351171549","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.555356Z","iopub.execute_input":"2025-03-31T11:17:10.555723Z","iopub.status.idle":"2025-03-31T11:17:10.573794Z","shell.execute_reply.started":"2025-03-31T11:17:10.555695Z","shell.execute_reply":"2025-03-31T11:17:10.572806Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data_num_rows","metadata":{"_uuid":"0c622537-247b-49ed-bcf5-1b4b4df5e316","_cell_guid":"ba87e709-3da6-4b95-b778-14b14e60b3d8","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.574721Z","iopub.execute_input":"2025-03-31T11:17:10.574988Z","iopub.status.idle":"2025-03-31T11:17:10.594194Z","shell.execute_reply.started":"2025-03-31T11:17:10.574965Z","shell.execute_reply":"2025-03-31T11:17:10.593377Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_data_num_rows","metadata":{"_uuid":"fc49ec80-1128-46ad-8af7-f3c36d4bd9de","_cell_guid":"14acff9f-8ab0-42fe-a345-7d3650fd14ce","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.595013Z","iopub.execute_input":"2025-03-31T11:17:10.595261Z","iopub.status.idle":"2025-03-31T11:17:10.611187Z","shell.execute_reply.started":"2025-03-31T11:17:10.595240Z","shell.execute_reply":"2025-03-31T11:17:10.610324Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"greater_then_50k_training_data = greater_then_50k_data.iloc[0:11000,:]\ngreater_then_50k_cv_data = greater_then_50k_data.iloc[11000:,:]\n\nless_then_50k_training_data = less_then_50k_data.iloc[0:18273,:]\nless_then_50k_cv_data = less_then_50k_data.iloc[18273:,:]","metadata":{"_uuid":"f6d63615-8360-4280-8950-ca72d24cc139","_cell_guid":"6e4522c2-51d2-4b8a-a152-61ae6a605459","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.612109Z","iopub.execute_input":"2025-03-31T11:17:10.612409Z","iopub.status.idle":"2025-03-31T11:17:10.628055Z","shell.execute_reply.started":"2025-03-31T11:17:10.612378Z","shell.execute_reply":"2025-03-31T11:17:10.626957Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"greater_then_50k_training_data.shape","metadata":{"_uuid":"6065f9bc-66e7-41d2-8b89-8593e760d355","_cell_guid":"733c3219-1d22-46b7-9e5d-52e51073f05a","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.629068Z","iopub.execute_input":"2025-03-31T11:17:10.629383Z","iopub.status.idle":"2025-03-31T11:17:10.646298Z","shell.execute_reply.started":"2025-03-31T11:17:10.629358Z","shell.execute_reply":"2025-03-31T11:17:10.645466Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"greater_then_50k_cv_data.shape","metadata":{"_uuid":"b55527da-9de9-4878-83a8-9a4779d5b1de","_cell_guid":"5387cac6-ad67-403a-807c-9e270b7a9db0","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.647143Z","iopub.execute_input":"2025-03-31T11:17:10.647460Z","iopub.status.idle":"2025-03-31T11:17:10.665166Z","shell.execute_reply.started":"2025-03-31T11:17:10.647430Z","shell.execute_reply":"2025-03-31T11:17:10.664181Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"less_then_50k_training_data.shape","metadata":{"_uuid":"2d589277-fbac-43c3-8c93-c643e763f5cb","_cell_guid":"9ae36e89-c64a-40c4-bbe9-626ab7565c56","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.666089Z","iopub.execute_input":"2025-03-31T11:17:10.666430Z","iopub.status.idle":"2025-03-31T11:17:10.682571Z","shell.execute_reply.started":"2025-03-31T11:17:10.666398Z","shell.execute_reply":"2025-03-31T11:17:10.681465Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"less_then_50k_cv_data.shape","metadata":{"_uuid":"91ef1b04-34d9-43da-b40b-e93829a79c3c","_cell_guid":"ad979710-5ef0-4af7-870a-7eb01f979370","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.683488Z","iopub.execute_input":"2025-03-31T11:17:10.683784Z","iopub.status.idle":"2025-03-31T11:17:10.701714Z","shell.execute_reply.started":"2025-03-31T11:17:10.683760Z","shell.execute_reply":"2025-03-31T11:17:10.700864Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data = pd.concat([less_then_50k_training_data,greater_then_50k_training_data],\n                         axis=0)","metadata":{"_uuid":"784965e5-9cf3-4acc-8a71-0f4a49fa52bd","_cell_guid":"363e2997-0611-4d47-b1c1-e738ec9c928e","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.702584Z","iopub.execute_input":"2025-03-31T11:17:10.702940Z","iopub.status.idle":"2025-03-31T11:17:10.720824Z","shell.execute_reply.started":"2025-03-31T11:17:10.702915Z","shell.execute_reply":"2025-03-31T11:17:10.719868Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data.shape","metadata":{"_uuid":"c82b7585-8faa-4b6d-a6d6-8d60bf2ce95e","_cell_guid":"ddfb548d-f6c5-4c18-92a7-1c0c209926e8","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.721972Z","iopub.execute_input":"2025-03-31T11:17:10.722249Z","iopub.status.idle":"2025-03-31T11:17:10.728641Z","shell.execute_reply.started":"2025-03-31T11:17:10.722226Z","shell.execute_reply":"2025-03-31T11:17:10.727907Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_data = pd.concat([less_then_50k_cv_data,greater_then_50k_cv_data],\n                         axis=0)","metadata":{"_uuid":"de8d5ed1-5de4-487d-91bb-6e80ce770ee0","_cell_guid":"09a36f4e-8e1f-4bbe-afe4-03c4cb54126e","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.729475Z","iopub.execute_input":"2025-03-31T11:17:10.729743Z","iopub.status.idle":"2025-03-31T11:17:10.750710Z","shell.execute_reply.started":"2025-03-31T11:17:10.729722Z","shell.execute_reply":"2025-03-31T11:17:10.749532Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_data.shape","metadata":{"_uuid":"15d53cba-3b19-43c8-ba7f-2cce8a8b9111","_cell_guid":"824602d1-8d1f-4b07-822e-cf6aacb24eb0","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.751778Z","iopub.execute_input":"2025-03-31T11:17:10.752131Z","iopub.status.idle":"2025-03-31T11:17:10.767140Z","shell.execute_reply.started":"2025-03-31T11:17:10.752098Z","shell.execute_reply":"2025-03-31T11:17:10.766311Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv_data.head()","metadata":{"_uuid":"458a6439-b77a-42dd-8b29-b73c3990a088","_cell_guid":"15746bac-1e84-4801-b86f-6c27b4339398","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.768132Z","iopub.execute_input":"2025-03-31T11:17:10.768487Z","iopub.status.idle":"2025-03-31T11:17:10.792931Z","shell.execute_reply.started":"2025-03-31T11:17:10.768453Z","shell.execute_reply":"2025-03-31T11:17:10.792003Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"likelihood_probabilities = dict(zip(data[\"income\"].unique(),\n                            [dict()]*data[\"income\"].unique().shape[0]))","metadata":{"_uuid":"04320cf9-3e77-4573-aa91-cb683eca8675","_cell_guid":"c642fca6-6010-4876-ad58-168e6396e29f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.794026Z","iopub.execute_input":"2025-03-31T11:17:10.794319Z","iopub.status.idle":"2025-03-31T11:17:10.813763Z","shell.execute_reply.started":"2025-03-31T11:17:10.794293Z","shell.execute_reply":"2025-03-31T11:17:10.812864Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"likelihood_probabilities","metadata":{"_uuid":"aa699f48-0f6c-4c0a-a000-39d87bbd352b","_cell_guid":"0fa5dfbd-0ee9-4b88-8752-c70b4a0eedea","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.817796Z","iopub.execute_input":"2025-03-31T11:17:10.818051Z","iopub.status.idle":"2025-03-31T11:17:10.823472Z","shell.execute_reply.started":"2025-03-31T11:17:10.818029Z","shell.execute_reply":"2025-03-31T11:17:10.822512Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prior_probabilities = dict(zip(data['income'].unique(), [0] * data['income'].unique().shape[0]))","metadata":{"_uuid":"00cd1a26-e1c8-46ea-a5da-ec52239719e6","_cell_guid":"2d4cbfdd-8844-4013-bd5e-256f9accb928","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.824712Z","iopub.execute_input":"2025-03-31T11:17:10.824946Z","iopub.status.idle":"2025-03-31T11:17:10.846246Z","shell.execute_reply.started":"2025-03-31T11:17:10.824926Z","shell.execute_reply":"2025-03-31T11:17:10.844936Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prior_probabilities","metadata":{"_uuid":"86db5f53-3a3a-4c3f-97a8-016b566e3ba9","_cell_guid":"5c0aeb69-c8c5-4d40-b52a-8b7c433ce1f5","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.847361Z","iopub.execute_input":"2025-03-31T11:17:10.847743Z","iopub.status.idle":"2025-03-31T11:17:10.853927Z","shell.execute_reply.started":"2025-03-31T11:17:10.847706Z","shell.execute_reply":"2025-03-31T11:17:10.853006Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for k in prior_probabilities.keys():\n    prior_probabilities[k] = (training_data[training_data['income'] == k].shape[0]/training_data.shape[0])","metadata":{"_uuid":"436e75be-536c-480f-a540-1928c36b6947","_cell_guid":"f91fe6ab-7273-4f3c-8c6d-397df9d3bbb8","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.854898Z","iopub.execute_input":"2025-03-31T11:17:10.855185Z","iopub.status.idle":"2025-03-31T11:17:10.880235Z","shell.execute_reply.started":"2025-03-31T11:17:10.855155Z","shell.execute_reply":"2025-03-31T11:17:10.879252Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" prior_probabilities","metadata":{"_uuid":"7eec8cf7-f3b4-4922-8ca2-31de5836925b","_cell_guid":"75fe2122-2ce9-422b-b4d1-81e692df1607","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-03-31T11:17:10.881297Z","iopub.execute_input":"2025-03-31T11:17:10.881632Z","iopub.status.idle":"2025-03-31T11:17:10.887143Z","shell.execute_reply.started":"2025-03-31T11:17:10.881597Z","shell.execute_reply":"2025-03-31T11:17:10.886318Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for k in likelihood_probabilities.keys():\n    \n    class_training_data = training_data[training_data['income'] == k]\n\n    for single_column in training_data.columns[0:-1]:\n        \n        blank_dict = dict()\n\n        for unique_value in data[single_column].unique():\n            \n            if unique_value in class_training_data[single_column].unique():\n                \n                unique_value_frequency = class_training_data[class_training_data[single_column] == unique_value].shape[0]\n                relative_frequency = unique_value_frequency / class_training_data.shape[0]\n                \n            else:\n                unique_value_frequency = 1\n                relative_frequency = unique_value_frequency / (class_training_data.shape[0] + data[single_column].unique().shape[0])\n\n            blank_dict[unique_value] = relative_frequency\n\n        likelihood_probabilities[k][single_column] = blank_dict","metadata":{"_uuid":"1b347903-4219-4b63-adf8-01d182a56499","_cell_guid":"53967f3b-11de-43c1-8bd3-794797702076","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:17:10.888070Z","iopub.execute_input":"2025-03-31T11:17:10.888309Z","iopub.status.idle":"2025-03-31T11:18:24.130401Z","shell.execute_reply.started":"2025-03-31T11:17:10.888287Z","shell.execute_reply":"2025-03-31T11:18:24.129385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data[training_data[\"income\"] == '<=50k'][\"age\"].unique()","metadata":{"_uuid":"d2d881ae-2a44-4912-a253-02b3ca16fe71","_cell_guid":"4be8a9ad-851a-4028-bbf4-a8a1a1d545b4","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.131400Z","iopub.execute_input":"2025-03-31T11:18:24.131757Z","iopub.status.idle":"2025-03-31T11:18:24.141046Z","shell.execute_reply.started":"2025-03-31T11:18:24.131722Z","shell.execute_reply":"2025-03-31T11:18:24.140102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"set(data['age'].unique()).difference(set(training_data[training_data['income'] == '<=50k'] ['age'].unique()))","metadata":{"_uuid":"adbcce2a-a65c-453a-aa27-35a1ab1453d1","_cell_guid":"b30f3364-a09f-42bd-8531-56e2410cead4","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.142273Z","iopub.execute_input":"2025-03-31T11:18:24.142622Z","iopub.status.idle":"2025-03-31T11:18:24.169820Z","shell.execute_reply.started":"2025-03-31T11:18:24.142598Z","shell.execute_reply":"2025-03-31T11:18:24.168906Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data.head()","metadata":{"_uuid":"aa4f31d0-1953-488b-91f3-b718a1fc0b4c","_cell_guid":"c16c73e3-4eb5-4f9e-9341-6181e64ca2d7","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.170770Z","iopub.execute_input":"2025-03-31T11:18:24.171087Z","iopub.status.idle":"2025-03-31T11:18:24.184509Z","shell.execute_reply.started":"2025-03-31T11:18:24.171063Z","shell.execute_reply":"2025-03-31T11:18:24.183552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def posterior_probability(income_type, single_row):\n    joint_probability = 1\n    single_row_dict = dict(single_row)\n    for single_column in list(single_row_dict.keys())[0:-1]:\n        joint_probability *= likelihood_probabilities[income_type][single_column][single_row[single_column]]\n\n    return joint_probability * prior_probabilities[income_type]","metadata":{"_uuid":"46a409c0-5c47-4962-8033-13649b62a268","_cell_guid":"4e76c31d-dc0a-42d2-a873-2d95a2f50684","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.185475Z","iopub.execute_input":"2025-03-31T11:18:24.185852Z","iopub.status.idle":"2025-03-31T11:18:24.200776Z","shell.execute_reply.started":"2025-03-31T11:18:24.185818Z","shell.execute_reply":"2025-03-31T11:18:24.199830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"income_type = list(data[\"income\"].unique())","metadata":{"_uuid":"2726d071-9700-48c4-a4b2-7f746a74b081","_cell_guid":"ffad6587-dbe5-4052-92e2-d692caf14d6c","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.201832Z","iopub.execute_input":"2025-03-31T11:18:24.202181Z","iopub.status.idle":"2025-03-31T11:18:24.219473Z","shell.execute_reply.started":"2025-03-31T11:18:24.202157Z","shell.execute_reply":"2025-03-31T11:18:24.218693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_income_type = list()\n\nfor i in range(training_data.shape[0]):\n\n    single_row = training_data.iloc[i]\n    p_less_than_50k_on_input_features = posterior_probability(income_type[0], single_row)\n    p_greater_than_50k_on_input_features = posterior_probability(income_type[1], single_row)\n\n    if p_greater_than_50k_on_input_features > p_less_than_50k_on_input_features:\n        predicted_income_type.append(income_type[1])\n    else:\n        predicted_income_type.append(income_type[0])","metadata":{"_uuid":"4f588a12-a863-472b-b95f-3cb9dd0d887b","_cell_guid":"0b2769e1-acd7-4957-9597-e34e80ebe5c9","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:24.220303Z","iopub.execute_input":"2025-03-31T11:18:24.220617Z","iopub.status.idle":"2025-03-31T11:18:30.519068Z","shell.execute_reply.started":"2025-03-31T11:18:24.220592Z","shell.execute_reply":"2025-03-31T11:18:30.518023Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_income_type = [income_type.index(x) for x in predicted_income_type]\nactual_income_type = [income_type.index(x) for x in training_data[\"income\"]]","metadata":{"_uuid":"fee2c0ed-d7f6-4eb7-adc7-27c8ad8e064f","_cell_guid":"b2c182ba-551a-4679-ad8e-92d0be82be8f","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:30.520014Z","iopub.execute_input":"2025-03-31T11:18:30.520353Z","iopub.status.idle":"2025-03-31T11:18:30.532156Z","shell.execute_reply.started":"2025-03-31T11:18:30.520322Z","shell.execute_reply":"2025-03-31T11:18:30.531231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(classification_report(y_true=actual_income_type, y_pred=predicted_income_type))","metadata":{"_uuid":"01823bf7-8500-404e-b011-0360c082b1af","_cell_guid":"6d82e92a-6f7e-4c20-8098-9a62c65b9066","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-03-31T11:18:30.533120Z","iopub.execute_input":"2025-03-31T11:18:30.533394Z","iopub.status.idle":"2025-03-31T11:18:30.607921Z","shell.execute_reply.started":"2025-03-31T11:18:30.533371Z","shell.execute_reply":"2025-03-31T11:18:30.606835Z"}},"outputs":[],"execution_count":null}]}