{
"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",
" lead_source \n",
" industry \n",
" employment_status \n",
" location \n",
" annual_income \n",
" number_of_courses_viewed \n",
" interaction_count \n",
" lead_score \n",
" converted \n",
" \n",
" \n",
" \n",
" \n",
" 0 \n",
" organic_search \n",
" technology \n",
" employed \n",
" europe \n",
" 88160.0 \n",
" 3 \n",
" 4 \n",
" 0.64 \n",
" 1 \n",
" \n",
" \n",
" 1 \n",
" social_media \n",
" technology \n",
" employed \n",
" south_america \n",
" 72688.0 \n",
" 3 \n",
" 7 \n",
" 0.72 \n",
" 1 \n",
" \n",
" \n",
" 2 \n",
" referral \n",
" retail \n",
" employed \n",
" europe \n",
" 44697.0 \n",
" 3 \n",
" 4 \n",
" 0.58 \n",
" 1 \n",
" \n",
" \n",
" 3 \n",
" paid_ads \n",
" manufacturing \n",
" student \n",
" NaN \n",
" NaN \n",
" 3 \n",
" 6 \n",
" 0.57 \n",
" 0 \n",
" \n",
" \n",
" 4 \n",
" referral \n",
" manufacturing \n",
" student \n",
" north_america \n",
" 24062.0 \n",
" 4 \n",
" 5 \n",
" 0.62 \n",
" 1 \n",
" \n",
" \n",
"
\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",
" annual_income \n",
" number_of_courses_viewed \n",
" interaction_count \n",
" lead_score \n",
" converted \n",
" \n",
" \n",
" \n",
" \n",
" annual_income \n",
" 1.000000 \n",
" 0.161300 \n",
" 0.122842 \n",
" 0.229496 \n",
" 0.189652 \n",
" \n",
" \n",
" number_of_courses_viewed \n",
" 0.161300 \n",
" 1.000000 \n",
" 0.721609 \n",
" 0.757204 \n",
" 0.395038 \n",
" \n",
" \n",
" interaction_count \n",
" 0.122842 \n",
" 0.721609 \n",
" 1.000000 \n",
" 0.915746 \n",
" 0.448624 \n",
" \n",
" \n",
" lead_score \n",
" 0.229496 \n",
" 0.757204 \n",
" 0.915746 \n",
" 1.000000 \n",
" 0.483246 \n",
" \n",
" \n",
" converted \n",
" 0.189652 \n",
" 0.395038 \n",
" 0.448624 \n",
" 0.483246 \n",
" 1.000000 \n",
" \n",
" \n",
"
\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. \n",
"
\n",
"
\n",
" Parameters \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" random_state\n",
" \n",
" random_state: int, RandomState instance, default=None Only used for `solver` == 'sag', 'saga' or 'liblinear' to shuffle the data. It has no effect on the other solvers. See :term:`Glossary <random_state>` for details. \n",
" \n",
" \n",
" 42 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" solver\n",
" \n",
" solver: {'lbfgs', 'liblinear', 'newton-cg', 'newton-cholesky', 'sag', 'saga'}, default='lbfgs' Algorithm to use in the optimization problem. Default is 'lbfgs'. To choose a solver, you might want to consider the following aspects: - 'lbfgs' is a good default solver because it works reasonably well for a wide class of problems. - For :term:`multiclass` problems (`n_classes >= 3`), all solvers except 'liblinear' minimize the full multinomial loss, 'liblinear' will raise an error. - 'newton-cholesky' is a good choice for `n_samples` >> `n_features * n_classes`, especially with one-hot encoded categorical features with rare categories. Be aware that the memory usage of this solver has a quadratic dependency on `n_features * n_classes` because it explicitly computes the full Hessian matrix. - For small datasets, 'liblinear' is a good choice, whereas 'sag' and 'saga' are faster for large ones; - 'liblinear' can only handle binary classification by default. To apply a one-versus-rest scheme for the multiclass setting one can wrap it with the :class:`~sklearn.multiclass.OneVsRestClassifier`. .. warning:: The choice of the algorithm depends on the penalty chosen (`l1_ratio=0` for L2-penalty, `l1_ratio=1` for L1-penalty and `0 < l1_ratio < 1` for Elastic-Net) and on (multinomial) multiclass support: ================= ======================== ====================== solver l1_ratio multinomial multiclass ================= ======================== ====================== 'lbfgs' l1_ratio=0 yes 'liblinear' l1_ratio=1 or l1_ratio=0 no 'newton-cg' l1_ratio=0 yes 'newton-cholesky' l1_ratio=0 yes 'sag' l1_ratio=0 yes 'saga' 0<=l1_ratio<=1 yes ================= ======================== ====================== .. note:: 'sag' and 'saga' fast convergence is only guaranteed on features with approximately the same scale. You can preprocess the data with a scaler from :mod:`sklearn.preprocessing`. .. seealso:: Refer to the :ref:`User Guide <Logistic_regression>` for more information regarding :class:`LogisticRegression` and more specifically the :ref:`Table <logistic_regression_solvers>` summarizing solver/penalty supports. .. versionadded:: 0.17 Stochastic Average Gradient (SAG) descent solver. Multinomial support in version 0.18. .. versionadded:: 0.19 SAGA solver. .. versionchanged:: 0.22 The default solver changed from 'liblinear' to 'lbfgs' in 0.22. .. versionadded:: 1.2 newton-cholesky solver. Multinomial support in version 1.6. \n",
" \n",
" \n",
" 'liblinear' \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" max_iter\n",
" \n",
" max_iter: int, default=100 Maximum number of iterations taken for the solvers to converge. \n",
" \n",
" \n",
" 1000 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" penalty\n",
" \n",
" penalty: {'l1', 'l2', 'elasticnet', None}, default='l2' Specify the norm of the penalty: - `None`: no penalty is added; - `'l2'`: add an L2 penalty term and it is the default choice; - `'l1'`: add an L1 penalty term; - `'elasticnet'`: both L1 and L2 penalty terms are added. .. warning:: Some penalties may not work with some solvers. See the parameter `solver` below, to know the compatibility between the penalty and solver. .. versionadded:: 0.19 l1 penalty with SAGA solver (allowing 'multinomial' + L1) .. deprecated:: 1.8 `penalty` was deprecated in version 1.8 and will be removed in 1.10. Use `l1_ratio` and `C` instead. `l1_ratio=0` for `penalty='l2'`, `l1_ratio=1` for `penalty='l1'`, `l1_ratio` set to any float between 0 and 1 for `penalty='elasticnet'`, and `C=np.inf` for `penalty=None`. \n",
" \n",
" \n",
" 'deprecated' \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" C\n",
" \n",
" C: float, default=1.0 Inverse of regularization strength; must be a positive float. Like in support vector machines, smaller values specify stronger regularization. `C=np.inf` results in unpenalized logistic regression. For a visual example on the effect of tuning the `C` parameter with an L1 penalty, see: :ref:`sphx_glr_auto_examples_linear_model_plot_logistic_path.py`. \n",
" \n",
" \n",
" 1.0 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" l1_ratio\n",
" \n",
" l1_ratio: float, default=0.0 The Elastic-Net mixing parameter, with `0 <= l1_ratio <= 1`. Setting `l1_ratio=1` gives a pure L1-penalty, setting `l1_ratio=0` a pure L2-penalty. Any value between 0 and 1 gives an Elastic-Net penalty of the form `l1_ratio * L1 + (1 - l1_ratio) * L2`. .. warning:: Certain values of `l1_ratio`, i.e. some penalties, may not work with some solvers. See the parameter `solver` below, to know the compatibility between the penalty and solver. .. versionchanged:: 1.8 Default value changed from None to 0.0. .. deprecated:: 1.8 `None` is deprecated and will be removed in version 1.10. Always use `l1_ratio` to specify the penalty type. \n",
" \n",
" \n",
" 0.0 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" dual\n",
" \n",
" dual: bool, default=False Dual (constrained) or primal (regularized, see also :ref:`this equation <regularized-logistic-loss>`) formulation. Dual formulation is only implemented for l2 penalty with liblinear solver. Prefer `dual=False` when n_samples > n_features. \n",
" \n",
" \n",
" False \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" tol\n",
" \n",
" tol: float, default=1e-4 Tolerance for stopping criteria. \n",
" \n",
" \n",
" 0.0001 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" fit_intercept\n",
" \n",
" fit_intercept: bool, default=True Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function. \n",
" \n",
" \n",
" True \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" intercept_scaling\n",
" \n",
" intercept_scaling: float, default=1 Useful only when the solver `liblinear` is used and `self.fit_intercept` is set to `True`. In this case, `x` becomes `[x, self.intercept_scaling]`, i.e. a "synthetic" feature with constant value equal to `intercept_scaling` is appended to the instance vector. The intercept becomes ``intercept_scaling * synthetic_feature_weight``. .. note:: The synthetic feature weight is subject to L1 or L2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) `intercept_scaling` has to be increased. \n",
" \n",
" \n",
" 1 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" class_weight\n",
" \n",
" class_weight: dict or 'balanced', default=None Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))``. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. .. versionadded:: 0.17 *class_weight='balanced'* \n",
" \n",
" \n",
" None \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" verbose\n",
" \n",
" verbose: int, default=0 For the liblinear and lbfgs solvers set verbose to any positive number for verbosity. \n",
" \n",
" \n",
" 0 \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" warm_start\n",
" \n",
" warm_start: bool, default=False When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. Useless for liblinear solver. See :term:`the Glossary <warm_start>`. .. versionadded:: 0.17 *warm_start* to support *lbfgs*, *newton-cg*, *sag*, *saga* solvers. \n",
" \n",
" \n",
" False \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" n_jobs\n",
" \n",
" n_jobs: int, default=None Does not have any effect. .. deprecated:: 1.8 `n_jobs` is deprecated in version 1.8 and will be removed in 1.10. \n",
" \n",
" \n",
" None \n",
" \n",
" \n",
" \n",
"
\n",
" \n",
"
\n",
" \n",
"
\n",
"
\n",
" Fitted attributes \n",
" \n",
" \n",
" \n",
" Name \n",
" Type \n",
" Value \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" classes_\n",
" \n",
" classes_: ndarray of shape (n_classes, ) A list of class labels known to the classifier. \n",
" \n",
" \n",
" ndarray[int64](2,) \n",
" [0,1] \n",
"\n",
"\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" coef_\n",
" \n",
" coef_: ndarray or CSR matrix of shape (1, n_features) or (n_classes, n_features) Coefficients of the features in the decision function. `coef_` is of shape (1, n_features) when the given problem is binary. By default, it will be created as a dense array, but can be turned to sparse (CSR format) through :meth:`sparsify` (which can be beneficial under L1 regularization when many coefficients are zero), and back to dense through :meth:`densify`. \n",
" \n",
" \n",
" ndarray[float64](1, 28) \n",
" [[-0. ,-0. , 0.01,...,-0. ,-0. , 0.05]] \n",
"\n",
"\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" intercept_\n",
" \n",
" intercept_: ndarray of shape (1,) or (n_classes,) Intercept (a.k.a. bias) added to the decision function. If `fit_intercept` is set to False, the intercept is set to zero. `intercept_` is of shape (1,) when the given problem is binary. \n",
" \n",
" \n",
" ndarray[float64](1,) \n",
" [-0.01] \n",
"\n",
"\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" n_features_in_\n",
" \n",
" n_features_in_: int Number of features seen during :term:`fit`. .. versionadded:: 0.24 \n",
" \n",
" \n",
" int \n",
" 28 \n",
"\n",
"\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" n_iter_\n",
" \n",
" n_iter_: ndarray of shape (1, ) Actual number of iterations for all classes. .. versionchanged:: 0.20 In SciPy <= 1.0.0 the number of lbfgs iterations may exceed ``max_iter``. ``n_iter_`` will now report at most ``max_iter``. \n",
" \n",
" \n",
" ndarray[int32](1,) \n",
" [12] \n",
"\n",
"\n",
" \n",
" \n",
" \n",
"
\n",
" \n",
"
\n",
"
"
],
"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
}