Python coding for Regression :

Step by step procedures of linear Regression in python:

  • The linear regression is introduced to predict Loan Amount from Applicants Income.
  • import pandas as pd
  • import numpy as np
  • from scipy.stats import mode
  • import matplotlib.pyplot as plt
  • train=pd.read_csv("C:\\Users\\abc\\Desktop\\Dataset\\train.csv")
  • test= pd.read_csv("C:\\Users\\abc\\Desktop\\Dataset\\test.csv")
  • x_test= np.array(test.la)
  • print(train.head())
  • print(train.shape)

  • Linear Regression:

  • from sklearn import linear_model
  • regr=linear_model.LinearRegression()
  • la=np.array(traintest.LoanAmount)
  • ai=np.array(traintest.ApplicantIncome)
  • we have to reshape it as we have converted it to numpy array
  • la=la.reshape((592,1))
  • ai=ai.reshape((592,1))
  • where 592 is the number of rows
  • linear_mod = regr.fit(la,ai)
  • R-square = regr.score(la,ai)
  • Intercept = regr.intercept_
  • Coeff = regr.coef_
  • Prediction = regr.predict(x_test)