Every well-constructed tower needs a solid design, and building a high-performance regression model is no different! Once the foundation (mathematics) of Linear Regression is established, we leverage Python and its powerful scikit-learn library for the implementation.
You can break down the steps to designing a Linear Regression model as follows:
- Start by importing the necessary libraries and classes.
- Load the dataset and isolate the features (independent variables) and target variables (dependent variables).
- Split the data into training and testing parts: the training set for learning and the testing set for evaluating the model's performance.
Here, it's crucial to understand that while splitting the data, the
test_size argument represents the proportion of the dataset to include in the test set. The random_state argument ensures reproducibility by controlling the shuffling applied to the data before applying the split.
- Create the Linear Regression model using
scikit-learn's LinearRegression class.
- Finally, assess the model using various performance metrics.
Let's implement this in Python and predict some wine quality:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
import pandas as pd
# Load the wine dataset
import datasets
red_wine = datasets.load_dataset('codesignal/wine-quality', split='red')
red_wine = pd.DataFrame(red_wine)
# Select features and target variable
features = red_wine.drop('quality', axis=1)
target = red_wine['quality']
# Split the dataset into a training set and a testing set
features_train, features_test, target_train, target_test = train_test_split(features, target, test_size=0.2, random_state=42)
# Instantiate and fit the model
model = LinearRegression()
model.fit(features_train, target_train)
# Predict the test features
predictions = model.predict(features_test)
# Evaluate the model
mse = metrics.mean_squared_error(target_test, predictions)
print('Mean Squared Error:', mse) # Mean Squared Error: 0.39002514396395416
To visualize our prediction, let's draw a plot showing the Actual vs Predicted difference:
import matplotlib.pyplot as plt
# Plot target vs prediction
plt.scatter(target_test, predictions, color='blue')
# Plot the ideal prediction line (with zero error)
plt.plot([target_test.min(), target_test.max()], [target_test.min(), target_test.max()], 'k--', lw=2)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.title('Actual vs Predicted')
plt.show()
