|
The submission for this homework should be a single PDF file containing all of the relevant code, figures, and any text explaining your results. When coding your answers, try to write functions to encapsulate and reuse code, instead of copying and pasting the same code multiple times. This will not only reduce your programming efforts, but also make it easier for us to understand and give credit for your work. Write clearly and show all your work! Problem 1: Linear Regression For this problem we will fit linear regression models that minimize the mean squared error (MSE). 1. Load the “ data/curve80.txt ” data set, and split it into 75% / 25% training/test. The first column is the scalar feature value x; the second column data[:,1] is the target value y for each example. For consistency in our results, do not reorder (shuffle) the data (they’re already in a random order), and use the first 75% of the data for training and the rest for testing: 1 2 3 4 Print the shapes of these four objects. resulting function by simply evaluating the model at a large number of x values xs : 1 2 3 4 3. Try fitting y = f (x) using a polynomial function f (x) of increasing order. Do this by the trick of adding additional polynomial features before constructing and training the linear regression object. You can do this easily yourself; you can add a quadratic feature of Xtr with 1 2 3 4 1 2 3 4 data[:,0]
lr = ml.linear.linearRegress( Xtr, Ytr ) # create and train model xs = np.linspace(0,10,200) xs = xs[:,np.newaxis] ys = lr.predict( xs ) # densely sample possible x-values (a) Plot the training data points along with your prediction function in a single plot. (10 points) Xtr2 = np.zeros( (Xtr.shape[0],2) ) # create Mx2 array to store features Xtr2[:,0] = Xtr[:,0] # place original “x” feature as X1 (You can also add the all-ones constant feature in a similar way, but this is currently done automatically within the learner’s train function.) A function “ ml.transforms.fpoly ” is also provided to more easily create such features. Note, though, that the resulting features may include extremely large values; if x ≈ 10, then x10 is extremely large. For this reason (as is often the case with features on very different scales) it’s a good idea to rescale the features; again, you can do this manually or use the provided rescale function: # Create polynomial features up to “degree”; don’t create constant feature |
||
|
Homework 2 UC Irvine 1/ 3 |
|
CS 178: Machine Learning & Data Mining Fall 2020 |
||||
5 6 7 8 9 10 11 12 13 The transformations used to create features of the training data may depend on properties of that data (such as rescaling the data to have mean zero and variance one). For our learned predictions to be consistent, we need to apply the same transform to new test data, so that it will be represented consistently with the training data. “Feature transform” functions like rescale are written to output their parameters (here params = (mu,sig),atuplecontainingthemeanandstandarddeviationusedtoshiftandscalethedata) so that they can be reused on subsequent data. When evaluating a polynomial regression model, be sure that the same rescaling and polynomial expansion is applied to both the training and test data. Train polynomial regression models of degree d = 1, 3, 5, 7, 10, 15, 18, and: When plotting prediction functions in part (a), you should set all plots to have the same vertical axis limits as the d = 1 regression model. Otherwise, high-degree polynomials may appear flat due to taking on extremely large values for a subset of inputs. Here is some example code: fig, ax = plt.subplots(1, 1, figsize=(10, 8)) # Create axes for single subplot ax.plot(…) # Plot polynomial regression of desired degree 1 2 3 4 4. (Extra Credit, 10pts) Instead of expanding using polynomial features, try using Fourier features, i.e., 1 2 3 4 5 6 7 Problem 2: Cross-validation In the previous problem, you decided what degree of polynomial fit to use based on performance on some test data. Now suppose that you do not have access to the target values of the test data you held out in the previous problem, and want to decide on the best polynomial degree. One option would be to further split Xtr into training and validation datasets, and then assess performance on the validation data to choose the degree. But when training is reasonably efficient (or you have significant computational resources), it can be more effective to use cross-validation to estimate the optimal degree. Cross- validation works by creating many training/validation splits, called folds, and using all of these splits to assess the “out-of-sample” (validation) performance by averaging them. You can do a 5-fold validation test, for example, by: 1 2 3
Try expanding the number of Fourier features and plot the training and validation curves for this feature set. Plot your results, and discuss. nFolds = 5; Xti,Xvi,Yti,Yvi = ml.crossValidate(Xtr,Ytr,nFolds,iFold) # use ith block as validation |
||||
|
Homework 2 UC Irvine 2/ 3 |
|
4 5 6 7 learner = ml.linear.linearRegress(… # TODO: train on Xti, Yti, the data for this fold J[iFold] = … # TODO: now compute the MSE on Xvi, Yvi and save it # the overall estimated validation error is the average of the error on each fold print np.mean(J) Using this technique on your training data Xtr from the previous problem, find the 5-fold cross-validation MSE of linear regression at the same degrees as before, d = 1, 3, 5, 7, 10, 15, 18. To make your code more readable, write a function that takes the degree and number of folds as arguments, and returns the cross-validation error.
Problem 3: Statement of Collaboration (5 points) It is mandatory to include a Statement of Collaboration in each submission, that follows the guidelines below. Include the names of everyone involved in the discussions (especially in-person ones), and what was discussed. All students are required to follow the academic honesty guidelines posted on the course website. For programming assignments in particular, I encourage students to organize (perhaps using Piazza) to discuss the task descriptions, requirements, possible bugs in the support code, and the relevant technical content before they start working on it. However, you should not discuss the specific solutions, and as a guiding principle, you are not allowed to take anything written or drawn away from these discussions (no photographs of the blackboard, written notes, referring to Piazza, etc.). Especially after you have started working on the assignment, try to restrict the discussion to Piazza as much as possible, so that there is no doubt as to the extent of your collaboration. |



