0% found this document useful (0 votes)
17 views22 pages

AILab Journal Karan

Uploaded by

mannyboi806
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)
17 views22 pages

AILab Journal Karan

Uploaded by

mannyboi806
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/ 22

1

ARTIFICIAL INTELLIGENCE & MACHINE LEARNING LAB


SUBJECT CODE: MCAL21
A Practical Journal Submitted in Fulfilment of the Degree of
M.C.A

Year 2023-2024
By
Ms. Pawar Karan Ramesh

(Seat No:1910558)
2

Centre for Distance and online Education (CDOE)


University Of Mumbai, Mumbai
CERTIFICATE

This to certify that, Ms. Pawar Karan Ramesh appearing


Master in Computer Application Seat No:1910558 has
satisfactorily completed the prescribed Practical of ARTIFICIAL
INTELLIGENCE & MACHINE LEARNING LAB as laid down by
the University of Mumbai for the academic year 2023-24

Teacher in charge Examiners Coordinator


CDOE, MCA
University of Mumbai

Date: -
Place: -
3

Index

Sr No Practical Sign
1 Write a PROLOG program to prove a person as a human

2 Explain the object and property relations

3 Write Towers of Hanoi program to apply PROLOG concept

4 Aim: Writing clauses in PROLOG to solve water jug


problem

5 SCIKIT LEARN

6 Puzzle Problem
7 Differentiate the Linear regression and logistic regression
with a real time example.
Linear Regression & Logistic Regression
8 Appropriate the Linear regression and logistic regression
with a real time example
9 Take a real time example and execute about KNN-
classification
10 Take a Data set available and execute on different inputs of
K-Means clustering algorithm
11 How to deploy machine learning models
4

Q.1. Write a PROLOG program to prove a person as a human


Ans: In Prolog, you can define predicates and rules to assert facts and relationships.

Explanation:
human/1 is a fact which asserts that certain individuals are human.
is_human/1 is a rule which checks if a given person is human based on the facts defined.
You can query is_human (Person). to check if a specific person is human. For example, ‘? -
is_human(john). will return true since "john" is listed as a human in the facts.

Explain the object and property relations


Ans: In Prolog, you can represent object-property relationships using facts and rules.
/* Facts */
property(elephant, gray).
property(tiger, orange).
property(lion, yellow).
property(rabbit, white).
/* Rules */
has_property (Object, Property): -
property (Object, Property).
/* Query */
? - has_property (elephant, gray).

• Explanation:
5

• property/2 is a fact representing the relationship between objects and their properties. For
example, property (elephant, gray) asserts that elephants are gray.
• has_property/2 is a rule that checks if a given object has a specific property based on the facts
defined.
• You can query has_property (Object, Property) to check if a particular object has a specific
property. For example,?- has_property(elephant, gray). will return true because an elephant is
defined as gray in the facts.

Q.2. Write Towers of Hanoi program to apply PROLOG concept


Ans. The Towers of Hanoi is a classic problem in computer science and recursion. In Prolog, we
can solve this problem using predicates to describe the rules of moving disks between pegs.
Here's a Prolog program to solve the Towers of Hanoi problem:
/* Define the predicate for solving Towers of Hanoi */
hanoi(N): -
move (N, left, middle, right).
/* Base case: Moving 0 disks requires no moves */
move(0, _, _, _) :- !.
/* Recursive case: Move N disks from Source to Destination using Auxiliary */
move(N, Source, Auxiliary, Destination) :-
N > 0,
M is N - 1,
move(M, Source, Destination, Auxiliary), % Move N-1 disks from Source to Auxiliary
format('Move disk from ~w to ~w~n', [Source, Destination]), % Move the Nth disk from
Source to Destination
move(M, Auxiliary, Source, Destination). % Move N-1 disks from Auxiliary to Destination
Explanation: hanoi(N): This predicate initiates the solution for the Towers of Hanoi problem with
N disks. It calls the move/4 predicate to perform the moves.
move/4: Base Case: move(0, _, _, _) states that moving 0 disks requires no moves (! is the cut
operator to prevent unnecessary backtracking).
Recursive Case: For N disks:
move(N, Source, Auxiliary, Destination) recursively solves the problem:
move(M, Source, Destination, Auxiliary): Moves M disks from Source to Auxiliary.
format('Move disk from ~w to ~w~n', [Source, Destination]): Prints the move of the Nth disk
from Source to Destination.
move(M, Auxiliary, Source, Destination): Moves M disks from Auxiliary to Destination.
This program uses recursion to solve the Towers of Hanoi problem effectively. To run this
program, you can query hanoi(N). where N is the number of disks you want to move. For
example, ?- hanoi(3). will output the sequence of moves required to solve the Towers of Hanoi
puzzle for 3 disks.
Q.3. Aim: Writing clauses in PROLOG to solve water jug problem
6

