You can now build and train a kNN classifier on XtrYtr and make predictions on

You can now build and train a knn classifier on

This preview shows page 2 - 4 out of 4 pages.

You can now build and train a kNN classifier on Xtr,Ytr and make predictions on some data Xva with it: 1 knn = ml . knn . knnClassify () # create the object and train it 2 knn . train ( Xtr , Ytr , K ) # where K is an integer, e.g. 1 for nearest neighbor prediction 3 YvaHat = knn . predict ( Xva ) # get estimates of y for each data point in Xva 4 5 # Alternatively, the constructor provides a shortcut to "train": 6 knn = ml . knn . knnClassify ( Xtr , Ytr , K ); 7 YvaHat = predict ( knn , Xva ); If your data are 2D, you can visualize the data set and a classifier’s decision regions using the function 1 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 iris , instead of the first four), 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
Image of page 2

Subscribe to view the full document.

CS 178: Machine Learning & Data Mining Fall 2019 1 K =[1,2,5,10,50,100,200]; 2 errTrain [ i ] = [ None ] * len ( K ) # (preallocate storage for training error) 3 for i , k in enumerate ( K ): 4 learner = ml . knn . knnClassify (... # TODO: complete code to train model 5 Yhat = learner . predict (... # TODO: predict results on training data 6 errTrain [ i ] = ... # TODO: count what fraction of predictions are wrong 7 #TODO: repeat prediction / error evaluation for validation data 8 9 plt . semilogx (... #TODO: average and plot results on semi-log scale 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) 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 (50 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”).
Image of page 3
Image of page 4

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes