0% found this document useful (0 votes)
2 views4 pages

ML LAB Manual-1

The document is a lab manual for implementing linear regression using Python with libraries such as NumPy, Matplotlib, and scikit-learn. It includes code for generating synthetic data, splitting it into training and testing sets, fitting a linear regression model, and evaluating the model's performance. Additionally, it provides a manual implementation of linear regression using gradient descent and visualizations of the results.

Uploaded by

vaishnavia2811
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)
2 views4 pages

ML LAB Manual-1

The document is a lab manual for implementing linear regression using Python with libraries such as NumPy, Matplotlib, and scikit-learn. It includes code for generating synthetic data, splitting it into training and testing sets, fitting a linear regression model, and evaluating the model's performance. Additionally, it provides a manual implementation of linear regression using gradient descent and visualizations of the results.

Uploaded by

vaishnavia2811
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/ 4

Machine Learning Lab Manual

Anaconda Website

1) Program to implement linear regression


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

# Generate synthetic data


np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1) # y = 4 + 3X + noise
# Split 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)

# Using sklearn's LinearRegression


model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

# Print model parameters


print(f"Intercept: {model.intercept_[0]}")
print(f"Slope: {model.coef_[0][0]}")
print(f"Mean Squared Error: {mean_squared_error(y_test, y_pred)}")

# Plot results
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Prediction')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

# Manual implementation using Gradient Descent


def gradient_descent(X, y, learning_rate=0.1, n_iterations=1000):
m = len(y)
theta = np.random.randn(2, 1) # Random initialization
X_b = np.c_[np.ones((m, 1)), X] # Add bias term

for iteration in range(n_iterations):


gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
theta -= learning_rate * gradients

return theta

theta_best = gradient_descent(X_train, y_train)


print(f"Manual Gradient Descent Intercept: {theta_best[0][0]}")
print(f"Manual Gradient Descent Slope: {theta_best[1][0]}")

# Plot manual implementation result


y_manual_pred = theta_best[0] + theta_best[1] * X_test
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X_test, y_manual_pred, color='green', linewidth=2, label='Manual Prediction')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

You might also like