ML | sklearn.linear_model.LinearRegression () in Python



Parameters:

fit_intercept: [boolean, Default is True] Whether to calculate intercept for the model.
normalize: [boolean, Default is False] Normalization before regression.
copy_X: [boolean, Default is True] If true, make a copy of X else overwritten .
n_jobs: [int, Default is 1] If -1 all CPU`s are used. This will speedup the working for large datasets to process.

This dataset shows R&D costs, administration and marketing costs of 50 companies, and profits. The goal is to prepare an ML model that can predict the value of a company`s profit when given values ​​for R&D costs, administration costs, and marketing costs.

To load the dataset, click here .

Code: using linear regression to predict company profit

# Importing Libraries

import numpy as np

import pandas as pd

 
# Import dataset < / code>

dataset = pd.read_csv ( ` https://media.python.engineering/wp-content/uploads/50_Startups.csv ` )

 

print ( "Dataset.head ()" , dataset.head ())

 
# Input Values ​​

x = dataset.iloc [:,: - 1 ]. values ​​

print ( " First 10 Input Values: " , x [ 0 : 10 ,:])

 

print ( "Dataset Info: " )

print (dataset .info ())

# Input Values ​​

x = dataset.iloc [:,: - 1 ]. values ​​

print ( " First 10 Input Values: " , x [ 0 : 10 ,:])

 

 
# Output values ​​

y = dataset.iloc [:, 3 ]. Values 

y1 = y

y1 = y1.reshape ( - 1 < code class = "plain">, 1 )

print ( " First 10 Output true value: " , y1 [ 0 : 10 ,:])

# Separate input and output for training and data validation
# Training: testing = 80:20

from sklearn.cross_validation import train_test_split

xtrain , xtest, ytrain, ytest = train_test_split (x, y, test_size = 0.2

  random_state = 0 )

 
# Function scaling
# Polylinear regression takes care of object scaling
# So we don`t have to do it manually

 

 
# Fitting the multilinear regression model to the learning model

from sklearn.linear_model import LinearRegression

 

regressor = LinearRegression ()

regressor.fit (xtrain, ytrain)

 
# predicting test case results

y_pred = regressor.predict (xtest)

  

y_pred1 = y_pred

y_pred1 = y_pred1.reshape ( - 1 , 1 )

 

print ( " RESULT OF LIN EAR REGRESSION PREDICTION: " )

print ( "First 10 Predicted value:" , y_pred1 [ 0 : 10 ,:])