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 Bar Charts

Besides plotting line charts, you can also plot bar charts using matplotlib. Bar charts are useful for comparing data. For example, you want to be able to compare the grades of a student over a number of semesters.

Using the same dataset that you used in the previous section, you can plot a bar chart using the bar() function as follows:

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import style
 
style.use("ggplot")
 
plt.bar(
    [1,2,3,4,5,6,7,8,9,10],
    [2,4.5,1,2,3.5,2,1,2,3,2],
    label = "Jim",
    color = "m",                    # m for magenta
    align = "center"
)
 
plt.title("Results")
plt.xlabel("Semester")
plt.ylabel("Grade")
 
plt.legend()
plt.grid(True, color="y") 

Figure 4.7 shows the bar chart plotted using the preceding code snippet.

Bar chart plotted using the preceding code snippet, to compare the grades of a student over a number of semesters.

Figure 4.7: Plotting a bar chart

Adding Another Bar to the Chart

Just like adding an additional line chart to...