Fitting and Plotting a Linear Model Using Simulated Data in Python

Linear regression is a fundamental technique in data science for modeling relationships between variables. In this post, we will explore how to fit a linear regression model and visualize both the fitted line and reference guides using Matplotlib. In this example, I have generated simulated data for explanation. However, you can use real-world data for linear regression as well. The example uses the scikit-learn library in Python to demonstrate linear regression.

Here is the complete Python code; feel free to copy and test it on your own datasets.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# generate simulated data -- 100 datapoints
X = np.random.randn(100).reshape(-1, 1) # data
y = np.round(np.random.rand(100))  # labels

# fit linear regression
model = LinearRegression()
model.fit(X, y)

# compute slope and intercept for line y=mx+b
slope = model.coef_[0]
intercept = model.intercept_
print(f"slope of the line: {slope} and intercept: {intercept}")

# generate plot
plt.figure(figsize=(8,6))
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label=f'Fit: y = {slope:.4f}x + {intercept:.4f}')
plt.plot(X, X, color='green', linestyle='--', label='y = x')  # dotted y=x line
plt.title(f"Linear Regression on simulated data")
plt.legend()
plt.grid(True)
plt.show()

In the above code, replace the X and y variables with your real data and use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.