0% found this document useful (0 votes)
3 views52 pages

Apostila2 Python

The document is a comprehensive guide on using Python for Artificial Intelligence and Data Science, authored by Prof. Dr. Thommas K. S. Flores at UFRN. It covers Python fundamentals, control structures, functions, object-oriented programming, and data handling tools such as NumPy and Pandas. Additionally, it includes sections on data cleaning, visualization, and a mini-project for practical application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views52 pages

Apostila2 Python

The document is a comprehensive guide on using Python for Artificial Intelligence and Data Science, authored by Prof. Dr. Thommas K. S. Flores at UFRN. It covers Python fundamentals, control structures, functions, object-oriented programming, and data handling tools such as NumPy and Pandas. Additionally, it includes sections on data cleaning, visualization, and a mini-project for practical application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Universidade Federal do Rio Grande do Norte (UFRN)‌

Centro de Ensino Superior do Seridó (CERES)‌‌


Departamento de Computação e Tecnologia (DCT)

P Y T H O N F O R A R T I F I C I A L I N T E L L I G E N C E A N D D ATA
SCIENCE

P ro f. D r. T h o m m a s K . S . F l o re s
[Link]@[Link]
Contents

I Python Language Fundamentals 1

1 Introduction 3
1.1 What Is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Why Python for AI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 What Is a Jupyter Notebook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Running Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Installing Libraries (pip) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Python Language Fundamentals 5


2.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Math Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Control Structures 11
3.1 if / elif / else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4 Functions in Python 15
4.1 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3 Advanced Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 Object-Oriented Programming (Classes) 19


5.1 Core Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.2 Car Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.3 Dataset Class (ML Application) . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

II Data Handling and Machine Learning Tools 21

6 Introduction to Data Handling 23


6.1 Essential Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

7 NumPy — Numerical Computing 25

Page i
Python for Artificial Intelligence CONTENTS

7.1 Why NumPy? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25


7.2 Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.3 Indexing and Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.4 Vectorized Math Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

8 Pandas — Data Manipulation 29


8.1 Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.2 DataFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.3 Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.4 Selecting and Filtering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.5 Grouping and Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

9 Data Cleaning 33
9.1 Detecting and Handling Missing Values . . . . . . . . . . . . . . . . . . . . . . . 33
9.2 Removing Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
9.3 Type Conversion and Normalization . . . . . . . . . . . . . . . . . . . . . . . . 34

10 Data Visualization 37
10.1 Initial Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.2 Line Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.3 Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
10.4 Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
10.5 Correlation Heatmap (Seaborn) . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

11 Reading Different Data Types 41


11.1 CSV, TXT, and Excel Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
11.2 Images with PIL and OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
11.3 Audio with Librosa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

12 Mini-Project — Student Dataset Analysis 43


12.1 Step 1 — Data Creation and Loading . . . . . . . . . . . . . . . . . . . . . . . 43
12.2 Step 2 — Initial Exploration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12.3 Step 3 — Data Cleaning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12.4 Step 4 — Analysis and Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12.5 Step 5 — Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

13 Best Practices in Python for Data Science 47


13.1 Project Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
13.2 Comments and Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
13.3 Reproducibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13.4 Best Practices Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13.5 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Page ii of 49
Part I

Python Language Fundamentals

Page 1
Chapter 1

Introduction

1.1 What Is Python?

Python is a high-level programming language created by Guido van Rossum in 1991. Its design
philosophy prioritizes code readability and simplicity, making it ideal for both beginners and
experienced developers. Python is interpreted (no compilation step required), dynamically typed
(types are resolved at runtime), and multi-paradigm (supports procedural, object-oriented, and
functional programming styles).

◦ Key Features of Python

• Clear and readable syntax — code often looks like English pseudocode
• Huge library ecosystem (over 400,000 packages on PyPI)
• Active community and extensive documentation
• Cross-platform: runs on Windows, Linux, and macOS
• Free and open source

1.2 Why Python for AI?

Python became the dominant language for Artificial Intelligence and Machine Learning due to
a combination of practical factors:
• Specialized libraries: NumPy, Pandas, Scikit-learn, TensorFlow, PyTorch

• Simple syntax that lets you focus on algorithms rather than language details

• Large scientific community that contributes code and tutorials

• Easy integration with C/C++ for performance-critical operations

• Native support for vector and matrix computation

◦ Python in the Job Market

According to the TIOBE Index (2024), Python is the #1 most popular programming
language in the world. More than 90% of Machine Learning projects use Python as their
primary language. Major companies such as Google, Meta, NASA, and Netflix use Python
in production systems.

1.3 What Is a Jupyter Notebook?

A Jupyter Notebook is an interactive development environment that lets you combine Python
code, formatted text (Markdown), mathematical equations, and visualizations in a single doc-

Page 3
Python for Artificial Intelligence CHAPTER 1. INTRODUCTION

ument. It is widely used in Data Science and AI because it supports simultaneous experimen-
tation and documentation.

◦ Cell Types in Jupyter

• Code cell: contains executable Python code


• Markdown cell: contains formatted text, headings, and lists
• Raw cell: plain text with no formatting (rarely used)

1.4 Running Cells

To execute a cell in Jupyter Notebook:


• Shift + Enter — run the cell and move to the next one

• Ctrl + Enter — run the cell and stay on it

• Alt + Enter — run the cell and insert a new one below

1.5 Installing Libraries (pip)

pip is Python’s standard package manager. Use the commands below in a terminal, or directly
inside a Jupyter cell (prefix with !):
1 # Install a single library
2 pip install library_name
3
4 # Inside a Jupyter Notebook ( use ! before the command )
5 ! pip install numpy
6 ! pip install pandas matplotlib seaborn
7
8 # Check installed version
9 ! pip show numpy
10
11 # Install multiple libraries at once
12 ! pip install numpy pandas matplotlib seaborn scikit - learn

• Tip

Always use virtual environments for different projects!


Create one with: python -m venv my_env
Activate on Linux/macOS: source my_env/bin/activate
Activate on Windows: my_env\Scripts\activate

≡ Chapter Summary

• Python is interpreted, dynamically typed, and multi-paradigm.


• It dominates AI/ML due to its ecosystem and readable syntax.
• Jupyter Notebooks combine code, text, and visualizations in one document.
• Use pip to install libraries; always prefer virtual environments.

Page 4 of 49
Chapter 2

Python Language Fundamentals

In this chapter we cover the building blocks of Python. Each concept is presented with an
explanation, practical examples, and exercises for practice.

2.1 Variables

A variable is a name that points to a value stored in memory. In Python, you do not need to
declare the type — Python infers it automatically.
1 # Creating variables in Python
2 name = ’ Maria ’ # string ( text )
3 age = 25 # integer
4 height = 1.68 # floating - point
5 student = True # boolean
6
7 # Printing values
8 print ( name ) # Output : Maria
9 print ( age ) # Output : 25
10

11 # Multiple assignment
12 x, y, z = 1, 2, 3
13 print (x , y , z ) # Output : 1 2 3
14
15 # Variable swap ( elegant Python style !)
16 x, y = y, x
17 print (x , y ) # Output : 2 1

• Variable naming rules

• Must start with a letter or underscore (_)


• No spaces (use_underscores_like_this)
• Cannot use reserved words (if, for, while, etc.)
• Python is case-sensitive: name and Name are different variables

✓ Exercise
1. Create variables to store: your name, your age, your height, and whether you wear
glasses (True/False).
2. Print all variables using print().
3. Swap the values of two numeric variables using multiple assignment.

2.2 Data Types

Python has several built-in data types. The most fundamental are:

Page 5
Python for Artificial Intelligence CHAPTER 2. PYTHON LANGUAGE FUNDAMENTALS

1 # int --- integers


2 population = 215000000
3 negative_temp = -15
4 print ( type ( population ) ) # < class ’ int ’>
5
6 # float --- decimal numbers ( floating - point )
7 pi = 3.14159
8 interest_rate = 0.05
9 print ( type ( pi ) ) # < class ’ float ’>
10
11 # str --- text ( string )
12 message = ’ Hello , World ! ’
13 language = " Python "
14 print ( type ( message ) ) # < class ’ str ’>
15
16 # bool --- True or False
17 connected = True
18 error = False
19 print ( type ( connected ) ) # < class ’ bool ’>
20

21 # Checking and converting types


22 num_str = ’ 42 ’
23 num_int = int ( num_str ) # converts string -> integer
24 num_float = float ( num_str ) # converts string -> float
25 print ( num_int + 8) # Output : 50

2.3 Math Operations

1 # Basic arithmetic operators


