0% found this document useful (0 votes)
7 views25 pages

Azure - Machine - Learning - Studio - Uniosun - 2023.ipynb - Colaboratory-1

The document outlines a notebook for Azure Machine Learning that includes questions about selecting appropriate Azure services for specific AI tasks, such as predicting ice cream sales and detecting windshield cracks. It also includes code snippets for data manipulation using pandas, downloading datasets, and cleaning data by removing unnecessary columns. The notebook serves as a practical guide for implementing machine learning models and understanding responsible AI principles.

Uploaded by

adebayosam2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views25 pages

Azure - Machine - Learning - Studio - Uniosun - 2023.ipynb - Colaboratory-1

The document outlines a notebook for Azure Machine Learning that includes questions about selecting appropriate Azure services for specific AI tasks, such as predicting ice cream sales and detecting windshield cracks. It also includes code snippets for data manipulation using pandas, downloading datasets, and cleaning data by removing unnecessary columns. The notebook serves as a practical guide for implementing machine learning models and understanding responsible AI principles.

Uploaded by

adebayosam2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.

ipynb - Colaboratory

1. You want to create a model to predict sales of ice cream based on historic data that
includes daily ice cream sales totals and weather measurements. Which Azure service
should you use?

A. Azure Machine Learning

B. Azure Bot

C. Language

2. You are designing an AI application that uses images to detect cracks in car windshields
and warn drivers when a windshield should be repaired or replaced. What AI workload is
described?

A. Computer Vision

B. Anomaly Detection

C. Natural Language Processing

3. A predictive app provides audio output for visually impaired users. Which principle of
Responsible AI is reflected here?

A. Transparency

B. Inclusiveness

C. Fairness

import pandas
!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-lear
!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-lear

--2023-06-23 10:07:55-- https://raw.githubusercontent.com/MicrosoftDocs/mslearn-int


Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133,
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|
HTTP request sent, awaiting response... 200 OK
Length: 21511 (21K) [text/plain]
Saving to: ‘graphing.py’

graphing.py 100%[===================>] 21.01K --.-KB/s in 0.001s

2023-06-23 10:07:55 (31.4 MB/s) - ‘graphing.py’ saved [21511/21511]

--2023-06-23 10:07:55-- https://raw.githubusercontent.com/MicrosoftDocs/mslearn-int


Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133,
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|
HTTP request sent, awaiting response... 200 OK
Length: 838 [text/plain]
Saving to: ‘doggy-boot-harness.csv’

doggy-boot-harness. 100%[===================>] 838 --.-KB/s in 0s

2023-06-23 10:07:55 (25.7 MB/s) - ‘doggy-boot-harness.csv’ saved [838/838]

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 1/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

pip install statsmodels

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/


Requirement already satisfied: statsmodels in /usr/local/lib/python3.10/dist-package
Requirement already satisfied: pandas>=0.25 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: patsy>=0.5.2 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: packaging>=21.3 in /usr/local/lib/python3.10/dist-pac
Requirement already satisfied: scipy>=1.3 in /usr/local/lib/python3.10/dist-packages
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-package
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/d
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from

data = {
'boot_size' : [ 39, 38, 37, 39, 38, 35, 37, 36, 35, 40,
40, 36, 38, 39, 42, 42, 36, 36, 35, 41,
42, 38, 37, 35, 40, 36, 35, 39, 41, 37,
35, 41, 39, 41, 42, 42, 36, 37, 37, 39,
42, 35, 36, 41, 41, 41, 39, 39, 35, 39
],
'harness_size': [ 58, 58, 52, 58, 57, 52, 55, 53, 49, 54,
59, 56, 53, 58, 57, 58, 56, 51, 50, 59,
59, 59, 55, 50, 55, 52, 53, 54, 61, 56,
55, 60, 57, 56, 61, 58, 53, 57, 57, 55,
60, 51, 52, 56, 55, 57, 58, 57, 51, 59
]
}

