Regression models

 

Expt No: 5                                           Regression models

Date:

 

Aim: To write a program to demonstrate various Regression models

 

Program

 

# Linear Regression, Bayesian Linear Regression and Polynomial Regression

 

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression, BayesianRidge

from sklearn.preprocessing import PolynomialFeatures

from sklearn.metrics import mean_squared_error

from sklearn.impute import SimpleImputer

import numpy as np

import matplotlib.pyplot as plt

 

# Load the dataset

df = pd.read_csv('HousingData.csv')

 

# Assume 'MEDV' as the dependent variable and the rest as independent variables

X = df.drop('MEDV', axis=1)

y = df['MEDV']

 

# Split the data into training and testing sets

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

 

# Handle missing values using simple imputation with mean

imputer = SimpleImputer(strategy='mean')

X_train_imputed = imputer.fit_transform(X_train)

X_test_imputed = imputer.transform(X_test)

 

# Linear Regression

lin_reg = LinearRegression()

lin_reg.fit(X_train_imputed, y_train)

lin_reg_train_pred = lin_reg.predict(X_train_imputed)

lin_reg_test_pred = lin_reg.predict(X_test_imputed)

 


 

# Bayesian Linear Regression

bayesian_reg = BayesianRidge()

bayesian_reg.fit(X_train_imputed, y_train)

bayesian_reg_train_pred = bayesian_reg.predict(X_train_imputed)

bayesian_reg_test_pred = bayesian_reg.predict(X_test_imputed)

 

# Polynomial Regression (degree=2)

poly_reg = PolynomialFeatures(degree=2)

X_train_poly = poly_reg.fit_transform(X_train_imputed)

X_test_poly = poly_reg.transform(X_test_imputed)

poly_lin_reg = LinearRegression()

poly_lin_reg.fit(X_train_poly, y_train)

poly_lin_reg_train_pred = poly_lin_reg.predict(X_train_poly)

poly_lin_reg_test_pred = poly_lin_reg.predict(X_test_poly)

 

# Calculate mean squared error

lin_reg_train_mse = mean_squared_error(y_train, lin_reg_train_pred)

lin_reg_test_mse = mean_squared_error(y_test, lin_reg_test_pred)

bayesian_reg_train_mse = mean_squared_error(y_train, bayesian_reg_train_pred)

bayesian_reg_test_mse = mean_squared_error(y_test, bayesian_reg_test_pred)

poly_lin_reg_train_mse = mean_squared_error(y_train, poly_lin_reg_train_pred)

poly_lin_reg_test_mse = mean_squared_error(y_test, poly_lin_reg_test_pred)

 

print("Linear Regression:")

print(f"  Train MSE: {lin_reg_train_mse:.2f}")

print(f"  Test MSE: {lin_reg_test_mse:.2f}")

 

print("Bayesian Linear Regression:")

print(f"  Train MSE: {bayesian_reg_train_mse:.2f}")

print(f"  Test MSE: {bayesian_reg_test_mse:.2f}")

 

print("Polynomial Regression (degree=2):")

print(f"  Train MSE: {poly_lin_reg_train_mse:.2f}")

print(f"  Test MSE: {poly_lin_reg_test_mse:.2f}")

 


 

# Plot actual vs predicted prices

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

plt.scatter(y_test, lin_reg_test_pred, color='blue', label='Linear Regression')

plt.scatter(y_test, bayesian_reg_test_pred, color='green', label='Bayesian Linear Regression')

plt.scatter(y_test, poly_lin_reg_test_pred, color='red', label='Polynomial Regression (degree=2)')

plt.xlabel('Actual Price')

plt.ylabel('Predicted Price')

plt.title('Actual vs Predicted Prices (Regression)')

plt.legend()

plt.show()

 

# Plot actual vs predicted prices with the fitted line for linear regression

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

plt.scatter(y_test, lin_reg_test_pred, color='blue', label='Linear Regression')

plt.xlabel('Actual Price')

plt.ylabel('Predicted Price')

plt.title('Actual vs Predicted Prices for Linear Regression')

plt.legend()

plt.show()

 

# Plot actual vs predicted prices with the fitted line for polynomial regression

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

plt.scatter(y_test, poly_lin_reg_test_pred, color='red', label='Polynomial Regression (degree=2)')

plt.xlabel('Actual Price')

plt.ylabel('Predicted Price')

plt.title('Actual vs Predicted Prices for Polynomial Regression')

plt.legend()

plt.show()

 

# Plot actual vs predicted prices for Bayesian Linear Regression

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

plt.scatter(y_test, bayesian_reg_test_pred, color='green', label='Bayesian Linear Regression')

plt.xlabel('Actual Price')

plt.ylabel('Predicted Price')

plt.title('Actual vs Predicted Prices for Bayesian Linear Regression')

plt.legend()

plt.show()



# Logistic Regression

# Single variable

 

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

 

# Generate synthetic dataset with multiple features

np.random.seed(42)

n_samples = 1000

 

# Generate features: transaction amount, transaction time, and transaction type

transaction_amount = np.random.normal(loc=50, scale=20, size=n_samples)

