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

Array Indexing

Accessing elements in the array is similar to accessing elements in a Python list:

print(r1[0])         # 1
print(r1[1])         # 2 

The following code snippet creates another array named r2, which is two‐dimensional:

list2 = [6,7,8,9,0]
r2 = np.array([list1,list2])     # rank 2 array
print(r2)
'''
[[1 2 3 4 5]
 [6 7 8 9 0]]
'''
print(r2.shape)             # (2,5) - 2 rows and 5 columns
print(r2[0,0])              # 1
print(r2[0,1])              # 2
print(r2[1,0])              # 6 

Here, r2 is a rank 2 array, with two rows and five columns.

Besides using an index to access elements in an array, you can also use a list as the index as follows:

list1 = [1,2,3,4,5]
r1 = np.array(list1)
print(r1[[2,4]])    # [3 5] 

Boolean Indexing

In addition to using indexing to access elements in an array, there is another very cool way to access elements in a NumPy array. Consider the following:

print(r1>2)     # [False False  True  True  True]...