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
CHAPTER 9: Supervised Learning—Classification Using K‐Nearest Neighbors (KNN)
End User License Agreement

Pandas Series

A Pandas Series is a one‐dimensional NumPy‐like array, with each element having an index (0, 1, 2, … by default); a Series behaves very much like a dictionary that includes an index. Figure 3.1 shows the structure of a Series in Pandas.

Structure of a Pandas series which is a one-dimensional NumPy-like array, with each element having an index (0, 1, 2, … by default.

Figure 3.1: A Pandas Series

To create a Series, you first need to import the pandas library (the convention is to use pd as the alias) and then use the Series class:

import pandas as pd
series = pd.Series([1,2,3,4,5])

The preceding code snippet will print the following output:

0    1
1    2
2    3
3    4
4    5
dtype: int64 

By default, the index of a Series starts from 0.

Creating a Series Using a Specified Index

You can specify an optional index for a Series using the index parameter:

series = pd.Series([1,2,3,4,5], index=['a','b','c','d','c'])

The preceding code snippet prints out the following:

a    1
b    2
c    3
d    4
c    5