Jovian
⭐️
Sign In

Multiple_Linear_Regression model creation

In [20]:
import pandas as pd
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score # to check how good is our model, <1 is best>
from sklearn.model_selection import train_test_split

Create some dummy data for regression test

In [42]:
reg = make_regression(n_samples=400,n_features=5,n_targets=1)
In [43]:
data = reg[0]
target = reg[1]

Make data frame

In [44]:
X = pd.DataFrame(data,columns=["feature_"+str(var) for var in range(1,6)])
X["target"] = target
X.head(5)
Out[44]:

Train and Test data

In [45]:
X_train,X_test,y_train,y_test = train_test_split(X.iloc[:,:-1],X.iloc[:,-1],test_size=.2,random_state=4)

Create on object of Linear Regression

In [46]:
lr = LinearRegression()
In [47]:
lr.fit(X_train,y_train)
Out[47]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
In [48]:
y_pred = lr.predict(X_test)
y_pred
Out[48]:
array([-171.86059352,  140.90169435,   36.45074127,   19.71965369,
       -144.2779443 ,  -53.92538542,  -42.51806132,  -76.4065584 ,
         63.61691734, -106.08330965,   76.01105756,   49.15833418,
          3.63914538,  128.00114914,  126.7780061 , -238.5464942 ,
         66.88542605,  -90.68423293, -181.53259071,   35.24973775,
        -28.77620587,  145.56021669,  -72.20063856,  194.21062673,
        -29.23060382,  108.0202948 , -135.7209067 , -201.48755883,
         27.22904489,  128.12493882,   64.7347561 ,  -91.80545969,
        147.50714956,  130.480821  ,   10.91280811,   34.56072507,
        -25.9166441 ,    5.38132705,   49.98135164,  229.25997859,
       -177.02433165,   28.42123897,   58.84374305,   55.59826349,
        -15.18746957, -125.16031614,   12.6457968 ,  -82.73985562,
       -169.89707889,    3.61531563,  149.83688191,   27.18841292,
         50.87163419,   59.25883565,  194.1558644 ,  132.27168613,
        -23.13836849,  181.07943906, -436.12900179, -147.89652937,
         80.20289538, -297.24048635,  270.34636677,   65.46781042,
        -24.55048131,  281.76366182, -181.38759079,   62.97245975,
         69.74020205,  108.68503657, -119.0025759 ,   37.60025414,
        -97.32185892,    7.88298892,  -43.78882536,  -53.53598119,
        -27.36284506, -228.79974604,   57.50125633, -165.9138434 ])

Evaluate the model using r2_score

R^2 (coefficient of determination )

In [50]:
r2_score(y_test,y_pred)
Out[50]:
1.0
In [ ]:
 
In [ ]:
 
In [ ]: