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

AIML Laboratory Set-B

Uploaded by

Anbuchelvan VK
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)
15 views

AIML Laboratory Set-B

Uploaded by

Anbuchelvan VK
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/ 7

6.

Develop a code by implementing the probability relationship check between two


datasetusing Bayesian Networks

Import pandas as pd

From pgmpy.models import BayesianNetwork

From pgmpy.inference import VariableElimination

# Load datasets

Data1 = pd.DataFrame({‘key’: [1, 2, 3], ‘A’: [1, 0, 1]})

Data2 = pd.DataFrame({‘key’: [1, 2, 3], ‘B’: [0, 1, 1], ‘C’: [1, 0, 1]})

# Combine data

Data = pd.merge(data1, data2, on=’key’)

# Define and fit model

Model = BayesianNetwork([(‘A’, ‘B’), (‘B’, ‘C’)])

Model.fit(data)

# Perform inference

Result = VariableElimination(model).query(variables=[‘C’], evidence={‘B’: 1})

Print(result)
Expected Output :
7.Develop a code to understand and predict an outcome variable based on the
inputRegression models.

Import numpy as np

From sklearn.linear_model import LinearRegression

Import matplotlib.pyplot as plt

From sklearn.metrics import mean_squared_error, r2_score

X = 2 * np.random.rand(100, 1)

Y = 4 + 3 * X + np.random.randn(100, 1)

Model = LinearRegression().fit(X, y)

Y_pred = model.predict(X)

Plt.scatter(X, y)

Plt.plot(X, y_pred, color=’blue’)

Plt.show()

Print(f’MSE: {mean_squared_error(y, y_pred):.2f}, R^2: {r2_score(y, y_pred):.2f}’)

Expected Output :
8.Develop a code to Build a decision trees to predict the expected output from the
desiredinput.

From sklearn.datasets import make_classification

From sklearn.model_selection import train_test_split

From sklearn.tree import DecisionTreeClassifier, plot_tree

From sklearn.metrics import accuracy_score

Import matplotlib.pyplot as plt

X, y = make_classification(n_samples=100, n_features=4)

X_train, X_test, y_train, y_test = train_test_split(X, y)

Model = DecisionTreeClassifier().fit(X_train, y_train)

Print(f’Accuracy: {accuracy_score(y_test, model.predict(X_test)):.2f}’)

Plot_tree(model, filled=True)

Plt.show()

Expected Output :
9.Develop a code to build random forests for the dataset by understand the
differencebetween Random and Decision Tree.

From sklearn.datasets import make_classification

From sklearn.model_selection import train_test_split

From sklearn.tree import DecisionTreeClassifier

From sklearn.ensemble import RandomForestClassifier

From sklearn.metrics import accuracy_score

X, y = make_classification(n_samples=100, n_features=4)

X_train, X_test, y_train, y_test = train_test_split(X, y)

Print(f’Decision Tree Accuracy: {accuracy_score(y_test,


DecisionTreeClassifier().fit(X_train, y_train).predict(X_test)):.2f}’)

Print(f’Random Forest Accuracy: {accuracy_score(y_test,


RandomForestClassifier().fit(X_train, y_train).predict(X_test)):.2f}’)

Expected Output:

Decision Tree Accuracy: 0.85

Random Forest Accuracy: 0.90


10.Develop a code to Build support vector machine models for classification Task.

From sklearn.datasets import make_classification

From sklearn.model_selection import train_test_split

From sklearn.svm import SVC

From sklearn.metrics import accuracy_score

# Generate and split data

X, y = make_classification(n_samples=100, n_features=2)

X_train, X_test, y_train, y_test = train_test_split(X, y)

# Train and evaluate SVM model

Model = SVC(kernel=’linear’).fit(X_train, y_train)

Accuracy = accuracy_score(y_test, model.predict(X_test))

Print(f’Accuracy: {accuracy:.2f}’)

Expected Output:

Accuracy: 0.95

You might also like