Linear Regression is a fundamental supervised learning algorithm used in machine learning for predicting a continuous output variable (dependent variable) based on one or more input variables (independent variables). The goal of linear regression is to establish a linear relationship between the dependent and independent variables, allowing for predictions on new data points.
Types of Linear Regression
- Simple Linear Regression: Uses one independent variable to predict the dependent variable.
- Multiple Linear Regression: Uses multiple independent variables to predict the dependent variable.
Code in Python :
# Import necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Load the Iris dataset
iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = X[:, 2] # Target: Petal length
# Use the remaining features (sepal length, sepal width, petal width) to predict petal length
X = X[:, [0, 1, 3]]
# Split the dataset 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)
# Create a Linear Regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the test data
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
# Output results
print(“Mean Squared Error (MSE):”, mse)
print(“R-squared (R2):”, r2)
# Display model coefficients and intercept
print(“Coefficients:”, model.coef_)
print(“Intercept:”, model.intercept_)