Linguistics 110

Professor Susan Lin

This introductory notebook will familiarize you with some of the basic strategies for data analysis that will be useful to you throughout the course. It will cover an overview of our software and an introduction to programming.

Table of Contents

1 - Computing Environment

2 - Introduction to Coding Concepts

       1 - Python Basics

       2 - Pandas

       3 - Visualization

Our Computing Environment, Jupyter notebooks

This webpage is called a Jupyter notebook. A notebook is a place to write programs and view their results.

Text cells

In a notebook, each rectangle containing text or code is called a cell.

Text cells (like this one) can be edited by double-clicking on them. They're written in a simple format called Markdown to add formatting and section headings. You don't need to learn Markdown, but you might want to.

After you edit a text cell, click the "run cell" button at the top that looks like ▶| to confirm any changes. (Try not to delete the instructions of the lab.)

Understanding Check 1 This paragraph is in its own text cell. Try editing it so that this sentence is the last sentence in the paragraph, and then click the "run cell" ▶| button . This sentence, for example, should be deleted. So should this one.

Code cells

Other cells contain code in the Python 3 language. Running a code cell will execute all of the code it contains.

To run the code in a code cell, first click on that cell to activate it. It'll be highlighted with a little green or blue rectangle. Next, either press ▶| or hold down the shift key and press return or enter.

Try running this cell:


In [ ]:
print("Hello, World!")

And this one:


In [ ]:
print("\N{WAVING HAND SIGN}, \N{EARTH GLOBE ASIA-AUSTRALIA}!")

The fundamental building block of Python code is an expression. Cells can contain multiple lines with multiple expressions. When you run a cell, the lines of code are executed in the order in which they appear. Every print expression prints a line. Run the next cell and notice the order of the output.


In [ ]:
print("First this line is printed,")
print("and then this one.")

Understanding Check 2 Change the cell above so that it prints out:

First this line,
then the whole 🌏,
and then this one.

Writing Jupyter notebooks

You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you'll need to create your own cells for text and code.

To add a cell, click the + button in the menu bar. It'll start out as a text cell. You can change it to a code cell by clicking inside it so it's highlighted, clicking the drop-down box next to the restart (⟳) button in the menu bar, and choosing "Code".

Understanding Check 3 Add a code cell below this one. Write code in it that prints out:

A whole new cell! ♪🌏♪

(That musical note symbol is like the Earth symbol. Its long-form name is \N{EIGHTH NOTE}.)

Run your cell to verify that it works.

Errors

Python is a language, and like natural human languages, it has rules. It differs from natural language in two important ways:

  1. The rules are simple. You can learn most of them in a few weeks and gain reasonable proficiency with the language in a semester.
  2. The rules are rigid. If you're proficient in a natural language, you can understand a non-proficient speaker, glossing over small mistakes. A computer running Python code is not smart enough to do that.

Whenever you write code, you'll make mistakes. When you run a code cell that has errors, Python will sometimes produce error messages to tell you what you did wrong.

Errors are okay; even experienced programmers make many errors. When you make an error, you just have to find the source of the problem, fix it, and move on.

We have made an error in the next cell. Run it and see what happens.