dataset = pandas.DataFrame(data)
(dataset)

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 2/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

boot_size harness_size

0 39 58

1 38 58

2 37 52

3 39 58

4 38 57

5 35 52

6 37 55

7 36 53

8 35 49

9 40 54

10 40 59

11 36 56

12 38 53

13 39 58

14 42 57

15 42 58

16 36 56

17 36 51

18 35 50

19 41 59

20 42 59

21 38 59

22 37 55

23 35 50

24 40 55

25 36 52

26 35 53

27 39 54

28 41 61

29 37 56

30 35 55

31 41 60

32 39 57
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 3/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory
32 39 57

33 41 56

34 42 61

35 42 58

36 36 53

37 37 57

38 37 57
dataset = pandas.read_csv('doggy-boot-harness.csv')
39 39 55

40 42 60
(dataset)
41 35 51

42 36 52

43 41 56

44 41 55

45 41 57

46 39 58

47 39 57

48 35 51

49 39 59

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 4/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

boot_size harness_size sex age_years

0 39 58 male 12.0

1 38 58 male 9.6

2 37 52 female 8.6

3 39 58 male 10.2

4 38 57 male 7.8

5 35 52 female 4.4

6 37 55 female 6.0

7 36 53 female 14.2

8 35 49 male 13.2

9 40 54 female 15.2

10 40 59 male 3.6

11 36 56 male 10.0

12 38 53 female 7.0

13 39 58 male 12.4

14 42 57 male 7.4

15 42 58 male 12.0

16 36 56 male 11.4

17 36 51 female 5.0

18 35 50 female 15.2

19 41 59 male 11.4

20 42 59 female 6.6

21 38 59 male 9.6

22 37 55 female 7.8

23 35 50 male 6.4

24 40 55 female 8.8

25 36 52 female 13.2

26 35
print(dataset.harness_size) 53 female 13.2

027 58 39 54 female 6.8


1 58
28 41 61 male 10.8
2 52
329 58 37 56 male 6.2
4 57
530 52 35 55 female 11.8
6 55
731 53 41 60 male 4.2
8 49
32 39 57 female 36
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 5/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory
32 39 57 female 3.6
9 54
10
33 59 41 56 male 5.2
11 56
12
34 53 42 61 male 5.8
13 58
14
35 57 42 58 male 6.6
15 58
36
16 56 36 53 female 4.6
17 51
37
18 50 37 57 male 14.0
19 59
38 37 57 male 10.2
20 59
21
39 59 39 55 female 8.4
22 55
23
40 50 42 60 male 12.4
24 55
41
25 52 35 51 female 3.8
26 53
42
27 54 36 52 female 4.2
28 61
43
29 56 41 56 male 11.0
30 55
44 41 55 female 13.6
31 60
32
45 57 41 57 female 12.6
33 56
34
46 61 39 58 male 14.6
35 58
47
36 53 39 57 male 15.0
37 57
48
38 57 35 51 female 14.2
39 55
49 39 59 male 9.4
40 60
41 51
42 52
43 56
44 55
45 57
46 58
47 57
48 51
49 59
Name: harness_size, dtype: int64

del dataset["sex"]
del dataset["age_years"]

print(dataset.columns.values)

['boot_size' 'harness_size']

(dataset)

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 6/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

boot_size harness_size

0 39 58

1 38 58

2 37 52

3 39 58

4 38 57

5 35 52

6 37 55

7 36 53

8 35 49

9 40 54

10 40 59

11 36 56

12 38 53

13 39 58

14 42 57

15 42 58

16 36 56

17 36 51

18 35 50

19 41 59

20 42 59

21 38 59

22 37 55

23 35 50

24 40 55

25 36 52

26 35 53

27 39 54

28 41 61

29 37 56

30 35 55

31 41 60

32 39 57
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 7/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory
32 39 57

33 41 56

34 42 61

35 42 58

36 36 53

37 37 57

38 37 57

39 39 55

40 42 60

