Starting from:

$30

CSC311-Homework 1 Code Solved


(a)   For each choice of dimension d ∈ [20,21,22,...,210], sample 100 points from the unit cube, and record the average squared Euclidean distance between all pairs of points, as well as the standard deviation of the squared Euclidean distances. (Recall that squared Euclidean distance is defined as kx − yk2 = Pj(xj − yj)2.) Plot both the average and the standard deviation as a function of d. (You may wish to use np.mean and np.std to compute the statistics, and matplotlib for plotting. You may find numpy.random.rand helpful in sampling from the unit cube.) Include the output figure in your solution PDF (hw1_writeup.pdf).

(b In this question, we aim to verify our simulations in (a) by deriving the analytical form of averaged distance and variance of distance. Suppose we sample two points X and Y independently from a unit cube in d dimensions and define the squared Euclidean distance R = Z1+···+Zd with Zi = (Xi−Yi)2. Given that  and Var[ , determine E[R] and Var[R] using the properties of expectation and variance. You may give your answer in terms of the dimension d.

Basic rule of expectation and variance: E[Zi+Zj] = E[Zi]+E[Zj] and if we further know Zi and Zj are independent, then Var[Zi + Zj] = Var[Zi] + Var[Zj].

(c)    [Optional, 0pts] In probability theory, one can derive that  for any random variable Z. (This fact is known as Markov’s Inequality.) Based on your answer to part (b), explain why does this support the claim that in high dimensions, “most points are far away, and approximately the same distance”?

2.  Decision Trees. This question is taken from a project by Lisa Zhang and Michael Guerzhoy.

In this question, you will use the scikit-learn decision tree classifier to classify real vs. fake news headlines. The aim of this question is for you to read the scikit-learn API and get comfortable with training/validation splits.

We will use a dataset of 1298 “fake news” headlines (which mostly include headlines of articles classified as biased, etc.) and 1968 “real” news headlines, where the “fake news” headlines are from https://www.kaggle.com/mrisdal/fake-news/data and “real news” headlines are from https://www.kaggle.com/therohk/million-headlines. The data were cleaned by removing words from fake news titles that are not a part of the headline, removing special characters from the headlines, and restricting real news headlines to those after October 2016 containing the word “trump”. For your interest, the cleaning script is available as clean_script.py on the course web page, but you do not need to run it. The cleaned-up data are available as clean_real.txt and clean_fake.txt on the course web page.

Each headline appears as a single line in the data file. Words in the headline are separated by spaces, so just use str.split() in Python to split the headlines into words.

You will build a decision tree to classify real vs. fake news headlines. Instead of coding the decision trees yourself, you will do what we normally do in practice — use an existing implementation. You should use the DecisionTreeClassifier included in sklearn. Note that figuring out how to use this implementation is a part of the assignment.

Here’s a link to the documentation of sklearn.tree.DecisionTreeClassifier: http:// scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier. html

All code should be included in the file hw1_code.py which you submit through MarkUs.

(a)   Write a function load_data which loads the data, preprocesses it using a vectorizer

(http://scikit-learn.org/stable/modules/classes.html#module-sklearn.feature_ extraction.text), and splits the entire dataset randomly into 70% training, 15% validation, and 15% test examples.

(b)   Write a function select_model which trains the decision tree classifier using at least 5 different values of max_depth, as well as two different split criteria (information gain and Gini coefficient), evaluates the performance of each one on the validation set, and prints the resulting accuracies of each model. You should use DecisionTreeClassifier, but you should write the validation code yourself. Include the output of this function in your solution PDF (hw1_writeup.pdf).

(c)   Now let’s stick with the hyperparameters which achieved the highest validation accuracy. Extract and visualize the first two layers of the tree. Your visualization may look something like what is shown below, but it does not have to be an image: it is perfectly fine to display text. It may be hand-drawn. Include your visualization in your solution PDF (hw1_writeup.pdf).

(d)  Write a function compute_information_gain which computes the information gain of a split on the training data. That is, compute I(Y,xi), where Y is the random variable signifying whether the headline is real or fake, and xi is the keyword chosen for the split.

Report the outputs of this function for the topmost split from the previous part, and for several other keywords.

3.Regularized Linear Regression. For this problem, we will use the linear regression model from the lecture:

D y = Xwjxj + b.

j=1
In lecture, we saw that regression models with too much capacity can overfit the training data and fail to generalize. We also saw that one way to improve generalization is regularization: adding a term to the cost function which favors some explanations over others. For instance, we might prefer that weights not grow too large in magnitude. We can encourage them to stay small by adding a penalty:

to the cost function, for some λ > 0. It is also possible to apply different regularization penalties in each dimension. The formulation would be:

where i indexes the data points, βj ≥ 0 for all j, and J is the same squared error cost function from lecture. Note that in this formulation, there is no regularization penalty on the bias parameter. Also note that when βj = 0, you don’t apply any regularization on j-th dimension. For this question, show your work in detail as most points are allocated in showing how you obtained your answer.

(a)   Determine the gradient descent update rules for the regularized cost function Jregβ . Your answer should have the form:

wj ← ··· b ← ···

This form of regularization is sometimes called “weight decay”. Based on this update rule, why do you suppose that is?

(b)   It’s also possible to solve the regularized regression problem directly by setting the partial derivatives equal to zero. In this part, for simplicity, we will drop the bias term from the model, so our model is:

D y = Xwjxj.
It is possible to derive a system of linear equations of the following form for Jregβ :
0
Determine formulas for Ajj0 and cj.

Based on your answer to part (b), determine formulas for A and c, and derive a closed-form solution for the parameter w. Note that, as usual, the inputs are organized into a design matrix X with one row per training example
 

More products