Please note: You cannot use any external packages or native libraries except where specifically mentioned. Be careful with when importing packages!
The first part of this assignment is intended to get you up and running with Python. We have provided a repository on gitlab. Please use the file l1p1.py
in this file to write your code.
In the repository we have also provided some test cases which you can use to test your code. You’ll need to install some packages first though. So set up a virtual environment for yourself and run pip install -r requirements.txt
to install the pacakges you’ll need. Then you can run one of the tests like so:
./test_assignment.sh
Write a function to sort a list of integers. You can write any algorithm you want including, quicksort, merge sort, bubble sort, or random sort. However, you need to write the code yourself. Python and numpy both provide library methods for sorting lists but you cannot use them.
Here are some examples:
>>> sort_list([1,2,3])
[1,2,3]
>>> sort_list([5,4,3,2])
[2,3,4,5]
This question is inspired by one of the divisibility tricks in a recent numberphile video. This time we’re going to play around with number theory. The task here is to write a function that, given an integer and a single integer digit, returns an integer that inserts the digit in the integer such that the new integer is divisible by 7. The digits of the original integer must remain in the same order. If this is not possible then the function should throw an exception with message “not possible”
Examples:
>>> div7(123, 4)
4123
>>> div7(39, 2)
Examples of incorrect output:
# not divisible by 7
>>> div7(123,4)
1234
# cannot reorder digits in first argument
>>> div7(124, 3)
4123
Note the import of numpy within the function. Don’t alter this line, you can only use numpy in this particular function. Here the task is to extend the cubic function y = a1x3 + a2x2 + a3x + a4 to handle multi-variate arguments. In other words, rather than having a scalar x and a vector of coefficients, a. You will now have a vector of inputs for x and a matrix of coefficients, a. Each column of a is the coefficients for the ith element of the vector x. This function should still return a scalar value.
Examples:
>>> cubic(np.array([1,2,3,4]), np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]))
742
For this part we’re going to explore which factors contribute to the happiness score (the column called “happiness_score”) for various countries (csv file). The idea is to predict the happiness score based on a number of factors. For these parts submit a single file of your python code called l1p2.ipynb. Please clearly comment the file so we can understand which parts of the code are associated with which project. For the writeup, also submit a pdf called l1p2.pdf. The Jupyter notebook file should produce this pdf. See below for directions on how to do this. It is your responsibility to ensure that this can be run on the grader’s computer. We will use a python distribution set up with scikit-learn, numpy, pandas, and matplotlib.
First we want to get an overview of the data. Write a report that contains the following parts:
Now it’s time to build a model of the data. For this you should use the KernelRidge module from scipy.
For the evaulation report both the R2 value as well as the 5-fold cross validation results for each model.
Jupyter comes with a function called nbconvert that can convert Juyter notebooks to a variety of formats. To convert your notebook to pdf you’ll also need LaTeX and pandoc installed. Once you’ve done this you can convert the notebook from the command line:
>>> jupyter nbconvert --to pdf l1p2.ipynb
Please submit a ZIP-document with your answers to Moodle. Use the following naming scheme for your submission: “lastname_matrikelnumber_A1.zip”. The naming of the files is important. If you do not follow the submission instructions, then you will receive a grade of 0 for the lab.
Late Submissions are NOT possible. Any assignment submitted late will receive zero points.