41 35 51

42 36 52

43 41 56

44 41 55

45 41 57

46 39 58

47 39
print(dataset.head()) 57

48 35 51
boot_size harness_size
049 3939 5859
1 38 58
2 37 52
3 39 58
4 38 57

print(dataset.tail())

boot_size harness_size
45 41 57
46 39 58
47 39 57
48 35 51
49 39 59

print(f"We have {len(dataset)} rows of data")

We have 50 rows of data

is_small = dataset.harness_size < 55

print(is_small)

0 False
1 False

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 8/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

2 True
3 False
4 False
5 True
6 False
7 True
8 True
9 True
10 False
11 False
12 True
13 False
14 False
15 False
16 False
17 True
18 True
19 False
20 False
21 False
22 False
23 True
24 False
25 True
26 True
27 True
28 False
29 False
30 False
31 False
32 False
33 False
34 False
35 False
36 True
37 False
38 False
39 False
40 False
41 True
42 True
43 False
44 False
45 False
46 False
47 False
48 True
49 False
Name: harness_size, dtype: bool

data_from_small_dogs = dataset[is_small]

print(data_from_small_dogs)

boot_size harness_size
2 37 52
5 35 52
7 36 53
8 35 49
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipynb… 9/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

9 40 54
12 38 53
17 36 51
18 35 50
23 35 50
25 36 52
26 35 53
27 39 54
36 36 53
41 35 51
42 36 52
48 35 51

print(f"\nNumber of dogs with harness size less than 55: {len(data_from_small_dogs)}")

Number of dogs with harness size less than 55: 16

data_smaller_paws = dataset[dataset.boot_size < 40].copy()

print(f"We now have {len(data_smaller_paws)} rows in our dataset. The last few rows are:")
data_smaller_paws.tail()

We now have 34 rows in our dataset. The last few rows are:
boot_size harness_size

42 36 52

46 39 58

47 39 57

48 35 51

49 39 59

import plotly.express
import graphing

plotly.express.scatter(data_smaller_paws, x="harness_size", y="boot_size")

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 10/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

39

38
boot_size

data_smaller_paws['harness_size_imperial'] = data_smaller_paws.harness_size / 2.54


37

plotly.express.scatter(data_smaller_paws, x="harness_size_imperial", y="boot_size")

36

39

35

50 52 54 56 58
38
harness_size
boot_size

37

36

35

19.5 20 20.5

dataset['boot_size'].mean()

38.32

import pandas
!pip install statsmodels
!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-lear
!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-lear

data = pandas.read_csv('doggy-boot-harness.csv')

data.head()

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 11/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/


Requirement already satisfied: statsmodels in /usr/local/lib/python3.10/dist-package
Requirement already satisfied: pandas>=0.25 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: patsy>=0.5.2 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: packaging>=21.3 in /usr/local/lib/python3.10/dist-pac
Requirement already satisfied: scipy>=1.3 in /usr/local/lib/python3.10/dist-packages
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-package
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/d
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from
--2023-06-23 10:13:05-- https://raw.githubusercontent.com/MicrosoftDocs/mslearn-int
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133,
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|
HTTP request sent, awaiting response... 200 OK
Length: 21511 (21K) [text/plain]
Saving to: ‘graphing.py.1’

graphing.py.1 100%[===================>] 21.01K --.-KB/s in 0.001s

2023-06-23 10:13:05 (14.1 MB/s) - ‘graphing.py.1’ saved [21511/21511]

--2023-06-23 10:13:05-- https://raw.githubusercontent.com/MicrosoftDocs/mslearn-int


Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133,
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|
HTTP request sent, awaiting response... 200 OK
Length: 838 [text/plain]
Saving to: ‘doggy-boot-harness.csv.1’

doggy-boot-harness. 100%[===================>] 838 --.-KB/s in 0s

2023-06-23 10:13:05 (14.3 MB/s) - ‘doggy-boot-harness.csv.1’ saved [838/838]

