![]() Then we will fit it by using the fit() method. So for fitting the model, we will create a regressor_OLS object of new class OLS of statsmodels library. Next, as per the Backward Elimination process, we need to choose a significant level(0.5), and then need to fit the model with all possible predictors.Firstly we will create a new feature vector x_opt, which will only contain a set of independent features that are significantly affecting the dependent variable. Now, we are actually going to apply a backward elimination process.We can check it by clicking on the x dataset under the variable explorer option.Īs we can see in the above output image, the first column is added successfully, which corresponds to the constant term of the MLR equation. Output: By executing the above line of code, a new column will be added into our matrix of features, which will have all values equal to 1. Here we have used axis =1, as we wanted to add a column. Print('Test Score: ', regressor.score(x_test, y_test))įrom the above code, we got training and test set result as: Print('Train Score: ', regressor.score(x_train, y_train)) #Fitting the MLR model to the training set:įrom sklearn.linear_model import LinearRegression X_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.2, random_state=0) # Splitting the dataset into training and test set.įrom sklearn.model_selection import train_test_split X= onehotencoder.fit_transform(x).toarray() Onehotencoder= OneHotEncoder(categorical_features= ) #Extracting Independent and dependent Variableįrom sklearn.preprocessing import LabelEncoder, OneHotEncoder We will use the same model which we build in the previous chapter of MLR. Let's start to apply it to our MLR model. This process is used to optimize the performance of the MLR model as it will only include the most affecting feature and remove the least affecting feature. So, in order to optimize the performance of the model, we will use the Backward Elimination method. Hence it is good to have only the most significant features and keep our model simple to get the better result. Unnecessary features increase the complexity of the model. But that model is not optimal, as we have included all the independent variables and do not know which independent model is most affecting and which one is the least affecting for the prediction. In the previous chapter, we discussed and successfully created our Multiple Linear Regression model, where we took 4 independent variables (R&D spend, Administration spend, Marketing spend, and state (dummy variables)) and one dependent variable (Profit). Need for Backward Elimination: An optimal Multiple Linear Regression model: Step-5: Rebuild and fit the model with the remaining variables. Step-3: Choose the predictor which has the highest P-value, such that. Step-2: Fit the complete model with all possible predictors/independent variables. Step-1: Firstly, We need to select a significance level to stay in the model. ![]() Steps of Backward Eliminationīelow are some main steps which are used to apply backward elimination process: There are various ways to build a model in Machine Learning, which are:Ībove are the possible methods for building the model in Machine learning, but we will only use here the Backward Elimination process as it is the fastest method. It is used to remove those features that do not have a significant effect on the dependent variable or prediction of output. Next → ← prev What is Backward Elimination?īackward elimination is a feature selection technique while building a machine learning model. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |