Multi-Output Regression using Sklearn
Hey fellow learners, today in this tutorial we will learn about another exciting topic. First of all, I am sure you might know what sklearn library (scikit-learn) in Python programming language is, in short Sklearn is a library that provides us with tools for machine learning modeling such as classifications, regressions, clustering, and dimensionality reduction in python. We will be understanding how scikit-learn performs multi-output Regression using different types of Regression.
Multi-output Regression as the name suggests is a regression in which we have a query point and we have to predict more than one attribute, so naturally, we would be provided with data with more than one target attribute.
First, let’s prepare data using the
make_regression function in scikit-learn library.
from sklearn.datasets import make_regression X,Y=make_regression(n_samples=1000,n_features=5,n_targets=2) print(X.shape,Y.shape)
(1000, 5) (1000, 2)
As you can see now we have data with five features and two target attributes.
now we split the data into train and test using the
train_test_split function in sklearn library.
from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test=train_test_split(X,Y,train_size=0.8,random_state=1)
now let’s understand how to perform different algorithms on this type of data.
Let’s fit the training data into the linear regression model using
sklearn.linear_model and then predict a test query point from the testing data.
from sklearn.linear_model import LinearRegression lr=LinearRegression() lr.fit(x_train,y_train) print(lr.predict(x_test.reshape(1,-1)))
[[-84.29519854 4.708592 ]]
As you can see we get two target attributes prediction using Linear Regression.
Random Forest Regressor
Let’s fit the training data into the Random Forest regression model using
sklearn.ensemble and then predict a test query point from the testing data.
from sklearn.ensemble import RandomForestRegressor rf= RandomForestRegressor() rf.fit(x_train,y_train) print(rf.predict(x_test.reshape(1,-1)))
As you can see we get two target attributes prediction using Random Forest Regression.
Decision Tree Regression
let’s fit the training data into the Decision Tree regression model using
sklearn.tree and then predict a test query point from the testing data.
.from sklearn.tree import DecisionTreeRegressor dt=DecisionTreeRegressor() dt.fit(x_train,y_train) print(dt.predict(x_test.reshape(1,-1)))
As you can see we get two target attributes prediction using Decision Tree Regression.
This is how we can perform Multi-Output Regression using sklearn library for different types of algorithms.
Leave a Reply