transaction_time = np.random.uniform(low=0, high=24, size=n_samples)  # Transaction time in hours

transaction_type = np.random.choice(['Online', 'In-person'], size=n_samples)

 

# Generate target variable: is_fraudulent

# Assume transactions made between 1:00 AM and 6:00 AM, online transactions,

# and high transaction amounts have a higher probability of being fraudulent

is_fraudulent = (((transaction_time >= 1) & (transaction_time <= 6)) |

                 (transaction_type == 'Online') |

                 (transaction_amount > 70)).astype(int)

 

# Create DataFrame

df = pd.DataFrame({

    'TransactionAmount': transaction_amount,

    'TransactionTime': transaction_time,

    'TransactionType': transaction_type,

    'IsFraudulent': is_fraudulent

})

 

# One-hot encode the 'TransactionType' feature

df = pd.get_dummies(df, columns=['TransactionType'])

 


 

# Separate datasets based on each feature for logistic regression

datasets = [('Transaction Amount', df[['TransactionAmount']]),

            ('Transaction Time', df[['TransactionTime']]),

            ('Transaction Type', df.drop(['TransactionAmount', 'TransactionTime', 'IsFraudulent'], axis=1))]

 

# Perform logistic regression for each feature

for feature_name, X_feature in datasets:

    X_train, X_test, y_train, y_test = train_test_split(X_feature, df['IsFraudulent'], test_size=0.2, random_state=42)

    model = LogisticRegression(solver='liblinear')

    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print(f"Accuracy based on {feature_name} only : {accuracy:.2f}")

    print()

 


 

# Logistic Regression

# Multiple variables

 

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

 

# Generate synthetic dataset with multiple features

np.random.seed(42)

n_samples = 1000

 

# Generate features: transaction amount, transaction time, and transaction type

transaction_amount = np.random.normal(loc=50, scale=20, size=n_samples)

transaction_time = np.random.uniform(low=0, high=24, size=n_samples)  # Transaction time in hours

transaction_type = np.random.choice(['Online', 'In-person'], size=n_samples)

 

# Generate target variable: is_fraudulent

# Assume transactions made between 1:00 AM and 6:00 AM, online transactions,

# and high transaction amounts have a higher probability of being fraudulent

is_fraudulent = (((transaction_time >= 1) & (transaction_time <= 6)) |

                 (transaction_type == 'Online') |

                 (transaction_amount > 70)).astype(int)

 

# Create DataFrame

df = pd.DataFrame({

    'TransactionAmount': transaction_amount,

    'TransactionTime': transaction_time,

    'TransactionType': transaction_type,

    'IsFraudulent': is_fraudulent

})

 

# One-hot encode the 'TransactionType' feature

df = pd.get_dummies(df, columns=['TransactionType'])

 

# Split the data into training and testing sets

X = df.drop('IsFraudulent', axis=1)

y = df['IsFraudulent']

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

 

# Fit logistic regression model

model = LogisticRegression(solver='liblinear')

model.fit(X_train, y_train)

 

# Predict the classes for the test set

y_pred = model.predict(X_test)

 

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy involving all three variables : {accuracy:.2f}")

 

 

 

 

Result: Thus the program to demonstrate Regression models were written and executed.



Sample Output

Linear Regression, Bayesian Linear Regression and Polynomial Regression:

 

Linear Regression:

  Train MSE: 22.40

  Test MSE: 25.00

 

Bayesian Linear Regression:

  Train MSE: 23.09

  Test MSE: 25.30

 

Polynomial Regression (degree=2):

  Train MSE: 6.53

  Test MSE: 16.46










 

# Logistic Regression

# Single variables

Accuracy based on Transaction Amount only : 0.67

Accuracy based on Transaction Time only : 0.69

Accuracy based on Transaction Type only : 0.86

 

# Logistic Regression

# Multiple variables

Accuracy involving all three variables : 0.91






Decision Tree and Random Forest

 

Expt No: 6                                           Decision Tree and Random Forest

Date:

 

Aim: To write a program to demonstrate Decision Tree and Random Forest.

 

Program

 

# Decision Tree

import pandas as pd

from sklearn.tree import DecisionTreeClassifier, plot_tree

import matplotlib.pyplot as plt

 

# Load the Iris dataset from a CSV file and drop Index column

iris_df = pd.read_csv("Iris.csv")

iris_df.drop('Id', axis=1, inplace=True)

 

# Separate features (X) and target variable (y)

X = iris_df[['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']]

y = iris_df['Species']

 

# Initialize and train a decision tree classifier

clf = DecisionTreeClassifier()

clf.fit(X, y)

 

# Plot the decision tree

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

plot_tree(clf, filled=True, feature_names=X.columns, class_names=clf.classes_)

plt.show()

 

 


 

# Random Forest

# Import necessary libraries

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import classification_report, confusion_matrix

from sklearn.tree import plot_tree

import matplotlib.pyplot as plt

 

# Load the Iris dataset from CSV file and drop Index field

iris_data = pd.read_csv("Iris.csv")

iris_data.drop('Id', axis=1, inplace=True)

 

