Linear Regression Example in sklearn
Linear regression is a linear approach to modeling the relationship between a scalar dependent variable and one or more explanatory variables.
In this example, we will use the popular Python library sklearn to perform linear regression on a sample dataset.
Sample Dataset
Let’s start by creating a sample dataset. We will use the following Python code to generate some random data:
import numpy as np import matplotlib.pyplot as plt # Generate random data np.random.seed(0) X = np.random.rand(100, 1) y = 2 + 3 * X + np.random.randn(100, 1)
Linear Regression
Next, we will use the sklearn library to perform linear regression on our sample dataset:
from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # Create a linear regression model model = LinearRegression() # Fit the model to the data model.fit(X, y) # Make predictions y_pred = model.predict(X) # Calculate mean squared error mse = mean_squared_error(y, y_pred) print('Mean Squared Error:', mse)
Visualizing the Results
Finally, we can visualize the results of our linear regression model using a scatter plot:
# Plot the data plt.scatter(X, y, color='blue') # Plot the regression line plt.plot(X, y_pred, color='red', linewidth=3) plt.show()
Conclusion
In this example, we demonstrated how to perform linear regression using the sklearn library in Python. We created a sample dataset, trained a linear regression model, and visualized the results. Linear regression is a powerful tool for modeling and predicting relationships between variables, and sklearn makes it easy to use in your Python projects.