1 of 61

AIBridge

Lecture 1

2 of 61

AIBridge

  • Bridge the gap between AI and [your choice]
  • 1st camp at UC Davis in June 2022, 2nd in Silicon Valley in March 2023, 3rd at Cornell in July 2023
  • Acquire basics: Python, basic ML algorithms, toolbox usage
  • Enable further learning
  • Enable easier communications and collaborations

  • AIFS - NSF/USDA AI Institute for Next Generation Food Systems

3 of 61

AI in Food Systems

  • Molecular breeding
    • Help breeders to run more efficient and targeted breeding programs
  • Agricultural production
    • Crop yield sensing and forecasting
    • Water and nitrogen stress sensing, prediction, accusation
  • Food processing
    • Tomato processing loss prediction
    • Sanitation classification
  • Nutrition
    • Use food photo and text to predict core ingredients
    • Dietary recommendation

4 of 61

WHAT IS AI/ML?

4

5 of 61

What can AI do

6 of 61

A High-Level View

6

7 of 61

Deep Learning

7

8 of 61

8

9 of 61

Our focus

10 of 61

Terminology alignment

  • Sample = (features, label)
  • Features: independent variables, attributes, predictors, input variables, input, covariates, explanatory variables, treatment variables,
  • Label: dependent variable, outcome, target variable, outcome variable, response variable
  • Samples: cases, observations, individuals, participants, data points

  • If you have other names for these, please let me know.

11 of 61

Class Structure

  • Two blocks a day
  • A block : Lecture + break + lab
    • Lab is the best part of this bootcamp
  • Recap
    • Overview of key knowledge points
    • Feedback from you (pace, clarity, etc.)
  • Learning by doing
    • Iris dataset
    • Wine dataset
  • Go through the process to complete a basic ML project

12 of 61

Schedule

  • Python: 1.5 days
    • Condensed with a focus on what we need for ML
  • ML: 3.5 days
    • Intuitions, toolbox, ML flow
  • Friday afternoon: Project presentation

13 of 61

Schedule

14 of 61

Typical Practices in ML/Programming

  • Find a sample
  • Read through it
  • Try it
  • Modify it
  • Google it

  • Basic skills to do these and practice them

15 of 61

Best Practices

  • Ask questions
  • Type along during lectures
  • Ask for help
  • Make good use of labs
  • Provide feedback

16 of 61

Learning by Doing

  • Iris
  • Wine
  • Your own on Day 5 PM

17 of 61

Resources

18 of 61

INTRODUCTION TO PYTHON

 

19 of 61

Python

  • Python is a popular programing language
  • Guido van Rossum, Dutch programmer, invented in late 1980s
  • Widely used in industry and academia, especially for ML applications.
  • R vs Python
    • Python better at large data amounts and machine learning

 

20 of 61

Lecture Outline

20

Control Flows

Logic

Variables

General Python Syntax

Google Colab

21 of 61

  • https://colab.research.google.com/
  • Stores everything on Google Drive
  • Can be shared with others and across devices
  • No setup required
  • Most packages/libraries preinstalled

  • Colab Pro

Follow along as we work through the Python language

21

Google Colab

22 of 61

22

Write code inside “cells”

Run a cell by clicking this button

Add new cells by clicking “+ Code”

Delete cells by clicking this button

Google Colab

23 of 61

Lecture Outline

23

Control Flows

Logic

Variables

General Python Syntax

Google Colab

24 of 61

  • Comments allow sections of the code to be more readable
    • Anything after a “#” is a comment
    • # I am a comment!

  • Functions take in inputs and give outputs
    • print(input)
      • The print function prints out the input
    • print("hello world")

24

General Python Syntax

Getting Started

25 of 61

Lecture Outline

Google Colab

25

General Python Syntax

Variables

Logic

Control Flows

26 of 61

  • A variable is a reserved place in memory (think: container) which can store a value
    • Creating variables: variable_name = value
  • Can be used anywhere after its assignment, but never before
  • Can re-assign values as needed
  • 7 types of values: Integer, Floating-point, String, Boolean, List, Tuple, and Dictionary
    • (More details about each type coming up in next slides)

26

var_a = 25

var_a = 70

print(var_a)

Variables

Overview

27 of 61

  • Cannot start with a number
  • Cannot include spaces
  • Cannot be a keyword: https://www.w3schools.com/python/python_ref_keywords.asp
  • Should be descriptive
  • *Good practice: all lowercase with underscores for spacing

Good examples: datapoint_number, petal_width, ...

27

"3rd_variable"

"my variable"

Variables

Names

28 of 61

  1. 70 ⇒ because the value of variable_b is set to be 70 in the second line
  2. 40 ⇒ because the value of variable_b is set to be the same as variable_a which is 40
  3. 25 ⇒ because the value of variable_b is set to be the same as variable_a which is 25

What does the following code output?

variable_a = 25

varaible_b = 70

variable_a = 40

variable_b = variable_a

print(variable_b)

Variables

Self-test

