E the three flower species ynames iris targetnames

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

#getting label names i.e the three flower species y_names = iris . target_names #taking random indices to split the dataset into train and test test_ids = np . random . permutation ( len ( x )) #splitting data and labels into train and test #keeping last 10 entries for testing, rest for training x_train = x [ test_ids [: - 10 ]] x_test = x [ test_ids [ - 10 :]] y_train = y [ test_ids [: - 10 ]] y_test = y [ test_ids [ - 10 :]] #classifying using decision tree clf = tree . DecisionTreeClassifier () #training (fitting) the classifier with the training set
Image of page 3
clf . fit ( x_train , y_train ) #predictions on the test dataset pred = clf . predict ( x_test ) print pred #predicted labels i.e flower species print y_test #actual labels print ( accuracy_score ( pred , y_test )) * 100 #prediction accuracy Since we’re splitting randomly and the classifier trains on every iteration, the accur code gives: [ 0 1 1 1 0 2 0 2 2 2 ] [ 0 1 1 1 0 2 0 2 2 2 ] 100.0 The first line contains the labels (i.e. flower species) of the testing data as predicted line contains the actual flower species as given in the dataset. We thus get an accura More on scikit-learn can be read in the documentation .
Image of page 4

You've reached the end of your free preview.

Want to read all 4 pages?

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors