{ "cells": [ { "cell_type": "markdown", "id": "7424e48d-63d4-4b9e-bccd-7246022300fa", "metadata": {}, "source": [ "# Homework3" ] }, { "cell_type": "code", "execution_count": 1, "id": "848bf60e-049a-473f-9b20-a21965273ebb", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "00aa0257-5bc5-4d0d-a2cc-cb51d101e0c2", "metadata": {}, "outputs": [], "source": [ "data = 'https://raw.githubusercontent.com/DataTalksClub/machine-learning-zoomcamp/main/cohorts/2026/data/course_lead_scoring_2026.csv'" ] }, { "cell_type": "code", "execution_count": 3, "id": "78600a24-fe4c-495f-b72f-70ec6c6ec360", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2026-10-14 07:51:10-- https://raw.githubusercontent.com/DataTalksClub/machine-learning-zoomcamp/main/cohorts/2026/data/course_lead_scoring_2026.csv\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.110.133, 185.199.111.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 278746 (272K) [text/plain]\n", "Saving to: ‘data_homework3.csv’\n", "\n", "data_homework3.csv 100%[===================>] 272.21K --.-KB/s in 0.02s \n", "\n", "2026-10-14 07:51:10 (11.4 MB/s) - ‘data_homework3.csv’ saved [278746/278746]\n", "\n" ] } ], "source": [ "!wget $data -O 'data_homework3.csv'" ] }, { "cell_type": "code", "execution_count": 26, "id": "c22c0c98-6ae9-45a8-adb0-a517a2ce42ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5000" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('data_homework3.csv')\n", "len(df)" ] }, { "cell_type": "code", "execution_count": 27, "id": "859778cd-e988-4059-b819-ca7628fe1dc3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
lead_sourceindustryemployment_statuslocationannual_incomenumber_of_courses_viewedinteraction_countlead_scoreconverted
0organic_searchtechnologyemployedeurope88160.0340.641
1social_mediatechnologyemployedsouth_america72688.0370.721
2referralretailemployedeurope44697.0340.581
3paid_adsmanufacturingstudentNaNNaN360.570
4referralmanufacturingstudentnorth_america24062.0450.621
\n", "
" ], "text/plain": [ " lead_source industry employment_status location \\\n", "0 organic_search technology employed europe \n", "1 social_media technology employed south_america \n", "2 referral retail employed europe \n", "3 paid_ads manufacturing student NaN \n", "4 referral manufacturing student north_america \n", "\n", " annual_income number_of_courses_viewed interaction_count lead_score \\\n", "0 88160.0 3 4 0.64 \n", "1 72688.0 3 7 0.72 \n", "2 44697.0 3 4 0.58 \n", "3 NaN 3 6 0.57 \n", "4 24062.0 4 5 0.62 \n", "\n", " converted \n", "0 1 \n", "1 1 \n", "2 1 \n", "3 0 \n", "4 1 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 28, "id": "d33c13eb-9623-465f-93a5-fce850b4d06a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lead_source str\n", "industry str\n", "employment_status str\n", "location str\n", "annual_income float64\n", "number_of_courses_viewed int64\n", "interaction_count int64\n", "lead_score float64\n", "converted int64\n", "dtype: object" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.dtypes" ] }, { "cell_type": "code", "execution_count": 29, "id": "0ddf02ae-b0b1-43a8-a599-dd19f07d240b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['lead_source', 'industry', 'employment_status', 'location',\n", " 'annual_income', 'number_of_courses_viewed', 'interaction_count',\n", " 'lead_score', 'converted'],\n", " dtype='str')" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "code", "execution_count": 30, "id": "031e1dc2-dfdd-49e4-8d55-8bd8a3d19075", "metadata": {}, "outputs": [], "source": [ "categorical = ['lead_source', 'industry', 'employment_status', 'location']\n", "numerical = ['annual_income', 'number_of_courses_viewed', 'interaction_count',\n", " 'lead_score', 'converted']" ] }, { "cell_type": "code", "execution_count": 31, "id": "339ad380-7428-4eec-9f94-8383420bdc16", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lead_source 148\n", "industry 240\n", "employment_status 194\n", "location 208\n", "annual_income 369\n", "number_of_courses_viewed 0\n", "interaction_count 0\n", "lead_score 35\n", "converted 0\n", "dtype: int64" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.isnull().sum()" ] }, { "cell_type": "code", "execution_count": 32, "id": "2adaee94-666f-46c2-9cd6-634531139e84", "metadata": {}, "outputs": [], "source": [ "for c in categorical:\n", " df[c] = df[c].fillna('NA')\n", "for c in numerical:\n", " df[c] = df[c].fillna(0.0)" ] }, { "cell_type": "markdown", "id": "8794dfc9-c4eb-4632-a473-b339981dfcc0", "metadata": {}, "source": [ "## Q1" ] }, { "cell_type": "code", "execution_count": 33, "id": "03c8aecb-93ee-46f2-834a-ceb529d78dc9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "industry\n", "technology 0.2346\n", "retail 0.1850\n", "healthcare 0.1680\n", "finance 0.1440\n", "education 0.1278\n", "manufacturing 0.0926\n", "NA 0.0480\n", "Name: proportion, dtype: float64" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.industry.value_counts(normalize=True)" ] }, { "cell_type": "markdown", "id": "880caf83-ea29-43ab-8da0-96199a55458c", "metadata": {}, "source": [ "## Q2" ] }, { "cell_type": "code", "execution_count": 34, "id": "11c0c4c0-1da2-4f16-b478-fadb77fcdb92", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
annual_incomenumber_of_courses_viewedinteraction_countlead_scoreconverted
annual_income1.0000000.1613000.1228420.2294960.189652
number_of_courses_viewed0.1613001.0000000.7216090.7572040.395038
interaction_count0.1228420.7216091.0000000.9157460.448624
lead_score0.2294960.7572040.9157461.0000000.483246
converted0.1896520.3950380.4486240.4832461.000000
\n", "
" ], "text/plain": [ " annual_income number_of_courses_viewed \\\n", "annual_income 1.000000 0.161300 \n", "number_of_courses_viewed 0.161300 1.000000 \n", "interaction_count 0.122842 0.721609 \n", "lead_score 0.229496 0.757204 \n", "converted 0.189652 0.395038 \n", "\n", " interaction_count lead_score converted \n", "annual_income 0.122842 0.229496 0.189652 \n", "number_of_courses_viewed 0.721609 0.757204 0.395038 \n", "interaction_count 1.000000 0.915746 0.448624 \n", "lead_score 0.915746 1.000000 0.483246 \n", "converted 0.448624 0.483246 1.000000 " ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.corr(numeric_only=True)" ] }, { "cell_type": "markdown", "id": "48ec7b17-d2a6-4d9c-9b75-fb2557474340", "metadata": {}, "source": [ "## Q3" ] }, { "cell_type": "code", "execution_count": 35, "id": "dee9ed6f-e819-440d-a9df-5f54976bbb39", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "code", "execution_count": 36, "id": "adb90829-8580-459c-9e28-a87922e9655a", "metadata": {}, "outputs": [], "source": [ "df_full_train, df_test = train_test_split(df, test_size=0.2, random_state=42)\n", "df_train, df_val = train_test_split(\n", " df_full_train, test_size=0.25, random_state=42\n", ")" ] }, { "cell_type": "code", "execution_count": 37, "id": "6abcaf71-5c7a-4ec6-b511-9892c0a94587", "metadata": {}, "outputs": [], "source": [ "df_train = df_train.reset_index(drop=True)\n", "df_val = df_val.reset_index(drop=True)\n", "df_test = df_test.reset_index(drop=True)\n", "df_full_train = df_full_train.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 39, "id": "57240029-b2ac-4dcf-ac1b-3fd488c78678", "metadata": {}, "outputs": [], "source": [ "y_train = df_train.converted.values\n", "y_val = df_val.converted.values\n", "y_test = df_test.converted.values\n", "\n", "y_full_train = df_full_train.converted.values" ] }, { "cell_type": "code", "execution_count": 41, "id": "f5095611-fe84-4054-8e1f-500d96cacb82", "metadata": {}, "outputs": [], "source": [ "del df_train['converted']\n", "del df_val['converted']\n", "del df_test['converted']\n", "del df_full_train['converted']" ] }, { "cell_type": "code", "execution_count": 42, "id": "9ad7f6f5-b794-4d0a-a939-2999002ee696", "metadata": {}, "outputs": [], "source": [ "from sklearn.metrics import mutual_info_score" ] }, { "cell_type": "code", "execution_count": 45, "id": "6598b1d5-9572-4ef9-bd28-b60c95767532", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lead_source 0.03434605791617973\n", "industry 0.0019945636715935217\n", "employment_status 0.018965604906982875\n", "location 0.001042763579701525\n" ] } ], "source": [ "for c in categorical:\n", " print(c,mutual_info_score(df_train[c],y_train))" ] }, { "cell_type": "markdown", "id": "e93e294f-f9bf-42e3-9435-0866e12ced60", "metadata": {}, "source": [ "## Q4" ] }, { "cell_type": "code", "execution_count": 46, "id": "8da240f0-8afe-4aae-9aaa-77aa433a8651", "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_extraction import DictVectorizer" ] }, { "cell_type": "code", "execution_count": 48, "id": "295bd205-6bab-40a0-ae86-d270430e6309", "metadata": {}, "outputs": [], "source": [ "train_dicts = df_train.to_dict(orient='records')" ] }, { "cell_type": "code", "execution_count": 50, "id": "ee2d4401-5f13-453f-8765-6b4175713df4", "metadata": {}, "outputs": [], "source": [ "dv = DictVectorizer(sparse=False)" ] }, { "cell_type": "code", "execution_count": 51, "id": "8b258316-4304-480a-9068-67036a9313c5", "metadata": {}, "outputs": [], "source": [ "X_train = dv.fit_transform(train_dicts)" ] }, { "cell_type": "code", "execution_count": 55, "id": "fdd7f282-84a3-4750-8c25-19eb743646b1", "metadata": {}, "outputs": [], "source": [ "val_dicts = df_val.to_dict(orient='records')\n", "X_val = dv.fit_transform(val_dicts)" ] }, { "cell_type": "code", "execution_count": 56, "id": "cfc918d4-d3f1-4115-a5b7-47b092968117", "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression" ] }, { "cell_type": "code", "execution_count": 57, "id": "6d02d993-b308-45c1-a0e1-f7de2b51ea81", "metadata": {}, "outputs": [], "source": [ "model = LogisticRegression(solver='liblinear', C=1.0, max_iter=1000, random_state=42)" ] }, { "cell_type": "code", "execution_count": 58, "id": "0933e9fa-b316-4b8c-a8fc-6189f0dbb641", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
LogisticRegression(max_iter=1000, random_state=42, solver='liblinear')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "LogisticRegression(max_iter=1000, random_state=42, solver='liblinear')" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 60, "id": "da5fc7e0-0416-4e7f-9b4d-61d39c5c8785", "metadata": {}, "outputs": [], "source": [ "y_pred = model.predict_proba(X_val)[:,1]" ] }, { "cell_type": "code", "execution_count": 61, "id": "67b895a4-f468-44e8-a703-881e1c26c8e6", "metadata": {}, "outputs": [], "source": [ "convert_decision = (y_pred >=0.5)" ] }, { "cell_type": "code", "execution_count": 66, "id": "2fdae043-957a-4f5a-b817-f4a358e0d71d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(0.645)" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "accuracy = round((y_val == convert_decision).mean(),3)\n", "accuracy" ] }, { "cell_type": "markdown", "id": "ef44ff47-c591-4644-a4ff-8d8c5615899b", "metadata": {}, "source": [ "## Q5" ] }, { "cell_type": "code", "execution_count": 68, "id": "2300e10e-c2c1-467f-ac31-5ba5c910b772", "metadata": {}, "outputs": [], "source": [ "numerical = ['annual_income', 'number_of_courses_viewed', 'interaction_count',\n", " 'lead_score']" ] }, { "cell_type": "code", "execution_count": 81, "id": "63a6de81-45ef-468a-82a2-f61f321b433d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['lead_source',\n", " 'industry',\n", " 'employment_status',\n", " 'location',\n", " 'annual_income',\n", " 'number_of_courses_viewed',\n", " 'interaction_count',\n", " 'lead_score']" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "columns = categorical+numerical\n", "deleted = ['lead_source', 'number_of_courses_viewed', 'interaction_count']\n", "columns" ] }, { "cell_type": "code", "execution_count": 83, "id": "600c0ab8-9abe-4a9b-8cfb-bd5709024488", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lead_source -0.0030000000000000027\n", "number_of_courses_viewed -0.0020000000000000018\n", "interaction_count -0.04400000000000004\n" ] } ], "source": [ "for c in deleted:\n", " valid_columns = columns.copy()\n", " valid_columns.remove(c)\n", " train_dicts = df_train[valid_columns].to_dict(orient='records')\n", " X_train = dv.fit_transform(train_dicts)\n", " val_dicts = df_val[valid_columns].to_dict(orient='records')\n", " X_val = dv.fit_transform(val_dicts)\n", " model.fit(X_train,y_train)\n", " y_pred = model.predict_proba(X_val)[:,1]\n", " convert_decision = (y_pred >=0.5)\n", " c_accuracy = (y_val == convert_decision).mean()\n", " print(c,c_accuracy-accuracy)" ] }, { "cell_type": "markdown", "id": "939aec7a-0fe8-4e44-b0ce-a8f58c663717", "metadata": {}, "source": [ "## Q6" ] }, { "cell_type": "code", "execution_count": 84, "id": "eacf08a2-4b59-4a70-b712-13633604d1a8", "metadata": {}, "outputs": [], "source": [ "c_list = [0.000001, 0.00001, 0.0001, 0.001]" ] }, { "cell_type": "code", "execution_count": 85, "id": "b3db7a42-b400-4c36-a300-203d4382117a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1e-06 0.598\n", "1e-05 0.598\n", "0.0001 0.613\n", "0.001 0.645\n" ] } ], "source": [ "for c in c_list:\n", " model = LogisticRegression(solver='liblinear', C=c, max_iter=1000, random_state=42)\n", " train_dicts = df_train.to_dict(orient='records')\n", " X_train = dv.fit_transform(train_dicts)\n", " val_dicts = df_val.to_dict(orient='records')\n", " X_val = dv.fit_transform(val_dicts)\n", " model.fit(X_train,y_train)\n", " y_pred = model.predict_proba(X_val)[:,1]\n", " convert_decision = (y_pred >=0.5)\n", " c_accuracy = (y_val == convert_decision).mean()\n", " print(c,c_accuracy)" ] }, { "cell_type": "code", "execution_count": null, "id": "9f216b0e-f326-4dfa-a3f1-d6d542aff5c1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.2" } }, "nbformat": 4, "nbformat_minor": 5 }