AIML PROGRAMS
AIML PROGRAMS
4)BAYESIAN NETWORK
PROGRAM:
import pandas as pd
import numpy as np
import pgmpy
def fetch_ucirepo(id):
class Data:
def __init__(self):
self.features = pd.DataFrame({
})
return Data()
heart_disease = fetch_ucirepo(id=45)
X = heart_disease.features
y = heart_disease.targets
data = X.copy()
data['target'] = y
data = data.astype(float)
# Drop rows with NaN values (or you can use imputation)
data.dropna(inplace=True)
model = BayesianNetwork([
('age', 'trestbps'),
('sex', 'trestbps'),
('cp', 'target'),
('trestbps', 'target'),
('chol', 'target'),
('fbs', 'target'),
('restecg', 'target'),
('thalach', 'target'),
('exang', 'target'),
('oldpeak', 'target'),
('slope', 'target'),
('ca', 'target'),
('thal', 'target')
])
model.fit(data, estimator=MaximumLikelihoodEstimator)
# Perform inference
infer = VariableElimination(model)
# Example: Query the probability of having heart disease (target=1) given some evidence
try:
# Adjust the evidence keys to match the actual column names in the DataFrame
print(q)
except KeyError as e:
print(f"KeyError: {e}. Make sure the evidence variables are part of the model.")
except ValueError as e:
print(f"ValueError: {e}. Check the values and data types of your evidence.")
OUTPUT:
Model nodes: ['age', 'trestbps', 'sex', 'cp', 'target', 'chol', 'fbs', 'restecg', 'thalach', 'exang',
'oldpeak', 'slope', 'ca', 'thal']
+-------------+---------------+
| target | phi(target) |
+=============+===============+
| target(0.0) | 0.5003 |
+-------------+---------------+
| target(1.0) | 0.4997 |
import numpy as np
import pandas as pd
print(cpd)
OUTPUT:
+------+-------+
| A(0) | 0.496 |
+------+-------+
| A(1) | 0.504 |
+------+-------+
+------+-------+
| C(0) | 0.486 |
+------+-------+
| C(1) | 0.514 |
+------+-------+
+------+---------------------+--------------------+
| C | C(0) | C(1) |
+------+---------------------+--------------------+
+------+---------------------+--------------------+
| D(1) | 0.49794238683127573 | 0.4961089494163424 |
+------+---------------------+--------------------+
+------+---------------------+---------------------+---------------------+---------------------+
+------+---------------------+---------------------+---------------------+---------------------+
+------+---------------------+---------------------+---------------------+---------------------+
+------+---------------------+---------------------+---------------------+---------------------+
+------+---------------------+---------------------+---------------------+---------------------+
import pandas as pd
import numpy as np
np.random.seed(42)
num_samples = 100
feature1 = np.random.rand(num_samples) * 10
feature2 = np.random.rand(num_samples) * 20
feature3 = np.random.rand(num_samples) * 30
noise = np.random.randn(num_samples) * 5
# Create a DataFrame
df = pd.DataFrame({
'feature1': feature1,
'feature2': feature2,
'feature3': feature3,
'target': target
})
df.to_csv('dataset.csv', index=False)
print(df.head())
import pandas as pd
import numpy as np
df = pd.read_csv('dataset.csv')
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
plt.xlabel('feature1')
plt.ylabel('target')
plt.legend()
plt.show()
OUTPUT:
print(data.head())
import pandas as pd
# Load data
data = pd.read_csv('example_data.csv')
X = data.drop(['target'], axis=1)
y = data['target']
# Split data into training and test sets
dt = DecisionTreeRegressor()
dt.fit(X_train, y_train)
y_pred_dt = dt.predict(X_test)
rf = RandomForestRegressor()
rf.fit(X_train, y_train)
y_pred_rf = rf.predict(X_test)
# Evaluate performance of Random Forest