Experiment 7 ml vtu
Experiment 7 ml vtu
Linear Regression
Definition
Linear Regression models the relationship between an independent variable (x) and a
dependent variable ( y ) using a straight-line equation:
y = mx + c
where:
• m is the slope (coefficient) of the line,
• c is the intercept,
• x is the independent variable,
• y is the dependent variable (predicted value).
Polynomial Regression
Polynomial regression is a type of regression analysis used in statistics and machine learning
when the relationship between the independent variable (input) and the dependent
variable (output) is not linear. While simple linear regression models the relationship as a
straight line, polynomial regression allows for more flexibility by fitting a polynomial
equation to the data.
Polynomial Regression is an extension of Linear Regression where the relationship between
variables is modeled using a polynomial equation:
Sample Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
# Sample dataset
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2.1,2.9, 3.7, 4.5, 6.1])
# Linear Regression
linear_model = LinearRegression()
linear_model.fit(X, y)
y_linear_pred = linear_model.predict(X)
# Plot results
plt.scatter(X, y, color='blue', label='Original Data')
plt.plot(X, y_linear_pred, color='red', label='Linear Regression')
plt.plot(X, y_poly_pred, color='green', linestyle='dashed', label='Polynomial Regression
(degree=2)')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.title("Linear vs Polynomial Regression")
plt.show()
# Print coefficients
print("Linear Regression Coefficients:", linear_model.coef_)
print("Linear Regression Intercept:", linear_model.intercept_)
print("Polynomial Regression Coefficients:", poly_model.coef_)
print("Polynomial Regression Intercept:", poly_model.intercept_)
Lab Program-7
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error, r2_score
print(f"{title}")
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R^2 Score:", r2_score(y_test, y_pred))
def linear_regression():
data = fetch_california_housing(as_frame=True)
X, y = data.data[["AveRooms"]], data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression().fit(X_train, y_train)
plot_results(X_test, y_test, model.predict(X_test), "AveRooms", "Median Home Value
($100K)", "Linear Regression - California Housing")
def polynomial_regression():
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-
mpg.data"
cols = ["mpg", "cyl", "disp", "hp", "wt", "acc", "yr", "origin"]
data = pd.read_csv(url, sep='\s+', names=cols, na_values="?").dropna()
X, y = data[["disp"]], data["mpg"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = make_pipeline(PolynomialFeatures(2), StandardScaler(),
LinearRegression()).fit(X_train, y_train)
plot_results(X_test, y_test, model.predict(X_test), "Displacement", "MPG", "Polynomial
Regression - Auto MPG")
if __name__ == "__main__":
linear_regression()
polynomial_regression()