boot_size harness_size sex age_years

0 39 58 male 12.0

1 38 58 male 9.6

2 37 52 female 8.6

import3statsmodels.formula.api
39 58as smf
male 10.2

4 38 57 male 7.8
model = smf.ols(formula = "boot_size ~ harness_size", data = data).fit()

print("Model trained!")

Model trained!

import joblib

model_filename = './avalanche_dog_boot_model.pkl'
joblib.dump(model, model_filename)

print("Model saved!")

Model saved!

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 12/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

model_loaded = joblib.load(model_filename)

print("We have loaded a model with the following parameters:")


print(model_loaded.params)

We have loaded a model with the following parameters:


Intercept 5.719110
harness_size 0.585925
dtype: float64

# Let's write a function that loads and uses our model


def load_model_and_predict(harness_size):
'''
This function loads a pretrained model. It uses the model
with the customer's dog's harness size to predict the size of
boots that will fit that dog.

harness_size: The dog harness size, in cm


'''

# Load the model from file and print basic information about it
loaded_model = joblib.load(model_filename)

print("We've loaded a model with the following parameters:")


print(loaded_model.params)

# Prepare data for the model


inputs = {"harness_size":[harness_size]}

# Use the model to make a prediction


predicted_boot_size = loaded_model.predict(inputs)[0]

return predicted_boot_size

# Practice using our model


predicted_boot_size = load_model_and_predict(45)

print("Predicted dog boot size:", predicted_boot_size)

We've loaded a model with the following parameters:


Intercept 5.719110
harness_size 0.585925
dtype: float64
Predicted dog boot size: 32.08575356590479

def check_size_of_boots(selected_harness_size, selected_boot_size):


'''
Calculates whether the customer has chosen a pair of doggy boots that
are a sensible size. This works by estimating the dog's actual boot
size from their harness size.

This returns a message for the customer that should be shown before
they complete their payment
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 13/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

selected_harness_size: The size of the harness the customer wants to buy


selected_boot_size: The size of the doggy boots the customer wants to buy
'''

# Estimate the customer's dog's boot size


estimated_boot_size = load_model_and_predict(selected_harness_size)

# Round to the nearest whole number because we don't sell partial sizes
estimated_boot_size = int(round(estimated_boot_size))

# Check if the boot size selected is appropriate


if selected_boot_size == estimated_boot_size:
# The selected boots are probably OK
return f"Great choice! We think these boots will fit your avalanche dog well."

if selected_boot_size < estimated_boot_size:


# Selected boots might be too small
return "The boots you have selected might be TOO SMALL for a dog as "\
f"big as yours. We recommend a doggy boots size of {estimated_boot_size}."

if selected_boot_size > estimated_boot_size:


# Selected boots might be too big
return "The boots you have selected might be TOO BIG for a dog as "\
f"small as yours. We recommend a doggy boots size of {estimated_boot_size}.

# Practice using our new warning system


check_size_of_boots(selected_harness_size=55, selected_boot_size=39)

We've loaded a model with the following parameters:


Intercept 5.719110
harness_size 0.585925
dtype: float64
'The boots you have selected might be TOO BIG for a dog as small as yours. We recom
mend a doggy boots size of 38 '

KNOWLEDGE CHECK

1. What makes machine-learning algorithms different from traditional algorithms?

A. Machine-learning algorithms are always more complicated to build than traditional


algorithms.

B. Machine-learning algorithms must be trained every time they're used.

C. Machine-learning algorithms are shaped by data directly as part of development. Traditional


algorithms are based almost entirely on theory or on opinions of the person writing the code.

2. When do we want to perform training?

A. Whenever we want to use a model

B. Only when we want to improve the model

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 14/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

C. Every time we load a model from file

3. What is the relationship between a model, an objective, and training data?

A. The training data is used to make changes to the model. These changes help the model get
better at achieving the objective.

B. The training data is used to make changes to the objective. These changes help the objective
be more like the model.

C. The model is used to make changes to the training data. These changes help the training
data get better at achieving the objective.

import seaborn as sns


sns.boxplot(dataset['boot_size']);

sns.boxplot(dataset['harness_size']);

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 15/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

https://docs.google.com/spreadsheets/d/1Ku_Zpj8mXrrkYUsJBynQifTzKcKuCR4O/edit#gid=19
67990280

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import sklearn

from sklearn.preprocessing import OneHotEncoder


from sklearn.model_selection import train_test_split

from google.colab import files


uploaded = files.upload()

Choose Files No file chosen Upload widget is only available when the cell has been
executed in the current browser session. Please rerun this cell to enable.
Saving AI Invasion In-Class Dataset xlsx to AI Invasion In-Class Dataset xlsx

import io
df = pd.read_excel(io.BytesIO(uploaded['AI_Invasion_In-Class_Dataset.xlsx']))

df.head()

Amount
Location Maker Model Year Colour (Million Type Distance_Km
₦)

Mercedes- Foreign
0 Abuja GLA 250 2015 Brown 14.50 50000.0
Benz Used

Nigerian
1 Abuja Hyundai Accent 2013 Red 1.55 NaN
Used

GX 460 Foreign
2 Lagos Lexus 2011 White 14.00 85000.0
Premium Used

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4487 entries, 0 to 4486
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 16/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

0 Location 4487 non-null object


1 Maker 4487 non-null object
2 Model 4487 non-null object
3 Year 4487 non-null int64
4 Colour 4487 non-null object
5 Amount (Million ₦) 4487 non-null float64
6 Type 4487 non-null object
7 Distance_Km 2932 non-null float64
dtypes: float64(2), int64(1), object(5)
memory usage: 280.6+ KB

df.describe()

Year Amount (Million ₦) Distance_Km

count 4487.000000 4487.000000 2.932000e+03

mean 2011.095610 11.309795 1.010383e+05

std 4.823362 20.585915 1.150914e+05

min 1982.000000 0.420000 1.000000e+00

25% 2008.000000 3.600000 5.237850e+04

50% 2011.000000 5.700000 7.900000e+04

75% 2014.000000 12.000000 1.099392e+05

max 2022.000000 454.000000 1.785448e+06

df.columns

Index(['Location', 'Maker', 'Model', 'Year', 'Colour', 'Amount (Million ₦)',


'Type', 'Distance_Km'],
dtype='object')

df.isnull().sum()

Location 0
Maker 0
Model 0
Year 0
Colour 0
Amount (Million ₦) 0
Type 0
Distance_Km 1555
dtype: int64

mean_value = df["Distance_Km"].mean()
print(mean_value)

df["Distance_Km"].fillna(mean_value, inplace=True)

101038.32128240108

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 17/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

df.isnull().sum()

Location 0
Maker 0
Model 0
Year 0
Colour 0
Amount (Million ₦) 0
Type 0
Distance_Km 0
dtype: int64

cat_features = {
"Location",
"Model",
"Maker",
"Year",
"Colour",
"Type",
}

for cat_feature in cat_features:


