I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
Chained conditionals allow you to check several conditions. Only one block of code will ever run, though.
To run a chained conditional, you use if/elif/else syntax. You can use as many elifs as you want.
if (condition1):
    run_this_code1
elif (condition2):
    run_this_code2
elif (condition3):
    run_this_code3
else:
    run_this_code4

You are not required to have an else block.
if (condition1):
    run_this_code1
elif (condition2):
    run_this_code2

Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
Can you write Python code for it?

did_homework = True
took_out_trash = True
cleaned_room = False
allowance = 0
if cleaned_room:
    allowance = 10
elif took_out_trash:
    allowance = 5
elif did_homework:
    allowance = 4
else:
    allowance = 2
print(allowance)


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
Comparison Operators
You can compare values together and get a boolean result
| Operator  |       Meaning|
|------|------|
| x == y      |        x equal to y |
| x != y       |        x not equal to y |
| x >  y      |        x greater than y |
| x < y         |       x less than y |
| x >= y      |        x greater than or equal to y |
| x <= y       |        x less than or equal to y |
| x is  y      |        x is the same as y |
| x is not  y   |      x is not the same as y |
By using the operators in an expression the result evaluates to a boolean. x and y can be any type of value
Can you write Python code for it?

print(5 == 6)

print("Coke" != "Pepsi")

# You can compare to variables too
allowance = 5

print(5 >= allowance)

print(allowance is True)


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
TRY IT
Check if did_homework is true, if so, print out "You can play a video game", otherwise print out "Go get your backpack"
Logical Operators
Logical operators allow you to combine two or more booleans. They are and, or, not
and Truth table  (only true if both values are true)
| val1  |  val2  |  val1 and val2 |
|-- |------|------|
| true  |  true  |  true |
| true  |  false |  false |
| false |  true  |  false |
| false |  false |  false |
or  Truth table  (true if at least 1 value is true)
| val1  |  val2  |  val1 or val2 |
|------|------|------|
| true  |  true  |  true |
| true  |  false |  true |
| false |  true  |  true |
| false |  false |  false |
not Truth table (the opposite of the value)
| val1  |  not val1 |
|------|------|
| true  |  false |
| false |  true |
Can you write Python code for it?

print(True and True)

print(False or True)

print(not False)


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
TRY IT
See if 5.0000001 is greater than 5
Conditional Execution
We can write programs that change their behavior depending on the conditions.
We use an if statement to run a block of code if a condition is true. It won't run if the condition is false.
if (condition):
    code_to_execute # if condition is true

In python indentation matters. The code to execute must be indented (4 spaces is best, though I like tabs) more than the if condition.
Can you write Python code for it?

# cleaned_room is true
if cleaned_room:
    print("Good kid! You can watch TV.")

# took_out_trash if false
if took_out_trash:
    print("Thank you!")

print(took_out_trash)


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
TRY IT
Try converting the string 'candy' into an integer. If there is an error, print "What did you think would happen?"
Short-circuit evaluation  of logical expressions
Python (and most other languages) are very lazy about logical expressions. As soon as it knows the value of the whole expression, it stops evaluating the expression.
if (condition1 and condition2):
    run_code

In the above example, if condition1 is false then condition2 is never evaluated.
Can you write Python code for it?

if (1 < 2) or (5/0):
    print("How did we do that?")


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
This can be useful when evaluating a user's input, to make sure it is what you expected.
Can you write Python code for it?

inp = input('Enter Fahrenheit Temperature:')
try:
    fahr = float(inp)
    cel = (fahr - 32.0) * 5.0 / 9.0
    print(cel)
except:
    print('Please enter a number')


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
You can include more than one statement in the block of code in the if statement. You can tell python that this code should be part of the if statement by indenting it. This is called a 'block' of code
if (condition):
    statement1
    statement2
    statement3

You can tell python that the statement is not part of the if block by dedenting  it to the original level
if (condition):
    statement1
    statement2
statement3 # statement3 will run even if condition is false
Can you write Python code for it?

# cleaned_room is true
if cleaned_room:
    print("Good job! You can watch TV.")
    print("Or play outside")

# took_out_trash is false
if took_out_trash:
    print("Thank you!")
    print("You are a good helper")
print("It is time for lunch")


I am working on the file "Lesson02_Conditionals/Conditional Execution.ipynb".
The first task is:
You can use the logical operators in if statements
Can you write Python code for it?

cleaned_room = True
took_out_trash = False
if cleaned_room and took_out_trash:
    print("Let's go to Chuck-E-Cheese's.")
else:
    print("Get to work!")

if not did_homework:
    print("You're going to get a bad grade.")


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
TRY IT
Create a function called pet that takes a parameter called animal. The function should print out 'Petting the ' + animal
Return
Python provides the return keyword to allow you to use a value that your function produces.
When you use return, you can assign the result of a function to a variable or use the function anywhere you would use that value.
When you return in a function, none of the additional lines in the function executes
def myfunction():
    return "Hello"

greeting = myfunction()
print(myfunction() + " , world")
Can you write Python code for it?

def cost_of_feed(type_of_feed):
    cost = None
    if type_of_feed == 'oats':
        cost = 20
    elif type_of_feed == 'corn':
        cost = 5
    elif type_of_feed == 'grass':
        cost = 0
    return cost

oats_cost = cost_of_feed('oats')

# Now I can use this value
oats_for_farm = 150 * oats_cost
print("It costs $" + str(oats_for_farm) + " to feed 150 horses")


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
TRY IT
Create a string with your name and store in a variable called name
Then find and print the length and max letter of name.
Type Conversion Functions
Some of the built in functions are to convert values of one type into another.
int(value) -> converts to int
float(value) -> converts to float
str(value) -> converts to string
bool(value) -> converts to boolean

If the value can't be converted, the function will throw an error
Can you write Python code for it?

# Integer
int('5')

# float
float(5)

# string
str(5)

#boolean
bool(1)

# Uh-Oh - python doesn't do number words
int('five')


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
TRY IT
Write a function called ranch() that prints out your favorite farm animal and the noise it makes. Call your function.
Parameters and arguments
You can have your function take one or more variables as input, these are called arguments. Arguments are assigned to parameters (named variables). You can use the parameters inside the body of the function
def myfunc(parameter1, parameter2):
    print(parameter1)

myfunc('hello', 'goodbye')

Giving the function different arguments can change the way it behaves.
Can you write Python code for it?

def feed_animal(type_of_feed):
    if type_of_feed == 'oats':
        print("Feeding the horse")
    elif type_of_feed == 'hay':
        print("Feeding the cows")
    else:
        # pigs eat anything
        print("Feeding the pigs")

feed_animal('hay')

feed_animal('apple')


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
There is also a function to generate a random integer.
randint(low, high) generates a random number between low and high (including both).
Can you write Python code for it?


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
You can also get help on functions within the modules
Can you write Python code for it?


I am working on the file "Lesson03_Functions/Functions.ipynb".
The first task is:
You can call your function within another function
Can you write Python code for it?

def cowgirl():
    yeehaw()
    print("I'm gonna lasso me some cattle")
    yeehaw()

# Got to run it
cowgirl()


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
By the way, at this point you could go back to the previous lesson's project Yahtzee and use a loop to play until you get a yahtzee.
Range
Python has a built in function called range() that returns a generator of integers in order
 # If you only include 1 parameter it starts at zero and goes to that number-1
 range(5) #-> [0, 1, 2, 3, 4]
 # If you include 2 parameters it goes from [start, stop)
 range(2,5) #-> [2,3,4]
 # If you include 3 parameters it goes from [start, stop) and skips by the third number
 range(1,10,2) #-> [1,3,5,7,9]

What is a generator? Well it is a list that only stores one item at a time. We can just make it a list by using type casting list(generator). You can use generators without casting them to lists when you are in a loop.
Can you write Python code for it?

cumulative_snowfall = list(range(1, 15))
print(list(cumulative_snowfall))


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
For loops
For loops are a faster way of writing a common loop pattern. Many loops go through each element in a list. Python makes this easy.
for item in list:
    run code for item
Can you write Python code for it?

# Let's rewrite the stock up loop as a for loop (we'll add the puppies in later)
stock_up = ['eggs', 'milk', 'bread', 'puppies', 'cereal', 'toilet paper']
for item in stock_up:
    print("Storm is coming, better stock up on " + item)


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
TRY IT
Create a list of famous storms and store it in a variable called storms.
Break and Continue
There are other ways to control a loop besides its condition.
The keyword break immediately exits a loop. With break you can write a loop without a terminating condition that is not an 'infinite loop'.
Continue stops that iteration of the loop and goes back to the condition check.
Can you write Python code for it?

while True:
    user_message = eval(input('> '))
    # Leave when the user talks about snow.
    if user_message == 'snow':
        print("I don't want to hear about that.")
        break
    print("and then what happened?")

stock_up = ['eggs', 'milk', 'bread', 'puppies', 'cereal', 'toilet paper']
index = 0
while index < len(stock_up):
    item = stock_up[index]
    index += 1
    # Make sure we don't stock up on puppies
    if item == 'puppies':
        print("No! We are not getting another puppy.")
        continue
    print("Storm is coming, better stock up on " + item)


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
TRY IT
Don't try it. Infinite loops aren't good code.
Lists
A list is a sequence of values. These values can be anything: strings, numbers, booleans, even other lists.
To make a list you put the items separated by commas between brackets [ ]
Can you write Python code for it?