In [ ]:
print("This line is missing something."

You should see something like this (minus our annotations):

The last line of the error output attempts to tell you what went wrong. The syntax of a language is its structure, and this SyntaxError tells you that you have created an illegal structure. "EOF" means "end of file," so the message is saying Python expected you to write something more (in this case, a right parenthesis) before finishing the cell.

There's a lot of terminology in programming languages, but you don't need to know it all in order to program effectively. If you see a cryptic message like this, you can often get by without deciphering it. (Of course, if you're frustrated, feel free to ask a friend or post on the class Piazza.)

Understanding Check 4 Try to fix the code above so that you can run the cell and see the intended message instead of an error.

Introduction to Programming Concepts

Run the cell below, which will allow you to check if your solutions to some of the coming exercises are correct.


In [ ]:
from client.api.notebook import Notebook
ok = Notebook('Intro.ok')

Part 1: Python basics

Before getting into the more advanced analysis techniques that will be required in this course, we need to cover a few of the foundational elements of programming in Python.

A. Expressions

The departure point for all programming is the concept of the expression. An expression is a combination of variables, operators, and other Python elements that the language interprets and acts upon. Expressions act as a set of instructions to be fed through the interpreter, with the goal of generating specific outcomes. See below for some examples of basic expressions.


In [ ]:
# Examples of expressions:

2 + 2

'me' + ' and I'

12 ** 2

6 + 4

You will notice that only the last line in a cell gets printed out. If you want to see the values of previous expressions, you need to call print on that expression. Try adding print statements to some of the above expressions to get them to display.

B. Variables

In the example below, a and b are Python objects known as variables. We are giving an object (in this case, an integer and a float, two Python data types) a name that we can store for later use. To use that value, we can simply type the name that we stored the value as. Variables are stored within the notebook's environment, meaning stored variable values carry over from cell to cell.


In [ ]:
a = 4
b = 10/5

Notice that when you create a variable, unlike what you previously saw with the expressions, it does not print anything out.


In [ ]:
# Notice that 'a' retains its value.
print(a)
a + b

Question 1: Variables

See if you can write a series of expressions that creates two new variables called x and y and assigns them values of 10.5 and 7.2. Then assign their product to the variable combo and print it.


In [ ]:
# Fill in the missing lines to complete the expressions.
x = ...
...
...
print(...)

Running the cell below will give you some feed back on your responses. Though the OK tests are not always comprehensive (passing all of the tests does not guarantee full credit for questions), they give you a pretty good indication as to whether or not you're on track.


In [ ]:
ok.grade('q01')

C. Lists

The next topic is particularly useful in the kind of data manipulation that you will see throughout 101B. The following few cells will introduce the concept of lists (and their counterpart, numpy arrays). Read through the following cell to understand the basic structure of a list.

A list is an ordered collection of objects. They allow us to store and access groups of variables and other objects for easy access and analysis. Check out this documentation for an in-depth look at the capabilities of lists.

To initialize a list, you use brackets. Putting objects separated by commas in between the brackets will add them to the list.


In [ ]:
# an empty list
lst = []
print(lst)

# reassigning our empty list to a new list
lst = [1, 3, 6, 'lists', 'are' 'fun', 4]
print(lst)

To access a value in the list, put the index of the item you wish to access in brackets following the variable that stores the list. Lists in Python are zero-indexed, so the indicies for lst are 0, 1, 2, 3, 4, 5, and 6.


In [ ]:
# Elements are selected like this:
example = lst[2]

# The above line selects the 3rd element of lst (list indices are 0-offset) and sets it to a variable named example.
print(example)

Slicing lists

As you can see from above, lists do not have to be made up of elements of the same kind. Indices do not have to be taken one at a time, either. Instead, we can take a slice of indices and return the elements at those indices as a separate list.


In [ ]:
### This line will store the first (inclusive) through fourth (exclusive) elements of lst as a new list called lst_2:
lst_2 = lst[1:4]

lst_2

Question 2: Lists

Build a list of length 10 containing whatever elements you'd like. Then, slice it into a new list of length five using a index slicing. Finally, assign the last element in your sliced list to the given variable and print it.


In [ ]:
### Fill in the ellipses to complete the question.
my_list = ...

my_list_sliced = my_list[...]

last_of_sliced = ...

print(...)

In [ ]:
ok.grade('q02')

Lists can also be operated on with a few built-in analysis functions. These include min and max, among others. Lists can also be concatenated together. Find some examples below.


In [ ]:
# A list containing six integers.
a_list = [1, 6, 4, 8, 13, 2]

# Another list containing six integers.
b_list = [4, 5, 2, 14, 9, 11]

print('Max of a_list:', max(a_list))
print('Min of b_list:', min(a_list))

# Concatenate a_list and b_list:
c_list = a_list + b_list
print('Concatenated:', c_list)

D. Numpy Arrays

Closely related to the concept of a list is the array, a nested sequence of elements that is structurally identical to a list. Arrays, however, can be operated on arithmetically with much more versatility than regular lists. For the purpose of later data manipulation, we'll access arrays through Numpy, which will require an import statement.

Now run the next cell to import the numpy library into your notebook, and examine how numpy arrays can be used.


In [ ]:
import numpy as np

In [ ]:
# Initialize an array of integers 0 through 9.
example_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# This can also be accomplished using np.arange
example_array_2 = np.arange(10)
print('Undoubled Array:')
print(example_array_2)

# Double the values in example_array and print the new array.
double_array = example_array*2
print('Doubled Array:')
print(double_array)

This behavior differs from that of a list. See below what happens if you multiply a list.


In [ ]:
example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
example_list * 2

Notice that instead of multiplying each of the elements by two, multiplying a list and a number returns that many copies of that list. This is the reason that we will sometimes use Numpy over lists. Other mathematical operations have interesting behaviors with lists that you should explore on your own.

E. Looping

Loops are often useful in manipulating, iterating over, or transforming large lists and arrays. The first type we will discuss is the for loop. For loops are helpful in traversing a list and performing an action at each element. For example, the following code moves through every element in example_array, adds it to the previous element in example_array, and copies this sum to a new array.


In [ ]:
new_list = []

for element in example_array:
    new_element = element + 5
    new_list.append(new_element)

new_list

The most important line in the above cell is the "for element in..." line. This statement sets the structure of our loop, instructing the machine to stop at every number in example_array, perform the indicated operations, and then move on. Once Python has stopped at every element in example_array, the loop is completed and the final line, which outputs new_list, is executed. It's important to note that "element" is an arbitrary variable name used to represent whichever index value the loop is currently operating on. We can change the variable name to whatever we want and achieve the same result, as long as we stay consistent. For example:


In [ ]:
newer_list = []

for completely_arbitrary_name in example_array:
    newer_element = completely_arbitrary_name + 5
    newer_list.append(newer_element)
    
newer_list

For loops can also iterate over ranges of numerical values. If I wanted to alter example_array without copying it over to a new list, I would use a numerical iterator to access list indices rather than the elements themselves. This iterator, called i, would range from 0, the value of the first index, to 9, the value of the last. I can make sure of this by using the built-in range and len functions.


In [ ]:
for i in range(len(example_array)):
    example_array[i] = example_array[i] + 5

example_array

Other types of loops

The while loop repeatedly performs operations until a conditional is no longer satisfied. A conditional is a boolean expression, that is an expression that evaluates to True or False.

In the below example, an array of integers 0 to 9 is generated. When the program enters the while loop on the subsequent line, it notices that the maximum value of the array is less than 50. Because of this, it adds 1 to the fifth element, as instructed. Once the instructions embedded in the loop are complete, the program refers back to the conditional. Again, the maximum value is less than 50. This process repeats until the the fifth element, now the maximum value of the array, is equal to 50, at which point the conditional is no longer true and the loop breaks.


In [ ]:
while_array = np.arange(10)        # Generate our array of values

print('Before:', while_array)

while(max(while_array) < 50):      # Set our conditional
    while_array[4] += 1            # Add 1 to the fifth element if the conditional is satisfied 
    
print('After:', while_array)

Question 3: Loops

In the following cell, partial steps to manipulate an array are included. You must fill in the blanks to accomplish the following:

  1. Iterate over the entire array, checking if each element is a multiple of 5
  2. If an element is not a multiple of 5, add 1 to it repeatedly until it is
  3. Iterate back over the list and print each element.

Hint: To check if an integer x is a multiple of y, use the modulus operator %. Typing x % y will return the remainder when x is divided by y. Therefore, (x % y != 0) will return True when y does not divide x, and False when it does.


In [ ]:
# Make use of iterators, range, length, while loops, and indices to complete this question.
question_3 = np.array([12, 31, 50, 0, 22, 28, 19, 105, 44, 12, 77])

for i in range(len(...)):
    while(...):
        question_3[i] = ...
        
for element in question_3:
    print(...)

In [ ]:
ok.grade('q03')

F. Functions!

Functions are useful when you want to repeat a series of steps on multiple different objects, but don't want to type out the steps over and over again. Many functions are built into Python already; for example, you've already made use of len() to retrieve the number of elements in a list. You can also write your own functions, and at this point you already have the skills to do so.

Functions generally take a set of parameters (also called inputs), which define the objects they will use when they are run. For example, the len() function takes a list or array as its parameter, and returns the length of that list.

The following cell gives an example of an extremely simple function, called add_two, which takes as its parameter an integer and returns that integer with, you guessed it, 2 added to it.


In [ ]:
# An adder function that adds 2 to the given n.
def add_two(n):
    return n + 2

In [ ]:
add_two(5)

Easy enough, right? Let's look at a function that takes two parameters, compares them somehow, and then returns a boolean value (True or False) depending on the comparison. The is_multiple function below takes as parameters an integer m and an integer n, checks if m is a multiple of n, and returns True if it is. Otherwise, it returns False.

if statements, just like while loops, are dependent on boolean expressions. If the conditional is True, then the following indented code block will be executed. If the conditional evaluates to False, then the code block will be skipped over. Read more about if statements here.


In [ ]:
def is_multiple(m, n):
    if (m % n == 0):
        return True
    else:
        return False

In [ ]:
is_multiple(12, 4)

In [ ]:
is_multiple(12, 7)

Sidenote: Another way to write is_multiple is below, think about why it works.

def is_multiple(m, n):
    return m % n == 0

Since functions are so easily replicable, we can include them in loops if we want. For instance, our is_multiple function can be used to check if a number is prime! See for yourself by testing some possible prime numbers in the cell below.


In [ ]:
# Change possible_prime to any integer to test its primality
# NOTE: If you happen to stumble across a large (> 8 digits) prime number, the cell could take a very, very long time
# to run and will likely crash your kernel. Just click kernel>interrupt if it looks like it's caught.

possible_prime = 9999991

for i in range(2, possible_prime):
    if (is_multiple(possible_prime, i)):
        print(possible_prime, 'is not prime')   
        break
    if (i >= possible_prime/2):
        print(possible_prime, 'is prime')
        break

Question 4: Writing functions

In the following cell, complete a function that will take as its parameters a list and two integers x and y, iterate through the list, and replace any number in the list that is a multiple of x with y.

Hint: use the is_multiple() function to streamline your code.


In [ ]:
def replace_with_y(lst, x, y):
    for i in range(...):
        if(...):
            ...
    return lst

In [ ]:
ok.grade('q04')

Part 2: Pandas Dataframes

We will be using Pandas dataframes for much of this class to organize and sort through economic data. Pandas is one of the most widely used Python libraries in data science. It is commonly used for data cleaning, and with good reason: it’s very powerful and flexible, among many other things. Like we did with numpy, we will have to import pandas.


In [ ]:
import pandas as pd

Creating dataframes

The rows and columns of a pandas dataframe are essentially a collection of lists stacked on top/next to each other. For example, if I wanted to store the top 10 movies and their ratings in a datatable, I could create 10 lists that each contain a rating and a corresponding title, and these lists would be the rows of the table:


In [ ]:
top_10_movies = pd.DataFrame(data=np.array(
            [[9.2, 'The Shawshank Redemption (1994)'],
            [9.2, 'The Godfather (1972)'],
            [9., 'The Godfather: Part II (1974)'],
            [8.9, 'Pulp Fiction (1994)'],
            [8.9, "Schindler's List (1993)"],
            [8.9, 'The Lord of the Rings: The Return of the King (2003)'],
            [8.9, '12 Angry Men (1957)'],
            [8.9, 'The Dark Knight (2008)'],
            [8.9, 'Il buono, il brutto, il cattivo (1966)'],
            [8.8, 'The Lord of the Rings: The Fellowship of the Ring (2001)']]), columns=["Rating", "Movie"])
top_10_movies

Alternatively, we can store data in a dictionary instead of in lists. A dictionary keeps a mapping of keys to a set of values, and each key is unique. Using our top 10 movies example, we could create a dictionary that contains ratings a key, and movie titles as another key.


In [ ]:
top_10_movies_dict = {"Rating" : [9.2, 9.2, 9., 8.9, 8.9, 8.9, 8.9, 8.9, 8.9, 8.8], 
                      "Movie" : ['The Shawshank Redemption (1994)',
                                'The Godfather (1972)',
                                'The Godfather: Part II (1974)',
                                'Pulp Fiction (1994)',
                                "Schindler's List (1993)",
                                'The Lord of the Rings: The Return of the King (2003)',
                                '12 Angry Men (1957)',
                                'The Dark Knight (2008)',
                                'Il buono, il brutto, il cattivo (1966)',
                                'The Lord of the Rings: The Fellowship of the Ring (2001)']}

Now, we can use this dictionary to create a table with columns Rating and Movie


In [ ]:
top_10_movies_2 = pd.DataFrame(data=top_10_movies_dict, columns=["Rating", "Movie"])
top_10_movies_2

Notice how both ways return the same table! However, the list method created the table by essentially taking the lists and making up the rows of the table, while the dictionary method took the keys from the dictionary to make up the columns of the table. In this way, dataframes can be viewed as a collection of basic data structures, either through collecting rows or columns.

Reading in Dataframes

Luckily for you, most datatables in this course will be premade and given to you in a form that is easily read into a pandas method, which creates the table for you. A common file type that is used for economic data is a Comma-Separated Values (.csv) file, which stores tabular data. It is not necessary for you to know exactly how .csv files store data, but you should know how to read a file in as a pandas dataframe.

We will read in a .csv file that contains data from Spring 2017 about VOT and closure.


In [ ]:
# Run this cell to read in the table
vot = pd.read_csv("data/vots.csv")

The pd.read_csv function expects a path to a .csv file as its input, and will return a data table created from the data contained in the csv. We have provided vots.csv in the data directory, which is all contained in the current working directory (aka the folder this assignment is contained in). For this reason, we must specify to the read_csv function that it should look for the csv in the data directory, and the / indicates that vots.csv can be found there.

Here is a sample of some of the rows in this datatable:


In [ ]:
vot.head()

Indexing Dataframes

Oftentimes, tables will contain a lot of extraneous data that muddles our data tables, making it more difficult to quickly and accurately obtain the data we need. To correct for this, we can select out columns or rows that we need by indexing our dataframes.

The easiest way to index into a table is with square bracket notation. Suppose you wanted to obtain all of the closure data from the data. Using a single pair of square brackets, you could index the table for "closure"


In [ ]:
# Run this cell and see what it outputs
vot["closure"]

Notice how the above cell returns an array of all the closure values in their original order. Now, if you wanted to get the first closure value from this array, you could index it with another pair of square brackets:


In [ ]:
vot["closure"][0]

Pandas columns have many of the same properties as numpy arrays. Keep in mind that pandas dataframes, as well as many other data structures, are zero-indexed, meaning indexes start at 0 and end at the number of elements minus one.

If you wanted to create a new datatable with select columns from the original table, you can index with double brackets.


In [ ]:
## Note: .head() returns the first five rows of the table
vot[['pclo', 'tclo', 'kclo', 'pvot', 'tvot', 'kvot']].head()

You can also use column indices instead of names.


In [ ]:
vot[[0, 1, 2, 3]].head()

Alternatively, you can also get rid of columns you dont need using .drop()


In [ ]:
vot.drop("gender", axis=1).head()

Finally, you can use square bracket notation to index rows by their indices with a single set of brackets. You must specify a range of values for which you want to index. For example, if I wanted the 20th to 30th rows of accounts:


In [ ]:
vot[20:31]

Filtering Data

As you can tell from the previous, indexing rows based on indices is only useful when you know the specific set of rows that you need, and you can only really get a range of entries. Working with data often involves huge datasets, making it inefficient and sometimes impossible to know exactly what indices to be looking at. On top of that, most data analysis concerns itself with looking for patterns or specific conditions in the data, which is impossible to look for with simple index based sorting.

Thankfully, you can also use square bracket notation to filter out data based on a condition. Suppose we only wanted closure and vot data for individuals taller than 160 centimeters:


In [ ]:
vot[vot["height"] >= 170][["closure", "vot"]]

The vot table is being indexed by the condition vot["height"] >= 170, which returns a table where only rows that have a "height" greater than $170$ is returned. We then index this table with the double bracket notation from the previous section to only get the closure and vot columns.

Suppose now we wanted a table with data from English speakers, and where the t VOT was less than .01 or k VOT is greater than .08.


In [ ]:
vot[(vot["language"] == "English") & ((vot["tvot"] < .01) | (vot["kvot"] > .08))]

Many different conditions can be included to filter, and you can use & and | operators to connect them together. Make sure to include parantheses for each condition!

Another way to reorganize data to make it more convenient is to sort the data by the values in a specific column. For example, if we wanted to find the the tallest person in our data set, we could sort the table by height:


In [ ]:
vot.sort_values("height")

But wait! The table looks like it's sorted in increasing order. This is because sort_values defaults to ordering the column in ascending order. To correct this, add in the extra optional parameter


In [ ]:
vot.sort_values("height", ascending=False)

Now we can clearly see that the tallest person was 185.42 cm tall.

Useful Functions for Numeric Data

Here are a few useful functions when dealing with numeric data columns. To find the minimum value in a column, call min() on a column of the table.


In [ ]:
vot["pvot"].min()

To find the maximum value, call max().


In [ ]:
vot["pvot"].max()

And to find the average value of a column, use mean().


In [ ]:
vot["pvot"].mean()

Part 3: Visualization

Now that you can read in data and manipulate it, you are now ready to learn about how to visualize data. To begin, run the cells below to import the required packages we will be using.


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

One of the advantages of pandas is its built-in plotting methods. We can simply call .plot() on a dataframe to plot columns against one another. All that we have to do is specify which column to plot on which axis. We include the extra variable kind='scatter' to override the default lineplot.


In [ ]:
vot.plot(x='pvot', y='kvot', kind='scatter')

The base package for most plotting in Python is matplotlib. Below we will look at how to plot with it. First we will extract the columns that we are interested in, then plot them in a scatter plot. Note that plt is the common convention for matplotlib.pyplot.


In [ ]:
closure = vot['tclo']
height = vot['height']

#Plot the data by inputting the x and y axis
plt.scatter(closure, height)

# we can then go on to customize the plot with labels
plt.xlabel("T Closure")
plt.ylabel("Height")

Though matplotlib is sometimes considered an "ugly" plotting tool, it is powerful. It is highly customizable and is the foundation for most Python plotting libraries. Check out the documentation to get a sense of all of the things you can do with it, which extend far beyond scatter and line plots. An arguably more attractive package is seaborn, which we will go over in future notebooks.

Question 5: Plotting

Try plotting two of the closure columns against one another.


In [ ]:
pclo = ...
kclo = ...

plt.scatter(pclo, kclo)
plt.xlabel(...)
plt.ylabel(...)

# note: plt.show() is the equivalent of print, but for graphs
plt.show()

In [ ]:
ok.grade('q05')

Some materials this notebook were taken from Data 8, CS 61A, and DS Modules lessons.