0% found this document useful (0 votes)
26 views

Deployment Code

The document discusses analyzing a dataset containing information about humans and cats using Python. It imports necessary libraries, loads and inspects the dataset, encodes categorical variables, and creates plots of height and number of legs versus population to visualize the data. The height and legs data is extracted from the dataset into NumPy arrays for plotting.

Uploaded by

Ali Hassan
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)
26 views

Deployment Code

The document discusses analyzing a dataset containing information about humans and cats using Python. It imports necessary libraries, loads and inspects the dataset, encodes categorical variables, and creates plots of height and number of legs versus population to visualize the data. The height and legs data is extracted from the dataset into NumPy arrays for plotting.

Uploaded by

Ali Hassan
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/ 11

6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [130]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
import joblib

In [3]:

data=pd.read_csv("Cat_human.csv")

In [4]:

data
Out[4]:

Color Eye_color Height Legs Moustache Tail Weight label

0 No black 5.14 2 No No 70.000000 human

1 No brown 6.80 2 No No 64.400000 human

2 Yes brown 5.00 2 Yes No 64.800000 human

3 No blue 5.90 2 No No 78.800000 human

4 No blue 6.56 2 No No 73.200000 human

... ... ... ... ... ... ... ... ...

195 brown gray 1.14 4 Yes Yes 2.304511 Cat

196 white yellow 1.39 4 Yes Yes 5.687970 Cat

197 white black 0.53 4 Yes Yes 6.364662 Cat

198 brown green 1.03 4 Yes Yes 6.590226 Cat

199 brown_white blue 0.83 4 Yes Yes 7.868421 Cat

200 rows × 8 columns

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 1/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [127]:

X
Out[127]:

Color Eye_color height Legs Moustache Tail weight

0 0 0 5.14 2 0 0 70.000000

1 0 2 6.80 2 0 0 64.400000

2 1 2 5.00 2 1 0 64.800000

3 0 1 5.90 2 0 0 78.800000

4 0 1 6.56 2 0 0 73.200000

... ... ... ... ... ... ... ...

195 3 3 1.14 4 1 1 2.304511

196 6 5 1.39 4 1 1 5.687970

197 6 0 0.53 4 1 1 6.364662

198 3 4 1.03 4 1 1 6.590226

199 4 1 0.83 4 1 1 7.868421

200 rows × 7 columns

In [95]:

Moustache=np.array(data["Moustache"])
Moustache
Out[95]:

