AILab Journal Karan
AILab Journal Karan
Year 2023-2024
By
Ms. Pawar Karan Ramesh
(Seat No:1910558)
2
Date: -
Place: -
3
Index
Sr No Practical Sign
1 Write a PROLOG program to prove a person as a human
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
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.
• 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.
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
solve(Init,Goal,Plan).
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.
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.
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):
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.
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.
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, ...):
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
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.
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):
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.
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, ...):
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.
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.
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.
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.
iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = iris.target # Target (species of Iris)
✓ 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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.metrics import pairwise_distances
17
# 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).
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.
Let's look at a simple support vector categorization example. To begin, we must first generate a
dataset: Implemention in python
import pickle
from sklearn.ensemble import RandomForestClassifier
app = Flask(__name__)
@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)
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.
2. Install Streamlit:
Ensure you have Streamlit installed. You can install it via pip:
pip install streamlit
import streamlit as st
import pickle
import numpy as np
Test your Streamlit application locally by running the following command in your terminal:
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.).
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~