weathers = ['rain', 'sun', 'snow']
tens = [10, 10, 10, 10]
print(weathers)
print(tens)


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
TRY IT
Write a loop that find the maximum word length of a list of words (provided).
Can you write Python code for it?

words = ['Oh', 'the', 'weather', 'outside', 'is', 'frightful', 'But', 'the', 'fire', 'is', 'so', 'delightful']


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
TRY IT
Write out a while loop that prints out the values 1 - 5
Infinite loops
One thing you need to be careful of with while loops is the case where the condition never turns false. This means that the loop will keep going and going forever. You'll hear your computer's fan heat up and the program will hang. If you are doing any significant amount of programming, you will write an infinite loop, it cannot be helped. But watch out for them anyways.
HINT To stop an infinite loop in a jupyter notebook select Kernel->Restart from the menu above.
Can you write Python code for it?

snowflakes = 0
# This condition is never false so this will run forever
while (snowflakes >= 0):
    snowflakes += 1

# If you ran this by accident, press the square (stop) button to kill the loop

# The other common infinite loop is forgetting to update to a counting variable
count = 0
while (count < 3):
    print("Let it snow!")


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
We can use loops to sum, average, or find the max or min
I'll give you some examples
Can you write Python code for it?

hourly_snow_fall = [4, 3, 2, 4, 1, 4, 3, 2, 1, 1, 1, 0]

# Finding the sum
total_snow = 0
for hour in hourly_snow_fall:
    total_snow += hour
print("Total snow " + str(total_snow))

# Finding the average
total_snow = 0
number_snowy_hours = 0
for hour in hourly_snow_fall:
    if hour == 0:
        # Ignore hours that it didn't snow
        continue
    total_snow += hour
    number_snowy_hours += 1
ave_snow = total_snow / number_snowy_hours
print("Average snow " + str(ave_snow))

# Finding the max
max_snow = 0
for hour in hourly_snow_fall:
    if max_snow < hour:
        max_snow = hour
print("Max snow was " + str(max_snow))


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
You can access a single element in a list by indexing in using brackets. List indexing starts at 0 so to get the first element, you use 0, the second element is 1 and so on.
list[index]
Can you write Python code for it?


I am working on the file "Lesson04_Iteration/Iterations.ipynb".
The first task is:
You can find the length of a list using the built-in function 'len'.
len(list)
Can you write Python code for it?


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
Be careful with length, it is the number of characters, not the last index.
The last index is len(string) - 1
Can you write Python code for it?

zlen = len(chinese_zodiac)
# WRONG
print(chinese_zodiac[zlen])
# RIGHT
print(chinese_zodiac[zlen - 1])


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
But actually you can use negative indexing to get the last character in a string. -1 is the last character, -2 is the second to last and so on.
Wondering why negative indexing starts with -1 and not 0? It's because -0 and 0 are the same thing, so you would just get the first character.
Can you write Python code for it?


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
Formatting
String concatenation gets old really fast, and casting numbers and booleans as strings does too. Luckily, there is a better option.
String formatting allows you to include variables directly in you string.
'string {var} '.format(var1)

Each parameter passed to format has an index and can be accessed in the string using {idx}. They don't have to be in order and can be repeated
Can you write Python code for it?

print('The {0} has {1} toes per limb and thus is considered {2}'.format('ox', 4, 'yin'))
print('The {0} has {1} toes per limb and thus is considered {2}'.format('tiger', 5, 'yang'))

# I learned something when creating this notebook


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
String Methods
There are several built in methods you can use on your strings. To find them all use dir(str).
'my string'.method_name(params)
Can you write Python code for it?


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
Strings and in
The in operator checks if a substring is in a string. It returns a boolean.
'substring' in 'string'

Hint case matters
Can you write Python code for it?

print('Cat in zodiac:')
print('Cat' in chinese_zodiac)

print('Dragon in zodiac:')
print('Dragon' in chinese_zodiac)


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
TRY IT
Change the case of your elements string to upper case and then split the result on spaces (' ')
 Challenge  do this by chaining string methods
Parsing Strings
You can use string methods and indexing to get exactly the substring you want.
Can you write Python code for it?

# Lets find the zodiac animals between Ox and Monkey
ox_idx = chinese_zodiac.find('Ox')
monkey_idx = chinese_zodiac.find('Monkey')

print(chinese_zodiac[ox_idx:monkey_idx])

# Wait, I wanted to exclude Ox
ox_end = ox_idx + len('Ox ')
print(chinese_zodiac[ox_end:monkey_idx])


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
TRY IT
Create a string with the 5 elements of the Chinese zodiac (wood fire earth metal water) and store it in a variable called elements. Get the 4th letter of elements.
You can get the length (number of characters) of a string by using the len operator
len(my_string)
Can you write Python code for it?


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
TRY IT
Create a string with your name and store it in a variable called name. Print the last character of your name using both indexing methods (positive and negative).
String Slices
You can take more than a single character; you can take a whole slice. To take a slice, give the first index and then the last index + 1. (The first index is inclusive, second index is exclusive)
string[0:5]
Can you write Python code for it?

second_animal = chinese_zodiac[4:6]
print(second_animal)


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
TRY IT
What happens when you omit both indices? Try it on the chinese_zodiac.
Immutable strings
Strings are immutable. You cannot change their contents; you must make a new string.
Can you write Python code for it?

# This will fail
chinese_zodiac[0] = 'A'

# This is better
print(chinese_zodiac)
chinese_zodiac_minus_r = chinese_zodiac[1:]
print(chinese_zodiac_minus_r)

ate_the_rat = "C" + chinese_zodiac_minus_r
print(ate_the_rat)


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
The split method slices the string into a list
It takes one parameter, the character(s) to split on
my_str.split(slice_string)

And the join method merges a list into a string
It operates on the 'glue' string, and the list is the parameter
glue_string.join(my_list)
Can you write Python code for it?

cz_list = chinese_zodiac.split(' ')
print(cz_list)

print(', '.join(cz_list))


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
You can even format the variables in various ways. Reference the docs for everything, there is just too much you can do and I only have so much time to show you.
https://docs.python.org/3.4/library/string.html
Can you write Python code for it?


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
You don't have to put the elements in the correct order
Can you write Python code for it?

print('The {1} has {0} toes per limb and thus is considered {2}'.format('ox', 4, 'yin'))
print('The {2} has {2} toes per limb and thus is considered {2}'.format('tiger', 5, 'yang'))


I am working on the file "Lesson05_Strings/Strings.ipynb".
The first task is:
lower, upper, title, capitalize, and swapcase all change the case of the string
HINT: use one of these methods to transform input to functions so that you don't have to worry about what case the user's input is in
Can you write Python code for it?


I am working on the file "Lesson06_Files/Files.ipynb".
The first task is:
* Note that sometimes you get a quoted line, instead of the title and salary. If a csv file has a comma inside a cell, the line is quoted. Thus, splitting is not the proper way to read a csv file, but it will work in a pinch. We'll learn about the csv module as well as other ways to read in tabular (excel-like) data in the second half of the class.
Can you write Python code for it?

# Skipping lines
fh = open('thingstodo.txt')
for line in fh:
    if line.startswith('Golden'):
        continue
    print(line)
fh.close()


I am working on the file "Lesson06_Files/Files.ipynb".
The first task is:
TRY IT
Open 'san-francisco-2013.csv' and print out the first line. You can use either method. If you are using the loop method, you can 'break' after printing the first line.
Searching through a file
When searching through a file, you can use string methods to discover and parse the contents.
Let's look at a few examples
Can you write Python code for it?

# Looking for a line that starts with something

# I want to see salary data of women with my first name
fh = open('san-francisco-2014.csv')
for line in fh:
    if line.startswith('Charlotte'):
        print(line)
fh.close()

# Looking for lines that contain a specific string
fh = open('san-francisco-2014.csv')
# Looking for all the department heads
for line in fh:
    # Remember if find doesn't find the string, it returns -1
    if line.find('Dept Head') != -1:
        print(line)
fh.close()

# Counting lines that match criteria
fh = open('san-francisco-2014.csv')
num_trainees = 0
for line in fh:
    # Remember if find doesn't find the string, it returns -1
    if line.find('Trainee') != -1:
        num_trainees += 1
fh.close()
print("There are {0} trainees".format(num_trainees))

# Splitting lines, this is great for excel like data (tsv, csv)
# I want to see salary data of women with my name
fh = open('san-francisco-2014.csv')
for line in fh:
    if line.startswith('Emily'):
        cols = line.split(',')
        print(cols)
        # Salary is 3rd column
        print(cols[1], cols[2], cols[-1])
fh.close()


I am working on the file "Lesson06_Files/Files.ipynb".
The first task is:
TRY IT
Open and close the san-francisco-2013.csv file
Text files and lines
A text file is just a sequence of lines, in fact if you read it in all at once it is returns a list of strings.
Each line is separated by the new line character "\n". This is the special character that is inserted into text files when you hit enter (or you can deliberately put it into strings by using the special \n syntax).
Can you write Python code for it?


I am working on the file "Lesson06_Files/Files.ipynb".
The first task is:
TRY IT
Print your name on two lines using only one print statement
Reading from files
There are two common ways to read through the file, the first (and usually better way) is to loop through the lines in the file.
for line in file_handle:
    print line

The second is to read all the lines at once and store as a string or list.
lines = file_handle.read() # stores as a single string
lines = file_handle.readlines() # stores as a list of strings (separates on new lines)

Unless you are going to process the lines in a file several times, use the first method. It uses way less memory which will be useful if you ever have big files
Can you write Python code for it?

fh = open('thingstodo.txt')
for line in fh:
    print(line.rstrip())
fh.close()

fh = open('thingstodo.txt')
contents = fh.read()
fh.close()

print(contents)
print(type(contents))

fh = open('thingstodo.txt')
lines = fh.readlines()
fh.close()

print(lines)
print(type(lines))


I am working on the file "Lesson06_Files/Files.ipynb".
The first task is:
You can also use with statements to write files
Can you write Python code for it?

with open('numbers2.txt', 'w') as fh:
    for i in range(5):
        fh.write(str(i) + '\n')
with open('numbers2.txt') as fh:
    for line in fh:
        print((line.rstrip()))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Aliasing
If you assign a list to another variable, it will still refer to the same list. This can cause trouble if you change one list because the other will change too.
Can you write Python code for it?

cooked_rolls = ['unagi roll', 'shrimp tempura roll']
my_order = cooked_rolls
my_order.append('hamachi')
print(my_order)
print(cooked_rolls)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Don't forget, you can use the for and in keywords to loop through a list
Can you write Python code for it?

for item in sushi_order:
    print(("I'd like to order the {}.".format(item)))

print("And hold the wasabi!")

for ind, item in enumerate(sushi_order):
    print(("I'd like to order the {0} for {1}.".format(item, prices[ind])))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Enumerate
While the zip function iterates over two lists, the built-in function enumerate loops through indices and elements of a list.  It returns a list of tuples containing the index and value of that element.
for index, value in enumerate(list):
    ...
Can you write Python code for it?

exotic_sushi = ['tako', 'toro', 'uni', 'hirame']
for index, item in enumerate(exotic_sushi):
    print((index, item))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Nested lists
Lists can contain other lists as elements.
This is a convenient alternative to a matrix. You can arrange lists of varying lengths (and contents) in a specific order and you can iterate over the elements (see below).
Can you write Python code for it?

everyones_order = [['california roll'], ['unagi', 'dragon roll'], sushi_order]
print(everyones_order)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Operators and Lists
The in operator allows you to see if an element is contained in a list
Can you write Python code for it?

sushi_order

print(('hamachi' in sushi_order))

if 'otoro' in sushi_order:
    print("Big spender!")


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Remember slices from strings? We can also use the slice operator on lists
Can you write Python code for it?

inexpensive = sushi_order[:2] #takes only the first two elements from list
print(inexpensive)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
TRY IT
Add 'rock n roll' to sushi_order then delete the first element of sushi_order
List Functions
max will return maximum value of list
min returns minimum value of list
sum returns the sum of the values in a list
len returns the number of elements in a list # Just a reminder
Can you write Python code for it?

numbers = [1, 1, 2, 3, 5, 8]

print((max(numbers)))
print((min(numbers)))
print((sum(numbers)))
print((len(numbers)))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
TRY IT
Create a variable called lots_of_sushi that repeats the inexpensive list two times
Can you write Python code for it?

lots_of_sushi = inexpensive*2
print(lots_of_sushi)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
TRY IT
Find the average of numbers using list functions (and not a loop!)
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
TRY IT
Update the last element in prices to be 21.00 and print out the new result
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
To check this, you can use the is operator to see if both variable refer to the same object
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
To create a single element tuple, you need to add a comma to the end of that element (it looks kinda weird)
Can you write Python code for it?

single_element_tuple = (1,)
print(single_element_tuple)
print((type(single_element_tuple)))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
To fix this, you can make a copy of the list using the list function
list takes a sequence and turns it into a list.
Alternatively you can use the copy() method: my_order = cooked_rolls.copy()
Can you write Python code for it?

cooked_rolls = ['unagi roll', 'shrimp tempura roll']
my_order = list(cooked_rolls)
my_order.append('hamachi')
print(my_order)
print(cooked_rolls)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
Tuples
Tuples are very similar to lists. The major difference is that tuples are immutable meaning that you can not add, remove, or assign new values to a tuple.
The creator of a tuple is the comma , but by convention people usually surround tuples with parenthesis.
Can you write Python code for it?

noodles = ('soba', 'udon', 'ramen', 'lo mein', 'somen', 'rice noodle')
print((type(noodles)))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You also have several options for removing elements

the pop method takes the index of the element to remove and returns the value of the element

the remove method takes the value of the element to remove

the del operator deletes the element or slice of the list that you give it
del l[1:]
Can you write Python code for it?

print(my_sushis)
last_sushi = my_sushis.pop(-1)
print(last_sushi)

my_sushis.remove('maguro')
print(my_sushis)

del my_sushis[1:]
print(my_sushis)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can access a single element in a list by indexing in using brackets. List indexing starts at 0 so to get the first element, you use 0, the second element is 1 and so on.
list[index]
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can create a tuple from any sequence using the tuple function
Can you write Python code for it?

sushi_tuple = tuple(my_order)
print(sushi_tuple)
# Remember strings are sequences
maguro = tuple('maguro')
print(maguro)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can find the length of a list using len
len(list)
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can loop through tuples the same way you  loop through lists, using for in
Can you write Python code for it?

for noodle in noodles:
    print(("Yummy, yummy {0} and {1}".format(noodle, 'sushi')))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can use negative indexing to get the last element of a list
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can use some arithmatic operators on lists
The + operator concatenates two lists
The * operator duplicates a list that many times
Can you write Python code for it?

print((sushi_order * 3))

exprep = ['rep'+str(i) for i in range(5)]
exprep

print((prices + sushi_order))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples - after class.ipynb".
The first task is:
You can use the indexing and slicing you learned for lists the same with tuples.
But, because tuples are immutable, you cannot use the append, pop, del, extend, or remove methods or even assign new values to indexes
Can you write Python code for it?

print((noodles[0]))
print((noodles[4:]))

# This should throw an error
noodles[0] = 'spaghetti'


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
Don't forget, you can use the for and in keywords to loop through a list
Can you write Python code for it?

for item in sushi_order:
    print("I'd like to order the {}.".format(item))

print("And hold the wasabi!")

for idx, item in enumerate(sushi_order):
    print("I'd like to order the {0} for {1}.".format(item, prices[idx]))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
Nested lists
Lists can contain other lists as elements.
This is a convenient alternative to a matrix. You can arrange lists of varying lengths (and contents) in a specific order and you can iterate over the elements (see below).
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
Remember slices from strings? We can also use the slice operator on lists
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
TRY IT
Create a tuple containing 'soy sauce' 'ginger' and 'wasabi' and save it in a variable called accompaniments
Zip
the zip function takes any number of lists of the same length and returns a generator for lists of tuples where the tuples will contain the i-th element from each of the lists.
This is really useful when combining lists that are related (especially for looping)
 remider  to print a generator, just cast the results as a list (you can cast them as a tuple instead, actually)
Can you write Python code for it?

print(list(zip([1,2,3], [4,5,6])))

sushi = ['salmon', 'tuna', 'sea urchin']
prices = [5.5, 6.75, 8]

sushi_and_prices = list(zip(sushi, prices))

sushi_and_prices

for sushi, price in sushi_and_prices:
    print("The {0} costs ${1}".format(sushi, price))


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
TRY IT
Create a variable called lots_of_sushi that repeats the inexpensive list two times
Adding and deleting elements
To add an element to a list, you have a few options

the append method adds an element or elements to the end of a list, if you pass it a list, the next element with be a list (making a list of lists)

the extend method takes a list of elements and adds them all to the end, not creating a list of lists

use the + operator like you saw before
Can you write Python code for it?

my_sushis = ['maguro', 'rock n roll']
my_sushis.append('avocado roll')
print(my_sushis)
my_sushis.append(['hamachi', 'california roll'])
print(my_sushis)

my_sushis = ['maguro', 'rock n roll']
my_sushis.extend(['hamachi', 'california roll'])
print(my_sushis)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
TRY IT
Get the california roll from the list everyones_order. As a challenge print all the items from the second person's order.
Mutable Lists
Lists are mutable, that means that you can change elements.
To assign a new value to an element
my_list = [1, 2, 3]
my_list[0] = 100
Can you write Python code for it?

sushi_order[0] = 'caterpillar roll'
print(sushi_order)


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
To access an element in a nested list, first index to the inner list, then index to the item.
Example:
list_of_lists = [[1,2], [3,4], []]
Acess the first index to the inner list and index to the item
python
    inner_list = list_of_lists[1] # [3,4]
    print inner_list[0] # 3
Or even quicker:
python
    list_of_lists[1][0] # 3
To get dragon roll from the sushi order, first we get the second element (index 1) then we get the the second item (index 1)
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
To fix this, you can make a copy of the list using the list function
list takes a sequence and turns it into a list.
Alternatively you can use the copy() method: my_order = cooked_rolls.copy()
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You also have several options for removing elements

the pop method takes the index of the element to remove and returns the value of the element

the remove method takes the value of the element to remove

the del operator deletes the element or slice of the list that you give it
del l[1:]
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You can access a single element in a list by indexing in using brackets. List indexing starts at 0 so to get the first element, you use 0, the second element is 1 and so on.
list[index]
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You can create a tuple from any sequence using the tuple function
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You can find the length of a list using len
len(list)
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You can use negative indexing to get the last element of a list
Can you write Python code for it?


I am working on the file "Lesson07_ListsandTuples/Lists and Tuples.ipynb".
The first task is:
You can use the indexing and slicing you learned for lists the same with tuples.
But, because tuples are immutable, you cannot use the append, pop, del, extend, or remove methods or even assign new values to indexes
Can you write Python code for it?