Software used: SWI-PROLOG


Program Listing:
database
visited_state(integer,integer)
predicates
state(integer,integer)
clauses
state(2,0).
state(X,Y):-
X < 4,
not(visited_state(4,Y)),
assert(visited_state(X,Y)),
write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\n"),
state(4,Y).
state(X,Y):- Y < 3,
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Fill the 3-Gallon Jug: (", X,",",Y,") --> (", X,",",3,")\n"),
state(X,3).
state(X,Y):- X > 0,
not(visited_state(0,Y)),
assert(visited_state(X,Y)),
write("Empty the 4-Gallon jug on ground: (", X,",",Y,") -->
(",0,",",Y,")\n"),
state(0,Y).
state(X,Y):- Y > 0,
not(visited_state(X,0)),
assert(visited_state(X,0)),
write("Empty the 3-Gallon jug on ground: (", X,",",Y,") -->
(",X,",",0,")\n"),
state(X,0).
state(X,Y):- X + Y >= 4,
Y > 0,
NEW_Y = Y - (4 - X),
7

not(visited_state(4,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour water from 3-Gallon jug to 4-gallon until it is full:
(",X,",",Y,") --> (", 4,",",NEW_Y,")\n"),
state(4,NEW_Y).
state(X,Y):- X + Y >=3,
X > 0,
NEW_X = X - (3 - Y),
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Pour water from 4-Gallon jug to 3-gallon until it is full:
(",X,",",Y,") --> (", NEW_X,",",3,")\n"),
state(NEW_X,3).
state(X,Y):- X + Y <=4,
Y > 0,
NEW_X = X + Y,
not(visited_state(NEW_X,0)),
assert(visited_state(X,Y)),
write("Pour all the water from 3-Gallon jug to 4-gallon:
(",X,",",Y,") --> (", NEW_X,",",0,")\n"),
state(NEW_X,0).
state(X,Y):- X+Y<=3,
X > 0,
NEW_Y = X + Y,
not(visited_state(0,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour all the water from 4-Gallon jug to 3-gallon:
(",X,",",Y,") --> (", 0,",",NEW_Y,")\n"),
state(0,NEW_Y).
state(0,2):- not(visited_state(2,0)),
assert(visited_state(0,2)),
write("Pour 2 gallons from 3-Gallon jug to 4-gallon: (", 0,",",2,") -->
(", 2,",",0,")\n"),
state(2,0).
8

state(2,Y):- not(visited_state(0,Y)),
assert(visited_state(2,Y)),
write("Empty 2 gallons from 4-Gallon jug on the ground:
(",2,",",Y,") --> (", 0,",",Y,")\n"),
state(0,Y).
goal:-
makewindow(1,2,3,"4-3 Water Jug Problem",0,0,25,80),
state(0,0).
Q.4. SCIKIT LEARN
Example:
from sklearn.datasets import load_iris
iris = load_iris ()
A= iris.data
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
print("Feature names:", feature_names)
print("Target names:", target_names)
print("\nFirst 10 rows of A:\n", A[:10])
Output:
Feature names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',
'petal width (cm)']
Target names: ['setosa' 'versicolor' 'virginica']
First 10 rows of X:
=
[
[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
9

[5. 3.4 1.5 0.2]


[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
]
MATPLOTLIB
Pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()

Q.5. Puzzle Problem:

/* This predicate initialises the problem states. The first argument of


solve/3 is the initial state, the 2nd the goal state, and the third the plan that
will be produced. */
test(Plan):-
write('Initial state:'),nl,
Init= [at(tile4,1), at(tile3,2), at(tile8,3), at(empty,4), at(tile2,5),
at(tile6,6), at(tile5,7), at(tile1,8), at(tile7,9)],
write_sol(Init),
Goal= [at(tile1,1), at(tile2,2), at(tile3,3), at(tile4,4), at(empty,5),
at(tile5,6), at(tile6,7), at(tile7,8), at(tile8,9)],
nl,write('Goal state:'),nl,
write(Goal),nl,nl,
10

solve(Init,Goal,Plan).

solve(State, Goal, Plan):-


solve(State, Goal, [], Plan).
/*Determines whether Current and Destination tiles are a valid move. */
is_movable(X1,Y1) :- (1 is X1 - Y1) ; (-1 is X1 - Y1) ; (3 is X1 - Y1) ; (-3
is X1 - Y1).
/*This predicate produces the plan. Once the Goal list is a subset of the
current State the plan is complete and it is written to the screen using
write_sol */
solve(State, Goal, Plan, Plan):-
is_subset(Goal, State), nl,
write_sol(Plan).

solve(State, Goal, Sofar, Plan):-


act(Action, Preconditions, Delete, Add),
is_subset(Preconditions, State),
\+ member(Action, Sofar),
delete_list(Delete, State, Remainder),
append(Add, Remainder, NewState),
solve(NewState, Goal, [Action|Sofar], Plan).
act(move(X,Y,Z),
[at(X,Y), at(empty,Z), is_movable(Y,Z)],
[at(X,Y), at(empty,Z)],
[at(X,Z), at(empty,Y)]).
/*Check is first list is a subset of the second */
is_subset([H|T], Set):-
member(H, Set),
is_subset(T, Set).
is_subset([], _).
/* Remove all elements of 1st list from second to create third. */
delete_list([H|T], Curstate, Newstate):-
remove(H, Curstate, Remainder),
delete_list(T, Remainder, Newstate).
delete_list([], Curstate, Curstate).

remove(X, [H|T], [H|R]):-


remove(X, T, R).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H), nl.
append([H|T], L1, [H|L2]):-
append(T, L1, L2).
append([], L, L).
member(X, [X|_]).
member(X, [_|T]):-
member(X, T).

Q.6. Differentiate the Linear regression and logistic regression with a real time example.

Linear Regression
11

Definition: Linear regression is a statistical method used to model the relationship between a
dependent variable (target variable) and one or more independent variables (predictor variables) by
fitting a linear equation to observed data.

Real-time Example: Predicting House Prices

Scenario: Suppose you are working for a real estate company, and your task is to predict the selling
price of houses based on various factors such as size (in square feet), number of bedrooms, and age of
the house.

Data: You collect a dataset where each entry consists of:

Independent variables (predictors): Size of the house, number of bedrooms, age of the house.
Dependent variable (target): Selling price of the house.
Model: Using linear regression, you can build a model that estimates the selling price (Y) of a house
based on the predictor variables (X1, X2, X3):

Y = β0 + β1*X1 + β2*X2 + β3*X3

Here, β0, β1, β2, β3 are coefficients that the model learns from the data to minimize the difference
between predicted prices and actual prices.
Output: The model will give you a linear equation that can be used to predict the selling price of any
new house based on its size, number of bedrooms, and age.

Logistic Regression
Definition: Logistic regression is used when the dependent variable is categorical. It predicts the
probability of occurrence of an event by fitting data to a logistic function.

Real-time Example: Predicting Customer Churn

Scenario: Imagine you work for a telecommunications company, and your task is to predict whether a
customer will churn (leave the company) based on various customer attributes.

Data: You have a dataset where each entry includes:

Independent variables (predictors): Customer demographics (age, gender, income), service usage
(number of calls, data usage), customer satisfaction scores.
Dependent variable (target): Binary outcome indicating whether the customer churned (1) or not (0).
Model: Using logistic regression, you build a model that predicts the probability (P) of a customer
churning based on the predictor variables (X1, X2, X3, ...):

P(churn = 1) = 1 / (1 + e^-(β0 + β1*X1 + β2*X2 + β3*X3 + ...))

Here, β0, β1, β2, β3, ... are coefficients learned from the data, and e is the base of the natural
logarithm.
Output: The logistic regression model provides a probability score for each customer, indicating the
likelihood of churn. For example, a probability greater than 0.5 might indicate a higher likelihood of
churn, while a probability less than 0.5 indicates a lower likelihood.

Key Differences
1. Nature of Dependent Variable:
Linear Regression: Dependent variable is continuous (numeric).
Logistic Regression: Dependent variable is categorical (binary in binary logistic regression, or
multinomial in multinomial logistic regression).
12

2. Output: Linear Regression: Outputs a continuous value.


Logistic Regression: Outputs a probability score between 0 and 1.

3. Model Equation: Linear Regression: Uses a linear equation to model the relationship between
variables.
Logistic Regression: Uses a logistic function (sigmoid function) to model the probability of a binary
outcome.

4. Application: Linear Regression: Used for predicting values like sales, prices, temperature, etc.,
where the outcome is continuous.
Logistic Regression: Used for classification tasks such as predicting customer churn, spam detection,
disease presence (yes/no), etc., where the outcome is binary or categorical.

In summary, the choice between linear regression and logistic regression depends on the nature of the
dependent variable (continuous or categorical) and the type of prediction task (prediction of values or
prediction of probabilities for binary outcomes).

Q.7. Appropriate the Linear regression and logistic regression with a real time example.
✓ Linear Regression Example: Predicting House Prices
Scenario:
You work for a real estate agency and your goal is to predict the selling price of houses based on
various features such as size, number of bedrooms, and age of the house.

Data: Independent variables (predictors):


Size of the house (in square feet)
Number of bedrooms
Age of the house (in years)

Dependent variable (target):


Selling price of the house (in dollars)

Model:
In this case, Linear Regression can be used to build a model that predicts the selling price (Y) of a
house based on the predictors (X1, X2, X3):

Y = β0 + β1*X1 + β2*X2 + β3*X3

✓ Here, Y is the predicted selling price.


✓ X1, X2, X3 are the independent variables (size, number of bedrooms, age).
✓ β0, β1, β2, β3 are coefficients that the model learns from the data.

Application:
Training: You train the Linear Regression model on historical data where you have records of house
sales with corresponding sizes, number of bedrooms, ages, and selling prices.
Prediction: Once trained, the model can predict the selling price of a new house based on its size,
number of bedrooms, and age.

Output: For example, if you input a house with 2000 square feet, 3 bedrooms, and is 10 years old into
the model, it might predict a selling price of $300,000.

✓ Logistic Regression Example: Predicting Customer Churn


Scenario: You work for a telecommunications company and your task is to predict whether a customer
will churn (cancel their subscription) based on various customer attributes.

Data: Independent variables (predictors):


13

Customer demographics (age, gender, income)


Service usage (number of calls, data usage)
Customer satisfaction scores

Dependent variable (target):


Binary outcome: Whether the customer churned (1) or not (0)

Model: Logistic Regression can be used to build a model that predicts the probability (P) of a
customer churning based on the predictors (X1, X2, X3, ...):

P(churn = 1) = 1 / (1 + e^-(β0 + β1*X1 + β2*X2 + β3*X3 + ...))

✓ Here, P(churn = 1) represents the probability of the customer churning.


✓ X1, X2, X3, ... are the independent variables (customer attributes).
✓ β0, β1, β2, β3, ... are coefficients learned from the data.
✓ e is the base of the natural logarithm.

Application:
Training: Train the Logistic Regression model on historical data where you have records of customers
along with their demographics, service usage, satisfaction scores, and whether they churned or not.
Prediction: Once trained, the model can predict the probability of a new customer churning based on
their attributes.
Output: For example, if you input a customer who is 30 years old, male, with moderate income, has
made few calls recently, and has a low satisfaction score, the model might predict a high probability
(e.g., 0.8) that this customer will churn.

Q.8. Take a real time example and execute about KNN- classification
Example: Classifying Iris Flowers Using K-Nearest Neighbors (KNN)
Scenario: You are tasked with classifying different species of Iris flowers (Setosa, Versicolor, and
Virginica) based on their sepal length, sepal width, petal length, and petal width measurements.

Dataset: The Iris dataset is a classic dataset in machine learning and statistics. It contains
measurements of 150 Iris flowers, with 50 samples from each of three species: Iris setosa, Iris
versicolor, and Iris virginica. Each flower's measurements include:

✓ Sepal length
✓ Sepal width
✓ Petal length
✓ Petal width

Objective: To predict the species of an Iris flower based on its sepal and petal measurements using the
K-Nearest Neighbors algorithm.

Steps to Execute KNN Classification:

Import the Iris dataset, typically available in many machine learning libraries like scikit-learn.

Split the dataset into training and testing sets. The training set will be used to train the KNN model,
and the testing set will be used to evaluate its performance.
Standardize or normalize the feature values if necessary to ensure all features contribute equally to the
distance computation in KNN.

Select an appropriate value for K, the number of neighbors to consider. This is a hyperparameter that
can significantly affect the performance of the model.
14

Instantiate the KNN classifier and fit it to the training data, where the features are the sepal and petal
measurements, and the labels are the species of Iris flowers.

Use the trained model to predict the species of Iris flowers in the testing set based on their sepal and
petal measurements.

Compare the predicted species with the actual species in the testing set to evaluate the accuracy of the
KNN classifier.
Use metrics such as accuracy, precision, recall, and F1-score to assess the performance of the model.

Example Code Snippet Using Python and scikit-learn:

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset


iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = iris.target # Target (species of Iris)

# Split data into training and testing sets


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

# Standardize features by scaling them


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Initialize KNN classifier


knn = KNeighborsClassifier(n_neighbors=3) # Set K=3 (you can experiment with different values of
K)

# Train the model


knn.fit(X_train_scaled, y_train)

# Make predictions on the test data


y_pred = knn.predict(X_test_scaled)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')

# Print classification report


print(classification_report(y_test, y_pred, target_names=iris.target_names))

Explanation of the Code:


1. Loading Data: We load the Iris dataset using load_iris() from scikit-learn.

2. Splitting Data: We split the dataset into training (X_train, y_train) and testing (X_test, y_test) sets
using train_test_split().
15

3. Scaling Features: We standardize the features using StandardScaler to ensure all features are on
the same scale.

4. KNN Classifier: We initialize a KNN classifier (KNeighborsClassifier) with n_neighbors=3,


indicating it will consider 3 nearest neighbors for classification.

5. Training: We train the KNN model on the scaled training data (X_train_scaled, y_train) using
fit().

6. Prediction: We make predictions on the scaled testing data (X_test_scaled) using predict().

7. Evaluation: We evaluate the model’s performance by calculating the accuracy score using
accuracy_score() and printing a detailed classification report using classification_report().

Output Interpretation: The accuracy score indicates how well the model classifies Iris flowers
correctly. The classification_report provides metrics (precision, recall, F1-score) for each class (Iris
setosa, Iris versicolor, Iris virginica) and overall.

This example demonstrates how KNN classification can be applied to predict the species of Iris
flowers based on their sepal and petal measurements, showcasing the practical implementation of the
KNN algorithm in a real-world scenario. Adjusting n_neighbors and exploring other hyperparameters
can further optimize the model's performance for different datasets and tasks.

Q.9. Take a Data set available and execute on different inputs of K-Means clustering
algorithm.
K-Means Clustering on Iris Dataset
Steps:
1. Load the Dataset:
load the Iris dataset from scikit-learn, which contains measurements of Sepal Length, Sepal Width,
Petal Length, and Petal Width for three species of Iris flowers (Setosa, Versicolor, and Virginica).

2. Preprocess the Data: Since K-Means clustering doesn't require training and testing sets like
supervised learning, we'll directly use the features (Sepal Length, Sepal Width, Petal Length, Petal
Width) for clustering.

3. Apply K-Means Clustering: apply K-Means clustering algorithm with different values of K and
observe how the clusters change.

4. Evaluate Clustering Results: Although K-Means is an unsupervised method, we can evaluate the
clustering results using metrics like inertia (sum of squared distances of samples to their closest
cluster center) and silhouette score (a measure of how similar an object is to its own cluster
compared to other clusters).

5. Visualize Clusters: visualize the clusters using scatter plots to see how the data points are grouped
together based on the chosen K.

✓ Example of code using google colab (Python Notebook)


import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

# Load the Iris dataset


16

iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = iris.target # Target (species of Iris)

# Define the range of K values to try


k_values = range(2, 6) # Try K from 2 to 5

# Iterate over each K value


for k in k_values:
# Initialize KMeans with K clusters
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(X)

# Predict the cluster labels


labels = kmeans.labels_

# Calculate silhouette score


silhouette_avg = silhouette_score(X, labels)

# Print the results


print(f"For K = {k}, Silhouette Score = {silhouette_avg:.3f}")

# Plotting the clusters


plt.figure(figsize=(6, 4))
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title(f'K-Means Clustering (K = {k})')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.show()

✓ Output Interpretation: The script will print the silhouette score for each K value, which helps in
determining the optimal number of clusters. A higher silhouette score indicates better-defined
clusters. It will also display scatter plots showing how the data points are grouped into clusters for
each value of K.

✓ Conclusion: By running the above code, you can observe how the Iris dataset clusters into
different groups based on the number of clusters (K) chosen for K-Means clustering. Adjusting K
allows you to explore different partitionings of the data and evaluate which number of clusters
best fits the underlying structure of the data.

Q.10. Take a Data set available and execute on different inputs of K-Medoid clustering
algorithm.
Ans: K-Medoids Clustering with Python
Step 1: Importing Required Libraries
First, make sure you have scikit-learn, numpy, and matplotlib installed. If not, you can install them
using pip:
pip install scikit-learn numpy matplotlib

Now, let's import the necessary libraries in Python:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.metrics import pairwise_distances
17

from sklearn_extra.cluster import KMedoids

Step 2: Generating a Sample Dataset


We'll generate a synthetic dataset using make_blobs from scikit-learn to demonstrate K-Medoids
clustering:

# Generate synthetic data


X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

Step 3: Implementing K-Medoids Clustering


Next, we'll implement K-Medoids clustering using the KMedoids class from sklearn_extra.cluster.

# Initialize K-Medoids algorithm


kmedoids = KMedoids(n_clusters=4, random_state=0)

# Fit the algorithm to data


kmedoids.fit(X)

# Get cluster labels


cluster_labels = kmedoids.labels_

# Get cluster centers (medoids)


medoid_indices = kmedoids.medoid_indices_
medoids = X[medoid_indices]

Step 4: Visualizing the Clusters


Finally, let's visualize the clusters along with their medoids using matplotlib:

# Plotting the clusters and medoids


plt.figure(figsize=(8, 6))

# Plot each cluster


for cluster_label in range(4):
cluster_mask = (cluster_labels == cluster_label)
plt.scatter(X[cluster_mask, 0], X[cluster_mask, 1], label=f'Cluster {cluster_label + 1}')

# Plot medoids
plt.scatter(medoids[:, 0], medoids[:, 1], marker='o', c='r', s=100, label='Medoids')

plt.title('K-Medoids Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()

✓ Explanation:
Step 1: We import necessary libraries and functions.
Step 2: We generate a synthetic dataset with 300 samples and 4 clusters.
Step 3: We apply the K-Medoids algorithm to the dataset, specifying n_clusters=4 for 4 clusters.
Step 4: We visualize the resulting clusters and mark the medoids (cluster centers).

✓ Customization: You can customize this example by:

Using your own dataset instead of make_blobs.


18

Adjusting the number of clusters (n_clusters) in KMedoids.


Changing the dataset features and visualizations based on your needs.

This example provides a basic framework for implementing K-Medoids clustering in Python. The
sklearn_extra library provides an efficient implementation of K-Medoids, suitable for various
datasets.

Q.11. WHAT IS THE PURPOSE OF SVM?

An SVM training algorithm creates a model that assigns new examples to


one of two categories, making it a non-probabilistic binary linear
classifier, given a series of training examples that are individually
designated as belonging to one of two categories.
Before you go any further, make sure you have a basic knowledge of this
topic. In this article, I'll show you how to use machine learning techniques
like scikit-learn to classify cancer UCI datasets using SVM.
Numpy, Pandas, matplot-lib, and scikit-learn are required.

Let's look at a simple support vector categorization example. To begin, we must first generate a
dataset: Implemention in python

# importing scikit learn with make_blobs


from sklearn.datasets.samples_generator import make_blobs
# creating datasets X containing n_samples
# Y containing two classes
X, Y = make_blobs(n_samples=500, centers=2,
random_state=0, cluster_std=0.40)
import matplotlib.pyplot as plt
# plotting scatters
plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='spring');
plt.show()

Support vector machines consider a region around the line of a particular


width in addition to drawing a line between two classes. Here's an
example of how it may appear:

# creating line space between -1 to 3.5


xfit = np.linspace(-1, 3.5)
# plotting scatter
plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='spring')
# plot a line between the different sets of data
for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
yfit = m * xfit + b
plt.plot(xfit, yfit, '-k')
19

plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none',


color='#AAAAAA', alpha=0.4)
plt.xlim(-1, 3.5);
plt.show()

Q.12.How to deploy machine learning models?

Example using Flask (a Python web framework) to deploy a scikit-learn model:

1. Train and save your model:

import pickle
from sklearn.ensemble import RandomForestClassifier

# Train your model


model = RandomForestClassifier()
model.fit(X_train, y_train)

# Save the model


with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
2. Create a Flask web application (app.py):

from flask import Flask, request, jsonify


import pickle
import numpy as np

app = Flask(__name__)

# Load the model


with open('model.pkl', 'rb') as f:
model = pickle.load(f)

@app.route('/predict', methods=['POST'])
def predict():
# Get data from request
data = request.get_json(force=True)
predict_data = np.array(data['data']).reshape(1, -1)
20

# Make prediction
prediction = model.predict(predict_data)

# Return prediction
return jsonify({'prediction': prediction.tolist()})

if __name__ == '__main__':
app.run(debug=True)

3. Deploy and test the Flask application:

Install Flask (pip install Flask) and dependencies.


Run python app.py to start the server locally.
Test the API using tools like Postman or curl (POST request to http://localhost:5000/predict with
JSON data).

This example demonstrates a basic deployment using Flask. For production, you would typically
handle more aspects like scalability, security, and monitoring based on your requirements and
deployment environment.

Deploy Machine Learning Models with Streamlit


1. Prepare Your Model:
Train and finalize your machine learning model as usual. Ensure it's saved in a format that can be
loaded by your Streamlit application (e.g., pickle file for scikit-learn models).

2. Install Streamlit:
Ensure you have Streamlit installed. You can install it via pip:
pip install streamlit

3. Create a Streamlit Application:


Create a new Python script (e.g., app.py) where you'll define your Streamlit application.

import streamlit as st
import pickle
import numpy as np

# Load your trained model


with open('model.pkl', 'rb') as model_file:
model = pickle.load(model_file)

# Define a function to predict the result from input features


def predict_species(sepal_length, sepal_width, petal_length, petal_width):
input_data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
prediction = model.predict(input_data)
species = prediction[0]
return species

# Create a title and description for your app


st.title('Iris Flower Species Prediction')
st.write('This app predicts the species of iris flowers based on their measurements.')
21

# Add input components for user to enter data


sepal_length = st.slider('Sepal Length', 4.0, 8.0, 5.0)
sepal_width = st.slider('Sepal Width', 2.0, 4.5, 3.0)
petal_length = st.slider('Petal Length', 1.0, 7.0, 4.0)
petal_width = st.slider('Petal Width', 0.1, 2.5, 1.0)

# Add a button to make predictions


if st.button('Predict'):
species_prediction = predict_species(sepal_length, sepal_width, petal_length, petal_width)
st.write(f'The predicted species is: {species_prediction}')

4. Run and Test Locally:

Test your Streamlit application locally by running the following command in your terminal:

streamlit run app.py

This command starts a local server and opens your Streamlit application in your default web browser.

5. Deploy to Production:
To deploy your Streamlit application to a server or cloud platform, follow these general steps (specific
details may vary depending on your chosen platform):

Create Requirements File: Create a requirements.txt file listing all dependencies needed (streamlit,
scikit-learn, etc.).

Deploy to Heroku (Example):


Sign up for a free Heroku account if you don't have one.
Install Heroku CLI and log in (Heroku) login:
web: streamlit run app.py
Initialize a Git repository, commit your code, and push to Heroku:
git init
git add .
git commit -m "Initial commit"
heroku create
git push heroku master
Open your deployed application with heroku open.
Deploy to Other Platforms:
For AWS, Google Cloud, or Azure, follow their respective documentation on deploying Python
applications.
Dockerize your Streamlit app for container-based deployments.
Tips for Streamlit Deployment:
22

Performance: Ensure your model loading and prediction are optimized, especially if handling large
models or datasets.
Security: Implement appropriate security measures, such as authentication and input validation,
depending on your deployment environment.
Monitoring: Monitor your deployed application for performance metrics and errors using tools
provided by your deployment platform or third-party services
Streamlit simplifies the deployment process by focusing on data applications' development and
interaction, making it ideal for rapid prototyping and sharing machine learning projects.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy