Simple Linear Regression in Python with Scikit-Learn: DEMO
In this demo, we will explore how to perform simple linear regression in Python using the Scikit-Learn library. Simple linear regression is a method to model the relationship between two variables by fitting a linear equation to observed data. It is the simplest form of regression analysis and is commonly used for prediction and forecasting.
Let’s begin by importing the necessary libraries:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
Next, we will generate some sample data for our demonstration:
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 3 + 4 * X + np.random.randn(100, 1)
Now, let’s fit a linear regression model to our data:
model = LinearRegression()
model.fit(X, y)
We can now make predictions using our model:
y_pred = model.predict(X)
Finally, let’s visualize our results:
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Simple Linear Regression Demo')
plt.show()
That’s it! We have successfully performed simple linear regression in Python using Scikit-Learn. You can further explore this topic by experimenting with different datasets and model parameters.