<jupyter_start><jupyter_text>![APSSDC-LOGO](https://drive.google.com/uc?export=download&id=15AKQ6_-BixW4K6mL6RPphF5EKXqYF2zj)  1. Linear Regression with Single Variable<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Step1: Define Business Use CaseOur Use case is to predict the salary of a person based on Years of Experience<jupyter_code>df = pd.read_csv("https://raw.githubusercontent.com/AP-State-Skill-Development-Corporation/Datasets/master/Regression/Salary_Data.csv")<jupyter_output><empty_output><jupyter_text>Step2: Data Exploration<jupyter_code>df.head()
df.shape
df.info()
import matplotlib.pyplot as plt

plt.scatter(df['YearsExperience'], df['Salary'])
plt.show()
df
df.corr()<jupyter_output><empty_output><jupyter_text>Step3: Select AlgorithmBased on the data exploration we have found that YearsExperience is Positively Linearly Coreleated with the salary so we have selected the linear regression $$ Salary = M * YearExperience + C $$ Predict the output values based on the input values<jupyter_code>df['YearsExperience'].values
x = df['YearsExperience'].values.reshape(-1, 1)
y = df['Salary']
x.shape
from sklearn.linear_model import LinearRegression<jupyter_output><empty_output><jupyter_text>Step4: Build the model<jupyter_code>model = LinearRegression()
model.fit(x, y)
model.coef_
model.intercept_<jupyter_output><empty_output><jupyter_text>$$Y = M * X + C$$so by building the model we have calculated the coefficient/slope and intercept as in the above cells$$salary = 9449.962 * X + 25792.200$$<jupyter_code>model.predict([[11]])
model.predict([[12]])
import numpy as np
new = np.arange(1, 25).reshape(-1, 1)
plt.scatter(df['YearsExperience'], df['Salary'])
plt.plot(new, model.predict(new))
plt.show()<jupyter_output><empty_output><jupyter_text>Step6: Evaluate<jupyter_code>model.score(x, y)<jupyter_output><empty_output><jupyter_text>Linear Regression with Multiple Variables  Step1: Define Business Use CaseOur Use case is to predict the CO2Emissions of a person based on few features<jupyter_code>co2 = pd.read_csv('https://raw.githubusercontent.com/AP-State-Skill-Development-Corporation/Datasets/master/Regression/FuelConsumptionCo2.csv')<jupyter_output><empty_output><jupyter_text>Step2: Data Exploration<jupyter_code>co2.head()
co2.columns
co2.shape
co2['MAKE'].value_counts().shape
co2['MAKE'].value_counts()
co2['VEHICLECLASS'].value_counts()
import seaborn as sns

sns.pairplot(df)<jupyter_output><empty_output><jupyter_text>Step3: Select AlgorithmBased on the data exploration we have found that `CO2Emissions` is Positively Linearly Coreleated with the `FUELCONSUMPTION_CITY`,`FUELCONSUMPTION_HWY`, `FUELCONSUMPTION_COMB` so we have selected the linear regression with multiple variables<jupyter_code>co2.columns
x = co2[['FUELCONSUMPTION_CITY','FUELCONSUMPTION_HWY', 'FUELCONSUMPTION_COMB']]
y = co2['CO2EMISSIONS']<jupyter_output><empty_output><jupyter_text>Equation for Linear Regression with Multiple Variables$$y = m_1x_1 + m_2x_2 + m_3x_3 + ....... m_nx_n + c$$  splitting the entire data in to two parts 1. Training Part2. Testing Part<jupyter_code>from sklearn.model_selection import train_test_split

x_tr, x_tt, y_tr, y_tt = train_test_split(x, y, test_size = 0.3, random_state = 42)
train_test_split(x, y, test_size = 0.3, random_state = 42)
x_tr.shape, x_tt.shape<jupyter_output><empty_output><jupyter_text>Step4: Build the model<jupyter_code>from sklearn.linear_model import LinearRegression

model.fit(x_tr, y_tr)
y_pred = model.predict(x_tt)
x_tt.head(1)
y_tt.head(1)
y_pred[0]<jupyter_output><empty_output><jupyter_text>Step6: Evaluate<jupyter_code>model.score(x_tt, y_tt)
model.intercept_
model.coef_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11637931034482758, 'fraction_numerical': 0.02101293103448276, 'mean_word_length': 8.167901234567902, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '67336', 'license': 'CC0-1.0', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1369, 'n_words': 269, 'path': 'Day02_04Sept2020/Linear_Regression_Model.ipynb', 'repo_name': 'AP-State-Skill-Development-Corporation/Machine-Learning-Using-Python-EB4', 'size': '3712'}