2 a , b = 10 , 3
3
4 print ( a + b) # Addition -> 13
5 print ( a - b) # Subtraction -> 7
6 print ( a * b) # Multiplication -> 30
7 print ( a / b) # Division -> 3.333...
8 print ( a // b ) # Integer division - > 3
9 print ( a % b) # Modulo ( remainder ) -> 1
10 print ( a ** b ) # Exponentiation -> 1000
11
12 # Comparison operators
13 print ( a > b ) # True
14 print ( a == b ) # False
15 print ( a != b ) # True
16 print ( a >= 10) # True
17
18 # Logical operators
19 x , y = True , False
20 print ( x and y ) # False
21 print ( x or y ) # True
22 print ( not x ) # False
23
24 # Built - in math functions
25 print ( abs ( -42) ) # absolute value -> 42
26 print ( round (3.567 , 2) ) # rounding -> 3.57
27 print ( max (4 , 7 , 2) ) # maximum -> 7
28 print ( min (4 , 7 , 2) ) # minimum -> 2
29 print ( sum ([1 , 2 , 3 , 4]) ) # sum -> 10

Page 6 of 49
Python for Artificial Intelligence CHAPTER 2. PYTHON LANGUAGE FUNDAMENTALS

✓ Exercise
1. Calculate the area of a circle with radius = 7 (use pi = 3.14159).
2. Check whether 17 is odd using the % (modulo) operator.
3. Calculate the hypotenuse of a right triangle with legs 3 and 4 (use ** 0.5 for square
root).

2.4 Strings

Strings are sequences of characters. In Python they are immutable and have dozens of useful
built-in methods.
1 # Creating strings
2 first = ’ Joan ’
3 last = ’ Smith ’
4
5 # Concatenation
6 full_name = first + ’ ’ + last
7 print ( full_name ) # Joan Smith
8
9 # f - strings ( modern , recommended approach )
10 age = 30
11 msg = f ’ Hello , { first }! You are { age } years old . ’
12 print ( msg )
13
14 # Useful string methods
15 text = ’ Python is Awesome ! ’
16 print ( text . strip () ) # removes leading / trailing spaces
17 print ( text . lower () ) # all lowercase
18 print ( text . upper () ) # ALL UPPERCASE
19 print ( text . replace ( ’ Python ’ , ’ AI ’) ) # substitutes text
20
21 # Splitting and checking
22 phrase = ’ machine learning is fascinating ’
23 words = phrase . split ( ’ ’) # split by space
24 print ( words ) # [ ’ machine ’, ’ learning ’, ...]
25 print ( len ( phrase ) ) # number of characters : 33
26 print ( ’ learning ’ in phrase ) # True
27

28 # Indexing and slicing


29 text = ’ Python ’
30 print ( text [0]) # P ( first character )
31 print ( text [ -1]) # n ( last character )
32 print ( text [0:3]) # Pyt ( indices 0 to 2)
33 print ( text [:: -1]) # nohtyP ( reversed )

✓ Exercise
1. Given the string ’Learning Python for AI’, extract only the word ’Python’ using
slicing.
2. Count how many times the letter ’a’ appears in the string (use the .count() method).
3. Transform the name ’ana paula’ into title case using the .title() method.

2.5 Lists

Page 7 of 49
Python for Artificial Intelligence CHAPTER 2. PYTHON LANGUAGE FUNDAMENTALS

Lists are ordered, mutable collections that can hold elements of any type. They are one of the
most commonly used data structures in Python.
1 # Creating lists
2 numbers = [1 , 2 , 3 , 4 , 5]
3 fruits = [ ’ apple ’ , ’ banana ’ , ’ orange ’]
4 mixed = [42 , ’ Python ’ , 3.14 , True ] # different types allowed
5
6 # Accessing elements ( index starts at 0)
7 print ( fruits [0]) # apple
8 print ( fruits [ -1]) # orange ( last element )
9 print ( fruits [1:3]) # [ ’ banana ’, ’ orange ’]
10

11 # Modifying lists
12 fruits . append ( ’ grape ’) # add to the end
13 fruits . insert (1 , ’ pineapple ’) # insert at position 1
14 fruits . remove ( ’ banana ’) # remove by value
15 removed = fruits . pop () # remove and return the last element
16
17 # Useful operations
18 numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6]
19 print ( len ( numbers ) ) # length : 8
20 print ( min ( numbers ) ) # minimum : 1
21 print ( max ( numbers ) ) # maximum : 9
22 print ( sum ( numbers ) ) # sum : 31
23 numbers . sort () # sort in place
24 print ( numbers ) # [1 , 1 , 2 , 3 , 4 , 5 , 6 , 9]
25
26 # List of lists (2 D matrix )
27 matrix = [[1 , 2 , 3] ,
28 [4 , 5 , 6] ,
29 [7 , 8 , 9]]
30 print ( matrix [1][2]) # 6 ( row 1 , column 2)

• Lists vs. Arrays

Python lists can hold mixed types, but are slower for numerical computations. For AI/ML,
use NumPy arrays (covered in Chapter 7) — they are much faster!

✓ Exercise
1. Create a list with a student’s grades: [7.5, 8.0, 6.5, 9.0, 7.0]. Compute the
average.
2. Add a new grade (8.5) to the list and recalculate the average.
3. Sort the list of grades in descending order.

2.6 Tuples

Tuples are like lists, but immutable — once created, they cannot be modified. They are used
for data that should not change.
1 # Creating tuples
2 coordinates = (10.5 , -23.8) # lat , lon of a city
3 dimensions = (1920 , 1080) # screen resolution
4 rgb = (255 , 128 , 0) # orange in RGB

Page 8 of 49
Python for Artificial Intelligence CHAPTER 2. PYTHON LANGUAGE FUNDAMENTALS

5
6 # Accessing elements ( same as lists )
7 print ( coordinates [0]) # 10.5
8 print ( rgb [ -1]) # 0
9
10 # Attempting to modify raises an error !
11 # coordinates [0] = 5.0 # TypeError !
12

13 # Tuple unpacking
14 latitude , longitude = coordinates
15 print ( f ’ Lat : { latitude } , Lon : { longitude } ’)
16
17 # Functions that return multiple values use tuples
18 def basic_stats ( numbers ) :
19 return min ( numbers ) , max ( numbers ) , sum ( numbers ) / len ( numbers )
20
21 minimum , maximum , average = basic_stats ([3 , 7 , 2 , 9 , 5])
22 print ( f ’ Min : { minimum } , Max : { maximum } , Avg : { average } ’)

2.7 Dictionaries

Dictionaries store key-value pairs. They are extremely useful for representing real-world objects
and structured data.
1 # Creating a dictionary
2 student = {
3 ’ name ’: ’ Carlos ’ ,
4 ’ age ’: 22 ,
5 ’ major ’: ’ Data Science ’ ,
6 ’ grades ’: [8.5 , 9.0 , 7.5]
7 }
8
9 # Accessing values
10 print ( student [ ’ name ’ ]) # Carlos
11 print ( student . get ( ’ email ’ , ’N / A ’) ) # N / A ( default value )
12
13 # Adding and updating
14 student [ ’ email ’] = ’ carlos@email . com ’
15 student [ ’ age ’] = 23 # update value
16
17 # Iterating over a dictionary
18 for key , value in student . items () :
19 print ( f ’{ key }: { value } ’)
20
21 # Useful methods
22 print ( student . keys () ) # all keys
23 print ( student . values () ) # all values
24 print ( ’ name ’ in student ) # True --- checks for a key
25

26 # Nested dictionary ( common in ML datasets )


27 dataset = {
28 ’ feature_1 ’: [1.2 , 3.4 , 5.6] ,
29 ’ feature_2 ’: [7.8 , 9.0 , 2.1] ,
30 ’ labels ’: [0 , 1 , 0]
31 }
32 print ( len ( dataset [ ’ feature_1 ’ ]) ) # 3 samples

Page 9 of 49
Python for Artificial Intelligence CHAPTER 2. PYTHON LANGUAGE FUNDAMENTALS

✓ Exercise
1. Create a dictionary representing a product: name, price, stock, and category.
2. Apply a 10% discount to the price and update the dictionary.
3. Check whether the key ’code’ exists in the dictionary; if not, add it with the value
’001’.

2.8 Sets

Sets are unordered collections of unique elements. They are ideal for removing duplicates and
performing set operations.
1 # Creating sets
2 fruits = { ’ apple ’ , ’ banana ’ , ’ orange ’ , ’ apple ’} # duplicate ignored
3 print ( fruits ) # { ’ apple ’, ’ banana ’, ’ orange ’}
4
5 # Creating from a list with duplicates
6 raw_grades = [7 , 8 , 7 , 9 , 8 , 10 , 7]
7 unique_grades = set ( raw_grades )
8 print ( unique_grades ) # {7 , 8 , 9 , 10}
9
10 # Set operations
11 langs_a = { ’ Python ’ , ’R ’ , ’ Julia ’}
12 langs_b = { ’ Python ’ , ’ Java ’ , ’C ++ ’}
13
14 print ( langs_a & langs_b ) # intersection : { ’ Python ’}
15 print ( langs_a | langs_b ) # union
16 print ( langs_a - langs_b ) # difference : { ’ R ’, ’ Julia ’}

≡ Chapter Summary

• Python provides four core collection types: list (ordered, mutable), tuple (ordered,
immutable), dict (key-value), set (unique elements).
• Strings are immutable sequences with dozens of built-in methods.
• Dictionaries are the go-to structure for real-world objects and ML datasets.

Page 10 of 49
Chapter 3

Control Structures

Control structures let a program make decisions and repeat actions. They form the foundation
of programming logic.

3.1 if / elif / else

The if statement executes code conditionally. Python uses indentation (4 spaces) to define
code blocks.
1 # Basic if / elif / else structure
2 temperature = 35
3
4 if temperature > 30:
5 print ( ’ Very hot ! ’)
6 elif temperature > 20:
7 print ( ’ Pleasant temperature ’)
8 elif temperature > 10:
9 print ( ’ Cold , wear a jacket ’)
10 else :
11 print ( ’ Very low temperature ! ’)
12
13 # Compound conditions
14 grade = 7.5
15 attendance = 80
16

17 if grade >= 7.0 and attendance >= 75:


