0% found this document useful (0 votes)
6 views21 pages

Lab 14 112

lab

Uploaded by

20-cs-112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

Lab 14 112

lab

Uploaded by

20-cs-112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DEPARTMENT OF COMPUTER SCIENCE

HITEC UNIVERSITY, TAXILA


Semester
“7 ”
TH

Section “C”
Lab report
#14

HUMAN COMPUTER INTERACTION

Submitted To:
Sir Abdullah shehroz

Submitted By:
Aliza Wajid
20-cs-112
Code 1:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal

# Function to generate multivariate normal data


def generate_multivariate_normal(mean, cov_matrix, num_samples):
np.random.seed(42) # Set seed for reproducibility
return np.random.multivariate_normal(mean, cov_matrix, num_samples)

# Function to plot normal curves


def plot_normal_curves(ax, data, labels):
for i in range(data.shape[1]):
mean = np.mean(data[:, i])
std_dev = np.std(data[:, i])
x = np.linspace(mean - 3 * std_dev, mean + 3 * std_dev, 100)
y = (1 / (std_dev * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mean) / std_dev)**2)
ax.plot(x, y, label=f'{labels[i]} Normal Curve')

# Function to plot variability scatter plot


def plot_variability_scatter(ax, data, labels):
ax.scatter(data[:, 0], data[:, 1], alpha=0.5)
ax.set_xlabel(labels[0])
ax.set_ylabel(labels[1])
ax.set_title('Variability Scatter Plot')

# Function to annotate correlation on the scatter plot


def annotate_correlation(ax, data, labels):
correlation = np.corrcoef(data[:, 0], data[:, 1])[0, 1]
ax.annotate(f'Correlation: {correlation:.2f}', xy=(0.5, 0.95), xycoords='axes fraction',
ha='center', fontsize=10)

# Set parameters
mean = [0, 0]
cov_matrix = [[1, 0.8], [0.8, 1]]
num_samples = 1000
labels = ['Variable A', 'Variable B']

# Generate multivariate normal data


data = generate_multivariate_normal(mean, cov_matrix, num_samples)

# Create subplots
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))

# Plot normal curves


plot_normal_curves(ax1, data, labels)
ax1.legend()
ax1.set_title('Normal Curves')

# Plot variability scatter plot


plot_variability_scatter(ax2, data, labels)
ax2.set_title('Variability Scatter Plot')

# Annotate correlation on the scatter plot


annotate_correlation(ax2, data, labels)

# Show plots
plt.tight_layout()
plt.show()
Code 2:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Function to generate correlated data


def generate_correlated_data(mean, std_dev, correlation, size):
cov_matrix = [[std_dev[0]**2, correlation * std_dev[0] * std_dev[1]],
[correlation * std_dev[0] * std_dev[1], std_dev[1]**2]]

data = np.random.multivariate_normal(mean, cov_matrix, size)


return data

# Function to plot normal curves


def plot_normal_curves(ax, mean, std_dev, labels):
x = np.linspace(mean[0] - 3 * std_dev[0], mean[0] + 3 * std_dev[0], 100)
y1 = norm.pdf(x, mean[0], std_dev[0])
ax.plot(x, y1, label=labels[0])

x = np.linspace(mean[1] - 3 * std_dev[1], mean[1] + 3 * std_dev[1], 100)


y2 = norm.pdf(x, mean[1], std_dev[1])
ax.plot(x, y2, label=labels[1])

# Function to plot variability scatter plot and incorporate correlation info


def plot_variability_scatter(ax, data, labels):
ax.scatter(data[:, 0], data[:, 1], alpha=0.5)
ax.set_xlabel(labels[0])
ax.set_ylabel(labels[1])
ax.set_title('Variability Scatter Plot')

# Function to annotate correlation on the scatter plot


def annotate_correlation(ax, correlation):
ax.annotate(f'Correlation: {correlation:.2f}', xy=(0.5, 0.95), xycoords='axes fraction',
ha='center', fontsize=10)

# Set parameters
mean = [0, 0]
std_dev = [1, 1]
correlation = 0.7
size = 1000
labels = ['Variable A', 'Variable B']

# Generate correlated data


data = generate_correlated_data(mean, std_dev, correlation, size)

# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Plot normal curves


plot_normal_curves(ax1, mean, std_dev, labels)
ax1.legend()
ax1.set_title('Normal Curves')

# Plot variability scatter plot and incorporate correlation info


plot_variability_scatter(ax2, data, labels)
annotate_correlation(ax2, correlation)

# Show plots
plt.tight_layout()
plt.show()

Code 3:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Function to generate synthetic data


def generate_synthetic_data(size=100, random_seed=42):
np.random.seed(random_seed)
X = 2 * np.random.rand(size, 1)
y = 4 + 3 * X + np.random.randn(size, 1)
return X, y

# Function to plot the data and regression line


def plot_data_and_regression_line(X, y, y_pred):
plt.scatter(X, y, label='Data')
plt.plot(X, y_pred, color='red', label='Linear Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression')
plt.legend()
plt.show()

# Generate synthetic data


X, y = generate_synthetic_data()

# 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)

# Initialize the linear regression model


model = LinearRegression()

# Fit the model to the training data


model.fit(X_train, y_train)

# Make predictions on the test data


y_pred = model.predict(X_test)

# Calculate and print the mean squared error on the test data
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error on Test Data: {mse:.2f}')

# Plot the data and regression line


plot_data_and_regression_line(X_test, y_test, y_pred)

You might also like