Book Image

Building Machine Learning Systems with Python - Second Edition

By : Luis Pedro Coelho, Willi Richert
Book Image

Building Machine Learning Systems with Python - Second Edition

By: Luis Pedro Coelho, Willi Richert

Overview of this book

<p>Using machine learning to gain deeper insights from data is a key skill required by modern application developers and analysts alike. Python is a wonderful language to develop machine learning applications. As a dynamic language, it allows for fast exploration and experimentation. With its excellent collection of open source machine learning libraries you can focus on the task at hand while being able to quickly try out many ideas.</p> <p>This book shows you exactly how to find patterns in your raw data. You will start by brushing up on your Python machine learning knowledge and introducing libraries. You’ll quickly get to grips with serious, real-world projects on datasets, using modeling, creating recommendation systems. Later on, the book covers advanced topics such as topic modeling, basket analysis, and cloud computing. These will extend your abilities and enable you to create large complex systems.</p> <p>With this book, you gain the tools and understanding required to build your own systems, tailored to solve your real-world data analysis problems.</p>
Table of Contents (20 chapters)
Building Machine Learning Systems with Python Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating our first classifier


Let's start with the simple and beautiful nearest neighbor method from the previous chapter. Although it is not as advanced as other methods, it is very powerful: as it is not model-based, it can learn nearly any data. But this beauty comes with a clear disadvantage, which we will find out very soon.

Starting with kNN

This time, we won't implement it ourselves, but rather take it from the sklearn toolkit. There, the classifier resides in sklearn.neighbors. Let's start with a simple 2-Nearest Neighbor classifier:

>>> from sklearn import neighbors
>>> knn = neighbors.KNeighborsClassifier(n_neighbors=2)
>>> print(knn)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', n_neighbors=2, p=2, weights='uniform')

It provides the same interface as all other estimators in sklearn: we train it using fit(), after which we can predict the class of new data instances using predict():

>>> knn.fit([[1],[2],[3],[4],[5],[6...