<jupyter_start><jupyter_text><NAME>   GIS & Remmote Sensing Data Scientist     In this notebook, we learn how to use scikit-learn to implement Multiple linear regression. We download a dataset that is related to fuel consumption and Carbon dioxide emission of cars. Then, we split our data into training and test sets, create a model using training set, Evaluate your model using test set, and finally use model to predict unknown value<jupyter_code># Importing the packages
import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
df = pd.read_csv("FuelConsumption.csv")

# take a look at the dataset
df.head()<jupyter_output><empty_output><jupyter_text>Lets select some features that we want to use for regression.<jupyter_code>cdf = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_CITY','FUELCONSUMPTION_HWY','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
cdf.head(9)<jupyter_output><empty_output><jupyter_text>Lets plot Emission values with respect to Engine siz<jupyter_code>plt.scatter(cdf.ENGINESIZE, cdf.CO2EMISSIONS,  color='blue')
plt.xlabel("Engine size")
plt.ylabel("Emission")
plt.show()<jupyter_output><empty_output><jupyter_text>Creating train and test dataset<jupyter_code>msk = np.random.rand(len(df)) < 0.8
train = cdf[msk]
test = cdf[~msk]<jupyter_output><empty_output><jupyter_text>Train data distribution<jupyter_code>plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')
plt.xlabel("Engine size")
plt.ylabel("Emission")
plt.show()<jupyter_output><empty_output><jupyter_text>Multiple Regression Model In reality, there are multiple variables that predict the Co2emission. When more than one independent variable is present, the process is called multiple linear regression. For example, predicting co2emission using FUELCONSUMPTION_COMB, EngineSize and Cylinders of cars. The good thing here is that Multiple linear regression is the extension of simple linear regression model.<jupyter_code>from sklearn import linear_model
regr = linear_model.LinearRegression()
x = np.asanyarray(train[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB']])
y = np.asanyarray(train[['CO2EMISSIONS']])
regr.fit (x, y)
# The coefficients
print ('Coefficients: ', regr.coef_)<jupyter_output>Coefficients:  [[10.0290951   8.01866335  9.56960304]]<jupyter_text>As mentioned before, __Coefficient__ and __Intercept__ , are the parameters of the fit line. Given that it is a multiple linear regression, with 3 parameters, and knowing that the parameters are the intercept and coefficients of hyperplane, sklearn can estimate them from our data. Scikit-learn uses plain Ordinary Least Squares method to solve this problem.  Ordinary Least Squares (OLS)OLS is a method for estimating the unknown parameters in a linear regression model. OLS chooses the parameters of a linear function of a set of explanatory variables by minimizing the sum of the squares of the differences between the target dependent variable and those predicted by the linear function. In other words, it tries to minimizes the sum of squared errors (SSE) or mean squared error (MSE) between the target variable (y) and our predicted output ($\hat{y}$) over all samples in the dataset.OLS can find the best parameters using of the following methods:    - Solving the model parameters analytically using closed-form equations    - Using an optimization algorithm (Gradient Descent, Stochastic Gradient Descent, Newton’s Method, etc.)  Prediction<jupyter_code>y_hat= regr.predict(test[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB']])
x = np.asanyarray(test[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB']])
y = np.asanyarray(test[['CO2EMISSIONS']])
print("Residual sum of squares: %.2f"
      % np.mean((y_hat - y) ** 2))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(x, y))<jupyter_output>Residual sum of squares: 561.72
Variance score: 0.87
{'fraction_non_alphanumeric': 0.0868898543317148, 'fraction_numerical': 0.012777919754663941, 'mean_word_length': 6.426944971537002, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '535254', 'license': 'MIT', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1192, 'n_words': 418, 'path': '02-Multiple Linear Regression.ipynb', 'repo_name': 'Massamba-Bienvenu/Machine-learning-projects', 'size': '3930'}