<jupyter_start><jupyter_text>Polynomial Regression  Importing Needed packages<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Reading the data in<jupyter_code>df = pd.read_csv("../data7raw/FuelConsumption.csv")

# take a look at the dataset
df.head()<jupyter_output><empty_output><jupyter_text>Selecting some features to use for regression.<jupyter_code>cdf = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
cdf.head(9)<jupyter_output><empty_output><jupyter_text>Ploting Emission values with respect to Engine size:<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>Polynomial regression $y = b + theta_1  x + theta_2 x^2$<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
train_x = np.asanyarray(train[['ENGINESIZE']])
train_y = np.asanyarray(train[['CO2EMISSIONS']])

test_x = np.asanyarray(test[['ENGINESIZE']])
test_y = np.asanyarray(test[['CO2EMISSIONS']])


poly = PolynomialFeatures(degree=2)
train_x_poly = poly.fit_transform(train_x)
train_x_poly
clf = linear_model.LinearRegression()
train_y_ = clf.fit(train_x_poly, train_y)
# The coefficients
print ('Coefficients: ', clf.coef_)
print ('Intercept: ',clf.intercept_)
plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')
XX = np.arange(0.0, 10.0, 0.1)
yy = clf.intercept_[0]+ clf.coef_[0][1]*XX+ clf.coef_[0][2]*np.power(XX, 2)
plt.plot(XX, yy, '-r' )
plt.xlabel("Engine size")
plt.ylabel("Emission")<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>from sklearn.metrics import r2_score

test_x_poly = poly.fit_transform(test_x)
test_y_ = clf.predict(test_x_poly)

print("Mean absolute error: %.2f" % np.mean(np.absolute(test_y_ - test_y)))
print("Residual sum of squares (MSE): %.2f" % np.mean((test_y_ - test_y) ** 2))
print("R2-score: %.2f" % r2_score(test_y_ , test_y) )<jupyter_output>Mean absolute error: 22.80
Residual sum of squares (MSE): 855.66
R2-score: 0.72<jupyter_text>Using cubic polynomial regression Polynomial regression<jupyter_code>poly = PolynomialFeatures(degree=3)
train_x_poly = poly.fit_transform(train_x)
train_x_poly
clf = linear_model.LinearRegression()
train_y_ = clf.fit(train_x_poly, train_y)
# The coefficients
print ('Coefficients: ', clf.coef_)
print ('Intercept: ',clf.intercept_)
plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')
XX = np.arange(0.0, 10.0, 0.1)
yy = clf.intercept_[0]+ clf.coef_[0][1]*XX+ clf.coef_[0][2]*np.power(XX, 2)
plt.plot(XX, yy, '-r' )
plt.xlabel("Engine size")
plt.ylabel("Emission")<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>from sklearn.metrics import r2_score

test_x_poly = poly.fit_transform(test_x)
test_y_ = clf.predict(test_x_poly)

print("Mean absolute error: %.2f" % np.mean(np.absolute(test_y_ - test_y)))
print("Residual sum of squares (MSE): %.2f" % np.mean((test_y_ - test_y) ** 2))
print("R2-score: %.2f" % r2_score(test_y_ , test_y) )<jupyter_output>Mean absolute error: 22.69
Residual sum of squares (MSE): 846.21
R2-score: 0.73
{'fraction_non_alphanumeric': 0.14725726019360516, 'fraction_numerical': 0.02376063361689645, 'mean_word_length': 7.589420654911839, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '207283', 'license': 'MIT', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1381, 'n_words': 238, 'path': 'notebooks/Module2-Regression/2.3.ML0101EN-Polynomial-Regression-Co2-py-v1.ipynb', 'repo_name': 'jesussantana/IBM-Machine-Learning-with-Python', 'size': '3409'}