Book Image

Python Machine Learning Cookbook

By : Prateek Joshi, Vahid Mirjalili
Book Image

Python Machine Learning Cookbook

By: Prateek Joshi, Vahid Mirjalili

Overview of this book

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields such as search engines, robotics, self-driving cars, and more. With this book, you will learn how to perform various machine learning tasks in different environments. We’ll start by exploring a range of real-life scenarios where machine learning can be used, and look at various building blocks. Throughout the book, you’ll use a wide variety of machine learning algorithms to solve real-world problems and use Python to implement these algorithms. You’ll discover how to deal with various types of data and explore the differences between machine learning paradigms such as supervised and unsupervised learning. We also cover a range of regression techniques, classification algorithms, predictive modeling, data visualization techniques, recommendation engines, and more with the help of real-world examples.
Table of Contents (19 chapters)
Python Machine Learning Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Splitting the dataset for training and testing


Let's see how to split our data properly into training and testing datasets.

How to do it…

  1. Add the following code snippet into the same Python file as the previous recipe:

    from sklearn import cross_validation
    
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
    classifier_gaussiannb_new = GaussianNB()
    classifier_gaussiannb_new.fit(X_train, y_train)

    Here, we allocated 25% of the data for testing, as specified by the test_size parameter. The remaining 75% of the data will be used for training.

  2. Let's evaluate the classifier on test data:

    y_test_pred = classifier_gaussiannb_new.predict(X_test)
  3. Let's compute the accuracy of the classifier:

    accuracy = 100.0 * (y_test == y_test_pred).sum() / X_test.shape[0]
    print "Accuracy of the classifier =", round(accuracy, 2), "%"
  4. Let's plot the datapoints and the boundaries on test data:

    plot_classifier(classifier_gaussiannb_new, X_test, y_test)
  5. You should see the following...