21 CP 46 - (ML LAB 3)
21 CP 46 - (ML LAB 3)
LAB NO 3
Name: Muhammad Huzaifa
Section: omega
output:
EXPLANATION:
In this task we have to fetch and plot data from different files and learn how to fetch data from different
files at the last there is one problem statement in which we have to fetch data and train model to predict
value of year 2020 using data set provided in the manual.
Task no 2:
Download the 3_gradient_descent zip file from Teams and Extract it find the files and Exercise folder
run the code and get your understanding?
Explanation:
The function now plots the regression line at each iteration with plt.plot(x, y_predicted, color='green',
alpha=0.1) to show the gradual convergence.
Task no 3:
T3: Modify the Example 1 code to handle multivariate linear regression. Update the synthetic dataset
to include multiple features.
Code:
import NumPy as np
theta = np.zeros(X.shape[1])
m = len(y)
# Calculate predictions
# Calculate errors
errors = predictions - y
# Update coefficients
cost = np.sum(errors ** 2) / (2 * m)
return theta
y = 2 * X1 + 3 * X2 + 1 + 0.1 * np.random.randn(100, 1)
y = y.T[0]
learning_rate = 0.01
num_iterations = 100
# Calculate and print the final cost using the trained model
output:
TASK NO 4:
T4: Modify the Example 1 code to implement stochastic gradient descent. Update the algorithm to
update the coefficients based on a single randomly chosen instance at each iteration.(Explore at your
End)
Code:
import numpy as np
# Initialize coefficients
theta = np.zeros(X.shape[1])
m = len(y)
for iteration in range(num_iterations):
shuffled_indices = np.random.permutation(m)
X_shuffled = X[shuffled_indices]
y_shuffled = y[shuffled_indices]
for i in range(m):
xi = X_shuffled[i:i+1]
yi = y_shuffled[i:i+1]
error = prediction - yi
errors = predictions - y
cost = np.sum(errors ** 2) / (2 * m)
return theta
y = 2 * X1 + 3 * X2 + 1 + 0.1 * np.random.randn(100, 1)
y = y.T[0]
learning_rate = 0.01
num_iterations = 100
# Calculate and print the final cost using the trained model
OUTPUT:
TASK NO 5:
Run All codes in the Lab Manuals.
Output:
EXAMPLE 1:
…………………………………THE END……………………………………………