18 print ( ’ Passed ! ’)
19 elif grade >= 5.0 and attendance >= 75:
20 print ( ’ Remediation required ’)
21 else :
22 print ( ’ Failed ’)
23
24 # Ternary operator ( one - liner if )
25 status = ’ adult ’ if grade >= 18 else ’ minor ’

3.2 for Loop

The for loop iterates over any sequence: list, string, range, dictionary, etc.
1 # Iterating over a list
2 algorithms = [ ’ Regression ’ , ’ Classification ’ , ’ Clustering ’]
3 for algo in algorithms :
4 print ( f ’ - { algo } ’)
5
6 # range () --- generates a sequence of numbers
7 for i in range (5) : # 0, 1, 2, 3, 4

Page 11
Python for Artificial Intelligence CHAPTER 3. CONTROL STRUCTURES

8 print (i , end = ’ ’)
9

10 for i in range (2 , 10 , 2) : # from 2 to 9 , step 2


11 print (i , end = ’ ’) # 2 4 6 8
12
13 # enumerate () --- index + value
14 fruits = [ ’ apple ’ , ’ banana ’ , ’ grape ’]
15 for index , fruit in enumerate ( fruits ) :
16 print ( f ’{ index }: { fruit } ’)
17
18 # zip () --- iterate two lists together
19 names = [ ’ Alice ’ , ’ Bob ’ , ’ Carol ’]
20 grades = [9.0 , 7.5 , 8.5]
21 for name , grade in zip ( names , grades ) :
22 print ( f ’{ name }: { grade } ’)
23
24 # Iterating over a dictionary
25 model = { ’ accuracy ’: 0.95 , ’ precision ’: 0.92 , ’ recall ’: 0.88}
26 for metric , value in model . items () :
27 print ( f ’{ metric }: { value :.2%} ’)

3.3 while Loop

The while loop repeats a block as long as a condition is true. Be careful to avoid infinite loops!
1 # Basic while
2 counter = 0
3 while counter < 5:
4 print ( f ’ Iteration { counter } ’)
5 counter += 1 # ESSENTIAL ! Without this : infinite loop
6
7 # Simulating algorithm convergence
8 error = 100.0
9 reduction = 0.7
10 iteration = 0
11
12 while error > 0.01: # continue until convergence
13 error = error * reduction
14 iteration += 1
15 print ( f ’ Iter { iteration }: error = { error :.4 f } ’)
16
17 print ( f ’ Converged in { iteration } iterations ! ’)

▲ Warning

Always ensure that the while loop condition will eventually become False. An infinite
loop freezes the program and requires a forced termination (Ctrl+C in the terminal or
restarting the kernel in Jupyter).

3.4 break and continue

1 # break --- exits the loop immediately


2 numbers = [2 , 4 , 6 , 7 , 8 , 10]
3 for n in numbers :
4 if n % 2 != 0: # if an odd number is found

Page 12 of 49
Python for Artificial Intelligence CHAPTER 3. CONTROL STRUCTURES

5 print ( f ’ Odd found : { n } ’)


6 break # stop the loop
7 print ( f ’{ n } is even ’)
8
9 # continue --- skips to the next iteration
10 print ( ’ Even numbers from 1 to 10: ’)
11 for n in range (1 , 11) :
12 if n % 2 != 0: # if odd
13 continue # skip to next number
14 print (n , end = ’ ’) # 2 4 6 8 10
15
16 # else on a for loop ( runs when loop finishes without break )
17 for n in [2 , 4 , 6 , 8]:
18 if n > 10:
19 break
20 else :
21 print ( ’ No number greater than 10 was found ’)

3.5 List Comprehension

An elegant and efficient syntax for creating lists from iterations. It is one of the most popular
Python features.
1 # Traditional form ( verbose )
2 squares = []
3 for n in range (10) :
4 squares . append ( n ** 2)
5

6 # List comprehension ( concise )


7 squares = [ n ** 2 for n in range (10) ]
8 print ( squares ) # [0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81]
9
10 # With a condition ( filter )
11 evens = [ n for n in range (20) if n % 2 == 0]
12 print ( evens ) # [0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18]
13
14 # Data transformation --- common in AI !
15 grades_str = [ ’ 7.5 ’ , ’ 8.0 ’ , ’ 9.5 ’ , ’ 6.0 ’]
16 grades_float = [ float ( g ) for g in grades_str ]
17

18 # Simple normalization ( scale 0 -1)


19 data = [10 , 20 , 30 , 40 , 50]
20 minimum , maximum = min ( data ) , max ( data )
21 normalized = [( x - minimum ) / ( maximum - minimum ) for x in data ]
22 print ( normalized ) # [0.0 , 0.25 , 0.5 , 0.75 , 1.0]
23

24 # Dict comprehension
25 words = [ ’ python ’ , ’ machine ’ , ’ learning ’]
26 lengths = { w : len ( w ) for w in words }
27 # { ’ python ’: 6 , ’ machine ’: 7 , ’ learning ’: 8}

✓ Exercise
1. Use list comprehension to create a list of cubes of numbers from 1 to 10.
2. Filter a list of temperatures, keeping only values above 25°C.
3. Create a dictionary mapping each number from 1 to 5 to its factorial.

Page 13 of 49
Python for Artificial Intelligence CHAPTER 3. CONTROL STRUCTURES

≡ Chapter Summary

• if/elif/else enables conditional execution; use ternary for one-liners.


• for iterates sequences; while repeats while a condition holds.
• break exits loops; continue skips to the next iteration.
• List comprehension is a concise, Pythonic way to build lists — essential in AI/ML.

Page 14 of 49
Chapter 4

Functions in Python

Functions are reusable blocks of code that perform a specific task. They make code more
organized, readable, and easier to maintain.

4.1 Defining Functions

1 # Basic function structure


2 def greeting () :
3 print ( ’ Hello , welcome to the world of AI ! ’)
4
5 greeting () # calling the function
6
7 # Function with parameters
8 def p e r s o n a l i z e d _ g r e e t i n g ( name ) :
9 print ( f ’ Hello , { name }! Ready to learn ? ’)
10
11 p e r s o n a l i z e d _ g r e e t i n g ( ’ Ana ’) # Hello , Ana ! Ready to learn ?
12 p e r s o n a l i z e d _ g r e e t i n g ( ’ Bruno ’) # Hello , Bruno ! Ready to learn ?

4.2 Return Values

1 # Function that returns a value


2 def calculate_area ( base , height ) :
3 area = base * height
4 return area
5
6 result = calculate_area (5 , 3)
7 print ( f ’ Area : { result } m ^2 ’) # Area : 15 m ^2
8
9 # Multiple return values
10 def basic_stats ( data ) :
11 mean = sum ( data ) / len ( data )
12 minimum = min ( data )
13 maximum = max ( data )
14 return mean , minimum , maximum # returns a tuple
15
16 grades = [7.5 , 8.0 , 6.5 , 9.0 , 7.0]
17 mean , low , high = basic_stats ( grades )
18 print ( f ’ Mean : { mean :.2 f } , Min : { low } , Max : { high } ’)

4.3 Advanced Parameters

1 # Default parameter values


2 def power ( base , exponent =2) : # default exponent is 2

Page 15
Python for Artificial Intelligence CHAPTER 4. FUNCTIONS IN PYTHON

3 return base ** exponent


4

5 print ( power (3) ) # 9 (3^2)


6 print ( power (3 , 3) ) # 27 (3^3)
7 print ( power (2 , 10) ) # 1024
8
9 # * args --- variable number of positional arguments
10 def sum_all (* numbers ) :
11 return sum ( numbers )
12
13 print ( sum_all (1 , 2 , 3) ) # 6
14 print ( sum_all (1 , 2 , 3 , 4 , 5) ) # 15
15
16 # ** kwargs --- variable number of keyword arguments
17 def create_model (** hyperparams ) :
18 for param , value in hyperparams . items () :
19 print ( f ’ { param } = { value } ’)
20
21 create_model ( learning_rate =0.01 , epochs =100 , batch_size =32)
22

23 # Docstring --- function documentation


24 def normalize ( data , minimum = None , maximum = None ) :
25 ’’’
26 Normalizes a list of numbers to the range [0 , 1].
27
28 Args :
29 data : list of numbers
30 minimum : minimum value ( optional )
31 maximum : maximum value ( optional )
32
33 Returns :
34 list with normalized values
35 ’’’
36 mn = minimum if minimum is not None else min ( data )
37 mx = maximum if maximum is not None else max ( data )
38 return [( x - mn ) / ( mx - mn ) for x in data ]
39
40 print ( normalize ([10 , 20 , 30 , 40 , 50]) )

4.4 Lambda Functions

Lambda creates anonymous single-line functions. They are commonly used with map(), filter(),
and sorted().
1 # Basic lambda
2 square = lambda x : x ** 2
3 print ( square (5) ) # 25
4

5 # Lambda with multiple parameters


6 add = lambda a , b : a + b
7 print ( add (3 , 4) ) # 7
8
9 # Used with sorted ()
10 students = [( ’ Alice ’ , 9.5) , ( ’ Bob ’ , 7.0) , ( ’ Carol ’ , 8.5) ]
11 # Sort by grade ( second element of each tuple )
12 ranked = sorted ( students , key = lambda x : x [1] , reverse = True )
13 print ( ranked ) # [( ’ Alice ’, 9.5) , ( ’ Carol ’, 8.5) , ( ’ Bob ’, 7.0) ]

Page 16 of 49
Python for Artificial Intelligence CHAPTER 4. FUNCTIONS IN PYTHON

14
15 # Used with map () --- applies function to each element
16 numbers = [1 , 2 , 3 , 4 , 5]
17 cubed = list ( map ( lambda x : x **3 , numbers ) )
18 print ( cubed ) # [1 , 8 , 27 , 64 , 125]
19
20 # Used with filter () --- filters elements
21 evens = list ( filter ( lambda x : x % 2 == 0 , numbers ) )
22 print ( evens ) # [2 , 4]

✓ Exercise
1. Write a function that receives a list of grades and returns each student’s status (Passed
/ Failed / Remediation).
2. Create a function using **kwargs that receives ML model characteristics and displays
them in a formatted way.
3. Use lambda + sorted() to sort a list of dictionaries by the ’salary’ field in descending
order.

≡ Chapter Summary

• Define functions with def; use return to send back values.


• Use default parameters, *args, and **kwargs for flexible signatures.
• Always document functions with docstrings.
• Lambda functions are anonymous, one-line functions ideal for map/filter/sorted.

Page 17 of 49
Python for Artificial Intelligence CHAPTER 4. FUNCTIONS IN PYTHON

Page 18 of 49
Chapter 5

Object-Oriented Programming (Classes)

Object-Oriented Programming (OOP) organizes code into objects that combine data (attributes)
and behavior (methods). In Machine Learning, classes are used to represent models, datasets,
and pipelines.

5.1 Core Concepts

◦ Essential Terminology

• Class: the blueprint or template for creating objects


• Object / Instance: an object created from a class
• Attribute: a variable belonging to an object (its data)
• Method: a function belonging to an object (its behavior)
• __init__: special method called when the object is created (constructor)
• self : reference to the object itself

5.2 Car Class Example

1 # Defining a class
2 class Car :
3 def __init__ ( self , make , model , year , fuel = ’ gasoline ’) :
4 self . make = make
5 self . model = model
6 self . year = year
7 self . fuel = fuel
8 self . speed = 0
9 self . running = False
10
11 def start ( self ) :
12 if not self . running :
13 self . running = True
14 print ( f ’{ self . model } started ! ’)
15 else :
16 print ( ’ The car is already running . ’)
17

18 def accelerate ( self , increment ) :


19 if self . running :
20 self . speed += increment
21 print ( f ’ Speed : { self . speed } mph ’)
22
23 def status ( self ) :
24 state = ’ on ’ if self . running else ’ off ’
25 print ( f ’{ self . make } { self . model } ({ self . year }) --- { state } ’)
26 print ( f ’ Current speed : { self . speed } mph ’)

Page 19
Python for Artificial Intelligence
CHAPTER 5. OBJECT-ORIENTED PROGRAMMING (CLASSES)

27
28 # Creating objects ( instances )
29 my_car = Car ( ’ Toyota ’ , ’ Corolla ’ , 2023)
30 car2 = Car ( ’ Tesla ’ , ’ Model 3 ’ , 2024 , ’ electric ’)
31
32 my_car . start ()
33 my_car . accelerate (60)
34 my_car . status ()

5.3 Dataset Class (ML Application)

1 class Dataset :
2 ’ ’ ’ Represents a dataset for Machine Learning . ’ ’ ’
3
4 def __init__ ( self , name ) :
5 self . name = name
6 self . data = []
7 self . columns = []
8
9 def add_column ( self , name , values ) :
10 self . columns . append ( name )
11 if not self . data :
12 self . data = [[ v ] for v in values ]
13 else :
14 for i , v in enumerate ( values ) :
15 self . data [ i ]. append ( v )
16
17 def shape ( self ) :
18 ’ ’ ’ Returns ( rows , columns ) --- just like Pandas ! ’ ’ ’
19 return ( len ( self . data ) , len ( self . columns ) )
20
21 def head ( self , n =5) :
22 print ( f ’ Dataset : { self . name } ’)
23 print ( ’ ’. join ( self . columns ) )
24 for row in self . data [: n ]:
25 print ( ’ ’. join ( str ( v ) for v in row ) )
26
27 # Creating and using the Dataset
28 ds = Dataset ( ’ Iris Simplified ’)
29 ds . add_column ( ’ sepal_length ’ , [5.1 , 4.9 , 6.2 , 5.8])
30 ds . add_column ( ’ sepal_width ’ , [3.5 , 3.0 , 2.8 , 3.2])
31 ds . add_column ( ’ species ’ , [0 , 0 , 1 , 1])
32
33 print ( f ’ Shape : { ds . shape () } ’)
34 ds . head ()

✓ Exercise
1. Create an Employee class with attributes: name, role, salary. Add methods to give a
raise (%) and display information.
2. Create an MLModel class with attributes: name, algorithm, accuracy. Add a method to
compare two models and return the better one.

Page 20 of 49
Part II

Data Handling and Machine Learning


Tools

Page 21
Chapter 6

Introduction to Data Handling

In Artificial Intelligence, data is the fuel that powers algorithms. Before training any model,
you must collect, clean, transform, and analyze data — steps that take up roughly 70–80% of
an AI project’s total time.

◦ Typical AI Project Pipeline

1. Data Collection → 2. Cleaning → 3. Exploration (EDA)


4. Preprocessing → 5. Modeling → 6. Evaluation → 7. Deployment
This textbook covers steps 1, 2, 3, and 4 in detail.

6.1 Essential Libraries

1 # Install all required libraries


2 ! pip install numpy pandas matplotlib seaborn openpyxl
3
4 # Standard industry imports ( with conventional aliases )
5 import numpy as np # numerical computing
6 import pandas as pd # data manipulation
7 import matplotlib . pyplot as plt # data visualization
8 import seaborn as sns # statistical visualization
9
10 # Display settings
11 pd . set_option ( ’ display . max_columns ’ , 20)
12 pd . set_option ( ’ display . precision ’ , 3)
13
14 print ( ’ Installed versions : ’)
15 print ( f ’ NumPy : { np . __version__ } ’)
16 print ( f ’ Pandas : { pd . __version__ } ’)

Page 23
Python for Artificial Intelligence CHAPTER 6. INTRODUCTION TO DATA HANDLING

Page 24 of 49
Chapter 7

NumPy — Numerical Computing

NumPy (Numerical Python) is the core library for scientific computing in Python. It provides
the ndarray (N-dimensional array) object and high-performance mathematical functions.

7.1 Why NumPy?

1 import numpy as np
2 import time
3
4 # Speed comparison : Python list vs NumPy array
5 size = 1 _000_000
6
7 # Python list
8 lst = list ( range ( size ) )
9 start = time . time ()
10 result_list = [ x * 2 for x in lst ]
11 time_list = time . time () - start
12
13 # NumPy array
14 array = np . arange ( size )
15 start = time . time ()
16 result_np = array * 2 # vectorized operation !
17 time_np = time . time () - start
18
19 print ( f ’ Python list : { time_list :.4 f } s ’)
20 print ( f ’ NumPy array : { time_np :.4 f } s ’)
21 print ( f ’ NumPy is ~{ time_list / time_np :.0 f } x faster ! ’)

7.2 Creating Arrays

1 import numpy as np
2
3 # From Python lists
4 vector = np . array ([1 , 2 , 3 , 4 , 5])
5 matrix = np . array ([[1 , 2 , 3] ,
6 [4 , 5 , 6] ,
7 [7 , 8 , 9]])
8
9 print ( vector . shape ) # (5 ,) --- 1D , 5 elements
10 print ( matrix . shape ) # (3 , 3) --- 3 rows , 3 columns
11 print ( matrix . dtype ) # int64
12 print ( matrix . ndim ) # 2 --- number of dimensions
13
14 # Special arrays --- heavily used in ML !
15 zeros = np . zeros ((3 , 4) ) # matrix of zeros

Page 25
Python for Artificial Intelligence CHAPTER 7. NUMPY — NUMERICAL COMPUTING

16 ones = np . ones ((2 , 3) ) # matrix of ones


17 eye = np . eye (4) # identity matrix
18
19 # Sequences
20 seq = np . arange (0 , 10 , 0.5) # from 0 to 9.5 , step 0.5
21 lin = np . linspace (0 , 1 , 11) # 11 evenly spaced points from 0 to
1
22 print ( lin ) # [0. 0.1 0.2 ... 1.0]
23
24 # Random arrays --- used for initializing neural network weights
25 rand_uniform = np . random . rand (3 , 3) # uniform [0 , 1)
26 rand_normal = np . random . randn (3 , 3) # standard normal
27 rand_ints = np . random . randint (0 , 10 , (3 , 3) ) # integers

7.3 Indexing and Slicing

1 import numpy as np
2

3 data = np . array ([[10 , 20 , 30] ,


4 [40 , 50 , 60] ,
5 [70 , 80 , 90]])
6
7 # Accessing by index
8 print ( data [0 , 0]) # 10 ( row 0 , column 0)
9 print ( data [1 , 2]) # 60 ( row 1 , column 2)
10 print ( data [ -1 , -1]) # 90 ( last row , last column )
11
12 # Slicing
13 print ( data [0 , :]) # [10 20 30] --- first row
14 print ( data [: , 1]) # [20 50 80] --- second column
15 print ( data [0:2 , 1:3]) # 2 x2 submatrix
16
17 # Boolean indexing --- very common in ML for filtering data !
18 values = np . array ([15 , 3 , 42 , 8 , 27 , 5 , 33])
19 mask = values > 10 # [ True , False , True , ...]
20 filtered = values [ mask ] # [15 , 42 , 27 , 33]
21 print ( filtered )

