# Lab 12: Models as HOFs

*Due at 9:00pm on 11/28/2018.*

## Starter Files

Download lab12.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.

## Submission

When you are done, submit the lab by uploading the `lab12.py`

file to okpy.org. You may submit more than once before the
deadline; only the final submission will be graded.

- To receive credit for this lab, you must complete Questions 1 and 2 in lab12.py and submit through OK.

## Review on HOFs

### Question 1: Polynomial

A polynomial function is a function with coefficients, variables and constants. A polynomial function is said to be the nth degree polynomial if there is a term in the function with the variable to the nth degree. For example, a 4th degree polynomial must contain the term x^4 with some coefficient multiplied to it.

Complete the function `polynomial`

, which takes in a degree and a list of coefficients. The function should output the corresponding polynomial function.

```
def polynomial(degree, coeffs):
"""
>>> fourth = polynomial(4, [3,6,2,1, 100])
>>> fourth(3) # 3*(3**4) + 6*(3**3) + 2*(3**2) + 1*(3**1) + 100
526
>>> third = polynomial(3, [2, 0, 0, 0])
>>> third(4) # 2*(4**3) + 0*(4**2) + 0*(4**1) + 0
128
"""
"*** YOUR CODE HERE ***"
return ______
def poly_func(x):
result = 0
nonlocal degree
for coefficient in coeffs:
result = coefficient*x**degree + result
degree -= 1
return result
return poly_func
```

Use OK to test your code:

`python3 ok -q polynomial --local`

### Question 2: Regression

Remember regression from Data 8? Here, we will build a regression model function together step-by-step in Python.

For Data8's lecture on linear regression, look here

To calculate standard units, search here

**A note on testing and assert statements.**

`Assert`

statements are a good way to make sure that your function is working the way you want it to work. You can use assert statement two ways, with or without an error message. To use it without an error message:

`>>> assert len([1,2,3]) == 3`

If the assertion statement pass (i.e., there is no problem with your statement), nothing happens. But if your statement does not hold true, `AssertionError`

will be displayed.

To use assert statement with an error message, add a string after a comma:

`>>> assert len([1,2,3]) == 3, "Length of list is not 3!!!!!!!"`

Instead of `AssertionError`

, your string message will be displayed instead.

Complete these functions below. No doctests will be provided except for the `regression`

function, so fill your test functions with assert statements to make sure your code works. Some examples have been provided. `testregression`

is optional, but it's a good practice!

```
def mean(x):
"""
Return the mean of x, where x is a list of data points.
"""
"*** YOUR CODE HERE ***"
return ""
return sum(x)/len(x)
def testmean():
assert mean([0,0,0,0,0]) == 0, "please pass me!"
"""***PUT YOUR TEST HERE***"""
def sd(x):
"""
Return the standard deviation of x, where x is a list of data points.
"""
"*** YOUR CODE HERE ***"
return ""
avg = mean(x)
result = 0
result = sum((each-avg)**2 for each in x)
return (result/len(x))**(1/2)
def testsd():
assert sd([0,0,0,0,0]) == 0, "pass me please!"
"""***PUT YOUR TEST HERE***"""
def standard_units(x):
"""
Return the standard units of x, where x is a list of data points.
"""
"*** YOUR CODE HERE ***"
return ""
avg = mean(x)
stand_dev = sd(x)
result = [(each-avg)/stand_dev for each in x]
return result
def testsu():
"""***PUT YOUR TEST HERE***"""
def corr_coeff(x, y):
"""
Return the correlation coefficients (r) of x and y, where x is a list of
data points and y is a list of data points corresponding to x.
"""
"*** YOUR CODE HERE ***"
return ""
x_stand = standard_units(x)
y_stand = standard_units(y)
product = [x_stand[i]*y_stand[i] for i in range(len(x_stand))]
return mean(product)
def testcorr_coeff():
"""***PUT YOUR TEST HERE***"""
def regression(points):
"""
Return a function that takes in x and predicts y, based on a linear regression model.
>>> tempVIcecream = [[35,1], [33,2], [37,3], [45,5],[47,6],[42,8],[43,9],[51,10]]
>>> predictor = regression(tempVIcecream)
>>> predictor(62) # Round to the third decimal place
14.597
"""
"*** YOUR CODE HERE ***"
return ""
x = [point[0] for point in points]
y = [point[1] for point in points]
def model(given):
r = corr_coeff(x, y)
sd_x = sd(x)
sd_y = sd(y)
mean_x = mean(x)
mean_y = mean(y)
slope = r*(sd_y/sd_x)
intercept = mean_y - slope*mean_x
return round(slope*given+intercept, 3)
return model
def testregression():
"""***PUT YOUR TEST HERE***"""
```

Use OK to test your code:

`python3 ok -q regression --local`

## SQLite

### Setup

The simplest way to start using SQLite is to download a precompiled binary from the SQLite website. The latest version of SQLite at the time of writing is 3.24.0, but you can check for additional updates on the website.

#### Windows

- Visit the download page linked above and navigate to the section Precompiled Binaries for Windows. Click on the link sqlite-tools-win32-x86-*.zip to download the binary.
- Unzip the file. There should be a
`sqlite3.exe`

file in the directory after extraction. Navigate to the folder containing the

`sqlite3.exe`

file and check that the version is at least 3.8.3:`$ cd path/to/sqlite $ ./sqlite3 --version 3.12.1 2016-04-08 15:09:49 fe7d3b75fe1bde41511b323925af8ae1b910bc4d`

#### MacOS Yosemite (10.10) or newer

SQLite comes pre-installed. Check that you have a version that's greater than 3.8.3:

```
$ sqlite3
SQLite version 3.8.10.2
```

#### Mac OS X Mavericks (10.9) or older

SQLite comes pre-installed, but it is the wrong version.

- Visit the download page linked above and navigate to the section Precompiled Binaries for Mac OS X (x86). Click on the link sqlite-tools-osx-x86-*.zip to download the binary.
- Unzip the file. There should be a sqlite3 file in the directory after extraction.
Navigate to the folder containing the sqlite3 file and check that the version is at least 3.8.3:

`$ cd path/to/sqlite $ ./sqlite3 --version .12.1 2016-04-08 15:09:49 fe7d3b75fe1bde41511b323925af8ae1b910bc4d`

#### Ubuntu

The easiest way to use SQLite on Ubuntu is to install it straight from the native repositories (the version will be slightly behind the most recent release):

```
$ sudo apt install sqlite3
$ sqlite3 --version
3.8.6 2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e
```

### Usage

You can start an interactive SQLite session in your Terminal or Git Bash with the following commands:

Ubuntu / Mac OS X (Yosemite or newer)

`sqlite3`

Windows / Mac OS X (Mavericks or older)

`./sqlite3`

While the interpreter is running, you can type `.help`

to see some of the commands you can run.

To exit out of the SQLite intepreter, type `.exit`

or `.quit`

or press `Ctrl-C`

. Remember that if you see `...>`

after pressing return, you probably forgot a `;`

.

You can also run all the statements in a `.sql`

file by doing the following:

Runs your code and then exits SQLite immediately afterwards.

Ubuntu / Mac OS X (Yosemite or newer)

`sqlite3 < lab12.sql`

Windows / Mac OS X (Mavericks or older)

`./sqlite3 < lab12.sql`

Runs your code and then opens an interactive SQLite session, which is similar to running Python code with the interactive -i flag.

Ubuntu / Mac OS X (Yosemite or newer)

`sqlite3 --init lab12.sql`

Windows / Mac OS X (Mavericks or older)

`./sqlite3 --init lab12.sql`