🔎 Find
Search this page
Previous
Next
IN‑V‑BAT‑AI — $1/day
K‑12 Math Exam AI Tutor


Search this page
Tap then use microphone

Python Lesson 14 Collected Knowledge
by Apolinario "Sam" Ortega, founder IN-V-BAT-AI

How to use Python machine learning algorithms.

⭐ Learn how to import Python library

 
# comment : Show me how machine learning algorithm do the classification 
# comment : Machine learning application: image recognition, spam detection

# comment : credit to the following open source creator  (sci-kitlearn.org)
# Code source: Gaël Varoquaux
#              Andreas Müller
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import warnings
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

# FIXED: modern ROC curve import
from sklearn.metrics import RocCurveDisplay

# FIXED: modern partial dependence import
from sklearn.inspection import PartialDependenceDisplay

from sklearn.inspection import permutation_importance

from sklearn.ensemble import HistGradientBoostingRegressor

# comment  Do shift + enter

☑ Output of Python code in Jupyter.

 
PassengerId	Survived	Pclass	Name	Sex	Age	SibSp	Parch	Ticket	Fare	Cabin	Embarked
0	1	0	3	Braund, Mr. Owen Harris	male	22.0	1	0	A/5 21171	7.2500	NaN	S
1	2	1	1	Cumings, Mrs. John Bradley (Florence Briggs Th...	female	38.0	1	0	PC 17599	71.2833	C85	C
2	3	1	3	Heikkinen, Miss. Laina	female	26.0	0	0	STON/O2. 3101282	7.9250	NaN	S
3	4	1	1	Futrelle, Mrs. Jacques Heath (Lily May Peel)	female	35.0	1	0	113803	53.1000	C123	S
4	5	0	3	Allen, Mr. William Henry	male	35.0	0	0	373450	8.0500	NaN	S

📘 compute the mean of the passenger age

 
# comment : compute the mean of the passenger age
titanic["Age"].mean()
# comment # do shift + enter

☑ Output of Python code in Jupyter

29.69911764705882

📘 compute median age and fare of the passenger

 
# comment : compute median age and fare of the passenger
titanic[["Age", "Fare"]].median()
# comment # do shift + enter

☑ Output of Python code in Jupyter.

Age     28.0000
Fare    14.4542
dtype: float64

📘 show me the descriptive statistics information

 
# comment : show me the descriptive statistics information
titanic[["Age", "Fare"]].describe()
# comment # do shift + enter

☑ Output of Python code in Jupyter.

	Age	Fare
count	714.000000	891.000000
mean	29.699118	32.204208
std	14.526497	49.693429
min	0.420000	0.000000
25%	20.125000	7.910400
50%	28.000000	14.454200
75%	38.000000	31.000000
max	80.000000	512.329200

📘 show the aggregate statistic data

 
# comment: show the aggregate statistic data
titanic.agg({'Age': ['min', 'max', 'median', 'skew'],  'Fare': ['min', 'max', 'median', 'mean']})
# comment # do shift + enter

☑ Output of Python code in Jupyter.

	Age	Fare
max	80.000000	512.329200
mean	NaN	32.204208
median	28.000000	14.454200
min	0.420000	0.000000
skew	0.389108	NaN

📘 What is the average age between male vs female

 
# comment : What is the average age between male vs female
titanic[["Sex", "Age"]].groupby("Sex").mean()
# comment # do shift + enter

☑ Output of Python code in Jupyter

	Age
Sex	
female	27.915709
male	30.726645

📘 Compute the average group by sex

 
# comment : Compute the average group by sex
titanic.groupby("Sex").mean()
# comment # do shift + enter

☑ Output of Python code in Jupyter

PassengerId	Survived	Pclass	Age	SibSp	Parch	Fare
Sex							
female	431.028662	0.742038	2.159236	27.915709	0.694268	0.649682	44.479818
male	454.147314	0.188908	2.389948	30.726645	0.429809	0.235702	25.523893

📘 What is the mean ticket fare price for each of the sex and cabin class combinations?

 
# comment : What is the mean ticket fare price for each of the sex and cabin class combinations?
titanic.groupby(["Sex", "Pclass"])["Fare"].mean()
# comment # do shift + enter

☑ Output of Python code in Jupyter

Sex     Pclass
female  1         106.125798
        2          21.970121
        3          16.118810
male    1          67.226127
        2          19.741782
        3          12.661633
Name: Fare, dtype: float64

📘 What is the number of passengers in each of the cabin classes?

 
# comment : What is the number of passengers in each of the cabin classes?
titanic["Pclass"].value_counts()
# comment # Do shift + enter

☑ Output of Python code in Jupyter

3    491
1    216
2    184
Name: Pclass, dtype: int64

📘 sort the passenger age from younger to older

 
# comment : sort the passenger age from younger to older
titanic.sort_values(by="Age").head()
# comment # do shift + enter

☑ Output of Python code in Jupyter

PassengerId	Survived	Pclass	Name	Sex	Age	SibSp	Parch	Ticket	Fare	Cabin	Embarked
803	804	1	3	Thomas, Master. Assad Alexander	male	0.42	0	1	2625	8.5167	NaN	C
755	756	1	2	Hamalainen, Master. Viljo	male	0.67	1	1	250649	14.5000	NaN	S
644	645	1	3	Baclini, Miss. Eugenie	female	0.75	2	1	2666	19.2583	NaN	C
469	470	1	3	Baclini, Miss. Helene Barbara	female	0.75	2	1	2666	19.2583	NaN	C
78	79	1	2	Caldwell, Master. Alden Gates	male	0.83	0	2	248738	29.0000	NaN	S

📘 sort the passenger age from oldest to youngest

 
# comment : sort the passenger age from oldest to youngest
titanic.sort_values(by=['Pclass', 'Age'], ascending=False).head()
# comment # do shift + enter

☑ Output of Python code in Jupyter

PassengerId	Survived	Pclass	Name	Sex	Age	SibSp	Parch	Ticket	Fare	Cabin	Embarked
851	852	0	3	Svensson, Mr. Johan	male	74.0	0	0	347060	7.7750	NaN	S
116	117	0	3	Connors, Mr. Patrick	male	70.5	0	0	370369	7.7500	NaN	Q
280	281	0	3	Duane, Mr. Frank	male	65.0	0	0	336439	7.7500	NaN	Q
483	484	1	3	Turkula, Mrs. (Hedwig)	female	63.0	0	0	4134	9.5875	NaN	S
326	327	0	3	Nysveen, Mr. Johan Hansen	male	61.0	0	0	345364	6.2375	NaN	S

PREVIOUS NEXT


🔗 Privacy 🔗 Disclaimer

Copyright 2026
Never Forget Again with IN-V-BAT-AI
INVenting Brain Assistant Tools using Artificial Intelligence
(IN-V-BAT-AI)

Since
April 27, 2009