29 of 61

  1. 70 ⇒ because the value of variable_b is set to be 70 in the second line
  2. 40 ⇒ because the value of variable_b is set to be the same as variable_a which is 40
  3. 25 ⇒ because the value of variable_b is set to be the same as variable_a which is 25

What does the following code output?

variable_a = 25

varaible_b = 70

variable_a = 40

variable_b = variable_a

print(variable_b)

Variables

Self-test

30 of 61

  • Non-fractional number
  • Positive or negative
  • No maximum or minimum practically

first_number = 1

second_number = 5

third_number = -3

30

Variables

Integer

31 of 61

  • “Float”
  • Decimal point number
  • Accurate within 2-55

petal_length = 3.5

petal_width = 4.0

pi = 3.14159265358

31

3.1415926

Floating (Decimal) Point

Variables

Floating-point

32 of 61

  • A string of characters
  • Put in quotations " " or ' '
  • *Block string (multi-line string): three quotation marks
  • *Special character (new line): "\n"

first_string = "s"

second_string = "string 2"

second_string = "another string"

32

Not this

Variables

String

33 of 61

  • True or False (capitalize)

first_boolean = True

second_boolean = False

33

Variables

Boolean

34 of 61

  • A list of values
    • my_list = [value_1, value_2, ...]
    • example_list = [5, 20, 11, 3, 10]
    • Can include multiple different data types
      • multi_type_list = ["hello world", True, 5]

34

 [a, b, c, d, e]

0       1       2       3      4

   

    

  • For a specific value in the list: my_list[index]
    • The index of the 1st item is 0,
    • a_value = my_second_list[2] # gets the THIRD value in the list
    • There is also negative indexing (index of -1 gets last element, -2 gets second from last, etc.)

Variables

List

35 of 61

What does the following code output?

  1. 22 ⇒ because value is set to the second item in the list
  2. 23 ⇒ because value is set to the third item in the list

my_list = [21, 22, 23, 24, 25]

value = my_list[2]

print(value)

Variables

Self-test

36 of 61

What does the following code output?

  1. 22 ⇒ because value is set to the second item in the list
  2. 23 ⇒ because value is set to the third item in the list

my_list = [21, 22, 23, 24, 25]

value = my_list[2]

print(value)

Variables

Self-test

37 of 61

  • Works the same as a list, but can’t be changed
  • Can contain multiple different data types

my_first_tuple = (object_1, object_2, ...)

my_second_tuple = (22, "hello!", True, 3.1415)

a_value = my_second_tuple[2] # gets the THIRD value in the tuple

37

Variables

* Tuple

38 of 61

  • A list of values with custom keys that are indices, like a list but indices are keys and not positions

my_dictionary={'apple':'fruit', 'banana':'fruit', 'cabbage':'vegetable', 'dragonfruit':'fruit','eggplant':'vegetable'}

print(my_dictionary['cabbage'])

38

Variables

* Dictionary

39 of 61

  • Types: int, float, str, bool, list, tuple
  • Convert types of variables to other types

my_float = float(my_string) #gives string in float form if possible

  • Compatible types:
    • int → float
    • float → int (always rounds down)
    • str → int
    • str → float
    • *[most types] → string
    • *list → tuple
    • *boolean → int/float (0 → False, anything else → True)
    • *str → list/tuple (only converts str to list/tuple of single characters)

39

Variables

Type Conversion

40 of 61

+

-

*

**

/

//

%

Addition

Subtraction

Multiplication

Exponentiation

Division

(turns int to float)

Floor Division

(rounds down the quotient)

Modulus

(returns the remainder)

x + y

1 + 2 == 3

x - y

2 – 1 == 1

x * y

2 * 3 == 6

x ** y

2 ** 3 == 8

x / y

8 / 2 == 4.0

x // y

9 // 4 == 2

x % y

10 % 4 == 2

Note: the double equal sign a == b is used to check for equality instead of assigning variables

Variables

Basic Arithmetic Operations

41 of 61

x = 4

x = x + 1

# x becomes 5

Changing a variable’s value:

x = 4

x = x - 2

# x becomes 2

x = 4

x = x * 2

# x becomes 8

Variables

Basic Arithmetic Operations

42 of 61

Lecture Outline

42

Control Flows

Logic

Variables

General Python Syntax

Google Colab

43 of 61

if statement_1:

Code segment 1

elif statement_2: # elif means else if

Code segment 2

else:

Code segment 3

43

Logic

Conditionals

44 of 61

x =

y =

if x == y:

print('x is equal to y')

elif x > y:

print('x is greater than y')

else:

print('x is less than y')

Logic

Conditionals

1

1

45 of 61

x = 4

y =

if x == y:

print('x is equal to y')

elif x > y:

print('x is greater than y')

else:

print('x is less than y')

Logic

Conditionals

1

46 of 61

x = 4

y =

if x == y:

print('x is equal to y')

elif x > y:

print('x is greater than y')

else:

print('x is less than y')

Logic

Conditionals

10

47 of 61