print(cat_feature, df[cat_feature].unique(), sep=":")
print("#"*50)
'Acadia SLE-1 AWD' 'Lincoln Navigator' 'S Class S 450 L (V222)'
'Torrent Base AWD' 'Corona' 'F-650' 'Sienna CE FWD'
'Accord Coupe 2.4ex-L Automatic' 'Escalade Off'
'Range Rover Velar P250 SE R-Dynamic 4x4' 'RAV4 LE FWD (2.5L 4cyl 6A)'
'TSX Automatic' 'Cooper John Cooper Works' 'Previa' 'Accord 2.4'
'Land Cruiser 5.7 V8 GXR' 'Creta' 'ILX' 'GLS-Class GLS450 4Matic'
'Camry 2.4 SE' 'Sienna XLE 7 Passenger Mobility' 'Corolla 1.8'
'CX-9 Grand Touring AWD' 'CR-V 2.0i LS Automatic' 'Serena'
'Accord 2 I-Vtec' 'Beetle 1.8 T' 'Sumo Victa Turbo DI' 'Avensis Sedan'
'Land Cruiser Prado 3 190hp' 'Tribute' 'Hilux 2.5 D-4d 4X4 SRX'
'Caliber 2.4 R/T' 'CLK' 'Highlander SE 4x4 V6 (3.5L 6cyl 8A)'
'3 2.5 S Grand Touring Sedan' 'Ridgeline RTL'
'Range Rover Velar P380 HSE R-Dynamic 4x4' 'F-150 SuperCrew 4x4'
'Hyundai Palisade' 'Civic LX Hatchback' 'Camry SE (2.5L 4cyl 8A)' 'Sunny'
'Beetle 2.5' 'ES 350 Luxury FWD' 'Sienna XLE Limited'
'Accord 3.5 EX Automatic' 'Forte SX Hatchback' 'Express Cargo Van 1500'
'Explorer Sport Track Automatic' 'Hilux SR5+ 4x4'
'FJ Cruiser 4x4 Automatic' 'Sorento EX 4dr SUV (3.3L 6cyl 6A)' 1117
'Santa Fe Sport 2.0T Off' 'Outlander 2.4' 'Avanza'
'GLS-Class GLS63 AMG Base' 'M Class ML350 4x2' 'X5 3.0si Activity'
'Tacoma Double Cab V6' 'Escalade' 'Corolla 1.8 CE'
'Sienna XLE 7-Passenger AWD' '4-Runner Limited 2WD' 'C280' 'E-350'
'GL Class GL 550' 'Peugeot 5008 2 HDi 180 GT' 'Nissan Armada' 'M6' 'TL'
'C-HR' 'Accord Coupe EX-L' 'Range Rover Evoque HSE Dynamic 4x4 5-Door'
'RAV4 Automatic' 'CX-9 Grand Touring' 'Charger' 'Camry Off'
'407 2 HDi ST Comfort' 'Versa 1.8 SL Hatchback' 'Land Cruiser Matt'
'Yaris Sedan' 'Camry 2.4 WT-i' '323i' 'Galaxy' '407 SW 3 V6 ST Sport'
'Frontier Crew Cab SE' 'Rush 1.5 RWD' 'Range Rover Sport Autobiography'
'CR-V EX 4dr SUV (2.4L 4cyl 5A)' '4-Runner Sport Edition 4x4 V8'
'Camry 2.4 XLE' 'Vibe 2.4 4WD' 'Altima Sedan 3.5 S'
'Camry 2.4 XLi Automatic' 'i8 1.5 Roadster' 'Highlander XLE' 'A5'
'Sienna LE FWD' 'ES 350 Luxury' 'RX 350L AWD' 'ES 350 Off' '206 1.4 D'
'RX 350 F SPORT AWD' 'CTS' 'RAV4 Limited AWD' '4-Runner Limited 4X4'
'Elantra Touring SE Automatic' 'TSX Sport Sedan' 'Passat 2' 'GLE43'
'406 Break 1 8i' 'Pathfinder 4' 'Solara' 'CC' 'Charger RT'
https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 18/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory
406 Break 1.8i Pathfinder 4 Solara CC Charger RT
'S-Class S 500 (W220)' 'Sorento EX'
'Highlander XLE V6 4x4 (3.5L 6cyl 6A)' 'Highlander Base FWD' 'Passat SE'
'IS 300 AWD' 'RX 350 2WD' 'Dart' 'Granvia' 'Fabia' 'Veloster' 'L200'
'Sienna 7 Passenger' 'A4 4.2' 220 'Highlander SE 3.5L 2WD' 'X5 4.8is'
'Pathfinder 4 V6 Automatic' 'C-Class C 300 (W204)' 'Malibu LS'
'C-HR Limited FWD' 'XC90' 'Frontier Automatic'
'Range Rover Velar P380 S 4x4' 'Santa Fe SE' 'Tucson Limited FWD'
'X5 4.8i Sports Activity' 'Elantra 2' 'F-150 Super Cab 4x4' 'Navigator'
'CX-9' 'CR-V LX 4WD Automatic' 'Outback']
##################################################
Location:['Abuja' 'Lagos' 'Ibadan']
##################################################
Maker:['Mercedes-Benz' 'Hyundai' 'Lexus' 'Toyota' 'Mazda' 'Honda' 'Land Rover'
'Porsche' 'Acura' 'Nissan' 'Pontiac' 'Ford' 'Jeep' 'Kia' 'Peugeot' 'BMW'
'Mitsubishi' 'Dodge' 'Chevrolet' 'Scion' 'Audi' 'Infiniti' 'Mini'
'Volkswagen' 'Suzuki' 'Chrysler' 'Volvo' 'Rolls-Royce' 'JAC' 'Subaru'
'Renault' 'GMC' 'Rover' 'IVM' 'Bentley' 'Opel' 'Lincoln' 'Hummer'
'Saturn' 'Cadillac' 'Lamborghini' 'Buick' 'Smart' 'Jaguar' 'Ferrari'
'Tata' 'Skoda']
##################################################
Year:[2015 2013 2011 2009 2008 2010 2014 2012 2022 2006 2021 2017 2007 2002
2016 2019 2020 2004 2018 2005 2003 2000 1999 2001 1989 1998 1982 1994
1993 1997]
##################################################
Type:['Foreign Used' 'Nigerian Used' 'Brand New']

