Titanic
Mon 30 June 2025
pip install scikit-learn
Collecting scikit-learn
Downloading scikit_learn-1.7.0-cp312-cp312-win_amd64.whl.metadata (14 kB)
Requirement already satisfied: numpy>=1.22.0 in c:\users\hp\miniconda3\envs\py312\lib\site-packages (from scikit-learn) (2.3.1)
Collecting scipy>=1.8.0 (from scikit-learn)
Downloading scipy-1.16.0-cp312-cp312-win_amd64.whl.metadata (60 kB)
Collecting joblib>=1.2.0 (from scikit-learn)
Downloading joblib-1.5.1-py3-none-any.whl.metadata (5.6 kB)
Collecting threadpoolctl>=3.1.0 (from scikit-learn)
Downloading threadpoolctl-3.6.0-py3-none-any.whl.metadata (13 kB)
Downloading scikit_learn-1.7.0-cp312-cp312-win_amd64.whl (10.7 MB)
---------------------------------------- 0.0/10.7 MB ? eta -:--:--
- -------------------------------------- 0.5/10.7 MB 16.4 MB/s eta 0:00:01
--- ------------------------------------ 1.0/10.7 MB 2.8 MB/s eta 0:00:04
---- ----------------------------------- 1.3/10.7 MB 1.9 MB/s eta 0:00:06
---- ----------------------------------- 1.3/10.7 MB 1.9 MB/s eta 0:00:06
----- ---------------------------------- 1.6/10.7 MB 1.6 MB/s eta 0:00:06
------- -------------------------------- 2.1/10.7 MB 1.6 MB/s eta 0:00:06
-------- ------------------------------- 2.4/10.7 MB 1.5 MB/s eta 0:00:06
--------- ------------------------------ 2.6/10.7 MB 1.5 MB/s eta 0:00:06
--------- ------------------------------ 2.6/10.7 MB 1.5 MB/s eta 0:00:06
----------- ---------------------------- 3.1/10.7 MB 1.5 MB/s eta 0:00:05
------------ --------------------------- 3.4/10.7 MB 1.5 MB/s eta 0:00:05
------------- -------------------------- 3.7/10.7 MB 1.5 MB/s eta 0:00:05
-------------- ------------------------- 3.9/10.7 MB 1.4 MB/s eta 0:00:05
--------------- ------------------------ 4.2/10.7 MB 1.4 MB/s eta 0:00:05
---------------- ----------------------- 4.5/10.7 MB 1.4 MB/s eta 0:00:05
----------------- ---------------------- 4.7/10.7 MB 1.4 MB/s eta 0:00:05
------------------ --------------------- 5.0/10.7 MB 1.4 MB/s eta 0:00:05
------------------- -------------------- 5.2/10.7 MB 1.4 MB/s eta 0:00:04
-------------------- ------------------- 5.5/10.7 MB 1.3 MB/s eta 0:00:04
--------------------- ------------------ 5.8/10.7 MB 1.4 MB/s eta 0:00:04
---------------------- ----------------- 6.0/10.7 MB 1.4 MB/s eta 0:00:04
----------------------- ---------------- 6.3/10.7 MB 1.3 MB/s eta 0:00:04
------------------------ --------------- 6.6/10.7 MB 1.3 MB/s eta 0:00:04
------------------------- -------------- 6.8/10.7 MB 1.3 MB/s eta 0:00:03
-------------------------- ------------- 7.1/10.7 MB 1.3 MB/s eta 0:00:03
--------------------------- ------------ 7.3/10.7 MB 1.3 MB/s eta 0:00:03
---------------------------- ----------- 7.6/10.7 MB 1.3 MB/s eta 0:00:03
---------------------------- ----------- 7.6/10.7 MB 1.3 MB/s eta 0:00:03
------------------------------ --------- 8.1/10.7 MB 1.3 MB/s eta 0:00:02
------------------------------ --------- 8.1/10.7 MB 1.3 MB/s eta 0:00:02
------------------------------- -------- 8.4/10.7 MB 1.3 MB/s eta 0:00:02
--------------------------------- ------ 8.9/10.7 MB 1.3 MB/s eta 0:00:02
---------------------------------- ----- 9.2/10.7 MB 1.3 MB/s eta 0:00:02
----------------------------------- ---- 9.4/10.7 MB 1.3 MB/s eta 0:00:01
----------------------------------- ---- 9.4/10.7 MB 1.3 MB/s eta 0:00:01
------------------------------------ --- 9.7/10.7 MB 1.3 MB/s eta 0:00:01
------------------------------------- -- 10.0/10.7 MB 1.3 MB/s eta 0:00:01
--------------------------------------- 10.5/10.7 MB 1.3 MB/s eta 0:00:01
---------------------------------------- 10.7/10.7 MB 1.3 MB/s eta 0:00:00
Downloading joblib-1.5.1-py3-none-any.whl (307 kB)
Downloading scipy-1.16.0-cp312-cp312-win_amd64.whl (38.4 MB)
---------------------------------------- 0.0/38.4 MB ? eta -:--:--
---------------------------------------- 0.3/38.4 MB ? eta -:--:--
---------------------------------------- 0.3/38.4 MB ? eta -:--:--
--------------------------------------- 0.5/38.4 MB 1.3 MB/s eta 0:00:30
--------------------------------------- 0.8/38.4 MB 1.2 MB/s eta 0:00:31
- -------------------------------------- 1.0/38.4 MB 1.2 MB/s eta 0:00:31
- -------------------------------------- 1.3/38.4 MB 1.2 MB/s eta 0:00:31
- -------------------------------------- 1.6/38.4 MB 1.2 MB/s eta 0:00:30
- -------------------------------------- 1.8/38.4 MB 1.2 MB/s eta 0:00:30
-- ------------------------------------- 2.1/38.4 MB 1.2 MB/s eta 0:00:30
-- ------------------------------------- 2.4/38.4 MB 1.2 MB/s eta 0:00:30
-- ------------------------------------- 2.6/38.4 MB 1.2 MB/s eta 0:00:29
--- ------------------------------------ 2.9/38.4 MB 1.2 MB/s eta 0:00:29
--- ------------------------------------ 3.1/38.4 MB 1.2 MB/s eta 0:00:29
--- ------------------------------------ 3.4/38.4 MB 1.2 MB/s eta 0:00:29
--- ------------------------------------ 3.7/38.4 MB 1.2 MB/s eta 0:00:29
---- ----------------------------------- 3.9/38.4 MB 1.2 MB/s eta 0:00:30
---- ----------------------------------- 4.2/38.4 MB 1.2 MB/s eta 0:00:28
---- ----------------------------------- 4.5/38.4 MB 1.2 MB/s eta 0:00:28
---- ----------------------------------- 4.7/38.4 MB 1.2 MB/s eta 0:00:28
----- ---------------------------------- 5.0/38.4 MB 1.2 MB/s eta 0:00:27
----- ---------------------------------- 5.2/38.4 MB 1.2 MB/s eta 0:00:28
----- ---------------------------------- 5.5/38.4 MB 1.2 MB/s eta 0:00:27
------ --------------------------------- 5.8/38.4 MB 1.2 MB/s eta 0:00:28
------ --------------------------------- 6.0/38.4 MB 1.2 MB/s eta 0:00:27
------ --------------------------------- 6.3/38.4 MB 1.2 MB/s eta 0:00:27
------ --------------------------------- 6.6/38.4 MB 1.2 MB/s eta 0:00:26
------- -------------------------------- 6.8/38.4 MB 1.2 MB/s eta 0:00:26
------- -------------------------------- 7.1/38.4 MB 1.2 MB/s eta 0:00:26
------- -------------------------------- 7.3/38.4 MB 1.2 MB/s eta 0:00:26
------- -------------------------------- 7.6/38.4 MB 1.2 MB/s eta 0:00:25
-------- ------------------------------- 7.9/38.4 MB 1.2 MB/s eta 0:00:25
-------- ------------------------------- 8.1/38.4 MB 1.2 MB/s eta 0:00:25
-------- ------------------------------- 8.4/38.4 MB 1.2 MB/s eta 0:00:25
-------- ------------------------------- 8.4/38.4 MB 1.2 MB/s eta 0:00:25
--------- ------------------------------ 8.9/38.4 MB 1.2 MB/s eta 0:00:24
--------- ------------------------------ 9.2/38.4 MB 1.2 MB/s eta 0:00:24
--------- ------------------------------ 9.2/38.4 MB 1.2 MB/s eta 0:00:24
---------- ----------------------------- 9.7/38.4 MB 1.2 MB/s eta 0:00:24
---------- ----------------------------- 10.0/38.4 MB 1.2 MB/s eta 0:00:23
---------- ----------------------------- 10.2/38.4 MB 1.2 MB/s eta 0:00:23
---------- ----------------------------- 10.5/38.4 MB 1.2 MB/s eta 0:00:23
----------- ---------------------------- 10.7/38.4 MB 1.2 MB/s eta 0:00:23
----------- ---------------------------- 11.0/38.4 MB 1.2 MB/s eta 0:00:23
----------- ---------------------------- 11.3/38.4 MB 1.2 MB/s eta 0:00:22
----------- ---------------------------- 11.3/38.4 MB 1.2 MB/s eta 0:00:22
------------ --------------------------- 11.8/38.4 MB 1.2 MB/s eta 0:00:22
------------ --------------------------- 12.1/38.4 MB 1.2 MB/s eta 0:00:22
------------ --------------------------- 12.3/38.4 MB 1.2 MB/s eta 0:00:22
------------- -------------------------- 12.6/38.4 MB 1.2 MB/s eta 0:00:21
------------- -------------------------- 12.8/38.4 MB 1.2 MB/s eta 0:00:21
------------- -------------------------- 12.8/38.4 MB 1.2 MB/s eta 0:00:21
------------- -------------------------- 13.4/38.4 MB 1.2 MB/s eta 0:00:21
------------- -------------------------- 13.4/38.4 MB 1.2 MB/s eta 0:00:21
-------------- ------------------------- 13.6/38.4 MB 1.2 MB/s eta 0:00:21
-------------- ------------------------- 13.9/38.4 MB 1.2 MB/s eta 0:00:20
-------------- ------------------------- 14.2/38.4 MB 1.2 MB/s eta 0:00:20
--------------- ------------------------ 14.4/38.4 MB 1.2 MB/s eta 0:00:20
--------------- ------------------------ 14.7/38.4 MB 1.2 MB/s eta 0:00:20
--------------- ------------------------ 14.9/38.4 MB 1.2 MB/s eta 0:00:20
--------------- ------------------------ 15.2/38.4 MB 1.2 MB/s eta 0:00:19
---------------- ----------------------- 15.5/38.4 MB 1.2 MB/s eta 0:00:19
---------------- ----------------------- 15.5/38.4 MB 1.2 MB/s eta 0:00:19
---------------- ----------------------- 16.0/38.4 MB 1.2 MB/s eta 0:00:19
---------------- ----------------------- 16.3/38.4 MB 1.2 MB/s eta 0:00:19
----------------- ---------------------- 16.5/38.4 MB 1.2 MB/s eta 0:00:18
----------------- ---------------------- 16.8/38.4 MB 1.2 MB/s eta 0:00:18
----------------- ---------------------- 17.0/38.4 MB 1.2 MB/s eta 0:00:18
------------------ --------------------- 17.3/38.4 MB 1.2 MB/s eta 0:00:18
------------------ --------------------- 17.6/38.4 MB 1.2 MB/s eta 0:00:17
------------------ --------------------- 17.6/38.4 MB 1.2 MB/s eta 0:00:17
------------------ --------------------- 17.8/38.4 MB 1.2 MB/s eta 0:00:17
------------------ --------------------- 18.1/38.4 MB 1.2 MB/s eta 0:00:17
------------------- -------------------- 18.4/38.4 MB 1.2 MB/s eta 0:00:17
------------------- -------------------- 18.6/38.4 MB 1.2 MB/s eta 0:00:17
------------------- -------------------- 18.9/38.4 MB 1.2 MB/s eta 0:00:16
------------------- -------------------- 19.1/38.4 MB 1.2 MB/s eta 0:00:16
-------------------- ------------------- 19.4/38.4 MB 1.2 MB/s eta 0:00:16
-------------------- ------------------- 19.7/38.4 MB 1.2 MB/s eta 0:00:16
-------------------- ------------------- 19.9/38.4 MB 1.2 MB/s eta 0:00:16
--------------------- ------------------ 20.2/38.4 MB 1.2 MB/s eta 0:00:15
--------------------- ------------------ 20.4/38.4 MB 1.2 MB/s eta 0:00:15
--------------------- ------------------ 20.7/38.4 MB 1.2 MB/s eta 0:00:15
--------------------- ------------------ 20.7/38.4 MB 1.2 MB/s eta 0:00:15
---------------------- ----------------- 21.2/38.4 MB 1.2 MB/s eta 0:00:15
---------------------- ----------------- 21.5/38.4 MB 1.2 MB/s eta 0:00:14
---------------------- ----------------- 21.5/38.4 MB 1.2 MB/s eta 0:00:14
---------------------- ----------------- 22.0/38.4 MB 1.2 MB/s eta 0:00:14
----------------------- ---------------- 22.3/38.4 MB 1.2 MB/s eta 0:00:14
----------------------- ---------------- 22.5/38.4 MB 1.2 MB/s eta 0:00:13
----------------------- ---------------- 22.5/38.4 MB 1.2 MB/s eta 0:00:13
----------------------- ---------------- 22.8/38.4 MB 1.2 MB/s eta 0:00:13
------------------------ --------------- 23.3/38.4 MB 1.2 MB/s eta 0:00:13
------------------------ --------------- 23.6/38.4 MB 1.2 MB/s eta 0:00:13
------------------------ --------------- 23.9/38.4 MB 1.2 MB/s eta 0:00:12
------------------------ --------------- 23.9/38.4 MB 1.2 MB/s eta 0:00:12
------------------------- -------------- 24.4/38.4 MB 1.2 MB/s eta 0:00:12
------------------------- -------------- 24.6/38.4 MB 1.2 MB/s eta 0:00:12
------------------------- -------------- 24.9/38.4 MB 1.2 MB/s eta 0:00:12
------------------------- -------------- 24.9/38.4 MB 1.2 MB/s eta 0:00:12
-------------------------- ------------- 25.2/38.4 MB 1.2 MB/s eta 0:00:11
-------------------------- ------------- 25.4/38.4 MB 1.2 MB/s eta 0:00:11
-------------------------- ------------- 25.7/38.4 MB 1.2 MB/s eta 0:00:11
-------------------------- ------------- 25.7/38.4 MB 1.2 MB/s eta 0:00:11
--------------------------- ------------ 26.2/38.4 MB 1.2 MB/s eta 0:00:10
--------------------------- ------------ 26.5/38.4 MB 1.2 MB/s eta 0:00:10
--------------------------- ------------ 26.7/38.4 MB 1.2 MB/s eta 0:00:10
---------------------------- ----------- 27.0/38.4 MB 1.2 MB/s eta 0:00:10
---------------------------- ----------- 27.3/38.4 MB 1.2 MB/s eta 0:00:10
---------------------------- ----------- 27.5/38.4 MB 1.2 MB/s eta 0:00:09
---------------------------- ----------- 27.8/38.4 MB 1.2 MB/s eta 0:00:09
---------------------------- ----------- 27.8/38.4 MB 1.2 MB/s eta 0:00:09
----------------------------- ---------- 28.3/38.4 MB 1.2 MB/s eta 0:00:09
----------------------------- ---------- 28.6/38.4 MB 1.2 MB/s eta 0:00:09
----------------------------- ---------- 28.6/38.4 MB 1.2 MB/s eta 0:00:09
------------------------------ --------- 28.8/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------ --------- 29.1/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------ --------- 29.1/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------ --------- 29.4/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------ --------- 29.6/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------ --------- 29.6/38.4 MB 1.2 MB/s eta 0:00:08
------------------------------- -------- 30.1/38.4 MB 1.2 MB/s eta 0:00:07
------------------------------- -------- 30.4/38.4 MB 1.2 MB/s eta 0:00:07
------------------------------- -------- 30.7/38.4 MB 1.2 MB/s eta 0:00:07
-------------------------------- ------- 30.9/38.4 MB 1.2 MB/s eta 0:00:07
-------------------------------- ------- 31.2/38.4 MB 1.2 MB/s eta 0:00:06
-------------------------------- ------- 31.2/38.4 MB 1.2 MB/s eta 0:00:06
-------------------------------- ------- 31.5/38.4 MB 1.2 MB/s eta 0:00:06
--------------------------------- ------ 31.7/38.4 MB 1.2 MB/s eta 0:00:06
--------------------------------- ------ 32.0/38.4 MB 1.2 MB/s eta 0:00:06
--------------------------------- ------ 32.2/38.4 MB 1.2 MB/s eta 0:00:06
--------------------------------- ------ 32.5/38.4 MB 1.2 MB/s eta 0:00:05
---------------------------------- ----- 32.8/38.4 MB 1.2 MB/s eta 0:00:05
---------------------------------- ----- 33.0/38.4 MB 1.2 MB/s eta 0:00:05
---------------------------------- ----- 33.3/38.4 MB 1.2 MB/s eta 0:00:05
---------------------------------- ----- 33.6/38.4 MB 1.2 MB/s eta 0:00:05
----------------------------------- ---- 33.8/38.4 MB 1.2 MB/s eta 0:00:04
----------------------------------- ---- 34.1/38.4 MB 1.2 MB/s eta 0:00:04
----------------------------------- ---- 34.3/38.4 MB 1.2 MB/s eta 0:00:04
------------------------------------ --- 34.6/38.4 MB 1.2 MB/s eta 0:00:04
------------------------------------ --- 34.9/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------ --- 34.9/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------ --- 35.1/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------ --- 35.1/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------- -- 35.7/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------- -- 35.9/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------- -- 35.9/38.4 MB 1.2 MB/s eta 0:00:03
------------------------------------- -- 36.4/38.4 MB 1.2 MB/s eta 0:00:02
------------------------------------- -- 36.4/38.4 MB 1.2 MB/s eta 0:00:02
-------------------------------------- - 36.7/38.4 MB 1.2 MB/s eta 0:00:02
-------------------------------------- - 37.2/38.4 MB 1.2 MB/s eta 0:00:02
-------------------------------------- - 37.2/38.4 MB 1.2 MB/s eta 0:00:02
--------------------------------------- 37.7/38.4 MB 1.2 MB/s eta 0:00:01
--------------------------------------- 38.0/38.4 MB 1.2 MB/s eta 0:00:01
--------------------------------------- 38.3/38.4 MB 1.2 MB/s eta 0:00:01
---------------------------------------- 38.4/38.4 MB 1.2 MB/s eta 0:00:00
Downloading threadpoolctl-3.6.0-py3-none-any.whl (18 kB)
Installing collected packages: threadpoolctl, scipy, joblib, scikit-learn
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
---------- ----------------------------- 1/4 [scipy]
-------------------- ------------------- 2/4 [joblib]
-------------------- ------------------- 2/4 [joblib]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
------------------------------ --------- 3/4 [scikit-learn]
---------------------------------------- 4/4 [scikit-learn]
Successfully installed joblib-1.5.1 scikit-learn-1.7.0 scipy-1.16.0 threadpoolctl-3.6.0
Note: you may need to restart the kernel to use updated packages.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
titanic = sns.load_dataset("titanic")
titanic.head()
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
titanic.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 survived 891 non-null int64
1 pclass 891 non-null int64
2 sex 891 non-null object
3 age 714 non-null float64
4 sibsp 891 non-null int64
5 parch 891 non-null int64
6 fare 891 non-null float64
7 embarked 889 non-null object
8 class 891 non-null category
9 who 891 non-null object
10 adult_male 891 non-null bool
11 deck 203 non-null category
12 embark_town 889 non-null object
13 alive 891 non-null object
14 alone 891 non-null bool
dtypes: bool(2), category(2), float64(2), int64(4), object(5)
memory usage: 80.7+ KB
titanic.isnull().sum()
survived 0
pclass 0
sex 0
age 177
sibsp 0
parch 0
fare 0
embarked 2
class 0
who 0
adult_male 0
deck 688
embark_town 2
alive 0
alone 0
dtype: int64
# Fill missing age values with the median
titanic["age"] = titanic["age"].fillna(titanic["age"].median())
# Check if there are still any missing values
titanic["age"].isnull().sum()
np.int64(0)
# Fill missing embarked values with mode
titanic["embarked"] = titanic["embarked"].fillna(titanic["embarked"].mode()[0])
# Convert categorical columns into numerical using one-hot encoding
titanic_encoded = pd.get_dummies(titanic, columns=["sex", "embarked", "pclass"], drop_first=True)
titanic_encoded.head()
| survived | age | sibsp | parch | fare | class | who | adult_male | deck | embark_town | alive | alone | sex_male | embarked_Q | embarked_S | pclass_2 | pclass_3 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 22.0 | 1 | 0 | 7.2500 | Third | man | True | NaN | Southampton | no | False | True | False | True | False | True |
| 1 | 1 | 38.0 | 1 | 0 | 71.2833 | First | woman | False | C | Cherbourg | yes | False | False | False | False | False | False |
| 2 | 1 | 26.0 | 0 | 0 | 7.9250 | Third | woman | False | NaN | Southampton | yes | True | False | False | True | False | True |
| 3 | 1 | 35.0 | 1 | 0 | 53.1000 | First | woman | False | C | Southampton | yes | False | False | False | True | False | False |
| 4 | 0 | 35.0 | 0 | 0 | 8.0500 | Third | man | True | NaN | Southampton | no | True | True | False | True | False | True |
# Optional: Drop columns that are not useful for prediction
titanic = titanic.drop(columns=["who", "adult_male", "deck", "embark_town", "alive", "class", "alone"], errors='ignore')
# Fill missing values
titanic["age"] = titanic["age"].fillna(titanic["age"].median())
titanic["fare"] = titanic["fare"].fillna(titanic["fare"].median())
titanic["embarked"] = titanic["embarked"].fillna(titanic["embarked"].mode()[0])
# Convert categorical columns to numeric
titanic_encoded = pd.get_dummies(titanic, columns=["sex", "embarked", "pclass"], drop_first=True)
# Define features and label
X = titanic_encoded.drop("survived", axis=1)
y = titanic_encoded["survived"]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
LogisticRegression(max_iter=200)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.
Parameters
| penalty | 'l2' | |
| dual | False | |
| tol | 0.0001 | |
| C | 1.0 | |
| fit_intercept | True | |
| intercept_scaling | 1 | |
| class_weight | None | |
| random_state | None | |
| solver | 'lbfgs' | |
| max_iter | 200 | |
| multi_class | 'deprecated' | |
| verbose | 0 | |
| warm_start | False | |
| n_jobs | None | |
| l1_ratio | None |
# Predict on the test set
y_pred = model.predict(X_test)
from sklearn.metrics import accuracy_score
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
Accuracy: 0.80
from sklearn.metrics import classification_report
# Detailed performance report
print(classification_report(y_test, y_pred))
precision recall f1-score support
0 0.81 0.86 0.83 105
1 0.78 0.72 0.75 74
accuracy 0.80 179
macro avg 0.80 0.79 0.79 179
weighted avg 0.80 0.80 0.80 179
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
# Plot confusion matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()

# Show feature importance
coefficients = pd.Series(model.coef_[0], index=X.columns)
coefficients.sort_values().plot(kind='barh', figsize=(10,6), title="Feature Impact on Survival")
plt.tight_layout()
plt.show()

Score: 15
Category: basics