==    !=    <    >    <=    >=

== True if the two sides are exactly the same (1 == 1 is True)

!=   True if the two sides are NOT the same (2 != 1 is True)

47

Logic

Conditionals

48 of 61

  • and: only runs if both are True

if 1 == 1 and 1 == 2:

code segment…

  • or: runs if at least one of them are True

if 1 == 1 or 1 == 2:

code segment…

48

x = 4

y = 4

if x < y or x == y:

  print("x is less than or equal to y")

Logic

Conditionals

49 of 61

Which of these conditions are successfully passed?

petal_width = 1.8

petal_length = 3.5

if petal_width < 3 or petal_length < 3:

print("condition 1 passed")

if petal_width < 3 and petal_length < 3:

print("condition 2 passed")

if petal_width < 3:

if petal_length < 3:

print("condition 3 passed")

Logic

Self-Test

50 of 61

petal_width = 1.8

petal_length = 3.5

if petal_width < 3 or petal_length < 3:

print("condition 1 passed")

if petal_width < 3 and petal_length < 3:

print("condition 2 passed")

if petal_width < 3:

if petal_length < 3:

print("condition 3 passed")

Which of these conditions are successfully passed?

Logic

Self-Test

51 of 61

Lecture Outline

51

Control Flows

Logic

Variables

General Python Syntax

Google Colab

52 of 61

We have this very large list of 11 words: 

How do we access and print out every word?

52

word_list = ["Lorem", "ipsum", "dolor", "sit", "amet", "fusce", "rhoncus", "mi", "viverra", "velit", "mattis"]

Control Flows

Hypothetical Scenario

53 of 61

print(word_list[0])

print(word_list[1])

print(word_list[2])

print(word_list[3])

print(word_list[4])

print(word_list[5])

print(word_list[6])

print(word_list[7])

print(word_list[8])

print(word_list[9])

print(word_list[10])

Horribly inefficient

A lot of tedious manual coding

Completely unscalable (what if there were 70 words)

53

word_list = ["Lorem", "ipsum", "dolor", "sit", "amet", "fusce", "rhoncus", "mi", "viverra", "velit", "mattis"]

Control Flows

Hypothetical Scenario

54 of 61

Only difference between all these lines is the index

54

print(word_list[0])

print(word_list[1])

print(word_list[2])

print(word_list[3])

print(word_list[4])

print(word_list[5])

print(word_list[6])

print(word_list[7])

print(word_list[8])

print(word_list[9])

print(word_list[10])

Control Flows

Hypothetical Scenario

55 of 61

  • How to use:  for iterator in iterable:
    • String, list, range, etc. iterable
    • Need indentation

55

for number in range(0, 11): #range goes through 0, 1, 2, … 10

  #this loop repeats 11 times and number changes to each number

  print(word_list[number])

word_list = ["Lorem", "ipsum", "dolor", "sit", "amet", "fusce", "rhoncus", "mi", "viverra", "velit", "mattis"]

Control Flows

For loops

56 of 61

for word in word_list:

  #this loop does the exact same thing but with less typing

  print(word)

56

for number in range(0, 11): #range goes through 0, 1, 2, … 10

  #this loop repeats 11 times and number changes to each number

  print(word_list[number])

word_list = ["Lorem", "ipsum", "dolor", "sit", "amet", "fusce", "rhoncus", "mi", "viverra", "velit", "mattis"]

Control Flows

For loops

57 of 61

word_list = ["Lorem", "ipsum", "dolor", "sit", "amet", "fusce", "rhoncus", "mi", "viverra", "velit", "mattis"]

57

Output:

for word in word_list:

  #this loop does the exact same thing but with less typing

  print(word)

Control Flows

Self-test

58 of 61

a.

for word in big_list:

print(word)

b.

for i in range(9):

print(big_list[i])

c.

for word in big_list:

print(big_list[word])

Which of the following code blocks will print out everything in the list?

big_list = ["Lorem", "Ipsum", "Dolor", "Sit", "Amet", "Consectetur", "Adipiscing", "Elit", "Sed"]

Control Flows

Self-test

59 of 61

Which of the following code blocks will print out everything in the list?

big_list = ["Lorem", "Ipsum", "Dolor", "Sit", "Amet", "Consectetur", "Adipiscing", "Elit", "Sed"]

a.

for word in big_list:

print(word)

b.

for i in range(9):

print(big_list[i])

c.

for word in big_list:

print(big_list[word])

Control Flows

Self-test

60 of 61

  • How to use: while statement:
    • The loop repeats as statement is true
    • Needs indentation

my_number = 0

while my_number < 6:

print(my_number)

my_number = my_number + 1

60

Control Flows

While

61 of 61

a_list = [3, 22, 1, 73, 40, 3, 19]

sum = 0

for i in range(0, 7):

sum = sum + a_list[i]

sum = sum / 2.4

sum = sum * -1

print(a_list[i])

print(sum)

Inside loop because of indentation

(tab)

61

Don’t worry about what this code does.

Control Flows

Indentation