df.drop("Model", axis=1, inplace=True)


df.head()

Amount (Million
Location Maker Year Colour Type Distance_Km
₦)

Mercedes- Foreign
0 Abuja 2015 Brown 14.50 50000.000000
Benz Used

Nigerian
1 Abuja Hyundai 2013 Red 1.55 101038.321282
Used

Foreign
2 Lagos Lexus 2011 White 14.00 85000.000000
Used

cat_features = ["Location","Maker","Year","Colour","Type"]

for cat_feature in cat_features:


df[f"{cat_feature}_cat"] = df[cat_feature].astype('category')
df[f"{cat_feature}_cat"] = df[f"{cat_feature}_cat"].cat.codes

df.head()

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 19/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

Amount
Location Maker Year Colour (Million Type Distance_Km Location_cat
df.drop(["Location","Maker","Year","Colour", "Type"],
₦) axis=1, inplace=True)
df.head()
Mercedes- Foreign
0 Abuja 2015 Brown 14.50 50000.000000 0
Benz Used
Amount
(Million Distance_Km Location_cat Maker_cat Year_cat Colour_cat Type_cat
Nigerian
1 Abuja
₦) Hyundai 2013 Red 1.55 101038.321282 0
Used
0 14.50 50000.000000 0 26
Foreign 22 3 1
2 Lagos Lexus 2011 White 14.00 85000.000000 2
Used
1 1.55 101038.321282 0 14 20 14 2

2 14.00 85000.000000 2 23 18 17 1

3 4.95 101038.321282 2 23 18 6 1

y = df["Amount (Million ₦)"]


