Model Development in Python


Once we have processed the data, the next step is the development of the Model. The Model will then be trained and after that will start making the predictions for us for the given input features.

Model Development in Python
Model Development in Python

A Model can be thought of as a mathematical equation used to predict a value given, one or more other values. It relates one or more independent variables to dependent variables.

1) Linear Regression

Simple linear regression is a method to help us understand the relationship between two variables.

Lets say X is the predictor or the independent variable and Y is the target or the dependent variable.

First step in python:

from sklearn.linear_model import LinearRegression

We define the predictor variable and target variable.

X=df['laptop']
Y=df['price']

We create the linear regression object.

lm=LinearRegression()

We then fit the train X and Y data in it.

lm.fit(X,Y)

We then make the predictions.

YPredict=lm.predict(X)
YPredict

The prediction gives the price of laptops for which it was not trained for. We can compare the prediction price of the laptop with the actual price to see how good our model is.

2) Multiple Regression

It is same as Linear Regression except that there are multiple linear equation variables for making the predictions.

3) Polynomial Regression

Polynomial Regression is a particular case of the general linear regression model or multiple linear regression models. We get non-linear relationship by squaring or setting higher order terms of the predictor variables.

How to create a PolynomialFeatures object of degree 2?

from sklearn.preprocessing import PolynomialFeatures

pr=PolynomialFeatures(degree=2)

Z_pre=fit_transform(Z)

How to fit the data?

It is done as follows:

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler

For simplifying the data for processing we perform the following steps.

Input=[('scale',StandardScaler()), ('polynomial', PolynomialFeatures(include_bias=False)), ('model',LinearRegression())]

pipe=Pipeline(Input)

pipe.fit(Z,y)

ypipe=pipe.predict(Z)
ypipe

Here we have developed the model and got the predicted output. But are the predicted outputs correct. To do this we evaluate our model to check its accuracy. We shall see the model evaluation in the next blog article.

Comments