7.4 Vectorized Math Operations

1 import numpy as np
2
3 a = np . array ([1 , 2 , 3 , 4 , 5])
4 b = np . array ([10 , 20 , 30 , 40 , 50])
5
6 # Element - wise operations ( no loops needed !)
7 print ( a + b ) # [11 22 33 44 55]
8 print ( a * b ) # [10 40 90 160 250]
9 print ( a ** 2) # [ 1 4 9 16 25]
10 print ( np . sqrt ( a ) ) # [1. 1.41 1.73 2. 2.23]
11
12 # Statistical functions
13 data = np . random . randn (100) # 100 normal values
14 print ( f ’ Mean : { data . mean () :.4 f } ’)
15 print ( f ’ Std : { data . std () :.4 f } ’)

Page 26 of 49
Python for Artificial Intelligence CHAPTER 7. NUMPY — NUMERICAL COMPUTING

16 print ( f ’ Min : { data . min () :.4 f } ’)


17 print ( f ’ Max : { data . max () :.4 f } ’)
18 print ( f ’ Median : { np . median ( data ) :.4 f } ’)
19
20 # Linear algebra --- the foundation of neural networks !
21 A = np . array ([[1 , 2] , [3 , 4]])
22 B = np . array ([[5 , 6] , [7 , 8]])
23

24 print ( np . dot (A , B ) ) # matrix product


25 print ( A . T ) # transpose
26 print ( np . linalg . det ( A ) ) # determinant
27
28 # Reshape --- change array dimensions
29 flat = np . arange (12) # [0 , 1 , 2 , ... , 11]
30 matrix_3x4 = flat . reshape (3 , 4) # 3 rows , 4 columns
31 print ( matrix_3x4 )

✓ Exercise
1. Create an array of 50 random values (normal distribution). Compute the mean, stan-
dard deviation, and the 25th, 50th, and 75th percentiles.
2. Create a 5×5 matrix with values from 0 to 24. Extract the main diagonal and the first
2 columns.
3. Normalize an array to the range [0, 1] using the formula: (x − min)/(max − min).

Page 27 of 49
Python for Artificial Intelligence CHAPTER 7. NUMPY — NUMERICAL COMPUTING

Page 28 of 49
Chapter 8

Pandas — Data Manipulation

Pandas is the primary library for handling tabular data in Python. It provides two core data
structures: Series (1D) and DataFrame (2D — similar to a spreadsheet).

8.1 Series

1 import pandas as pd
2 import numpy as np
3
4 # Series --- 1 D array with an index
5 temps = pd . Series ([22 , 25 , 19 , 28 , 31] ,
6 index =[ ’ Mon ’ , ’ Tue ’ , ’ Wed ’ , ’ Thu ’ , ’ Fri ’ ])
7 print ( temps )
8

9 # Operations on a Series
10 print ( temps . mean () ) # mean
11 print ( temps . max () ) # maximum
12 print ( temps > 25) # boolean mask
13 print ( temps [ temps > 25]) # filtering

8.2 DataFrame

1 import pandas as pd
2

3 # Creating a DataFrame from a dictionary


4 data = {
5 ’ name ’: [ ’ Ana ’ , ’ Bruno ’ , ’ Carla ’ , ’ Diego ’ , ’ Elena ’] ,
6 ’ age ’: [25 , 30 , 28 , 35 , 22] ,
7 ’ city ’: [ ’ NY ’ , ’ LA ’ , ’ NY ’ , ’ TX ’ , ’ FL ’] ,
8 ’ salary ’: [4500 , 7200 , 5800 , 9100 , 3900] ,
9 ’ level ’: [ ’ Jr ’ , ’ Sr ’ , ’ Mid ’ , ’ Sr ’ , ’ Jr ’]
10 }
11
12 df = pd . DataFrame ( data )
13 print ( df )
14

15 # Inspecting the DataFrame


16 print ( df . shape ) # (5 , 5) --- rows , columns
17 print ( df . dtypes ) # type of each column
18 print ( df . describe () ) # statistics for numeric columns
19 print ( df . info () ) # general summary
20 print ( df . head (3) ) # first 3 rows
21 print ( df . tail (2) ) # last 2 rows

8.3 Reading Data

Page 29
Python for Artificial Intelligence CHAPTER 8. PANDAS — DATA MANIPULATION

1 import pandas as pd
2
3 # --- CSV ---
4 df_csv = pd . read_csv ( ’ data . csv ’) # default separator : comma
5 df_csv2 = pd . read_csv ( ’ data . csv ’ ,
6 sep = ’; ’ ,
7 encoding = ’utf -8 ’ ,
8 parse_dates =[ ’ date ’ ])
9
10 # --- TXT ( tab - separated ) ---
11 df_txt = pd . read_csv ( ’ data . txt ’ ,
12 sep = ’\ t ’ ,
13 header =0)
14
15 # --- Excel ---
16 # Requires : pip install openpyxl
17 df_excel = pd . read_excel ( ’ data . xlsx ’ ,
18 sheet_name = ’ Sheet1 ’ ,
19 header =0)
20
21 # --- Saving data ---
22 df . to_csv ( ’ output . csv ’ , index = False )
23 df . to_excel ( ’ output . xlsx ’ , index = False )

8.4 Selecting and Filtering Data

1 import pandas as pd
2
3 data = { ’ name ’: [ ’ Ana ’ , ’ Bruno ’ , ’ Carla ’ , ’ Diego ’] ,
4 ’ age ’: [25 , 30 , 28 , 35] ,
5 ’ salary ’: [4500 , 7200 , 5800 , 9100] ,
6 ’ city ’: [ ’ NY ’ , ’ LA ’ , ’ NY ’ , ’ TX ’ ]}
7
8 df = pd . DataFrame ( data )
9
10 # Selecting columns
11 print ( df [ ’ name ’ ]) # one column ( Series )
12 print ( df [[ ’ name ’ , ’ salary ’ ]]) # multiple columns
13
14 # Selection by position ( iloc ) and by label ( loc )
15 print ( df . iloc [0]) # first row
16 print ( df . iloc [1:3]) # rows 1 and 2
17 print ( df . loc [0 , ’ name ’ ]) # row 0 , column ’ name ’
18
19 # Filtering by condition
20 older = df [ df [ ’ age ’] > 28]
21 ny_employees = df [ df [ ’ city ’] == ’ NY ’]
22 senior = df [( df [ ’ salary ’] > 5000) & ( df [ ’ age ’] < 35) ]
23
24 # isin () --- check multiple values
25 eastern = df [ df [ ’ city ’ ]. isin ([ ’ NY ’ , ’ LA ’ ]) ]
26

27 # Sorting
28 df_sorted = df . sort_values ( ’ salary ’ , ascending = False )
29

Page 30 of 49
Python for Artificial Intelligence CHAPTER 8. PANDAS — DATA MANIPULATION

30 # Adding computed columns


31 df [ ’ annual_salary ’] = df [ ’ salary ’] * 12
32 df [ ’ category ’] = df [ ’ salary ’ ]. apply (
33 lambda x : ’ High ’ if x > 6000 else ’ Normal ’)

8.5 Grouping and Aggregation

1 # groupby () --- group and aggregate


2 avg_by_city = df . groupby ( ’ city ’) [ ’ salary ’ ]. mean ()
3 print ( avg_by_city )
4
5 # Multiple aggregations
6 summary = df . groupby ( ’ city ’) . agg ({
7 ’ salary ’: [ ’ mean ’ , ’ min ’ , ’ max ’ , ’ count ’] ,
8 ’ age ’: ’ mean ’
9 })
10 print ( summary )
11

12 # pivot_table --- dynamic cross - tab


13 table = pd . pivot_table ( df ,
14 values = ’ salary ’ ,
15 index = ’ city ’ ,
16 aggfunc = ’ mean ’)
17 print ( table )

✓ Exercise
1. Create a DataFrame with data for 6 products (name, price, category, stock). Filter
products with stock below 10.
2. Calculate the average price per category using groupby().
3. Add a ’total_value’ column = price * stock. Sort by total value in descending order.

Page 31 of 49
Python for Artificial Intelligence CHAPTER 8. PANDAS — DATA MANIPULATION

Page 32 of 49
Chapter 9

Data Cleaning

Real-world data is rarely perfect. Missing values, duplicate records, wrong data types, and
outliers are common issues that must be addressed before training models.

▲ Common Data Problems


• Null values (NaN, None, N/A)
• Duplicate records
• Incorrect data types (numbers stored as strings)
• Out-of-range values (outliers)
• Formatting inconsistencies (’NY’, ’ny’, ’New York’)
• Irrelevant or redundant columns

9.1 Detecting and Handling Missing Values

1 import pandas as pd
2 import numpy as np
3
4 # Dataset with intentional problems
5 data = {
6 ’ name ’: [ ’ Alice ’ , ’ Bob ’ , None , ’ Diana ’ , ’ Eva ’] ,
7 ’ age ’: [25 , None , 30 , 28 , 35] ,
8 ’ salary ’: [4500 , 6000 , 5500 , None , 7000] ,
9 ’ city ’: [ ’ NY ’ , ’ LA ’ , ’ NY ’ , ’ NY ’ , None ] ,
10 }
11 df = pd . DataFrame ( data )
12
13 # Detecting nulls
14 print ( df . isnull () )
15 print ( df . isnull () . sum () )
16 print ( df . isnull () . mean () * 100)
17