print(noodles[0])
print(noodles[4:])

# This should throw an error
noodles[0] = 'spaghetti'


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
Dictionary Operators
The in operator returns a boolean for whether the key is in the dictionary or not.
key in dictionary
Can you write Python code for it?

print('raspberry' in fruit_season)
print('mangos' in fruit_season)


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
Let's use this to create a more realistic fruit season dictionary
Can you write Python code for it?

true_fruit_season = {
    'raspberry': ['May', 'June'],
    'apple': ['September', 'October', 'November', 'December'],
    'peach': ['July', 'August'],
    'grape': ['August', 'September', 'October']
}

print (true_fruit_season)

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

for month in months:
    print ('It is {0}'.format(month))
    for fruit, season in true_fruit_season.items():
        if month in season:
            print ("\tEat {0}".format(fruit))


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
More complex dictionaries
Dictionary keys and values can be almost anything. The keys must be hashable which means it cannot change. That means that lists and dictionaries cannot be keys (but strings, tuples, and integers can).
Values can be just about anything, though.
Can you write Python code for it?

my_complicated_dictionary = {
    (1, 2, 3): 6,
    'weevil': {
        'e': 2,
        'i': 1,
        'l': 1,
        'v': 1,
        'w': 1,
    },
    9: [3, 3]
}
print (my_complicated_dictionary)


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
TRY IT
Check if 'broccoli' is in vegetable_season. If so, print 'Yum, little trees!'
Can you write Python code for it?

if 'broccoli' in vegetable_season:
    print('Yum, little trees!')
else:
    print('No little trees.')


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
TRY IT
Create a dictionary called vegetable_season with Eggplant-> July and Onion -> May
Can you write Python code for it?

vegetable_season = {
    'Eggplant': 'July',
    'Onion': 'May'
}
print(vegetable_season)


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
TRY IT
Loop through the sorted keys of the vegetable_season dictionary. For each key, print the month it is in season
Can you write Python code for it?

for fruit in sorted(fruit_season.keys()):
    print('In {0} {1} is in season.'.format(fruit_season[fruit], fruit))


I am working on the file "Lesson08_Dictionaries/Dictionary - after class.ipynb".
The first task is:
You can use this in if statement
Can you write Python code for it?

if 'pineapple' in fruit_season:
    print('Lets eat tropical fruit')
else:
    print("Temperate fruit it is.")


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
Rules on keys
Keys in dictionary must be unique. If you try to make a duplicate key, the data will be overwritten
Keys must be hashable. What this means is they must come from immutable values and be comparable. You can use strings, numbers, tuples, sets, (most) objects. You cannot use lists or dictionaries as keys.
Can you write Python code for it?

duplicate_fruit_season = {
    'raspberry': 'May',
    'raspberry': 'June',
}
print(duplicate_fruit_season)

mutable_key = {
    ['watermelon', 'cantaloupe', 'honeydew']: 'July'
}

# The solution is to use a tuple instead
immutable_key = {
    ('watermelon', 'cantelope', 'honeydew'): 'July'
}


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
TRY IT
Add a key to the true_fruit_season for 'watermelons' the season is July, August, and September
Project: Acrostic
Create an acrostic poem generator.
You will create a function that takes a name and generates an acrostic poem

Create a dictionary that has each of the capital letters as keys and an adjective that start with the letter as the value and store in variable named adjectives. (Reference: http://www.enchantedlearning.com/wordlist/adjectives.shtml)
Create a function called acrostic that takes one parameter name.
In the acrostic function capitalize the name (use the upper method)
For each letter in the name
Get the adjective corresponding to that letter and store in a variable called current_adj
Print out Letter-current_adj

 Challenge  instead of just one adjective have each letter's value be a list of adjectives. Use the random module to select a random adjective instead of always selecting the same one.
Bonus Material
Auto generating the dictionary for the acrostic:
Can you write Python code for it?

# If you have a list of adjectives
my_dict = {}

# Imaging this is the full alphabet
for i in ['A', 'B', 'C']:
    my_dict[i] = []

for i in ['Adoreable', 'Acceptable', 'Bad', 'Cute', 'Basic', 'Dumb']:
    first_char = i[0]
    if first_char in my_dict:
        my_dict[first_char].append(i)
print(my_dict)

# Generating from a file
my_dict = {}

for i in ['A', 'B', 'C']:
    my_dict[i] = []

# adjectives.txt has one adjective per line
with open('adjectives.txt') as fh:
    for line in fh:
        word = line.rstrip().title()
        first_char = word[0]
        if first_char in my_dict:
            my_dict[first_char].append(word)

print(my_dict['A'])


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
TRY IT
Create a dictionary called vegetable_season with Eggplant-> July and Onion -> May
Dictionary Operators
The in operator returns a boolean for whether the key is in the dictionary or not.
key in dictionary
Can you write Python code for it?


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
TRY IT
Loop through the sorted keys of the vegetable_season dictionary. For each key, print the month it is in season
More complex dictionaries
Dictionary keys and values can be almost anything. The keys must be hashable which means it cannot change. That means that lists and dictionaries cannot be keys (but strings, tuples, and integers can).
Values can be just about anything, though.
Can you write Python code for it?

my_complicated_dictionary = {
    (1, 2, 3): 6,
    'weevil': {
        'e': 2,
        'i': 1,
        'l': 1,
        'v': 1,
        'w': 1,
    },
    9: [3, 3]
}
print(my_complicated_dictionary)


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
To access a value, you index into it similarly to a list using square brackets.
value_of_key1 = my_dict['key1']
Can you write Python code for it?

raspberry_season = fruit_season['raspberry']
print(raspberry_season)


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
To add an item to the dictionary set the value equal to the indexed keys
dict['new_key'] = value
Can you write Python code for it?

fruit_season['strawberry'] = 'May'
print(fruit_season)


I am working on the file "Lesson08_Dictionaries/Dictionary.ipynb".
The first task is:
Trying to access a key not in the dictionary throws an error
Can you write Python code for it?


I am working on the file "Lesson10.ipynb".
The first task is:
IMDB

dataset
Can you write Python code for it?

import pandas as pd
import re
from bs4 import BeautifulSoup

train = pd.read_csv('imdb/labeledTrainData.tsv', header=0,
                delimiter="\t", quoting=3)
test = pd.read_csv('imdb/testData.tsv', header=0, delimiter="\t",
               quoting=3 )

y_train = train['sentiment']

def review_to_wordlist(review):
    '''
    Meant for converting each of the IMDB reviews into a list of words.
    '''
    # First remove the HTML.
    review_text = BeautifulSoup(review, "html5lib").get_text()

    # Use regular expressions to only include words.
    review_text = re.sub("[^a-zA-Z]"," ", review_text)

    # Convert words to lower case and split them into separate words.
    words = review_text.lower().split()

    # Return a list of words
    return(words)

print train.head()
traindata = []
for i in xrange(0,len(train['review'])):
    traindata.append(" ".join(review_to_wordlist(train['review'][i])))
testdata = []
print test.head()
for i in xrange(0,len(test['review'])):
    testdata.append(" ".join(review_to_wordlist(test['review'][i])))

from sklearn.feature_extraction.text import TfidfVectorizer as TFIV

tfv = TFIV(min_df=3,  max_features=None,
        strip_accents='unicode', analyzer='word',token_pattern=r'\w{1,}',
        ngram_range=(1, 2), use_idf=1,smooth_idf=1,sublinear_tf=1,
        stop_words = 'english')

X_all = traindata + testdata # Combine both to fit the TFIDF vectorization.
lentrain = len(traindata)

tfv.fit(X_all) # This is the slow part!
X_all = tfv.transform(X_all)

X = X_all[:lentrain] # Separate back into training and test sets.
X_test = X_all[lentrain:]

from sklearn.linear_model import LogisticRegression as LR
from sklearn.grid_search import GridSearchCV

grid_values = {'C':[30]} # Decide which settings you want for the grid search.

model_LR = GridSearchCV(LR(dual = True, random_state = 0),
                        grid_values, scoring = 'roc_auc', cv = 20)
# Try to set the scoring on what the contest is asking for.
# The contest says scoring is for...


I am working on the file "Lesson10.ipynb".
The first task is:
MNIST
Can you write Python code for it?

import pandas as pd

train = pd.read_csv('minst/train.csv')
test = pd.read_csv('minst/test.csv')

print train.shape
print test.shape

y_train = train['label']
X_train = train.drop('label', axis=1)

X_test = test

import tensorflow as tf
import tensorlayer as tl

sess = tf.InteractiveSession()

# 准备数据
X_train, y_train, X_val, y_val, X_test, y_test = \
                                tl.files.load_mnist_dataset(shape=(-1,784))

# 定义 placeholder
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')

# 定义模型
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800,
                                act = tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800,
                                act = tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
network = tl.layers.DenseLayer(network, n_units=10,
                                act = tf.identity,
                                name='output_layer')
# 定义损失函数和衡量指标
# tl.cost.cross_entropy 在内部使用 tf.nn.sparse_softmax_cross_entropy_with_logits() 实现 softmax
y = network.outputs
cost = tl.cost.cross_entropy(y, y_, name = 'cost')
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
y_op = tf.argmax(tf.nn.softmax(y), 1)

# 定义 optimizer
train_params = network.all_params
train_op = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999,
                            epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)

# 初始化 session 中的所有参数
tl.layers.initialize_global_variables(sess)

# 列出模型信息
network.print_params()
network.print_layers()

# 训练模型
tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_,
            acc=acc, batch_size=500, n_epoch=500, print_freq=5,
           ...


I am working on the file "Lesson10.ipynb".
The first task is:
NLTK
Can you write Python code for it?

sent1 = 'The cat is walking in the bedroom'
sent2 = 'A dog was running across the kitchen.'

from sklearn.feature_extraction.text import CountVectorizer
count_vec = CountVectorizer()

sentences = [sent1, sent2]

print count_vec.fit_transform(sentences).toarray()

print count_vec.get_feature_names()

import nltk
#nltk.download() # download tokenizers/punkt/english.pickle

tokens_l = nltk.word_tokenize(sent1)
print tokens_l


I am working on the file "Lesson10.ipynb".
The first task is:
Titanic

dataset

Variable    Definition  Key
survival    Survival    0 = No, 1 = Yes
pclass  Ticket class    1 = 1st, 2 = 2nd, 3 = 3rd
sex Sex
Age Age in years
sibsp   # of siblings / spouses aboard the Titanic
parch   # of parents / children aboard the Titanic
ticket  Ticket number
fare    Passenger fare
cabin   Cabin number
embarked    Port of Embarkation C = Cherbourg, Q = Queenstown, S = Southampton
Variable Notes
pclass: A proxy for socio-economic status (SES)
1st = Upper
2nd = Middle
3rd = Lower
age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5
sibsp: The dataset defines family relations in this way...
Sibling = brother, sister, stepbrother, stepsister
Spouse = husband, wife (mistresses and fiancés were ignored)
parch: The dataset defines family relations in this way...
Parent = mother, father
Child = daughter, son, stepdaughter, stepson
Some children travelled only with a nanny, therefore parch=0 for them.
Can you write Python code for it?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

train = pd.read_csv('titanic/train.csv')
#print train.info()
test = pd.read_csv('titanic/test.csv')
#print test.info()

train.isnull().any()

features = ['Pclass','Sex','Age','SibSp','Parch','Embarked','Fare']
X_train = train[features]
X_test = test[features]

X_train['Age'].fillna(train['Age'].mean(), inplace=True)
X_train['Embarked'].fillna('S', inplace=True)
X_test['Age'].fillna(train['Age'].mean(), inplace=True)
X_test['Embarked'].fillna('S', inplace=True)

y_train = train['Survived']

X_train.isnull().any()

X_train.shape

from sklearn.feature_extraction import DictVectorizer
dict_vec = DictVectorizer(sparse=False)
X_train = dict_vec.fit_transform(X_train.to_dict(orient='record'))
print dict_vec.feature_names_

print X_train

X_test = dict_vec.transform(X_test.to_dict(orient='record'))

from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()

from sklearn.cross_validation import cross_val_score

#cross_val_score(rfc, X_train, y_train, cv=5).mean()

cross_val_score(rfc, X_train, y_train, cv=5).mean()

#print test['PassengerId']
rfc.fit(X_train, y_train)
#rfc_submission = pd.DataFrame({'PassengerId':test['PassengerId'], 'Survived':rfc_y_predict})

X_test = X_test.astype(np.int)
# rfc_y_predict = rfc.predict(X_test)

rfc_y_predict = rfc.predict(X_test)

rfc_y_predict

rfc_submisson = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': rfc_y_predict})

rfc_submisson.to_csv('rfc_submission.csv', index=False)


I am working on the file "Lesson10.ipynb".
The first task is:
手写体 SVM
加载数据
Can you write Python code for it?

from sklearn.datasets import load_digits

digits = load_digits()
digits.data.shape


I am working on the file "Lesson10.ipynb".
The first task is:
数据载入

dataset
Can you write Python code for it?

df = pd.read_csv('breast-cancer-wisconsin.data', names=[
    'Sample code number',
    'Clump Thickness',
    'Uniformity of Cell Size',
    'Uniformity of Cell Shape',
    'Marginal Adhesion',
    'Single Epithelial Cell Size'
    'Bare Nuclei',
    'Bland Chromatin',
    'Normal Nucleoli',
    'Mitoses',
    'Class'
])

df.head()

df.shape

df.info()

df = df.replace(to_replace='?', value=np.nan)

df.isnull().sum()

df.dropna(how='any', inplace=True)

df.shape

df.isnull().sum()


I am working on the file "Lesson10.ipynb".
The first task is:
模型训练
Can you write Python code for it?

lsvc = LinearSVC()

lsvc.fit(X_train, y_train)

y_predict = lsvc.predict(X_test)

y_predict

from sklearn.metrics import classification_report

print 'Accuracy of SVM', lsvc.score(X_test, y_test)

print classification_report(y_test, y_predict, target_names=digits.target_names.astype(str))


I am working on the file "Lesson10.ipynb".
The first task is:
测试数据
Can you write Python code for it?

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=33)

from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.fit_transform(X_test)


I am working on the file "Lesson10.ipynb".
The first task is:
测试样本
Can you write Python code for it?

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df[df.columns[0:9]], df[df.columns[9]], test_size=0.25, random_state=33)

y_train.value_counts()

y_test.value_counts()

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.fit_transform(X_test)

X_train

lr = LogisticRegression()
sgdb = SGDClassifier()

lr.fit(X_train, y_train)

lr_y_predict = lr.predict(X_test)

lr_y_predict

y_test

sgdb.fit(X_train, y_train)

sgdb_y_predict = sgdb.predict(X_test)

sgdb_y_predict


I am working on the file "Lesson10.ipynb".
The first task is:
监督方法
KNN
Can you write Python code for it?

from sklearn.datasets import load_iris

iris = load_iris()
iris.data.shape

iris.DESCR

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.25, random_state=33)

from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.fit_transform(X_test)

knc = KNeighborsClassifier()
knc.fit(X_train, y_train)

y_predict = knc.predict(X_test)

from sklearn.metrics import classification_report
print 'Accuracy of K-Nearest Neighbor', knc.score(X_test, y_test)

print classification_report(y_test, y_predict, target_names=digits.target_names.astype(str))


I am working on the file "Lesson10_Regexs/RegularExpressions.ipynb".
The first task is:
If you want to find all the matches, not just the first, you can use the findall method. It returns a list of all the matches
re.findall(pattern, string)
Can you write Python code for it?

time_pattern = '\d\d:\d\d:\d\d'
print(re.findall(time_pattern, django_logs))


I am working on the file "Lesson10_Regexs/RegularExpressions.ipynb".
The first task is:
If you want to have only part of the match returned to you in findall, you can use parenthesis to set a capture point
pattern = 'sads (part to capture) asdjklajsd'
print re.findall(pattern, string) # prints part to capture
Can you write Python code for it?

time_pattern = '(\d\d):\d\d:\d\d'
hours = re.findall(time_pattern, django_logs)
print(sorted(hours))

# you can capture more than one match
time_pattern = '(\d\d):(\d\d):\d\d'
times = re.findall(time_pattern, django_logs)
print(times)

# Unpacking the tuple in the first line
for hours, mins in times:
    print("{} hr {} min".format(hours, mins))


I am working on the file "Lesson10_Regexs/RegularExpressions.ipynb".
The first task is:
Searching
The simplest thing you can do with regexs in python is search through text to see if there is a match. To do this you use the methods search or match. match only checks if it matches at the beginning of the string and search check the whole string.
re.match(pattern, string)
re.search(pattern, string)
Can you write Python code for it?

print(re.match('a', 'abcde'))
print(re.match('c', 'abcde'))

print(re.search('a', 'abcde'))
print(re.search('c', 'abcde'))

print(re.match('version', django_logs))
print(re.search('version', django_logs))

if re.search('commit', django_logs):
    print("Someone has been doing work.")


I am working on the file "Lesson10_Regexs/RegularExpressions.ipynb".
The first task is:
TRY IT
Capture the host of the email address (alphanumerics between @ and .com) Hint remember to escape the . in .com
Practice
There is a lot more that you can do, but it can feel overwhelming. The best way to learn is with practice. A great way to experiment is this website http://www.regexr.com/ You can put a section of text and see what regexs match patterns in your text. The site also has a cheatsheet for special characters.
Can you write Python code for it?


I am working on the file "Lesson10_Regexs/RegularExpressions.ipynb".
The first task is:
TRY IT
Search for the word May in the django logs
Special Characters
So far we can't do anything that you couldn't do with find, but don't worry. Regexs have many special characters to allow you to look for thing like the beginning  of a word, whitespace or classes of characters.
You include the character in the pattern.

^         Matches the beginning of a line
$         Matches the end of the line
.         Matches any character
\s        Matches whitespace
\S        Matches any non-whitespace character
*        Repeats a character zero or more times
*?       Repeats a character zero or more times (non-greedy)
+        Repeats a character one or more times
+?        Repeats a character one or more times (non-greedy)
?         Repeats a character 0 or one time
[aeiou]   Matches a single character in the listed set
[^XYZ]    Matches a single character not in the listed set
[a-z0-9]  The set of characters can include a range
{10}      Specifics a match the preceding character(s) {num} number or times
\d        Matches any digit
\b        Matches a word boundary

Hint if you want to match the literal character (like $) as opposed to its special meaning, you would escape it with a \
Can you write Python code for it?

# Start simple, match any character 2 times
print(re.search('..', django_logs))

# just to prove it works
print(re.search('..', 'aa'))
print(re.search('..', 'a'))
print(re.search('..', '^%'))

# to match a commit hash (numbers and letters a-f repeated) we can use a regex
commit_pattern = '[0-9a-f]+'
print(re.search(commit_pattern, django_logs))

# Let's match the time syntax
time_pattern = '\d\d:\d\d:\d\d'
time_pattern = '\d{2}:\d{2}:\d{2}'
print(re.search(time_pattern, django_logs))


I am working on the file "Lesson11_JSONandAPIs/JSONandAPIs.ipynb".
The first task is:
Loading data
You can load data from json format into python from either a string using the loads method or a file handle using the load method.
my_list = json.loads('[1, 2, 3]')
with open(my_file.json) as fh:
    my_dict = json.load(fh)
Can you write Python code for it?

# Load from file
with open('shapes.json') as fh:
    shapes = json.load(fh)
print(shapes)

# Load from string
complex_shapes_string = '["pentagon", "spiral", "double helix"]'
complex_shapes = json.loads(complex_shapes_string)
print(complex_shapes)


I am working on the file "Lesson11_JSONandAPIs/JSONandAPIs.ipynb".
The first task is:
Now the result is a string, but it is valid json and we know how to turn a json string into a python dictionary: json.loads()
Can you write Python code for it?

game_data = json.loads(data)
print(type(game_data))


I am working on the file "Lesson11_JSONandAPIs/JSONandAPIs.ipynb".
The first task is:
TRY IT
Create a string called three_d_json which cotains the string '["cube", "sphere"]' and then load that data into a python list using json.load.
Using JSON data
Once you load data from python format, you can now use the data like you would any other python dictionary or list.
Can you write Python code for it?

for shape in shapes:
    title_shape = shape.title()
    area_formula = shapes[shape]['area']
    print("{}'s area can be calculated using {}".format(title_shape, area_formula))


I am working on the file "Lesson11_JSONandAPIs/JSONandAPIs.ipynb".
The first task is:
TRY IT
create a list of 4 sided shapes and store in a variable called quads, dump quad to json and store the result in a variable called quads_json.
Web APIs
Web APIs are a way to retreive and send data to and from a url. The urls have a pattern so that you can retreive data programtically. With REST APIs specifically, you build a url putting data in the correct places to retreive the data you need. Many Web APIs (the best ones) return their data in JSON format.
There are many free api's available, most require that you sign up to recieve an API key. You will need to read the API docs for any specific api to figure out how to get the data you want.
Here are some fun APIs to try out:
* Dropbox: https://www.dropbox.com/developers
* Google Maps: https://developers.google.com/maps/web/
* Twitter: https://dev.twitter.com/docs
* YouTube: https://developers.google.com/youtube/v3/getting-started
* Soundcloud: http://developers.soundcloud.com/docs/api/guide#playing
* Stripe: https://stripe.com/docs/tutorials/checkout
* Instagram: http://instagram.com/developer/
* Twilio: https://www.twilio.com/docs
* Yelp: http://www.yelp.com/developers/getting_started
* Facebook: https://developers.facebook.com/docs/facebook-login/login-flow-for-web
* Etsy: https://www.etsy.com/developers/documentation
We are going to use the steam api because certain endpoints don't require an app id (and who has time to sign up for one when there is python to learn?)
The endpoint we will use is one that will get us metadata info about a specific game:
http://store.steampowered.com/api/appdetails?appids=<id number>

If the game doesn't exist it returns json that looks like this:
    {"1":{"success":false}}

