ML 7th and 10th Program
ML 7th and 10th Program
Program:
import pandas as pd
from pgmpy.models import BayesianNetwork
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.inference import VariableElimination
# Prompt the user for input and ensure it's a valid integer
def get_input(prompt, valid_choices):
while True:
try:
user_input = int(input(prompt))
if user_input not in valid_choices:
print(f"Please enter a valid value from {valid_choices}")
else:
return user_input
except ValueError:
print("Invalid input. Please enter a number.")
Output:
For age Enter { SuperSeniorCitizen:0, SeniorCitizen:1, MiddleAged:2,
Youth:3, Teen:4 }
For Gender Enter { Male:0, Female:1 }
For Family History Enter { yes:1, No:0 }
For diet Enter { High:0, Medium:1 }
For lifeStyle Enter { Athlete:0, Active:1, Moderate:2, Sedentary:3 }
For cholesterol Enter { High:0, BorderLine:1, Normal:2 }
Enter age :4
Enter Gender :1
Enter Family history :0
Enter diet :1
Enter Lifestyle :1
Enter cholestrol :1
Probability of heart disease:
[0. 1.]
The most likely outcome for heart disease is: Disease
---------------------------------------------------------------------------------------------
Program:
import numpy as np
import matplotlib.pyplot as plt
def gaussian_kernel(x, x0, tau):
return np.exp(-np.sum((x - x0)**2) / (2 * tau**2))
def compute_weights(X, x0, tau):
m = X.shape[0]
weights = np.zeros(m)
for i in range(m):
weights[i] = gaussian_kernel(X[i], x0, tau)
return np.diag(weights)
def locally_weighted_regression(X, y, x0, tau):
X_b = np.c_[np.ones((X.shape[0], 1)), X] # Add intercept term
x0_b = np.r_[1, x0] # Add intercept term to the query point
W = compute_weights(X, x0, tau)
theta = np.linalg.inv(X_b.T @ W @ X_b) @ (X_b.T @ W @ y)
return x0_b @ theta
def plot_lwr(X, y, tau):
X_range = np.linspace(np.min(X), np.max(X), 300)
y_pred = [locally_weighted_regression(X, y, x0, tau) for x0 in X_range]
Output: