Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebValley 2022 Python Programming

WebValley 2022 Python Programming

Support Slides for the Python Programming Course for the FBK WebValley Summer School 2022 (https://webvalley.fbk.eu)

Valerio Maggio

July 20, 2022
Tweet

More Decks by Valerio Maggio

Other Decks in Programming

Transcript

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

    View Slide

  2. Materials
    Shout out to Giuseppe Mastrandrea

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

    View Slide

  3. View Slide

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

    View Slide

  5. 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

    View Slide

  6. 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!

    View Slide

  7. 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

    View Slide

  8. How?

    View Slide

  9. Let’s clarify a bit!

    View Slide

  10. 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

    View Slide

  11. From Problem to Coding
    Through Algorithm

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. 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?)

    View Slide

  16. 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?)

    View Slide

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

    View Slide

  18. 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?)

    View Slide

  19. 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

    View Slide

  20. 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

    View Slide

  21. Task 2
    2.a Pokémon Dataset

    View Slide

  22. 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?)

    View Slide

  23. 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?)

    View Slide

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

    View Slide

  25. 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?)

    View Slide

  26. 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)

    View Slide

  27. 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

    View Slide

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

    View Slide

  29. 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

    View Slide

  30. 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

    View Slide

  31. Task 2
    2.b Similarity Function

    View Slide

  32. 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:

    View Slide

  33. T2.b - De
    f
    ine Similarity Function

    View Slide

  34. 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?)

    View Slide

  35. Similarity Data Structure

    View Slide

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

    View Slide

  37. 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?)

    View Slide

  38. Top K Results

    View Slide

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

    View Slide

  40. 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?)

    View Slide

  41. 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

    View Slide

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

    View Slide