|
This homework (and many subsequent ones) involves data analysis, and discussion of methods and results, using Python. You must submit a single PDF file that contains all answers, including any text needed to describe your results, the complete code snippets used to answer each problem, any figures that were generated, and scans of any (clearly readable) work on paper that you want the graders to consider. It is important that you include enough detail that we know how you solved each problem, since otherwise we will be unable to grade it. We recommend that you use Jupyter/iPython notebooks to write your report. It will help you not only ensure all of the code for the solutions is included, but also provide an easy way to export your results to a PDF file.1 We recommend liberal use of Markdown cells to create headers for each problem and sub-problem, explaining your implementation/answers, and including any mathematical equations. For parts of the homework you do on paper, scan it in such that it is legible (there are a number of free Android/iOS scanning apps, if you do not have access to a scanner), and include it as an image in the iPython notebook. If you have any questions about using iPython, ask us on Piazza. If you decide not to use iPython notebooks, and instead create your PDF file with Word or LaTeX, make sure all of the answers can be generated from the code snippets included in the document. TL;DR: (1) submit a single, standalone PDF report, with all code; (2) I recommed iPython notebooks. Problem 0: Get Connected (0 points, but it will make the course easier!) Please visit our class forum on Piazza: piazza.com/uci/fall2020/cs178. Piazza will be the place to post your questions and discussions, rather than by email to the instructor or TA. Often, other students have the same or similar questions, and will be helped by seeing the online discussion. Problem 1: Python & Data Exploration In this problem, we will compute some basic statistics and create visualizations of an example data set. First, download the zip file for Homework 1, which contains some course code (the mltools directory) and a dataset of New York area real estate sales, “nyc_housing”. Load the data into Python: 1 2 3 4 5 6
These data are from the “NYC Open Data” initiative, and consist of three real-valued features and a class value Y representing in which of three boroughs the house or apartment was located (Manhattan, the Bronx, or Staten Island). 1. Use X.shape to get the number of features and the number of data points. Report both numbers, mentioning which number is which. (5 points)
1For example, by doing a Print Preview in Chrome and printing it to a PDF. Please also remember to check the resulting PDF before submitting. |
||
|
Homework 1 UC Irvine 1/ 4 |
|
CS 178: Machine Learning & Data Mining Fall 2020 |
||||||
|
Problem 2: k-nearest-neighbor predictions (25 points) In this problem, you will continue to use the NYC Housing data and create a k-nearest-neighbor (kNN) classifier using the provided knnClassify python class. While completing this problem, please explore the implementation to become familiar with how it works. First, we will shuffle and split the data into training and validation subsets:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Make sure to set the random number seed to 0 before calling shuffleData as in the example above (and in general, for every assignment). This ensures consistent behavior each time the code is run. Learner Objects Our learners (the parameterized functions that do the prediction) will be defined as python objects, derived from either an abstract classifier or abstract regressor class. The abstract base classes have a few useful functions, such as computing error rates or other measures of quality. More importantly, the learners will all follow a generic behavioral pattern, allowing us to train the function on one data set (i.e., set the parameters of the model to perform well on those data), and then make predictions on another data set. You can now build and train a kNN classifier on Xtr,Ytr and make predictions on some data Xva with it:
1 2 3 4 5 6 7 1 If your data are 2D, you can visualize the data set and a classifier’s decision regions using the function ml.plotClassify2D( knn, Xtr, Ytr ); # make 2D classification plot with data (Xtr,Ytr) This function plots the training data and colored points as per their labels, then calls knn ’s predict function on a densely spaced grid of points in the 2D space, and uses this to produce the background color. Calling the function with knn=None will plot only the data. 1. Modify the code listed above to use only the first two features of X (e.g., let X be only the first two columns of nych , instead of the first three), and visualize (plot) the classification boundary for varying values of K = [1, 5, 10, 50] using plotClassify2D . (10 points) 2. Again using only the first two features, compute the error rate (number of misclassifications) on both the training and validation data as a function of K = [1, 2, 5, 10, 50, 100, 200]. You can do this most easily with a for-loop: |
||||||
|
Homework 1 UC Irvine 2/ 4 |
|
CS 178: Machine Learning & Data Mining Fall 2020 |
||
1 2 3 4 5 6 7 8 9 3. Create the same error rate plots as the previous part, but with all the features in the dataset. Are the plots very different? Is your recommendation for the best K different? (5 points) Problem 3: Naïve Bayes Classifiers (35 points) In order to reduce my email load, I decide to implement a machine learning algorithm to decide whether or not I should read an email, or simply file it away instead. To train my model, I obtain the following data set of binary-valued features about each email, including whether I know the author or not, whether the email is long or short, and whether it has any of several key words, along with my final decision about whether to read it ( y = +1 for “read”, y = −1 for “discard”). x1 x2 x3 x4 x5 y know author? is long? has ‘research’ has ‘grade’ has ‘lottery’ ⇒ read? 0 0 1 1 0 -1 1 1 0 1 0 -1 0 1 1 1 1 -1 1 1 1 1 0 -1 0 1 0 0 0 -1 101111 001001 100001 101101 1 1 1 1 1 -1 I decide to try a naïve Bayes classifier to make my decisions and compute my uncertainty. In the case of any ties where both classes have equal probability, we will prefer to predict class +1.
Plot the resulting error rate functions using a semi-log plot ( semilogx ), with training error in red and validation error in green. Based on these plots, what value of K would you recommend? (10 points) |
||
|
Homework 1 UC Irvine 3/ 4 |
|
CS 178: Machine Learning & Data Mining Fall 2020 |
|
Problem 4: Gaussian Bayes Classifiers (15 points) Now, using the NYC Housing data, we will explore a classifier based on Bayes rule. Again, we’ll use only the first two features of NYC Housing, shuffled and split in to training and validation sets as before.
1 2 Problem 5: 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. bc = ml.bayes.gaussClassify( Xtr, Ytr ); ml.plotClassify2D(bc, Xtr, Ytr); Also compute the empirical error rate (number of misclassified points) on the training and validation data. (5 points) |
|
Homework 1 UC Irvine 4/ 4 |



