Book Image

Python Machine Learning (Wiley)

By : Wei-Meng Lee
Book Image

Python Machine Learning (Wiley)

By: Wei-Meng Lee

Overview of this book

With computing power increasing exponentially and costs decreasing at the same time, this is the best time to learn machine learning using Python. Machine learning tasks that once required enormous processing power are now possible on desktop machines. Python Machine Learning begins by covering some fundamental libraries used in Python that make machine learning possible. You'll learn how to manipulate arrays of numbers with NumPy and use pandas to deal with tabular data. Once you have a firm foundation in the basics, you'll explore machine learning using Python and the scikit-learn libraries. You'll learn how to visualize data by plotting different types of charts and graphs using the matplotlib library. You'll gain a solid understanding of how the various machine learning algorithms work behind the scenes. The later chapters explore the common machine learning algorithms, such as regression, clustering, and classification, and discuss how to deploy the models that you have built, so that they can be used by client applications running on mobile and desktop devices. By the end of the book, you'll have all the knowledge you need to begin machine learning using Python.
Table of Contents (16 chapters)
Free Chapter
1
Cover
2
Introduction
11
CHAPTER 9: Supervised Learning—Classification Using K‐Nearest Neighbors (KNN)
15
Index
16
End User License Agreement

Plotting Line Charts

To see how easy it is to use matplotlib, let's plot a line chart using Jupyter Notebook. Here is a code snippet that plots a line chart:

%matplotlib inline
import matplotlib.pyplot as plt
 
plt.plot(
    [1,2,3,4,5,6,7,8,9,10],
    [2,4.5,1,2,3.5,2,1,2,3,2]
) 

Figure 4.1 shows the line chart plotted.

“A line graph plotted using matplotlib displaying the output of the plotting commands in line within front-ends like Jupyter Notebook.”

Figure 4.1: A line graph plotted using matplotlib

The first statement tells matplotlib to display the output of the plotting commands in line within front‐ends likes Jupyter Notebook. In short, it means display the chart within the same page as your Jupyter Notebook:

%matplotlib inline 

To use matplotlib, you import the pyplot module and name it plt (its commonly used alias):

import matplotlib.pyplot as plt 

To plot a line chart, you use the plot() function from the pyplot module, supplying it with two arguments as follows:

  1. A list of values representing the x‐axis
  2. A list of values representing the y‐axis
[1,2,3,4,5,6,7,8,9,10],
    [2,4.5,1...