Home Data Python Machine Learning Workbook for Beginners

Python Machine Learning Workbook for Beginners

By AI Sciences
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Chapter 1: Introduction and Environment Set Up
About this book

Machine Learning (ML) is the lifeblood of businesses worldwide. ML tools empower organizations to identify profitable opportunities fast and help them to better understand potential risks. The ever-expanding data, cost-effective data storage, and competitively priced powerful processing continue to drive the growth of ML.

This is the best time you could enter the exciting machine learning universe. Industries are reinventing themselves constantly by developing more advanced data analysis models. These models analyze larger and more complex data than ever while delivering instantaneous and more accurate results on enormous scales.

In this backdrop, it is evident that hands-on practice is everything in machine learning. Tons of theory will amount to nothing if you don’t have enough hands-on practice. Textbooks and online classes mislead you into a false sense of mastery. The easy availability of learning resources tricks you and you become overconfident. But when you try to apply the theoretical concepts you have learned, you realize it’s not that simple.

This is where projects play a crucial role in your learning journey. Projects are doubtless the best investment of your time. You’ll not only enjoy learning but you’ll also make quick progress. And unlike studying boring theoretical concepts, you’ll find that working on projects is easier to stay motivated.

The projects in this book cover ten different interesting topics. Each project will help you refine your ML skills and apply them in the real world. These projects also present you with an opportunity to enrich your portfolio, making it simpler to find a great job, explore interesting career paths, and even negotiate a higher pay package. Overall, this learning-by-doing book will help you accomplish your machine learning career goals faster.

The code bundle for this course is available at https://www.aispublishing.net/ai-sciences-book

Publication date:
March 2021
Publisher
Packt
Pages
279
ISBN
9781801813907

 

CHAPTER

image

Introduction and Environment Set Up

Data science libraries exist in various programming languages. However, you will be using Python programming language for data science and machine learning since Python is flexible, easy to learn, and offers the most advanced data science and machine learning libraries. Furthermore, Python has a huge data science community from where you can take help whenever you want.

In this chapter, you will see how to set up the Python environment needed to run various data science and machine learning libraries. The chapter also contains a crash Python course for absolute beginners to Python. Finally, the different data science and machine learning libraries that we are going to study in this book have been discussed. The chapter ends with a simple exercise.

 

1.1. Difference between Data Science and Machine Learning?

The terms data science and machine learning are often interchangeably used. However, the two terms are different. Data science is a sphere of study that uses scientific approaches and mathematical techniques such as statistics to draw out meaning and insights from data. As per Dr. Thomas Miller from Northwestern University, data science is “a combination of information technology, modeling and business management.”

Machine learning, on the other hand, is an approach consisting of mathematical algorithms that enable computers to make decisions without being explicitly performed. Rather, machine learning algorithms learn from data, and then, based on the insights from the dataset, they make decisions without human input.

In this book, you will complete projects that involve both data science and machine learning since you will be importing datasets, analyzing them, and finally, implementing machine learning models with them.

 

1.2. Steps in Learning Data Science and Machine Learning

Know What Data Science and Machine Learning Is All About

Before you delve deep into developing data science and machine learning applications, you have to know what the field of data science and machine learning is, what you can do with that, and what are some of the best tools and libraries that you can use.

Learn a Programming Language

If you wish to be a data science and machine learning expert, you have to learn programming. There is no working around this fact. Although there are several cloud-based machine learning platforms like Amazon Sage Maker and Azure ML Studio where you can create data science applications without writing a single line of code, however, to get finegrained control over your applications, you will need to learn programming.

Though you can program natural language applications in any programming language, I would recommend that you learn Python programming language. Python is one of the most commonly used libraries for data science and machine learning, with myriads of basic and advanced data science and ML libraries. In addition, many data science applications are based on deep learning and machine learning techniques. Again, Python is the language that provides easy to use libraries for deep learning and machine learning.

Start with the Basics

Start with very basic data science applications. I would rather recommend that you should not start developing data science applications right away. Start with basic mathematical and numerical operations like computing dot products and matrix multiplication, etc.

Learn Machine Learning and Deep Learning Algorithms

Data Science, machine learning, and deep learning go hand to hand. Therefore, you have to learn machine learning and deep learning algorithms. In machine learning, start with the supervised learning techniques. Supervised machine learning algorithms are divided into two types, i.e., regression and classification.

Develop Data Science Applications

Once you are familiar with basic machine learning and deep learning algorithms, you are good to go for developing data science applications. Data science applications can be of different types, i.e., predicting house prices, recognizing images, classifying text, etc. Being a beginner, you should try to develop versatile data science applications, and later, when you find your area of interest, e.g., natural language processing or image recognition, delve deep into that. It is important to mention that this book provides a very generic introduction to data science, and you will see applications of data science to structured data, textual data, and image data. However, this book is not dedicated to any specific data science field.

Deploying Data Science Applications

To put a data science or machine learning application into production so that anyone can use it, you need to deploy it to production. There are several ways to deploy data science applications. You can use dedicated servers containing REST APIs that can be used to call various functionalities in your data science application. To deploy such applications, you need to learn Python Flask, Docker, or similar web technology. In addition to that, you can also deploy your applications using Amazon Web Services or any other cloud-based deployment platform.

To be an expert data science and machine learning practitioner, you need to perform the aforementioned six steps in an iterative manner.

 

1.3. Environment Setup

1.3.1. Windows Setup

The time has come to install Python on Windows using an IDE. In fact, we will use Anaconda throughout this book, right from installing Python to writing multi-threaded codes in the coming lectures. Now, let us get going with the installation.

This section explains how you can download and install Anaconda on Windows.

Follow these steps to download and install Anaconda.

1.Open the following URL in your browser.
https://www.anaconda.com/distribution/

2.The browser will take you to the following webpage. Select the latest version of Python (3.7 at the time of writing this book). Now, click the Download button to download the executable file. Depending upon the speed of your internet, the file will download within 2–3 minutes.

image

3.Run the executable file after the download is complete. You will most likely find the download file in your download folder. The name of the file should be similar to “Anaconda3- 5.1.0-Windows-x86_64.” The installation wizard will open when you run the file, as shown in the following figure. Click the Next button.

image

4.Now click I Agree on the License Agreement dialog as shown in the following screenshot.

image

5.Check the Just Me radio button from the Select Installation Type dialogue box. Click the Next button to continue.

image

6.Now, the Choose Install Location dialog will be displayed. Change the directory if you want, but the default is preferred. The installation folder should at least have 3 GB of free space for Anaconda. Click the Next button.

image

7.Go for the second option, Register Anaconda as my default Python 3.7 in the Advanced Installation Options dialog box. Click the Install button to start the installation, which can take some time to complete.

image

8.Click Next once the installation is complete.

image

9.Click Skip on the Microsoft Visual Studio Code Installation dialog box.

image

10.You have successfully installed Anaconda on your Windows. Excellent job. The next step is to uncheck both checkboxes on the dialog box. Now, click on the Finish button.

image

1.3.2. Mac Setup

Anaconda’s installation process is almost the same for Mac. It may differ graphically, but you will follow the same steps you followed for Windows. The only difference is that you have to download the executable file, which is compatible with the Mac operating system.

This section explains how you can download and install Anaconda on Mac.

Follow these steps to download and install Anaconda.

1.Open the following URL in your browser.
https://www.anaconda.com/distribution/

2.The browser will take you to the following webpage. Select the latest version of Python for Mac. (3.7 at the time of writing this book). Now, click the Download button to download the executable file. Depending upon the speed of your internet, the file will download within 2–3 minutes.

image

3.Run the executable file after the download is complete. You will most likely find the download file in your download folder. The name of the file should be similar to “Anaconda3-5.1.0-Windows-x86_64.” The installation wizard will open when you run the file, as shown in the following figure. Click the Continue button.

image

4.Now, click, Continue on the Welcome to Anaconda 3 Installer window, as shown in the following screenshot.

image

5.The Important Information dialog will pop up. Simply click Continue to go with the default version that is Anaconda 3.

image

6.Click Continue on the Software License Agreement Dialog.

image

7.It is mandatory to read the license agreement and click the Agree button before you can click the Continue button again.

image

8.Simply click Install on the next window that appears.

image

The system will prompt you to give your password. Use the same password you use to login to your Mac computer. Now, click on Install Software.

image

9.Click Continue on the next window. You also have the option to install Microsoft VSCode at this point.

image

The next screen will display the message that the installation has been completed successfully. Click on the Close button to close the installer.

image

There you have it. You have successfully installed Anaconda on your Mac computer. Now, you can write Python code in Jupyter and Spyder the same way you wrote it in Windows.

1.3.3. Linux Setup

We have used Python’s graphical installers for installation on Windows and Mac. However, we will use the command line to install Python on Ubuntu or Linux. Linux is also more resource-friendly, and the installation of software is particularly easy as well.

Follow these steps to install Anaconda on Linux (Ubuntu distribution).

1.Go to the following link to copy the installer bash script from the latest available version.
https://www.anaconda.com/distribution/

image

2.The second step is to download the installer bash script. Log into your Linux computer and open your terminal. Now, go to /temp directory and download the bash you downloaded from Anaconda’s home page using curl.

3.You should also use the cryptographic hash verification through the SHA-256 checksum to verify the integrity of the installer.

$ sha256sum Anaconda3-5.2.0-Linux-x86_64.sh

You will get the following output.

09f53738b0cd3bb96f5b1bac488e5528df9906be2480fe61df40e0e0d19e3d48 Anaconda3-5.2.0-Linux-x86_64.sh

4.The fourth step is to run the Anaconda Script, as shown in the following figure.

$ bash Anaconda3-5.2.0-Linux-x86_64.sh

The command line will generate the following output. You will be asked to review the license agreement. Keep on pressing Enter until you reach the end.

Output

Welcome to Anaconda3 5.2.0

In order to continue the installation process, please review the license agreement.

Please, press Enter to continue

>>>

Do you approve the license terms? [yes|No]

Type, Yes, when you get to the bottom of the License Agreement.

5.The installer will ask you to choose the installation location after you agree to the license agreement. Simply press Enter to choose the default location. You can also specify a different location if you want.

Output

Anaconda3 will now be installed on this location: /home/tola/anaconda3

- Press ENTER to confirm the location

- Press CTRL-C to abort the installation

- Or specify a different location below

[/home/tola/anaconda3] >>>

The installation will proceed once you press Enter. Once again, you have to be patient as the installation process takes some time to complete.

6.You will receive the following result when the installation is complete. If you wish to use the conda command, type Yes.

Output

Installation finished.

Do you wish the installer to prepend Anaconda3 install location to path in your /home/tola/.bashrc? [yes|no]

[no]>>>

At this point, you will also have the option to download the Visual Studio Code. Type yes or no to install or decline, respectively.

7.Use the following command to activate your brand new installation of Anaconda3.

$ source `/.bashrc

8.You can also test the installation using the conda command.

$ conda list

Congratulations. You have successfully installed Anaconda on your Linux system.

1.3.4. Using Google Colab Cloud Environment

In addition to local Python environments such as Anaconda, you can run deep learning applications on Google Colab, as well, which is Google’s platform for deep learning with GPU support. All the codes in this book have been run using Google Colab. Therefore I would suggest that you use Google Colab, too.

To run deep learning applications via Google Colab, all you need is a Google/Gmail account. Once you have a Google/ Gmail account, you can simply go to:

https://colab.research.google.com/

Next, click on File -> New notebook, as shown in the following screenshot.

image

Next, to run your code using GPU, from the top menu, select Runtime -> Change runtime type, as shown in the following screenshot:

image

You should see the following window. Here from the dropdown list, select GPU, and click the Save button.

image

To make sure you are running the latest version of TensorFlow, execute the following script in the Google Colab notebook cell. The following script will update your TensorFlow version.

pip install --upgrade tensorflow

To check if you are really running TensorFlow version > 2.0, execute the following script.

1. import tensorflow as tf

2. print(tf.__version__)

With Google Cloud, you can import the datasets from your Google drive. Execute the following script. And click on the link that appears, as shown below:

image

You will be prompted to allow Google Colab to access your Google drive. Click the Allow button, as shown below:

image

You will see a link appear, as shown in the following image (the link has been blinded here).

image

Copy the link and paste it in the empty field in the Google Colab cell, as shown below:

image

This way, you can import datasets from your Google drive to your Google Colab environment.

In the next chapter, you will see how to write your first program in Python, along with other Python programming concepts.

About the Author
  • AI Sciences

    AI Sciences are experts, PhDs, and artificial intelligence practitioners, including computer science, machine learning, and Statistics. Some work in big companies such as Amazon, Google, Facebook, Microsoft, KPMG, BCG, and IBM.

    AI sciences produce a series of courses dedicated to beginners and newcomers on techniques and methods of machine learning, statistics, artificial intelligence, and data science. They aim to help those who wish to understand techniques more easily and start with less theory and less extended reading. Today, they publish more comprehensive courses on specific topics for wider audiences.

    Their courses have successfully helped more than 100,000 students master AI and data science.

    Browse publications by this author
Python Machine Learning Workbook for Beginners
Unlock this book and the full library FREE for 7 days
Start now