AIBridge
Lecture 1
AIBridge
AI in Food Systems
WHAT IS AI/ML?
4
What can AI do
A High-Level View
6
Deep Learning
7
8
Our focus
Terminology alignment
Class Structure
Schedule
Schedule
Typical Practices in ML/Programming
Best Practices
Learning by Doing
Resources
INTRODUCTION TO PYTHON
Python
Lecture Outline
20
Control Flows
Logic
Variables
General Python Syntax
Google Colab
Follow along as we work through the Python language
21
Google Colab
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
Lecture Outline
23
Control Flows
Logic
Variables
General Python Syntax
Google Colab
24
General Python Syntax
Getting Started
Lecture Outline
Google Colab
25
General Python Syntax
Variables
Logic
Control Flows
26
var_a = 25
var_a = 70
print(var_a)
Variables
Overview
Good examples: datapoint_number, petal_width, ...
27
"3rd_variable"
"my variable"
Variables
Names
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
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
first_number = 1
second_number = 5
third_number = -3
30
Variables
Integer
petal_length = 3.5
petal_width = 4.0
pi = 3.14159265358
31
3.1415926
Floating (Decimal) Point
Variables
Floating-point
first_string = "s"
second_string = "string 2"
second_string = "another string"
32
Not this
Variables
String
first_boolean = True
second_boolean = False
33
Variables
Boolean
34
[a, b, c, d, e]
0 1 2 3 4
Variables
List
What does the following code output?
my_list = [21, 22, 23, 24, 25]
value = my_list[2]
print(value)
Variables
Self-test
What does the following code output?
my_list = [21, 22, 23, 24, 25]
value = my_list[2]
print(value)
Variables
Self-test
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
my_dictionary={'apple':'fruit', 'banana':'fruit', 'cabbage':'vegetable', 'dragonfruit':'fruit','eggplant':'vegetable'}
print(my_dictionary['cabbage'])
38
Variables
* Dictionary
my_float = float(my_string) #gives string in float form if possible
39
Variables
Type Conversion
+ | - | * | ** | / | // | % |
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
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
Lecture Outline
42
Control Flows
Logic
Variables
General Python Syntax
Google Colab
if statement_1:
Code segment 1
elif statement_2: # elif means else if
Code segment 2
else:
Code segment 3
43
Logic
Conditionals
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
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
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
== != < > <= >=
== 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
if 1 == 1 and 1 == 2:
code segment…
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
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
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
Lecture Outline
51
Control Flows
Logic
Variables
General Python Syntax
Google Colab
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
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
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
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
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
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
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
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
my_number = 0
while my_number < 6:
print(my_number)
my_number = my_number + 1
60
Control Flows
While
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