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

Building a simple classifier


Let's see how to build a simple classifier using some training data.

How to do it…

  1. We will use the simple_classifier.py file that is already provided to you as reference. Assuming that you imported the numpy and matplotlib.pyplot packages like we did in the last chapter, let's create some sample data:

    X = np.array([[3,1], [2,5], [1,8], [6,4], [5,2], [3,5], [4,7], [4,-1]])
  2. Let's assign some labels to these points:

    y = [0, 1, 1, 0, 0, 1, 1, 0]
  3. As we have only two classes, the y list contains 0s and 1s. In general, if you have N classes, then the values in y will range from 0 to N-1. Let's separate the data into classes based on the labels:

    class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
    class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
  4. To get an idea about our data, let's plot it, as follows:

    plt.figure()
    plt.scatter(class_0[:,0], class_0[:,1], color='black', marker='s')
    plt.scatter(class_1[:,0], class_1[:,1], color='black', marker='x')

    This is a...