Reader small image

You're reading from  The Applied Artificial Intelligence Workshop

Product typeBook
Published inJul 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781800205819
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Anthony So
Anthony So
author image
Anthony So

Anthony So is a renowned leader in data science. He has extensive experience in solving complex business problems using advanced analytics and AI in different industries including financial services, media, and telecommunications. He is currently the chief data officer of one of the most innovative fintech start-ups. He is also the author of several best-selling books on data science, machine learning, and deep learning. He has won multiple prizes at several hackathon competitions, such as Unearthed, GovHack, and Pepper Money. Anthony holds two master's degrees, one in computer science and the other in data science and innovation.
Read more about Anthony So

William So
William So
author image
William So

William So is a Data Scientist with both a strong academic background and extensive professional experience. He is currently the Head of Data Science at Douugh and also a Lecturer for Master of Data Science and Innovation at the University of Technology Sydney. During his career, he successfully covered the end-end spectrum of data analytics from ML to Business Intelligence helping stakeholders derive valuable insights and achieve amazing results that benefits the business. William is a co-author of the "The Applied Artificial Intelligence Workshop" published by Packt.
Read more about William So

Zsolt Nagy
Zsolt Nagy
author image
Zsolt Nagy

Zsolt Nagy is an engineering manager in an ad tech company heavy on data science. After acquiring his MSc in inference on ontologies, he used AI mainly for analyzing online poker strategies to aid professional poker players in decision making. After the poker boom ended, he put extra effort into building a T-shaped profile in leadership and software engineering.
Read more about Zsolt Nagy

View More author details
Right arrow

1. Introduction to Artificial Intelligence

Activity 1.01: Generating All Possible Sequences of Steps in a Tic-Tac-Toe Game

Solution:

The following steps will help you to complete this activity:

  1. Open a new Jupyter Notebook file.
  2. Reuse the function codes of Steps 2–9 from the previous, Exercise 1.02, Creating an AI with Random Behavior for the Tic-Tac-Toe Game.
  3. Create a function that maps the all_moves_from_board_list function to each element of a list of boards. This way, we will have all of the nodes of a decision tree in each depth:
    def all_moves_from_board_list(board_list, sign):
        move_list = []
        for board in board_list:
            move_list.extend(all_moves_from_board(board, sign))
        return move_list

    In the preceding code snippet, we have defined the all_moves_from_board function, which will enumerate all the possible moves from the board and add the...

2. An Introduction to Regression

Activity 2.01: Boston House Price Prediction with Polynomial Regression of Degrees 1, 2, and 3 on Multiple Variables

Solution:

  1. Open a Jupyter Notebook.
  2. Import the required packages and load the Boston House Prices data from sklearn into a DataFrame:
    import numpy as np
    import pandas as pd
    from sklearn import preprocessing
    from sklearn import model_selection
    from sklearn import linear_model
    from sklearn.preprocessing import PolynomialFeatures
    file_url = 'https://raw.githubusercontent.com/'\
               'PacktWorkshops/'\
               'The-Applied-Artificial-Intelligence-Workshop/'\
               'master/Datasets/boston_house_price.csv'
    df = pd.read_csv(file_url)

    The output of df is as follows:

    Figure 2.28: Output displaying the dataset

    Earlier in...

3. An Introduction to Classification

Activity 3.01: Increasing the Accuracy of Credit Scoring

Solution:

  1. Open a new Jupyter Notebook file and execute all the steps from the previous exercise, Exercise 3.04, K-Nearest Neighbors Classification in Scikit-Learn.
  2. Import neighbors from sklearn:
    from sklearn import neighbors
  3. Create a function called fit_knn that takes the following parameters: k, p, features_train, label_train, features_test, and label_test. This function will fit KNeighborsClassifier with the training set and print the accuracy score for the training and testing sets, as shown in the following code snippet:
    def fit_knn(k, p, features_train, label_train, \
                features_test, label_test):
        classifier = neighbors.KNeighborsClassifier(n_neighbors=k, p=p)
        classifier.fit(features_train, label_train)
        return classifier.score...

