Linear Regression Model Implementation

Linear Regression Model Implementation

By maaz.waheed December 7, 2025 at 04:29:53 AM 2 min read 38 views
in Regrssion
Tags: #machine-learning #linear-regression #scikit-learn #data-science #r2-score #tutorial #python

Linear Regression Model Implementation

Author: Maaz Waheed

Overview

This implementation demonstrates a complete workflow for building and evaluating a Linear Regression model using scikit-learn.

Code Implementation

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Split data into training and test sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

# Fit Linear Regression model
lr = LinearRegression()
lr.fit(x_train, y_train)

# Print model score
print("Linear Regression R^2 score:", lr.score(x_test, y_test))

# Make prediction on new data
new_data = [[42491, 3, 2, 1500, 2000, 1, 0, 0, 3, 7, 1500, 0, 2000, 0, 122004, 52.9, -114.5, 1500, 2000, 2, 10]]
predicted_price = lr.predict(new_data)
print("Predicted Price:", predicted_price[0][0])

Key Components

1. Data Splitting

  • Uses train_test_split() with 80/20 train-test ratio
  • random_state=42 ensures reproducible results
  • Features: x, Target: y

2. Model Training

  • Initializes LinearRegression() with default parameters
  • Fits model on training data: x_train, y_train

3. Model Evaluation

  • R² Score: Measures proportion of variance explained by the model
  • Evaluated on test set to assess generalization performance

4. Prediction

  • Demonstrates prediction on new data with 21 features
  • Output format: Single prediction value

Parameters

  • test_size: 0.2 (20% testing, 80% training)
  • random_state: 42 (ensures consistent splits)
  • LinearRegression: All default parameters

Example Output

Linear Regression R^2 score: 0.7019654586300859
Predicted Price: 325323.83949833363

Interpretation

  • R² Score (0.702): Model explains approximately 70.2% of the variance in the target variable
  • Predicted Price: $325,323.84 for the given input features

Comments

Note: Comments are currently restricted to MBK Tech authorized members only. Public commenting is not available at this time.

Please login to leave a comment.

No comments yet.