array(['No', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'No', 'No', 'No', 'No',
'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'No',
'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No',
'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes',
'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No',
'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No',
'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes',
'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No',
'No', 'No', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes'],
dtype=object)

In [96]:

from sklearn.preprocessing import LabelEncoder


encoder = LabelEncoder()
encoder.fit(Moustache)
# Transform the data using the fitted encoder
Moustache = encoder.transform(Moustache)
Moustache
Out[96]:

array([0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 2/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [97]:

plt.figure(figsize=(20,10))
plt.plot(Moustache, marker="*",color="orange")
plt.xlabel("Population")
plt.ylabel("Moustache")
plt.title("Moustache graph comparison")
plt.show()

In [ ]:

# Increase the size of the graph


plt.figure(figsize=(18, 10))
plt.plot(height, marker='o', markersize=2,color='red')
plt.xlabel('population')
plt.ylabel('Height')
plt.title('Line Graph of height')
plt.show()

In [8]:

height=np.array(data["Height"])

In [9]:

height
Out[9]:

array([5.14, 6.8 , 5. , 5.9 , 6.56, 5.56, 5.96, 6.08, 5.58, 6.06, 5.5 ,
6.7 , 6.78, 5.66, 5.74, 6.9 , 5.24, 6.46, 5.2 , 6.02, 6.44, 5.22,
5.52, 6.26, 6.68, 6.24, 5.1 , 6.6 , 6.1 , 5.4 , 6.22, 5.16, 6.88,
6.98, 6.42, 5.7 , 5.08, 6.96, 6.36, 5.54, 5.84, 6.5 , 6.12, 6.54,
6.62, 6. , 5.44, 6.92, 5.62, 6.32, 6.14, 6.66, 6.34, 6.72, 6.58,
6.86, 5.38, 6.18, 5.88, 6.94, 6.64, 5.04, 6.48, 6.84, 6.82, 5.36,
5.6 , 6.28, 5.72, 5.76, 5.82, 6.04, 5.28, 5.18, 6.2 , 5.92, 5.12,
6.3 , 5.48, 6.38, 6.76, 5.26, 5.3 , 5.02, 5.94, 5.32, 5.78, 5.42,
5.8 , 5.98, 6.16, 5.86, 5.64, 6.74, 5.46, 5.34, 6.4 , 6.52, 5.68,
5.06, 1.06, 0.66, 0.77, 1.04, 0.6 , 0.87, 0.75, 0.84, 1.43, 1.36,
1.38, 1. , 0.68, 0.82, 1.26, 1.29, 1.23, 1.44, 1.35, 1.19, 1.27,
1.15, 0.65, 1.31, 1.34, 1.48, 0.97, 0.62, 0.81, 1.28, 1.07, 1.49,
0.55, 0.94, 0.61, 0.51, 1.2 , 0.78, 0.74, 0.56, 0.63, 0.64, 1.3 ,
1.1 , 1.05, 1.11, 0.57, 0.52, 1.16, 0.76, 0.96, 0.8 , 1.4 , 0.69,
1.37, 0.99, 0.58, 0.85, 0.91, 1.33, 1.13, 0.98, 1.21, 1.01, 0.71,
1.41, 0.5 , 1.25, 1.24, 0.72, 0.89, 1.17, 0.86, 0.67, 1.32, 0.92,
1.09, 1.47, 0.88, 1.46, 0.95, 1.18, 0.9 , 0.7 , 1.22, 1.12, 0.73,
0.54, 1.02, 0.59, 0.93, 1.45, 0.79, 1.08, 1.42, 1.14, 1.39, 0.53,
1.03, 0.83])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 3/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [27]:

# Increase the size of the graph


plt.figure(figsize=(18, 10))
plt.plot(height, marker='o', markersize=2,color='red')
plt.xlabel('population')
plt.ylabel('Height')
plt.title('Line Graph of height')
plt.show()

In [29]:

legs=np.array(data["Legs"])

In [69]:

# Increase the size of the graph


plt.figure(figsize=(18, 10))
plt.plot(legs, marker='o', markersize=2,color='blue')
plt.xlabel('population')
plt.ylabel('Legs')
plt.title('Line Graph of height')
plt.show()

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 4/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [31]:

weight=np.array(data["Weight"])

In [68]:

# Increase the size of the graph


plt.figure(figsize=(18, 10))
plt.plot(weight, marker='o', markersize=2,color='Green')
plt.xlabel('population')
plt.ylabel('Weight')
plt.title('Line Graph of height')
plt.show()

In [51]:

from sklearn.preprocessing import LabelEncoder


encoder = LabelEncoder()
Color=np.array(data["Color"])
encoder.fit(Color)
# Transform the data using the fitted encoder
Color = encoder.transform(Color)

In [55]:

Color

Out[55]:

array([0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 3, 4, 5, 4, 5, 6, 4, 5, 5, 3,
2, 4, 3, 6, 5, 6, 5, 5, 3, 5, 5, 5, 2, 3, 6, 5, 2, 2, 4, 2, 5, 6,
6, 3, 3, 6, 5, 2, 6, 2, 4, 3, 3, 4, 5, 4, 2, 6, 2, 3, 6, 3, 5, 2,
4, 2, 2, 5, 2, 4, 3, 2, 3, 6, 6, 3, 2, 2, 5, 6, 5, 2, 6, 4, 3, 4,
4, 2, 5, 4, 2, 2, 4, 3, 6, 4, 4, 6, 4, 6, 6, 5, 3, 3, 4, 3, 6, 6,
3, 4])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 5/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [61]:

# Increase the size of the graph


plt.figure(figsize=(18, 10))
plt.plot(Color, marker='o', markersize=2,color='Green')
plt.xlabel('population')
plt.ylabel('Skin Color')
plt.title('Line Graph of height')
plt.show()

In [59]:

Eye_color=np.array(data["Eye_color"])
encoder.fit(Eye_color)
# Transform the data using the fitted encoder
Eye_color = encoder.transform(Eye_color)
Eye_color
Out[59]:

array([0, 2, 2, 1, 1, 2, 1, 3, 0, 2, 3, 0, 0, 2, 1, 3, 2, 2, 2, 2, 3, 1,
3, 0, 0, 1, 3, 3, 1, 2, 0, 0, 1, 1, 1, 1, 3, 2, 2, 1, 1, 3, 3, 3,
3, 1, 0, 0, 2, 1, 0, 0, 2, 3, 2, 3, 3, 3, 2, 2, 0, 2, 3, 2, 3, 0,
0, 1, 0, 3, 1, 2, 1, 3, 0, 2, 0, 3, 2, 2, 0, 0, 0, 1, 1, 3, 0, 3,
0, 1, 3, 0, 1, 1, 3, 0, 1, 2, 1, 2, 5, 0, 0, 0, 5, 3, 3, 4, 1, 4,
0, 1, 4, 5, 0, 4, 4, 0, 1, 0, 5, 5, 3, 0, 0, 5, 3, 5, 4, 4, 3, 5,
4, 1, 5, 5, 4, 5, 1, 3, 5, 0, 3, 3, 0, 4, 3, 5, 0, 1, 1, 1, 3, 3,
4, 5, 4, 0, 5, 1, 4, 1, 0, 1, 4, 1, 3, 5, 1, 3, 5, 1, 4, 3, 0, 5,
4, 1, 1, 0, 4, 3, 1, 3, 0, 1, 1, 3, 0, 3, 0, 3, 4, 5, 4, 3, 5, 0,
4, 1])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 6/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [62]:

plt.figure(figsize=(18, 10))
plt.plot(Eye_color, marker='o', markersize=2,color='Green')
plt.xlabel('population')
plt.ylabel('Eye_color')
plt.title('Line Graph of height')
plt.show()

In [64]:

Tail=np.array(data["Tail"])
encoder.fit(Tail)
# Transform the data using the fitted encoder
Tail = encoder.transform(Tail)
Tail
Out[64]:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 7/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [66]:

plt.figure(figsize=(18, 10))
plt.plot(Tail, marker='o', markersize=2,color='Grey')
plt.xlabel('population')
plt.ylabel('Tail')
plt.title('Line Graph of height')
plt.show()

In [77]:

Legs=np.array(data["Legs"])
legs

Out[77]:

array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4])

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 8/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [90]:

plt.figure(figsize=(20,10))
plt.plot(Legs, marker="*",color="orange")
plt.xlabel("Population")
plt.ylabel("No of legs")
plt.title("No of legs graph comparison")
plt.show()

In [126]:

ame from the arrays


({'Color': Color, 'Eye_color': Eye_color, 'height': height, 'Legs': Legs, 'Moustache': Moustache,'Tail': Tail, 'weight': weigh

Out[126]:

Color Eye_color height Legs Moustache Tail weight

0 0 0 5.14 2 0 0 70.000000

1 0 2 6.80 2 0 0 64.400000

2 1 2 5.00 2 1 0 64.800000

3 0 1 5.90 2 0 0 78.800000

4 0 1 6.56 2 0 0 73.200000

... ... ... ... ... ... ... ...

195 3 3 1.14 4 1 1 2.304511

196 6 5 1.39 4 1 1 5.687970

197 6 0 0.53 4 1 1 6.364662

198 3 4 1.03 4 1 1 6.590226

199 4 1 0.83 4 1 1 7.868421

200 rows × 7 columns

In [106]:

print(X.shape)
print(Y.shape)
(200, 7)
(200,)

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 9/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [108]:

Y
Out[108]:

0 human
1 human
2 human
3 human
4 human
...
195 Cat
196 Cat
197 Cat
198 Cat
199 Cat
Name: label, Length: 200, dtype: object

In [110]:

encoder = LabelEncoder()
encoder.fit(Y)
# Transform the data using the fitted encoder
Y_encoded = encoder.transform(Y)
Y_encoded
Out[110]:

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0])

In [111]:

print(Y_encoded.shape)
(200,)

In [115]:

from sklearn.model_selection import train_test_split


# Apply train-test split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y_encoded, test_size=0.2, random_state=42)

In [116]:

from sklearn.linear_model import LogisticRegression


from sklearn.metrics import accuracy_score

In [117]:

# Create an instance of LogisticRegression


model = LogisticRegression()

# Train the model


model.fit(X_train, Y_train)

# Predict on the testing data


Y_pred = model.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(Y_test, Y_pred)
print("Accuracy:", accuracy)
Accuracy: 1.0

In [119]:

from sklearn.metrics import confusion_matrix


# Calculate confusion matrix
cm = confusion_matrix(Y_test, Y_pred)
print("Confusion Matrix:")
print(cm)
Confusion Matrix:
[[19 0]
[ 0 21]]

Now code for Deployment Senario

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 10/11
6/21/23, 4:41 AM 21-June-2023 - Jupyter Notebook

In [129]:

import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression
import joblib

# Separate the features (X) and the target variable (y)


X = data.drop('label', axis=1)
y = data['label']

# Define the preprocessing steps for each type of column


numeric_features = ['Height', 'Weight','Legs']
categorical_features = ['Color', 'Eye_color', 'Moustache', 'Tail']

numeric_transformer = Pipeline(steps=[
('scaler', StandardScaler())
])

categorical_transformer = Pipeline(steps=[
('encoder', OneHotEncoder())
])

# Apply the preprocessing steps to the respective columns


preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
])

# Encode the target variable


label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)

# Create the pipeline with preprocessing and the classifier


pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', LogisticRegression())
])

# Fit the pipeline on the training data


pipeline.fit(X, y_encoded)

# Save the pipeline and the label encoder


joblib.dump(pipeline, 'model.pkl')
joblib.dump(label_encoder, 'label_encoder.pkl')

Out[129]:

['label_encoder.pkl']

localhost:8888/notebooks/Desktop/Navttac/ML-1st/21-June-2023.ipynb 11/11

You might also like