18 # --- Treatment strategies ---


19
20 # 1. Drop rows with any null
21 df_clean = df . dropna ()
22
23 # 2. Drop only if a specific column has nulls
24 df_no_null_name = df . dropna ( subset =[ ’ name ’ ])
25
26 # 3. Fill with a fixed value
27 df [ ’ city ’] = df [ ’ city ’ ]. fillna ( ’ Unknown ’)
28
29 # 4. Fill with mean ( numeric ) --- common in ML
30 df [ ’ age ’] = df [ ’ age ’ ]. fillna ( df [ ’ age ’ ]. mean () )
31 df [ ’ salary ’] = df [ ’ salary ’ ]. fillna ( df [ ’ salary ’ ]. median () )

Page 33
Python for Artificial Intelligence CHAPTER 9. DATA CLEANING

32
33 print ( df )

9.2 Removing Duplicates

1 import pandas as pd
2
3 df = pd . DataFrame ({
4 ’ id ’: [1 , 2, 2, 3, 4, 4] ,
5 ’ product ’: [ ’A ’ , ’B ’ , ’B ’ , ’C ’ , ’D ’ , ’D ’] ,
6 ’ price ’: [10 , 20 , 20 , 30 , 40 , 45] # different price !
7 })
8
9 # Check for duplicates
10 print ( df . duplicated () )
11 print ( df . duplicated () . sum () )
12
13 # Remove exact duplicates
14 df_clean = df . drop_duplicates ()
15
16 # Duplicates based on specific columns ( keep first occurrence of ’ id
’)
17 df_unique = df . drop_duplicates ( subset =[ ’ id ’] , keep = ’ first ’)
18 print ( df_unique )

9.3 Type Conversion and Normalization

1 import pandas as pd
2 import numpy as np
3
4 df = pd . DataFrame ({
5 ’ date ’: [ ’ 2024 -01 -15 ’ , ’ 2024 -02 -20 ’ , ’ 2024 -03 -10 ’] ,
6 ’ price_str ’: [ ’$1 ,500.00 ’ , ’$2 ,300.50 ’ , ’ $890 .00 ’] ,
7 ’ quantity ’: [ ’ 10 ’ , ’ 25 ’ , ’7 ’] ,
8 ’ temperature ’: [36.5 , 102.0 , 37.2] # outlier !
9 })
10
11 # Type conversions
12 df [ ’ date ’] = pd . to_datetime ( df [ ’ date ’ ])
13 df [ ’ quantity ’] = pd . to_numeric ( df [ ’ quantity ’ ])
14
15 # Clean string price and convert
16 df [ ’ price ’] = ( df [ ’ price_str ’]
17 . str . replace ( ’$ ’ , ’ ’ , regex = False )
18 . str . replace ( ’ , ’ , ’ ’ , regex = False )
19 . astype ( float ) )
20
21 # Detect outliers using z - score
22 mean = df [ ’ temperature ’ ]. mean ()
23 std = df [ ’ temperature ’ ]. std ()
24 df [ ’ z_score ’] = ( df [ ’ temperature ’] - mean ) / std
25 outliers = df [ df [ ’ z_score ’ ]. abs () > 2]
26 print ( ’ Detected outliers : ’)
27 print ( outliers )
28

Page 34 of 49
Python for Artificial Intelligence CHAPTER 9. DATA CLEANING

29 # Min - Max Normalization


30 col = df [ ’ price ’]
31 df [ ’ price_norm ’] = ( col - col . min () ) / ( col . max () - col . min () )
32
33 # Z - score Standardization
34 df [ ’ price_std ’] = ( col - col . mean () ) / col . std ()
35
36 print ( df [[ ’ price ’ , ’ price_norm ’ , ’ price_std ’ ]])

✓ Exercise
1. Create a DataFrame with 10 rows containing at least 3 null values in different columns.
Apply a different strategy to handle each column.
2. Given a dataset with prices as strings (’$1,200.00’), convert them to float correctly.
3. Detect and remove outliers from a numeric column using the IQR (Interquartile Range)
method.

Page 35 of 49
Python for Artificial Intelligence CHAPTER 9. DATA CLEANING

Page 36 of 49
Chapter 10

Data Visualization

Visualization is essential for understanding data before building models. A good plot can reveal
patterns, outliers, and relationships that would be difficult to spot in tables.

10.1 Initial Setup

1 import matplotlib . pyplot as plt


2 import seaborn as sns
3 import numpy as np
4 import pandas as pd
5
6 # Recommended style for presentations
7 plt . style . use ( ’ seaborn - v0_8 - whitegrid ’)
8 sns . set_palette ( ’ husl ’) # harmonious color palette

10.2 Line Chart

Ideal for time-series data and trends.


1 import matplotlib . pyplot as plt
2 import numpy as np
3
4 epochs = np . arange (1 , 51)
5 train_loss = 2.0 * np . exp ( -0.08 * epochs ) + np . random . rand (50) *
0.05
6 val_loss = 2.2 * np . exp ( -0.06 * epochs ) + np . random . rand (50) *
0.08
7
8 fig , ax = plt . subplots ( figsize =(10 , 5) )
9
10 ax . plot ( epochs , train_loss , label = ’ Train ’ , color = ’ steelblue ’ ,
linewidth =2)
11 ax . plot ( epochs , val_loss , label = ’ Validation ’ , color = ’ coral ’ ,
12 linewidth =2 , linestyle = ’ -- ’)
13
14 ax . set_title ( ’ Training Loss Over Epochs ’ , fontsize =14 , fontweight = ’
bold ’)
15 ax . set_xlabel ( ’ Epoch ’ , fontsize =12)
16 ax . set_ylabel ( ’ Loss ( Error ) ’ , fontsize =12)
17 ax . legend ( fontsize =11)
18 ax . grid ( True , alpha =0.3)
19
20 plt . tight_layout ()
21 plt . savefig ( ’ loss_curve . png ’ , dpi =150)
22 plt . show ()

Page 37
Python for Artificial Intelligence CHAPTER 10. DATA VISUALIZATION

10.3 Bar Chart

For comparing categories.


1 import matplotlib . pyplot as plt
2 import numpy as np
3
4 algorithms = [ ’ Linear \ nRegression ’ , ’ Decision \ nTree ’ ,
5 ’ Random \ nForest ’ , ’ SVM ’ , ’ Neural \ nNetwork ’]
6 accuracies = [0.82 , 0.85 , 0.91 , 0.88 , 0.94]
7 colors = [ ’ #2196 F3 ’ , ’ #4 CAF50 ’ , ’# FF9800 ’ , ’ #9 C27B0 ’ , ’# F44336 ’]
8

9 fig , ax = plt . subplots ( figsize =(10 , 6) )


10 bars = ax . bar ( algorithms , accuracies , color = colors ,
11 alpha =0.85 , edgecolor = ’ white ’)
12
13 for bar , acc in zip ( bars , accuracies ) :
14 ax . text ( bar . get_x () + bar . get_width () / 2 ,
15 bar . get_height () + 0.005 ,
16 f ’{ acc :.0%} ’ ,
17 ha = ’ center ’ , va = ’ bottom ’ , fontsize =11 , fontweight = ’ bold ’
)
18
19 ax . set_ylim (0.7 , 1.0)
20 ax . set_title ( ’ Accuracy by ML Algorithm ’ , fontsize =14 , fontweight = ’
bold ’)
21 ax . set_ylabel ( ’ Accuracy ’ , fontsize =12)
22 ax . axhline ( y =0.9 , color = ’ red ’ , linestyle = ’ -- ’ , alpha =0.5 , label = ’
Target : 90% ’)
23 ax . legend ()
24
25 plt . tight_layout ()
26 plt . show ()

10.4 Histogram

For visualizing the distribution of a numeric variable.


1 import matplotlib . pyplot as plt
2 import numpy as np
3
4 np . random . seed (42)
5 data_a = np . random . normal ( loc =70 , scale =10 , size =500)
6 data_b = np . random . normal ( loc =80 , scale =8 , size =500)
7
8 fig , ax = plt . subplots ( figsize =(10 , 5) )
9
10 ax . hist ( data_a , bins =30 , alpha =0.6 , color = ’ steelblue ’ ,
11 label = ’ Model A ’ , edgecolor = ’ white ’)
12 ax . hist ( data_b , bins =30 , alpha =0.6 , color = ’ coral ’ ,
13 label = ’ Model B ’ , edgecolor = ’ white ’)
14
15 ax . axvline ( data_a . mean () , color = ’ steelblue ’ , linestyle = ’ -- ’ ,
linewidth =2 ,
16 label = f ’ Mean A : { data_a . mean () :.1 f } ’)
17 ax . axvline ( data_b . mean () , color = ’ coral ’ , linestyle = ’ -- ’ , linewidth
=2 ,

Page 38 of 49
Python for Artificial Intelligence CHAPTER 10. DATA VISUALIZATION

18 label = f ’ Mean B : { data_b . mean () :.1 f } ’)


19

20 ax . set_title ( ’ Score Distribution of Models ’ , fontsize =14 , fontweight


= ’ bold ’)
21 ax . set_xlabel ( ’ Score ’ , fontsize =12)
22 ax . set_ylabel ( ’ Frequency ’ , fontsize =12)
23 ax . legend ()
24

25 plt . tight_layout ()
26 plt . show ()

10.5 Correlation Heatmap (Seaborn)

1 import seaborn as sns


2 import pandas as pd
3 import numpy as np
4 import matplotlib . pyplot as plt
5

6 np . random . seed (42)


7 df = pd . DataFrame ({
8 ’ Temperature ’: np . random . normal (25 , 5 , 100) ,
9 ’ Humidity ’: np . random . normal (60 , 10 , 100) ,
10 ’ Pressure ’: np . random . normal (1013 , 5 , 100) ,
11 ’ Wind_Speed ’: np . random . normal (15 , 3 , 100) ,
12 ’ Precipitation ’: np . random . normal (2 , 1 , 100) ,
13 })
14
15 correlation = df . corr ()
16
17 fig , ax = plt . subplots ( figsize =(8 , 6) )
18 sns . heatmap ( correlation , annot = True , fmt = ’ .2 f ’ , cmap = ’ coolwarm ’ ,
19 center =0 , square = True , linewidths =0.5 , ax = ax )
20 ax . set_title ( ’ Correlation Matrix --- Weather Data ’ ,
21 fontsize =13 , fontweight = ’ bold ’)
22 plt . tight_layout ()
23 plt . show ()

✓ Exercise
1. Create a line chart showing monthly sales over 2 years.
2. Build a 2×2 subplot grid with 4 different plot types to analyze a dataset of your choice.
3. Create a correlation heatmap for a dataset with at least 5 numeric variables.

Page 39 of 49
Python for Artificial Intelligence CHAPTER 10. DATA VISUALIZATION

Page 40 of 49
Chapter 11

Reading Different Data Types

AI projects often work with data beyond tables: images, audio, video, and text. This section
shows how to load each type.

11.1 CSV, TXT, and Excel Files

1 import pandas as pd
2

3 # --- CSV ---


4 df = pd . read_csv ( ’ dataset . csv ’ , sep = ’ , ’ , encoding = ’utf -8 ’)
5
6 # Create a sample CSV and read it back
7 df_sample = pd . DataFrame ({ ’x ’: [1 , 2 , 3] , ’y ’: [4 , 5 , 6]})
8 df_sample . to_csv ( ’/ tmp / sample . csv ’ , index = False )
9 df_loaded = pd . read_csv ( ’/ tmp / sample . csv ’)
10 print ( df_loaded )
11
12 # Helper function : inspect any DataFrame
13 def inspect ( df , name = ’ Dataset ’) :
14 print ( f ’ === { name } === ’)
15 print ( f ’ Shape : { df . shape } ’)
16 print ( f ’ Columns : { list ( df . columns ) } ’)
17 print ( f ’ Types :\ n { df . dtypes } ’)
18 print ( f ’ Nulls :\ n { df . isnull () . sum () } ’)
19 print ( df . head () )

11.2 Images with PIL and OpenCV

1 # pip install Pillow opencv - python


2 from PIL import Image
3 import numpy as np
4
5 # --- PIL / Pillow ---
6 img = Image . open ( ’ photo . jpg ’)
7 print ( f ’ Mode : { img . mode } ’) # RGB , RGBA , L ( grayscale )
8 print ( f ’ Size : { img . size } ’) # ( width , height )
9 print ( f ’ Format : { img . format } ’) # JPEG , PNG , etc .
10
11 # Convert to NumPy array
12 img_array = np . array ( img )
13 print ( f ’ Shape : { img_array . shape } ’) # ( height , width , 3) for RGB
14 print ( f ’ Dtype : { img_array . dtype } ’) # uint8 (0 -255)
15
16 # Basic operations
17 img_gray = img . convert ( ’L ’) # grayscale

Page 41
Python for Artificial Intelligence CHAPTER 11. READING DIFFERENT DATA TYPES

18 img_resized = img . resize ((224 , 224) ) # resize ( CNN standard size )


19

20 # --- OpenCV ---


21 import cv2
22 img_cv = cv2 . imread ( ’ photo . jpg ’) # BGR ( not RGB !)
23 img_rgb = cv2 . cvtColor ( img_cv , cv2 . COLOR_BGR2RGB ) # convert to
RGB
24 print ( f ’ Shape : { img_cv . shape } ’) # ( height , width , channels )
25
26 # Preprocessing for ML
27 img_norm = img_rgb / 255.0 # normalize to [0 , 1]
28 img_batch = img_norm [ np . newaxis ] # add batch dimension
29 print ( f ’ Ready for ML : { img_batch . shape } ’) # (1 , H , W , 3)

• Tip

In image-based AI projects, neural networks expect NumPy arrays normalized to [0, 1].
For convolutional networks (CNN), the standard input format is (batch, height, width,
channels). Always resize images to a fixed size (e.g., 224 × 224 for VGG/ResNet).

11.3 Audio with Librosa

1 # pip install librosa soundfile


2 import librosa
3 import numpy as np
4 import matplotlib . pyplot as plt
5
6 # Load an audio file
7 audio , sample_rate = librosa . load ( ’ audio . wav ’ , sr =22050)
8 print ( f ’ Duration : { len ( audio ) / sample_rate :.2 f } seconds ’)
9 print ( f ’ Sample rate : { sample_rate } Hz ’)
10 print ( f ’ Samples : { len ( audio ) } ’)
11
12 # Extracting features for ML
13 # MFCC --- Mel - Frequency Cepstral Coefficients
14 mfcc = librosa . feature . mfcc ( y = audio , sr = sample_rate , n_mfcc =13)
15 print ( f ’ MFCC shape : { mfcc . shape } ’) # (13 , n_frames )
16
17 # Spectrogram
18 spectrum = librosa . stft ( audio )
19 spectrum_db = librosa . amplitude_to_db ( np . abs ( spectrum ) )
20
21 # Visualize the spectrogram
22 plt . figure ( figsize =(12 , 4) )
23 librosa . display . specshow ( spectrum_db , sr = sample_rate ,
24 x_axis = ’ time ’ , y_axis = ’ hz ’)
25 plt . colorbar ( format = ’ %+2.0 f dB ’)
26 plt . title ( ’ Spectrogram ’)
27 plt . tight_layout ()
28 plt . show ()

Page 42 of 49
Chapter 12

Mini-Project — Student Dataset Analysis

Let us put everything together! We will create a simulated student dataset and perform a
complete analysis, from data generation to visualization.

◦ Project Goal

Analyze a student dataset to answer questions such as:


• What is the grade distribution by major?
• Is there a correlation between study hours and performance?
• What characteristics distinguish passing students from failing ones?

12.1 Step 1 — Data Creation and Loading

1 import pandas as pd
2 import numpy as np
3 import matplotlib . pyplot as plt
4 import seaborn as sns
5
6 # Configuration
7 np . random . seed (42)
8 plt . style . use ( ’ seaborn - v0_8 - whitegrid ’)
9 sns . set_palette ( ’ husl ’)
10
11 # Create a synthetic student dataset
12 n = 200 # 200 students
13
14 majors = np . random . choice (
15 [ ’ Data Science ’ , ’ Engineering ’ , ’ Mathematics ’ , ’ Physics ’] , n )
16
17 study_hours = np . random . normal ( loc =5 , scale =2 , size = n ) . clip (0 ,
12)
18 sleep_hours = np . random . normal ( loc =7 , scale =1.5 , size = n ) . clip (4 ,
10)
19 absences = np . random . poisson ( lam =3 , size = n )
20
21 # Grade influenced by study hours and absences
22 raw_grade = 5 + 0.5 * study_hours - 0.2 * absences + np . random . randn
(n)
23 grade = raw_grade . clip (0 , 10) . round (1)
24
25 df = pd . DataFrame ({
26 ’ major ’: majors ,
27 ’ study_hours ’: study_hours . round (1) ,
28 ’ sleep_hours ’: sleep_hours . round (1) ,
29 ’ absences ’: absences ,
30 ’ grade ’: grade ,

Page 43
Python for Artificial Intelligence
CHAPTER 12. MINI-PROJECT — STUDENT DATASET ANALYSIS

31 })
32

33 # Intentionally introduce some null values


34 null_idx = np . random . choice ( df . index , 10 , replace = False )
35 df . loc [ null_idx [:5] , ’ sleep_hours ’] = np . nan
36 df . loc [ null_idx [5:] , ’ absences ’] = np . nan
37
38 print ( ’ Dataset created ! ’)
39 print ( df . head (10) )

12.2 Step 2 — Initial Exploration

1 print ( ’ === DATASET OVERVIEW === ’)


2 print ( f ’ Shape : { df . shape } ’)
3 print ( f ’\ nData types :\ n { df . dtypes } ’)
4 print ( f ’\ nDescriptive statistics :\ n { df . describe () . round (2) } ’)
5 print ( f ’\ nNull values per column :\ n { df . isnull () . sum () } ’)
6 print ( f ’\ nCount by major :\ n { df [" major "]. value_counts () } ’)

12.3 Step 3 — Data Cleaning

1 # Handle null values


2 df [ ’ sleep_hours ’] = df [ ’ sleep_hours ’ ]. fillna ( df [ ’ sleep_hours ’ ]. mean
() )
3 df [ ’ absences ’] = df [ ’ absences ’ ]. fillna ( df [ ’ absences ’ ]. median () )
4
5 print ( f ’ Remaining nulls : { df . isnull () . sum () . sum () } ’)
6

7 # Create a status column


8 def classify_grade ( grade ) :
9 if grade >= 7.0:
10 return ’ Passed ’
11 elif grade >= 5.0:
12 return ’ Remediation ’
13 else :
14 return ’ Failed ’
15
16 df [ ’ status ’] = df [ ’ grade ’ ]. apply ( classify_grade )
17 df [ ’ absences ’] = df [ ’ absences ’ ]. astype ( int )
18

19 print ( ’\ nStatus distribution : ’)


20 print ( df [ ’ status ’ ]. value_counts () )

12.4 Step 4 — Analysis and Statistics

1 # Statistics by major
2 print ( ’ === AVERAGE GRADE BY MAJOR === ’)
3 grade_by_major = df . groupby ( ’ major ’) [ ’ grade ’ ]. agg ([ ’ mean ’ , ’ std ’ , ’
count ’ ])
4 grade_by_major . columns = [ ’ Mean ’ , ’ Std ’ , ’ Students ’]
5 print ( grade_by_major . round (2) )
6
7 # Correlations

Page 44 of 49
Python for Artificial Intelligence
CHAPTER 12. MINI-PROJECT — STUDENT DATASET ANALYSIS

8 print ( ’\ n === CORRELATIONS WITH GRADE === ’)


9 num_cols = [ ’ study_hours ’ , ’ sleep_hours ’ , ’ absences ’ , ’ grade ’]
10 corrs = df [ num_cols ]. corr () [ ’ grade ’ ]. drop ( ’ grade ’)
11 print ( corrs . round (3) )
12
13 # Pass rate by major
14 print ( ’\ n === PASS RATE BY MAJOR === ’)
15 df [ ’ passed ’] = ( df [ ’ grade ’] >= 7.0) . astype ( int )
16 rate = df . groupby ( ’ major ’) [ ’ passed ’ ]. mean () * 100
17 print ( rate . round (1) . astype ( str ) + ’% ’)

12.5 Step 5 — Visualization

1 fig , axes = plt . subplots (2 , 2 , figsize =(14 , 10) )


2 fig . suptitle ( ’ Student Performance Analysis ’ ,
3 fontsize =16 , fontweight = ’ bold ’)
4
5 # Plot 1: Grade distribution
6 axes [0 , 0]. hist ( df [ ’ grade ’] , bins =20 , color = ’ steelblue ’ ,
7 edgecolor = ’ white ’ , alpha =0.8)
8 axes [0 , 0]. axvline ( df [ ’ grade ’ ]. mean () , color = ’ red ’ , linestyle = ’ -- ’ ,
9 label = f ’ Mean : { df [" grade "]. mean () :.1 f } ’)
10 axes [0 , 0]. set_title ( ’ Grade Distribution ’ , fontweight = ’ bold ’)
11 axes [0 , 0]. set_xlabel ( ’ Grade ’)
12 axes [0 , 0]. legend ()
13
14 # Plot 2: Grades by major
15 sns . boxplot ( data = df , x = ’ major ’ , y = ’ grade ’ , ax = axes [0 , 1])
16 axes [0 , 1]. set_title ( ’ Grades by Major ’ , fontweight = ’ bold ’)
17 axes [0 , 1]. set_xticklabels ( axes [0 , 1]. get_xticklabels () , rotation
=15)
18
19 # Plot 3: Study hours vs grade
20 colors_status = { ’ Passed ’: ’ green ’ , ’ Remediation ’: ’ orange ’ , ’ Failed
’: ’ red ’}
21 for status , color in colors_status . items () :
22 subset = df [ df [ ’ status ’] == status ]
23 axes [1 , 0]. scatter ( subset [ ’ study_hours ’] , subset [ ’ grade ’] ,
24 c = color , alpha =0.6 , label = status , s =40)
25
26 axes [1 , 0]. set_title ( ’ Study Hours vs Grade ’ , fontweight = ’ bold ’)
27 axes [1 , 0]. set_xlabel ( ’ Study Hours ’)
28 axes [1 , 0]. set_ylabel ( ’ Grade ’)
29 axes [1 , 0]. legend ()
30
31 # Plot 4: Status pie chart
32 counts = df [ ’ status ’ ]. value_counts ()
33 axes [1 , 1]. pie ( counts . values , labels = counts . index ,
34 autopct = ’ %1.1 f %% ’ , colors =[ ’ green ’ , ’ orange ’ , ’ red ’] ,
35 startangle =90 ,
36 wedgeprops ={ ’ edgecolor ’: ’ white ’ , ’ linewidth ’: 2})
37 axes [1 , 1]. set_title ( ’ Status Distribution ’ , fontweight = ’ bold ’)
38
39 plt . tight_layout ()
40 plt . savefig ( ’ student_analysis . png ’ , dpi =150 , bbox_inches = ’ tight ’)
41 plt . show ()
42

Page 45 of 49
Python for Artificial Intelligence
CHAPTER 12. MINI-PROJECT — STUDENT DATASET ANALYSIS

43 print ( ’ Analysis complete ! Chart saved to student_analysis . png ’)

≡ Chapter Summary

• Study hours have a positive correlation with the final grade.


• Absences have a negative correlation with performance.
• Each major shows a different grade distribution.
• Passed/failed groups can be visually separated (useful for clustering).
• These insights guide feature selection for ML models.

Page 46 of 49
Chapter 13

Best Practices in Python for Data Science

Good code is not just code that works — it is code that others (and your future self) can
understand, maintain, and extend.

13.1 Project Structure

1 # --- RECOMMENDED PROJECT STRUCTURE ---


2 # ml_project /
3 # | - - data /
4 # | - - raw / # original data ( never modify !)
5 # | - - processed / # cleaned and processed data
6 # ‘-- external / # data from external sources
7 # | - - notebooks / # Jupyter Notebooks for exploration
8 # | - - src / # reusable Python code
9 # | - - __init__ . py
10 # | | - - data . py # data processing functions
11 # | - - features . py # feature engineering
12 # | ‘-- models . py # model definitions
13 # | - - tests / # automated tests
14 # | - - requirements . txt # project dependencies
15 # ‘-- README . md # documentation

13.2 Comments and Docstrings

1 # --- Comment best practices ---


2
3 # Bad : comments the obvious
4 x = x + 1 # increments x by 1
5
6 # Good : explains the WHY
7 x = x + 1 # adjusts 0 - based index for 1 - based display
8
9 # --- DOCSTRINGS ( function documentation ) ---
10 def p rep ro ce ss_ da ta set ( df , target , scale = True ) :
11 ’’’
12 Prepares the DataFrame for ML model training .
13
14 Args :
15 df ( pd . DataFrame ) : input dataset
16 target ( str ) : name of the target column
17 scale ( bool ) : if True , normalizes features . Default : True
18

19 Returns :
20 X ( np . ndarray ) : feature matrix
21 y ( np . ndarray ) : target vector

Page 47
Python for Artificial Intelligence
CHAPTER 13. BEST PRACTICES IN PYTHON FOR DATA SCIENCE

22
23 Raises :
24 ValueError : if ’ target ’ column does not exist in the
DataFrame
25 ’’’
26 if target not in df . columns :
27 raise ValueError ( f ’ Column { target } not found ! ’)

13.3 Reproducibility

1 import numpy as np
2 import random
3
4 # ALWAYS set seeds for reproducible results !
5 SEED = 42
6 np . random . seed ( SEED )
7 random . seed ( SEED )
8

9 # For TensorFlow :
10 # import tensorflow as tf
11 # tf . random . set_seed ( SEED )
12
13 # For PyTorch :
14 # import torch
15 # torch . manual_seed ( SEED )
16
17 # Use constants for important values
18 LEARNING_RATE = 0.001
19 EPOCHS = 100
20 BATCH_SIZE = 32
21 TEST_RATIO = 0.2

13.4 Best Practices Checklist

◦ Before Finalizing Any Script or Notebook

□ Seeds defined for reproducibility


□ Imports organized at the top (stdlib → third-party → local)
□ Functions have descriptive docstrings
□ No magic numbers — use named constants
□ Error handling with try/except where needed
□ Original data not modified (work on copies)
□ Library versions documented in [Link]
□ README updated with usage instructions
□ Code tested with different input scenarios
□ Plots have clear titles, axis labels, and legends

Page 48 of 49
Python for Artificial Intelligence
CHAPTER 13. BEST PRACTICES IN PYTHON FOR DATA SCIENCE

• Tip

Use Black to automatically format your Python code:


pip install black then black my_script.py
Use isort to organize imports automatically:
pip install isort then isort my_script.py

13.5 Next Steps

Congratulations on completing this textbook! You now have the foundations needed to advance
to the next AI/ML topics:
1. Scikit-learn — library for classical Machine Learning (Regression, Classification, Cluster-
ing)

2. Statistics for Data Science — distributions, hypothesis testing, confidence intervals

3. Feature Engineering — creating and selecting features for models

4. Introduction to Neural Networks with TensorFlow/Keras or PyTorch

5. Natural Language Processing (NLP) with NLTK and spaCy

6. Computer Vision with OpenCV and convolutional networks

7. MLOps — deploying and monitoring models in production

◦ Recommended Resources
• Kaggle ([Link]) — datasets, competitions, and free courses
• [Link] — practical Deep Learning course (free)
• Google Colab — free Jupyter environment with GPU support
• Official docs: [Link], [Link], [Link]
• Hands-On Machine Learning (Aurélien Géron) — O’Reilly Media
• YouTube: 3Blue1Brown (Math), Sentdex (Python/ML), StatQuest (Statistics)

Happy coding, and good models!

Page 49 of 49

You might also like