If the game does exist it returns json that looks like this:
"100":{
"success":true,
"data":{
  "type":"game",
  "name":"Counter-Strike: Condition Zero",
  "steam_appid":80,
  "required_age":0,
  "is_free":false,
  "detailed_description":"With its extensive Tour of Duty campaign, a near-limitless number of skirmish modes, updates and...
Can you write Python code for it?

import urllib.request, urllib.error, urllib.parse

game_id = str(251990)
connection = urllib.request.urlopen('http://store.steampowered.com/api/appdetails?appids=' + game_id)
data = connection.read()
type(data)


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
Here is something I do all the time. It is a little more complicated than the above examples, so take your time trying to understand it. What I like to do is to read the csv data and transform it to a dictionary of lists. This allows me to use it in many different ways later in the code. It is most useful with larger dataset that I will be analyzing and using many different times. (You can even print it out as JSON!)
Can you write Python code for it?

# Lets see our pace for each walk
with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    header = next(reader)
    # This is the dictionary we will put our data from the csv into
    # The key's are the column headers and the values is a list of
    # all the data in that column (transformed into floats)
    data = {}
    # Initialize our dictionary with keys from header and values
    # as empty lists
    for column in header:
        data[column] = []
    for row in reader:
        # Enumerate give us the index and the value so
        # we don't have to use a count variable
        for index, column in enumerate(header):
            # convert data point to float
            data_point = float(row[index])
            # append data to dictionary's list for that column
            data[column].append(data_point)
    # look at that beautiful data. You can do anything with that!
    print(data)


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
Next we create a csv reader. You give it a file handle and optionally the dialect, the separator (usually commas or tabs), and the quote character.
with open(filename, 'r') as fh:
    reader = csv.reader(fh, delimiter='\t', quotechar='"')
Can you write Python code for it?

with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
TRY IT
Find the average number of squats done from the workouts.txt file. Feel free to copy the code for opening from the previous TRY IT.
Writing CSVs
The csv module also contains code for writing csvs.
To write, you create a writer using the writer method and give it a filehandle and optionally delimiter and quotechar.
with open('my_file.csv', 'w') as fh:
    writer = csv.writer(fh, delimiter=',', quotechar='"')

Then use the writerow method with a list to write as it's argument.
writer.writerow([item1, item2])
Can you write Python code for it?

import random
with open('sleep.csv', 'w') as fh:
    writer = csv.writer(fh, delimiter='\t', quotechar='"')
    header = ['day', 'sleep (hr)']
    writer.writerow(header)
    for i in range(1,11):
        hr_sleep = random.randint(4,10)
        writer.writerow([i, hr_sleep])

#open the file to prove you wrote it. (Open in excel for best results)


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
TRY IT
Open up the file workout.txt (tab delimited, tab='\t') with the csv reader and print out each row.
Doesn't that look nice?
Well there are a few problems that I can see. First the header, how do we deal with that?
Headers
The easiest way I have found is to use the next method (that is available with any generator) before the for loop and to store that in a header variable. That reads the first line and stores it (so that you can use it later) and then advances the pointer to the next line so when you run the for loop it is only on the data.
header = reader.next()
for row in reader:
    #process data
Can you write Python code for it?

with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    header = next(reader)
    for row in reader:
        print(row)
    print("Header", header)


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
TRY IT
Open workouts with a csv reader. Save the header line to a variable called header. Convert each value in the data rows to ints and print them out.
Analyzing our data
You can use just about everything we have learned up until this point to analyze your data: if statements, regexs, math, data structures. Let's look at some examples.
Can you write Python code for it?

# Let's find the average distance for all walks.

with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    header = next(reader)
    # Empty list for storing all distances
    walks = []
    for row in reader:
        #distance is in the first column
        dist = row[0]
        # Convert to float so we can do math
        dist = float(dist)
        # Append to our list
        walks.append(dist)

    # Use list aggregation methods to get average distance
    ave_dist = sum(walks) / len(walks)
    print("Average distance walked: {0:.1f}".format(ave_dist))

# Let's see our pace for each walk
with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    header = next(reader)
    for row in reader:
        #distance is in the first column
        dist = row[0]
        # Convert to float so we can do math
        dist = float(dist)
        #time in minutes is in the second column
        time_minutes = row[1]
        # Convert to float so we can do math
        time_minutes = float(time_minutes)
        # calculate pace as minutes / kilometer
        pace = time_minutes /dist
        print("Pace: {0:.1f} min/km".format(pace))

# If you want a challenge, try to make this seconds/mile

# We can filter data. Let's get the ave pace only for walks longer than
# 3 km

# Let's see our pace for each walk
with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    header = next(reader)
    paces = []
    for row in reader:
        #distance is in the first column
        dist = row[0]
        # Convert to float so we can do math
        dist = float(dist)
        # Don't count short walks
        if dist >= 3.0:
            #time in minutes is in the second column
            time_minutes = row[1]
            # Convert to float so we can do math
            time_minutes = float(time_minutes)
            pace = time_minutes /dist
            paces.append(pace)

ave_pace = sum(paces) / len(paces)
print("Average walking pace: {0:.1f}...


I am working on the file "Lesson12_TabularData/Tabular Data.ipynb".
The first task is:
The reader doesn't do anything yet. It is a generator that allows you to loop through the data (it is very similar to a file handle).
To loop through the data you just write a simple for loop
for row in reader:
    #process row

The each row will be a list with each element corresponding to a single column.
Can you write Python code for it?

with open('walks.csv', 'r') as fh:
    reader = csv.reader(fh, delimiter=',')
    for row in reader:
        print(row)


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
Color

fg - foreground color of widget
bg - background color of widget

Color can be in hex: "#ff0000" or in words "red", "green", "blue". Hex is preferred.
Can you write Python code for it?

# A basic button
root = Tkinter.Tk()
# Widgets go here
w = Tkinter.Button(root, text='Hello', fg="red", bg="#00aaff")
w.pack()

# Run the gui
root.mainloop()


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
Deleting the main window
To quit a Tkinter application, you run the destroy method on the main window
root.destroy()

This won't do anything until you are broken out of the main loop, but it will cleanup after that happens.
Can you write Python code for it?


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
Entry
An entry accepts a single text line from a user.
w = Entry(root)

To get the data from an entry, you can use the get method
w = Entry(root)
text = w.get()
Can you write Python code for it?

# A basic entry
root = Tkinter.Tk()
e = Tkinter.Entry(root)
e.pack()
root.mainloop()

# An entry where we get the text entered.
def what_entered():
    print(e.get())

root = Tkinter.Tk()

e = Tkinter.Entry(root)
e.pack()

# Click to button to see what teh text entered is
b = Tkinter.Button(root, text='Say What?', command=what_entered)
b.pack()

root.mainloop()


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
If you want to be able to update the text in a label, you will need to use a StringVar for the text, and instead of using text keyword param, you will use textvaribale keyword parameter.
var = Tkinter.StringVar()
l = Label(root, texvariable=var)
l.pack()

var.set('New text')
Can you write Python code for it?

# A changeable label
root = Tkinter.Tk()
# Widgets go here
v = Tkinter.StringVar()
w = Tkinter.Label(root, textvariable=v)
w.pack()
v.set('Aaaah')

# Run the gui
root.mainloop()


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
TRY IT
Create a GUI that has a Label that says "What is your name?" and an entry to enter it. If you are feeling confident, add a button that will print out the name to the screen when pressed.
There are many other types of widgets including canvas for drawing images and shapes, menubutton for creating menus, radio and lists for making selection, and lots of other. Look to the documentation for more info: https://docs.python.org/2/library/tkinter.html
Event driven programming
GUI programming uses a different model of programming than we are used to. In the past our program has run from top to bottom and the only way to change the outputs was to change the inputs (or use random...)
In GUIs we use event driven programming where we create our application and then listen for events like a user clicking a button or entering some data. When an event is created, then the callback function associated with that event runs. Then the next event causes a callback to run. This all happens in an infinite loop (mainloop) which is broken by you either calling destroy or close in your code in response to an event or by clicking the close button for the window.
In Event driven programming, the user determines the workflow. It will take some time to get used to thinking in these terms.
TRY IT
Give yourself a few minutes to think about event driven programming and how it is different than the previous programs you have written.
Attributes
In the keyword arguments all widgets can take some attribute parameters. These can determine the widget's dimensions, colors, fonts, image, etc.
Size

height - height of element
width - width of element
Can you write Python code for it?

# A basic button
root = Tkinter.Tk()
# Widgets go here
w = Tkinter.Button(root, text='Hello', width=50, height=1)
w.pack()

# Run the gui
root.mainloop()


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
TRY IT
Create a tkinter app main window and store it in the variable top run the mainloop function on it.
Widgets
So far our GUI has been pretty boring. To make it useful we have to add "widgets" to it. Widgets are things like buttons, textboxes, labels, etc. Anything you would expect to have in a desktop application.
You need to create the widget after creating the main window, but before running mainloop
You will also need to let Tkinter know how to layout your widgets. Right now we will just use the pack method with no arguments.
widget.pack()

Button
You can create a button that when pressed will run a function. To do that you run the Button function. It takes the parent window as the first parameter, and then a list of key value params as options for look and feel, text, and command - the function that will run when the button is pushed. It returns a reference to the widget.
my_button = Tkinter.Button(window, text='my text', callback=myfunction)
Can you write Python code for it?

# A basic button
root = Tkinter.Tk()
# Widgets go here
w = Tkinter.Button(root, text='Hello')
w.pack()

# Run the gui
root.mainloop()

# a button with a callback
def my_callback():
    print("Here i am")

root = Tkinter.Tk()
# Widgets go here
w = Tkinter.Button(root, text='Hello', command=my_callback)
w.pack()

# Run the gui
root.mainloop()


I am working on the file "Lesson13_GUIs/GUI.ipynb".
The first task is:
That didn't do much, did it. To actually create the window you have to run the mainloop function on your main window
root.mainloop()

This creates the window and starts listening for actions that the user takes. This is an infinite loop, and is one of the cases where an infinite loop is actually useful (don't worry, clicking the close button on the window will break out for the loop).
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
And run the following cell to test it's performance.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
And run the following cell to test it's performance. It should be
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
End of Project 1.
Watch the next video to see Andrew's solution, then continue on to the next lesson.
Transforming Text into Numbers
The cells here include code Andrew shows in the next video. We've included it so you can run the code along with the video without having to type in everything.
Can you write Python code for it?

from IPython.display import Image

review = "This was a horrible, terrible movie."

Image(filename='sentiment_network.png')

review = "The movie was excellent"

Image(filename='sentiment_network_pos.png')


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Examine the new ratios you've calculated for the same words from before:
Can you write Python code for it?

print("Pos-to-neg ratio for 'the' = {}".format(pos_neg_ratios["the"]))
print("Pos-to-neg ratio for 'amazing' = {}".format(pos_neg_ratios["amazing"]))
print("Pos-to-neg ratio for 'terrible' = {}".format(pos_neg_ratios["terrible"]))


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Examine the ratios you've calculated for a few words:
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
If everything worked, now you should see neutral words with values close to zero. In this case, "the" is near zero but slightly positive, so it was probably used in more positive reviews than negative reviews. But look at "amazing"'s ratio - it's above 1, showing it is clearly a word with positive sentiment. And "terrible" has a similar score, but in the opposite direction, so it's below -1. It's now clear that both of these words are associated with specific, opposing sentiments.
Now run the following cells to see more ratios.
The first cell displays all the words, ordered by how associated they are with postive reviews. (Your notebook will most likely truncate the output so you won't actually see all the words in the list.)
The second cell displays the 30 words most associated with negative reviews by reversing the order of the first list and then looking at the first 30 words. (If you want the second cell to display all the words, ordered by how associated they are with negative reviews, you could just write reversed(pos_neg_ratios.most_common()).)
You should continue to see values similar to the earlier ones we checked – neutral words will be close to 0, words will get more positive as their ratios approach and go above 1, and words will get more negative as their ratios approach and go below -1. That's why we decided to use the logs instead of the raw ratios.
Can you write Python code for it?

# words most frequently seen in a review with a "POSITIVE" label
pos_neg_ratios.most_common()

# words most frequently seen in a review with a "NEGATIVE" label
list(reversed(pos_neg_ratios.most_common()))[0:30]

# Note: Above is the code Andrew uses in his solution video,
#       so we've included it here to avoid confusion.
#       If you explore the documentation for the Counter class,
#       you will see you could also find the 30 least common
#       words like this: pos_neg_ratios.most_common()[:-31:-1]


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Note: The data in reviews.txt we're using has already been preprocessed a bit and contains only lower case characters. If we were working from raw data, where we didn't know it was all lower case, we would want to add a step here to convert it. That's so we treat different variations of the same word, like The, the, and THE, all the same way.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Project 1: Quick Theory Validation
There are multiple ways to implement these projects, but in order to get your code closer to what Andrew shows in his solutions, we've provided some hints and starter code throughout this notebook.
You'll find the Counter class to be useful in this exercise, as well as the numpy library.
Can you write Python code for it?

from collections import Counter
import numpy as np


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Project 2: Creating the Input/Output Data
TODO: Create a set named vocab that contains every word in the vocabulary.
Can you write Python code for it?

# TODO: Create set named "vocab" containing all of the words from all of the reviews
vocab = set(total_counts.keys())


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Project 4: Reducing Noise in Our Input Data
TODO: Attempt to reduce the noise in the input data like Andrew did in the previous video. Specifically, do the following:
* Copy the SentimentNetwork class you created earlier into the following cell.
* Modify update_input_layer so it does not count how many times each word is used, but rather just stores whether or not a word was used.
Can you write Python code for it?

import time
import sys
import numpy as np

# Encapsulate our neural network in a class
class SentimentNetwork:
    def __init__(self, reviews,labels,hidden_nodes = 10, learning_rate = 0.1):
        """Create a SentimenNetwork with the given settings
        Args:
            reviews(list) - List of reviews used for training
            labels(list) - List of POSITIVE/NEGATIVE labels associated with the given reviews
            hidden_nodes(int) - Number of nodes to create in the hidden layer
            learning_rate(float) - Learning rate to use while training

        """
        # Assign a seed to our random number generator to ensure we get
        # reproducable results during development
        np.random.seed(1)

        # process the reviews and their associated labels so that everything
        # is ready for training
        self.pre_process_data(reviews, labels)

        # Build the network to have the number of hidden nodes and the learning rate that
        # were passed into this initializer. Make the same number of input nodes as
        # there are vocabulary words and create a single output node.
        self.init_network(len(self.review_vocab),hidden_nodes, 1, learning_rate)

    def pre_process_data(self, reviews, labels):

        # populate review_vocab with all of the words in the given reviews
        review_vocab = set()
        for review in reviews:
            for word in review.lower().replace(",", " ").replace(".", " ").split(" "):
                review_vocab.add(word)

        # Convert the vocabulary set to a list so we can access words via indices
        self.review_vocab = list(review_vocab)

        # populate label_vocab with all of the words in the given labels.
        label_vocab = set()
        for label in labels:
            label_vocab.add(label)

        # Convert the label vocabulary set to a list so we can access labels via indices
        self.label_vocab = list(label_vocab)

        # Store the sizes of the...


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Project 5: Making our Network More Efficient
TODO: Make the SentimentNetwork class more efficient by eliminating unnecessary multiplications and additions that occur during forward and backward propagation. To do that, you can do the following:
* Copy the SentimentNetwork class from the previous project into the following cell.
* Remove the update_input_layer function - you will not need it in this version.
* Modify init_network:

You no longer need a separate input layer, so remove any mention of self.layer_0
You will be dealing with the old hidden layer more directly, so create self.layer_1, a two-dimensional matrix with shape 1 x hidden_nodes, with all values initialized to zero
Modify train:
Change the name of the input parameter training_reviews to training_reviews_raw. This will help with the next step.
At the beginning of the function, you'll want to preprocess your reviews to convert them to a list of indices (from word2index) that are actually used in the review. This is equivalent to what you saw in the video when Andrew set specific indices to 1. Your code should create a local list variable named training_reviews that should contain a list for each review in training_reviews_raw. Those lists should contain the indices for words found in the review.
Remove call to update_input_layer
Use self's  layer_1 instead of a local layer_1 object.
In the forward pass, replace the code that updates layer_1 with new logic that only adds the weights for the indices used in the review.
When updating weights_0_1, only update the individual weights that were used in the forward pass.
Modify run:
Remove call to update_input_layer
Use self's  layer_1 instead of a local layer_1 object.
Much like you did in train, you will need to pre-process the review so you can work with word indices, then update layer_1 by adding weights for the indices used in the review.
Can you write Python code for it?

import time
import sys
import numpy as np

# Encapsulate our neural network in a class
class SentimentNetwork:
    def __init__(self, reviews,labels,hidden_nodes = 10, learning_rate = 0.1):
        """Create a SentimenNetwork with the given settings
        Args:
            reviews(list) - List of reviews used for training
            labels(list) - List of POSITIVE/NEGATIVE labels associated with the given reviews
            hidden_nodes(int) - Number of nodes to create in the hidden layer
            learning_rate(float) - Learning rate to use while training

        """
        # Assign a seed to our random number generator to ensure we get
        # reproducable results during development
        np.random.seed(1)

        # process the reviews and their associated labels so that everything
        # is ready for training
        self.pre_process_data(reviews, labels)

        # Build the network to have the number of hidden nodes and the learning rate that
        # were passed into this initializer. Make the same number of input nodes as
        # there are vocabulary words and create a single output node.
        self.init_network(len(self.review_vocab),hidden_nodes, 1, learning_rate)

    def pre_process_data(self, reviews, labels):

        # populate review_vocab with all of the words in the given reviews
        review_vocab = set()
        for review in reviews:
            for word in review.split(" "):
                review_vocab.add(word)

        # Convert the vocabulary set to a list so we can access words via indices
        self.review_vocab = list(review_vocab)

        # populate label_vocab with all of the words in the given labels.
        label_vocab = set()
        for label in labels:
            label_vocab.add(label)

        # Convert the label vocabulary set to a list so we can access labels via indices
        self.label_vocab = list(label_vocab)

        # Store the sizes of the review and label vocabularies.
       ...


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Project 6: Reducing Noise by Strategically Reducing the Vocabulary
TODO: Improve SentimentNetwork's performance by reducing more noise in the vocabulary. Specifically, do the following:
* Copy the SentimentNetwork class from the previous project into the following cell.
* Modify pre_process_data:

Add two additional parameters: min_count and polarity_cutoff
Calculate the positive-to-negative ratios of words used in the reviews. (You can use code you've written elsewhere in the notebook, but we are moving it into the class like we did with other helper code earlier.)
Andrew's solution only calculates a postive-to-negative ratio for words that occur at least 50 times. This keeps the network from attributing too much sentiment to rarer words. You can choose to add this to your solution if you would like.
Change so words are only added to the vocabulary if they occur in the vocabulary more than min_count times.
Change so words are only added to the vocabulary if the absolute value of their postive-to-negative ratio is at least polarity_cutoff
Modify __init__:
Add the same two parameters (min_count and polarity_cutoff) and use them when you call pre_process_data
Can you write Python code for it?

import time
import sys
import numpy as np

# Encapsulate our neural network in a class
class SentimentNetwork:
    ## New for Project 6: added min_count and polarity_cutoff parameters
    def __init__(self, reviews,labels,min_count = 10,polarity_cutoff = 0.1,hidden_nodes = 10, learning_rate = 0.1):
        """Create a SentimenNetwork with the given settings
        Args:
            reviews(list) - List of reviews used for training
            labels(list) - List of POSITIVE/NEGATIVE labels associated with the given reviews
            min_count(int) - Words should only be added to the vocabulary
                             if they occur more than this many times
            polarity_cutoff(float) - The absolute value of a word's positive-to-negative
                                     ratio must be at least this big to be considered.
            hidden_nodes(int) - Number of nodes to create in the hidden layer
            learning_rate(float) - Learning rate to use while training

        """
        # Assign a seed to our random number generator to ensure we get
        # reproducable results during development
        np.random.seed(1)

        # process the reviews and their associated labels so that everything
        # is ready for training
        ## New for Project 6: added min_count and polarity_cutoff arguments to pre_process_data call
        self.pre_process_data(reviews, labels, polarity_cutoff, min_count)

        # Build the network to have the number of hidden nodes and the learning rate that
        # were passed into this initializer. Make the same number of input nodes as
        # there are vocabulary words and create a single output node.
        self.init_network(len(self.review_vocab),hidden_nodes, 1, learning_rate)

    ## New for Project 6: added min_count and polarity_cutoff parameters
    def pre_process_data(self, reviews, labels, polarity_cutoff, min_count):

        ## ----------------------------------------
        ## New for Project 6: Calculate...


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to actually train the network. During training, it will display the model's accuracy repeatedly as it trains so you can see how well it's doing.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to check your vocabulary size. If everything worked correctly, it should print 74074
Can you write Python code for it?

vocab_size = len(vocab)
print(vocab_size)


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to create a SentimentNetwork that will train on all but the last 1000 reviews (we're saving those for testing). Here we use a learning rate of 0.1.
Can you write Python code for it?

mlp = SentimentNetwork(reviews[:-1000],labels[:-1000], learning_rate=0.1)


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to recreate the network and train it. Notice we've gone back to the higher learning rate of 0.1.
Can you write Python code for it?

mlp = SentimentNetwork(reviews[:-1000],labels[:-1000], learning_rate=0.1)
mlp.train(reviews[:-1000],labels[:-1000])


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to test the network's performance against the last 1000 reviews (the ones we held out from our training set).
We have not trained the model yet, so the results should be about 50% as it will just be guessing and there are only two possible values to choose from.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to test updating the input layer with the first review. The indices assigned may not be the same as in the solution, but hopefully you'll see some non-zero values in layer_0.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to train your network with a much larger polarity cutoff.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following cell to train your network with a small polarity cutoff.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following two cells to list the words used in positive reviews and negative reviews, respectively, ordered from most to least commonly used.
Can you write Python code for it?

# Examine the counts of the most common words in positive reviews
positive_counts.most_common()

# Examine the counts of the most common words in negative reviews
negative_counts.most_common()


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Run the following two cells. They should print out 'NEGATIVE' and 0, respectively.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
TODO: Complete the implementation of get_target_for_labels. It should return 0 or 1,
          depending on whether the given label is NEGATIVE or POSITIVE, respectively.
Can you write Python code for it?

def get_target_for_label(label):
    """Convert a label to `0` or `1`.
    Args:
        label(string) - Either "POSITIVE" or "NEGATIVE".
    Returns:
        `0` or `1`.
    """
    # TODO: Your code here
    return 1 if label=="POSITIVE" else 0


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
TODO: Create a numpy array called layer_0 and initialize it to all zeros. You will find the zeros function particularly helpful here. Be sure you create layer_0 as a 2-dimensional matrix with 1 row and vocab_size columns.
Can you write Python code for it?

# TODO: Create layer_0 matrix with dimensions 1 by vocab_size, initially filled with zeros
layer_0 = np.zeros((1,vocab_size))


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
Take a look at the following image. It represents the layers of the neural network you'll be building throughout this notebook. layer_0 is the input layer, layer_1 is a hidden layer, and layer_2 is the output layer.
Can you write Python code for it?

from IPython.display import Image
Image(filename='sentiment_network_2.png')


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
That most likely didn't train very well. Part of the reason may be because the learning rate is too high. Run the following cell to recreate the network with a smaller learning rate, 0.01, and then train the new network.
Can you write Python code for it?

mlp = SentimentNetwork(reviews[:-1000],labels[:-1000], learning_rate=0.01)
mlp.train(reviews[:-1000],labels[:-1000])


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
That probably wasn't much different. Run the following cell to recreate the network one more time with an even smaller learning rate, 0.001, and then train the new network.
Can you write Python code for it?

mlp = SentimentNetwork(reviews[:-1000],labels[:-1000], learning_rate=0.001)
mlp.train(reviews[:-1000],labels[:-1000])


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
That should have trained much better than the earlier attempts. It's still not wonderful, but it should have improved dramatically. Run the following cell to test your model with 1000 predictions.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
That should have trained much better than the earlier attempts. Run the following cell to test your model with 1000 predictions.
Can you write Python code for it?


I am working on the file "Lesson14-Sentiment Analysis/sentiment-network/Sentiment_Classification_Projects.ipynb".
The first task is:
We'll create three Counter objects, one for words from postive reviews, one for words from negative reviews, and one for all the words.
Can you write Python code for it?

# Create three Counter objects to store positive, negative and total counts
positive_counts = Counter()
negative_counts = Counter()
total_counts = Counter()