Slide 1

Slide 1 text

Python Programming Class Valerio Maggio (he/him/his) @leriomaggio [email protected]

Slide 2

Slide 2 text

Materials Shout out to Giuseppe Mastrandrea 
 (GH @giuseppemastrandrea) • Lecture materials for this class has been adapted from https://github.com/WebValley2022/python-programming

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Introduction The Programming Game The Program 👩💻 🧑💼 The Programmer The User creates uses generates Results

Slide 5

Slide 5 text

Introduction The Programming Game The Program 👩💻 🧑💼 The Programmer The User uses 
 (i.e. coding) provides generates Output data The WebValley Team Input data • So a Program: • asks the user for some input data • makes some computations • returns a value or a series of values passed to Programming Language to create

Slide 6

Slide 6 text

Quick Check and Recap • Q: What is Python? • A: Programming language • Q: What is a program? • A: A program is a software that resolves a particular task/problem • Key Components: • User • Input data • Output data • (soon) Algorithm!

Slide 7

Slide 7 text

The Problem What we’ll work on for the next hours • We will implement an innovative Pokémon Search Engine • iow, our software program will look for the K top Pokémon that are most similar to some input Pokémon characteristics (i.e. the search query) • The search query (i.e. input data to our program) will be some characteristics (numbers) denoting a Pokémon

Slide 8

Slide 8 text

How?

Slide 9

Slide 9 text

Let’s clarify a bit!

Slide 10

Slide 10 text

The Problem • Q: What do we mean by "most similar to the search query” ? • Q: What kind of data can a user provide to our program ? • Q: (once we’ve clari f ied this) 
 what are the instructions to actually solve the problem? • The problem is: 
 “(somehow) determine what Pokémons are most similar to a speci f ied set of characteristics denoting a Pokémon provided by the user.” • There's a lot of ambiguity in what we just said

Slide 11

Slide 11 text

From Problem to Coding Through Algorithm

Slide 12

Slide 12 text

The Algorithm • The algorithm is the heart of our software • Input Data (search query given by the user). A series of numbers representing a Pokémon: • HP (Hit Points) • Speed • Attack • Defense • Special Attack • Special Defence • The search query then will be a series of numbers • Based on those numbers, the program will return (output data) A list of K Pokémons • K is a parameter of the algorithm

Slide 13

Slide 13 text

The Program Pokémon Search Engine K 
 (Parameter) HP (Hit Points) Speed Attack Defense Special Attack Special Defence Input Data // Search Query // Pokémon The Program

Slide 14

Slide 14 text

How to Proceed? • Breaking down a problem into many small sub-problems (i.e. Tasks) • For example: 
 Problem: “start from a bunch of numbers and extract K Pokémons according to certain similarity measure” • What will our algorithm do? • Search for the K Pokèmon with the highest “similarity” • Breakdown into Tasks • Compare the data inserted by the user and those of existing Pokémon • Generalisation -> compare any pair of Pokémon • Sort the results of the comparison • Return the top K

Slide 15

Slide 15 text

How to Proceed? • Task 1: • Ask the user for Pokémon data and store them accordingly • Task 2: • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 16

Slide 16 text

Let’s get cracking! • Task 1: • Ask the user for Pokémon data and store them accordingly • Task 2: • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 17

Slide 17 text

T1 - Ask the user for Pokémon data and store them accordingly

Slide 18

Slide 18 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 19

Slide 19 text

Task 2 2 Similarity Function • What can we use to compare two series of numbers? • A Pokémon with these scores (user's inputs) should be compared with all the other existing Pokémons • So.. time to acquire some Pokémon Data • CSV f ile (Comma Separated Values) with all data from all generations of Pokemon • Task 2.a.1: Read and Store Pokémon Data

Slide 20

Slide 20 text

Get Pokemon Dataset • I am going to add the data to the repository • Q: What’s the git command I am going to type in ? • Once done, here is how to get a copy of the data • git pull origin main

Slide 21

Slide 21 text

Task 2 2.a Pokémon Dataset

Slide 22

Slide 22 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 23

Slide 23 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 24

Slide 24 text

T2.a - Read all Pokémon Data and Store them accordingly

Slide 25

Slide 25 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 26

Slide 26 text

Task 2 2.b Similarity Function 30 43 55 68 78 39 45 58 60 80 Bulbasaur Ivysaur Venusaur Chameleon Chameleon Charizard • What function to get a similarity degree? • Hypothesis: let's start from just 1 feature (e.g., hit points)

Slide 27

Slide 27 text

Task 2.b Introducing Tuxemon 30 43 55 68 78 39 45 58 60 80 Bulbasaur Ivysaur Venusaur Charmeleon Chameleon Charizard Tuxemon 64 K=1: Ivysaur K=2: Ivysaur, Charmeleon K=3: Ivysaur, Charmeleon, Charizard

Slide 28

Slide 28 text

Task 2.b Introducing Tuxemon 64 64 64 64 64 64 19 4 16 25 6 14

Slide 29

Slide 29 text

Task 2 2.b Similarity Function • Let's add another attribute (attack) • We still have to compute a distance • In this case we calculate the distance on a 2D plane 40 53 65 78 90 30 43 55 68 80 54, 55 78, 84 58, 64 39, 52 80, 82 60, 62 45, 49

Slide 30

Slide 30 text

Task 2 2.b Similarity Function • Euclidean distance • We use Pythagoras Theorem • The distance is the green line (hypotenuse) of the triangle in f igure

Slide 31

Slide 31 text

Task 2 2.b Similarity Function

Slide 32

Slide 32 text

Task 2 2.b Similarity Function Adding more axes on our chart makes our formula slightly more complex 3 Dimensions (HP, Attack, Defence): 4 Dimensions (HP, Attack, Defence, Special Attack): n Dimensions:

Slide 33

Slide 33 text

T2.b - De f ine Similarity Function

Slide 34

Slide 34 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 35

Slide 35 text

Similarity Data Structure

Slide 36

Slide 36 text

T2.c - Compute the Similarity score of search query and each Pokémon

Slide 37

Slide 37 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 38

Slide 38 text

Top K Results

Slide 39

Slide 39 text

T2.d - Return the top K most similar Pokémon

Slide 40

Slide 40 text

Let’s get cracking! • Task 1: ✅ • Ask the user for Pokémon data and store them accordingly • Task 2: ✅ • (+1) Read all Pokémon data and store accordingly • De f ine the Pokémon Similarity Function • Compute similarity between the search query and existing Pokémon • Return the top K most similar Pokémon • Task 3: • Return and Show algorithm’s output • (what will be the algorithm output?)

Slide 41

Slide 41 text

Bonus Track • Visualise Results: • Using Jupyter we can show images of the K nearest Pokémon! • Visual feedback • Images taken from https://veekun.com/dex/downloads and rescaled

Slide 42

Slide 42 text

Any Question? Valerio Maggio (he/him/his) [email protected] @leriomaggio