# Split features and target variable

X = iris_data.drop('Species', axis=1)

y = iris_data['Species']

 

# Split the dataset 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)

 

# Create a Random Forest classifier

rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

 

# Train the classifier on the training data

rf_classifier.fit(X_train, y_train)

 

# Select one of the decision trees from the Random Forest

tree_to_display = rf_classifier.estimators_[0]

 


 

# Display the selected decision tree

plt.figure(figsize=(12, 12))

plot_tree(tree_to_display, feature_names=X.columns,

class_names=iris_data['Species'].unique(), filled=True)

plt.show()

 

 

 

 

Result: Thus the program to demonstrate Decision Tree and Random Forest was written and executed.

 

 Sample Output

 

# Decision Tree


# Random Forest 






 

 


 

Bayesian Networks

 

Expt No: 4                                           Bayesian Networks

Date:

 

Aim: To write a program to demonstrate Bayesian Network

 

Program

 

from pgmpy.models import BayesianNetwork

from pgmpy.factors.discrete import TabularCPD

from pgmpy.inference import VariableElimination

import networkx as nx

import matplotlib.pyplot as plt

 

# Define the structure of the Bayesian Network

model = BayesianNetwork([('Burglary', 'Alarm'), ('Earthquake', 'Alarm'), ('Alarm', 'JohnCalls'), ('Alarm', 'MaryCalls')])

 

# Define Conditional Probability Distributions (CPDs)

cpd_burglary = TabularCPD(variable='Burglary', variable_card=2, values=[[0.999], [0.001]])

cpd_earthquake = TabularCPD(variable='Earthquake', variable_card=2, values=[[0.998], [0.002]])

cpd_alarm = TabularCPD(variable='Alarm', variable_card=2, values=[[0.999, 0.71, 0.06, 0.05], [0.001, 0.29, 0.94, 0.95]], evidence=['Burglary', 'Earthquake'], evidence_card=[2, 2])

cpd_johncalls = TabularCPD(variable='JohnCalls', variable_card=2, values=[[0.95, 0.1], [0.05, 0.9]], evidence=['Alarm'], evidence_card=[2])

cpd_marycalls = TabularCPD(variable='MaryCalls', variable_card=2, values=[[0.99, 0.3], [0.01, 0.7]], evidence=['Alarm'], evidence_card=[2])

 

# Add CPDs to the model

model.add_cpds(cpd_burglary, cpd_earthquake, cpd_alarm, cpd_johncalls, cpd_marycalls)

 

# Check if the model is correctly defined

model.check_model()

 

# Perform some inference queries

infer = VariableElimination(model)

 


 

# Query: P(Alarm | Burglary=True, Earthquake=False)

query_result_alarm = infer.query(variables=['Alarm'], evidence={'Burglary': True, 'Earthquake': False})

 

# Query: P(JohnCalls | Burglary=True, Earthquake=False)

query_result_johncalls = infer.query(variables=['JohnCalls'], evidence={'Burglary': True, 'Earthquake': False})

 

# Query: P(MaryCalls | Burglary=True, Earthquake=False)

query_result_marycalls = infer.query(variables=['MaryCalls'], evidence={'Burglary': True, 'Earthquake': False})

 

# Print the CPDs

print("\nConditional Probability Distributions (CPDs):")

for cpd in model.get_cpds():

    print(cpd.variable)

    print(cpd.values)

    print()

 

# Print the query results

print("\nInference Results:")

print("P(Alarm | Burglary=True, Earthquake=False):")

print(query_result_alarm.values)

print("\nP(JohnCalls | Burglary=True, Earthquake=False):")

print(query_result_johncalls.values)

print("\nP(MaryCalls | Burglary=True, Earthquake=False):")

print(query_result_marycalls.values)

 

# Create a directed graph

graph = nx.DiGraph(model.edges())

 

# Plot the graph

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

pos = nx.spring_layout(graph)  # Positions for all nodes

nx.draw(graph, pos, with_labels=False, node_size=2000, node_color="skyblue", font_size=16, font_weight="bold", arrowsize=20)

labels = {node: node for node in model.nodes()}  # Node labels

nx.draw_networkx_labels(graph, pos, labels, font_color="black")

plt.title("Bayesian Network")

plt.show()





Result: Thus the program to demonstrate Bayesian Network was written and executed.

 

Sample Output

Conditional Probability Distributions (CPDs):

Burglary

[0.999 0.001]

 

Earthquake

[0.998 0.002]

 

Alarm

[[[0.999 0.71 ]

  [0.06  0.05 ]]

 

 [[0.001 0.29 ]

  [0.94  0.95 ]]]

 

JohnCalls

[[0.95 0.1 ]

 [0.05 0.9 ]]

 

MaryCalls

[[0.99 0.3 ]

 [0.01 0.7 ]]

 

 

Inference Results:

P(Alarm | Burglary=True, Earthquake=False):

[0.06 0.94]

 

P(JohnCalls | Burglary=True, Earthquake=False):

[0.151 0.849]

 

P(MaryCalls | Burglary=True, Earthquake=False):

[0.3414 0.6586]