X = df.drop("Amount (Million ₦)", axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

from sklearn.linear_model import LinearRegression


reg = LinearRegression()
reg.fit(X_train, y_train)

▾ LinearRegression
LinearRegression()

reg.predict(X_test)

array([ 8.3635797 , 21.44820119, -4.80832081, ..., -0.96444375,


17.36340861, 1.28371493])

from sklearn.metrics import mean_absolute_error


y_pred = reg.predict(X_test)
print("MAE",mean_absolute_error(y_test,y_pred))

MAE 7.563289386706154

from sklearn.tree import DecisionTreeRegressor

dt_reg = DecisionTreeRegressor()
dt_reg.fit(X_train, y_train)
y_pred = dt_reg.predict(X_test)

print("MAE",mean_absolute_error(y_test,y_pred))

MAE 5.057523970799307

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 20/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

from sklearn.svm import SVR

sv_reg = SVR()
sv_reg.fit(X_train, y_train)
y_pred = sv_reg.predict(X_test)
print("MAE",mean_absolute_error(y_test,y_pred))

MAE 6.827446911476295

from sklearn.preprocessing import StandardScaler


s = StandardScaler()
X_train = s.fit_transform(X_train)

Using Numpy and Sympy to solve mathematical operations

Solve the equation x² − 5x + 6 = 0

from sympy import symbols, Eq, solve


x = symbols('x')
eq1 = Eq(x**2 -5*x + 6, 0)
sol = solve(eq1, dict = True)
(sol)

[{x: 2}, {x: 3}]

a = 3^2
b = int(4^2)
print(a == b)

False

Exercise 1: Solving Polynomials

4e² - 20e + 25 = 0

from sympy import symbols, Eq, solve


e = symbols('e')
eq1 = Eq(4*e**2 -20*e + 25)
sol = solve(eq1, dict = True)
sol

<ipython-input-8-d48b71a8ecc1>:3: SymPyDeprecationWarning:

Eq(expr) with a single argument with the right-hand side


defaulting to 0 is deprecated. Use Eq(expr, 0) instead.

See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-eq
for details.

This has been deprecated since SymPy version 1.5. It


will be removed in a future version of SymPy.

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 21/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

eq1 = Eq(4*e**2 -20*e + 25)


[{e: 5/2}]

saved_pin = 20221609
new_pin = input("Please enter your pin: ")
if int(new_pin) == saved_pin:
print("Pin was correctly entered, you are now logged in")
else:
print("You do not have permission to access this account")

Please enter your pin: 20221609


Pin was correctly entered, you are now logged in

Solving Two Equations with Two Unknown

x−y+3=0

x+y−5=0

from sympy import symbols, Eq, solve

x, y = symbols('x y')
eq1 = Eq(x - y + 3, 0)
eq2 = Eq(x + y - 5, 0)

sol = solve((eq1, eq2), (x, y))


sol

{x: 1, y: 4}

sol_dict = solve((eq1, eq2), (x, y))


print(f'x = {sol_dict[x]}')
print(f'y = {sol_dict[y]}')

x = 1
y = 4

Question 1: 6h² + 13h -5 = 0 Question 2: 4x2 + x - 1

p(x) = x4 - 4x2 + 3x

def p(x):
return x**4 - 4*x**2 + 3*x

for x in [-1, 0, 2, 3, 4]:


print(x, p(x))

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 22/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

-1 -6
0 0
2 6
3 54
4 204

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-3, 3, 50, endpoint=True)


F = p(X)
plt.plot(X,F)

plt.show()

Question 2: Differentiate x - 4x² +6x -2 with respect to x

from sympy import symbols, diff

x = symbols("x")
f = x**3 -4*x**2 +6*x -2
ans = diff(f, x)
ans

3x2 − 8x + 6

Question 3: ∫sinx dx

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 23/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

from sympy import *

x = symbols('x')
f = sin(x)
integral = integrate(f, x)
integral

− cos (x)

∫2x cos (x2 – 5)

from sympy import *


x = symbols ('x')
f = 2 * x * cos(x**2 - 5)
integral = integrate(f, x)
integral

sin (x2 − 5)

∫8x3 dx

from sympy import *


x = symbols ('x')
dx = 8 * x ** 3
integral = integrate (dx, x)
integral

2x4

09076425843

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 24/25
11/08/2023, 11:18 Azure_Machine_Learning_Studio_Uniosun_2023.ipynb - Colaboratory

Colab paid products - Cancel contracts here

https://colab.research.google.com/github/holatung/DSN-AI-INVASION-OSUN/blob/main/Azure_Machine_Learning_Studio_Uniosun_2023.ipyn… 25/25

You might also like