<jupyter_start><jupyter_text>Model and Cost Function : Linear Regression  Model Representation **x** : input / feature variables  **y** : output target variables  $(x^{(i)},y^{(i)})$ : Training exampleList of $(x^{(i)},y^{(i)})$ :Training set. a list of **m** training examples  **(i)** : index into training set **GOAL :** Given a training set, learn a function $h : x \rightarrow y$, so that **h(x)** is a "good" predictor of the corresponding value of "y"  **Regression :** Target variable is continuous  **Classification :** Target variable is discrete  Linear Regression Function  $ h_\theta(x) = \theta_0 + \theta_1x $ with :  $\theta_0 = intercept $  $\theta_1 = slope$   (gradient) **Ex 1 : Pandas modelisation of Fuel Consumption**<jupyter_code>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>Linear Regression Model<jupyter_code>from sklearn import linear_model

lr = linear_model.LinearRegression()

x = np.asanyarray(df[['ENGINESIZE']])
y = np.asanyarray(df[['CO2EMISSIONS']])

lr.fit(x,y)<jupyter_output><empty_output><jupyter_text>Slope<jupyter_code>lr.coef_<jupyter_output><empty_output><jupyter_text>Intercept<jupyter_code>lr.intercept_<jupyter_output><empty_output><jupyter_text>Plot the output<jupyter_code>plt.scatter(df.ENGINESIZE,df.CO2EMISSIONS, color='blue')
plt.plot(x,lr.coef_[0][0]*x + lr.intercept_[0],'-r')

plt.xlabel("Engine size")
plt.ylabel("Emission")<jupyter_output><empty_output><jupyter_text>**Ex 2 : Auto.csv** Predict he miles per gallon **"mpg"** variable for several independent variables<jupyter_code>import seaborn as sns
df = pd.read_csv("Auto.csv")

# take a look at the dataset
df.head()
df.shape
X = df['weight']
y = df['mpg']
sns.scatterplot(X,y)<jupyter_output><empty_output><jupyter_text>Reshaping the data<jupyter_code>X = np.asanyarray(df[['weight']])
y = np.asanyarray(df[['mpg']])<jupyter_output><empty_output><jupyter_text>Divide the variables into testing and training sets<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.33,random_state=42)<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>lr.fit(X_train, y_train)
plt.scatter(df['weight'],df['mpg'], color='blue')
plt.plot(X_train,lr.coef_[0][0]*X_train + lr.intercept_[0],'-r')

plt.xlabel("Weight")
plt.ylabel("MPG")<jupyter_output><empty_output><jupyter_text>Make prediction from the training<jupyter_code>pred = lr.predict(X_test)
pred[:5]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15001870557426114, 'fraction_numerical': 0.0074822297044519264, 'mean_word_length': 7.913333333333333, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '222018', 'license': 'MIT', 'n_tokens_mistral': 1020, 'n_tokens_neox': 985, 'n_words': 200, 'path': 'Andrew Ng - Coursera/Week 1/Linear Regression.ipynb', 'repo_name': 'chikoungoun/Machine-Learning', 'size': '2673'}