4. An Introduction to Decision Trees

Activity 4.01: Car Data Classification

Solution:

  1. Open a new Jupyter Notebook file.
  2. Import the pandas package as pd:
    import pandas as pd
  3. Create a new variable called file_url that will contain the URL to the raw dataset:
    file_url = 'https://raw.githubusercontent.com/'\
               'PacktWorkshops/'\
               'The-Applied-Artificial-Intelligence-Workshop/'\
               'master/Datasets/car.csv'
  4. Load the data using the pd.read_csv() method.:
    df = pd.read_csv(file_url) 
  5. Print the first five rows of df:
    df.head()

    The output will be as follows:

    Figure 4.13: The first five rows of the dataset

  6. Import the preprocessing module from sklearn:
    from sklearn import preprocessing
  7. Create a function called encode() that takes a DataFrame...

5. Artificial Intelligence: Clustering

Activity 5.01: Clustering Sales Data Using K-Means

Solution:

  1. Open a new Jupyter Notebook file.
  2. Load the dataset as a DataFrame and inspect the data:
    import pandas as pd
    file_url = 'https://raw.githubusercontent.com/'\
               'PacktWorkshops/'\
               'The-Applied-Artificial-Intelligence-Workshop/'\
               'master/Datasets/'\
               'Sales_Transactions_Dataset_Weekly.csv'
    df = pd.read_csv(file_url)
    df

    The output of df is as follows:

    Figure 5.18: Output showing the contents of the dataset

    If you look at the output, you will notice that our dataset contains 811 rows, with each row representing a product. It also contains 107 columns, with the first column...

6. Neural Networks and Deep Learning

Activity 6.01: Finding the Best Accuracy Score for the Digits Dataset

Solution:

  1. Open a new Jupyter Notebook file.
  2. Import tensorflow.keras.datasets.mnist as mnist:
    import tensorflow.keras.datasets.mnist as mnist
  3. Load the mnist dataset using mnist.load_data() and save the results into (features_train, label_train), (features_test, label_test):
    (features_train, label_train), \
    (features_test, label_test) = mnist.load_data()
  4. Print the content of label_train:
    label_train

    The expected output is this:

    array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

    The label column contains numeric values that correspond to the 10 handwritten digits: 0 to 9.

  5. Print the shape of the training set:
    features_train.shape

    The expected output is this:

    (60000, 28, 28)

    The training set is composed of 60,000 observations of shape 28 by 28. We will need to flatten the input for our neural network.

  6. Print the shape of the testing set:
    features_test.shape

    The expected...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Applied Artificial Intelligence Workshop
Published in: Jul 2020Publisher: PacktISBN-13: 9781800205819
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Anthony So

Anthony So is a renowned leader in data science. He has extensive experience in solving complex business problems using advanced analytics and AI in different industries including financial services, media, and telecommunications. He is currently the chief data officer of one of the most innovative fintech start-ups. He is also the author of several best-selling books on data science, machine learning, and deep learning. He has won multiple prizes at several hackathon competitions, such as Unearthed, GovHack, and Pepper Money. Anthony holds two master's degrees, one in computer science and the other in data science and innovation.
Read more about Anthony So

author image
William So

William So is a Data Scientist with both a strong academic background and extensive professional experience. He is currently the Head of Data Science at Douugh and also a Lecturer for Master of Data Science and Innovation at the University of Technology Sydney. During his career, he successfully covered the end-end spectrum of data analytics from ML to Business Intelligence helping stakeholders derive valuable insights and achieve amazing results that benefits the business. William is a co-author of the "The Applied Artificial Intelligence Workshop" published by Packt.
Read more about William So

author image
Zsolt Nagy

Zsolt Nagy is an engineering manager in an ad tech company heavy on data science. After acquiring his MSc in inference on ontologies, he used AI mainly for analyzing online poker strategies to aid professional poker players in decision making. After the poker boom ended, he put extra effort into building a T-shaped profile in leadership and software engineering.
Read more about Zsolt Nagy