Multi-target Linear Regression
Explore the concept of multi-target linear regression where multiple output variables are predicted simultaneously from input features. Understand matrix formulations, the Frobenius norm, and how to implement and evaluate this model using Python and sklearn. Gain practical skills in handling multi-output data sets for real-world data science problems.
Multi-target data sets
It’s common in real applications to predict more than one target from given features. The
The linear system can then be modeled as:
where,
Objective
The goal is to find that minimizes the objective , where the subscript represents the Frobenius norm.
Frobenius norm
A Frobenius norm of any matrix is defined as the square root of the sum of squares of all its elements. Formally, for any matrix, , the Frobenius norm is .
Frobenius norm in Python
We can use np.linalg.norm to compute different types of norms of vectors and matrices. For matrix input, the default type is the Frobenius norm, and we can also set it explicitly as a parameter, ord='fro' as described in the code below:
An executable and extendable example of computing the Frobenius norm is given below.
Note: and are matrices, and so is . Ideally, we want to be a zero matrix!
Implementation
We’ve already worked with a real dataset.
In this lesson, we’ll predict both Y1 and Y2 based on the rest of the features.
Changes in the code
Single target
def getAy(data):
y = data.pop('Y2')
y = np.array(y)[:,np.newaxis]
A = np.array(data)
A = np.hstack((np.ones((A.shape[0],1)),A))
return A,y
Multiple targets
def getAY(data):
y1 = data.pop('Y1')
y1 = np.array(y1)[:,np.newaxis]
y2 = data.pop('Y2')
y2 = np.array(y2)[:,np.newaxis]
Y = np.hstack((y1,y2))
A = np.array(data)
A = np.hstack((np.ones((A.shape[0],1)),A))
return A,Y
Computing mean-squared error
For multiple targets, we define the mean-squared error as .
mse = (np.linalg.norm(test_Y_pred-test_Y)**2)/test_Y.shape[0]
The code below is a complete example of computing the mean-squared error.
Visualization
We can make separate visualizations for each target attribute. However, a more intuitive way is to visualize the error of the prediction vector. If is the prediction vector for the test point, then we can define a difference vector as . The squared length of the difference vector is precisely the squared error. In the case of an exact solution, the difference vector is zero. When the difference vector is depicted as a point in the plane, the good prediction will keep the point near to the origin. We can observe that most predictions are within the radius of (green circle).
Putting it all together
The complete executable code of the visualization is given below:
Multi-target regression using sklearn
We can also use sklearn to perform multi-target linear regression as shown below:
from sklearn.linear_model import LinearRegression as LR
from sklearn.multioutput import MultiOutputRegressor as MLR
model = MLR(LR()).fit(train_A, train_Y)
test_Y_pred = model.predict(test_A)
The code below utilizes sklearn for multi-target linear regression: