5 Types Regression in 45 Lines of Code
5 Types Regression in 45 Lines of Code
5 Types Regression in 45 Lines of Code
1 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
2 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
3 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
4 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = dataset.iloc[:,:-1].values
#Gender column
ct = ColumnTransformer([("Gender", OneHotEncoder(), [0])],
remainder = 'passthrough')
X = ct.fit_transform(X)
5 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
# Split the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
6 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
7 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
df
8 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
df_1 = df.head(25)
df_1.plot(kind=’bar’,figsize=(16,10))
plt.grid(which=’major’, linestyle=’-’, linewidth=’0.5',
color=’green’)
plt.grid(which=’minor’, linestyle=’:’, linewidth=’0.5',
color=’black’)
plt.show()
9 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
#evaluation Metrics
from sklearn import metrics
print('Mean Absolute Error:',
metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:',
metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
10 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
11 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = dataset.iloc[:,:-1].values
#All columns except the last column (by defining the upper
bound)
y = dataset.iloc[:, 8].values
#Gender column
ct = ColumnTransformer([("Gender", OneHotEncoder(), [0])],
remainder = 'passthrough')
X = ct.fit_transform(X)
#Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size = 0.2, random_state = 0)
12 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
#evaluation Metrics
from sklearn import metrics
print('Mean Absolute Error:',
metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:',
metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
13 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
joblib.dump(regressor, 'regressor.pkl')
14 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
15 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
#Gender column
ct = ColumnTransformer([("Gender", OneHotEncoder(), [0])],
remainder = 'passthrough')
dataset = ct.fit_transform(dataset)
#anyway we wont use this column we will simply use 1
independent variable i.e. column ‘Length’ of abalone data
set and X our dependent variable i.e.‘number of rings’
from the last column.
X = dataset[:,10:]
y = dataset[:,3]
X_poly = poly_reg.fit_transform(X)
#will transform X into 2 more features(^2,#containing
features and Square root of features)
16 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
poly_reg.fit(X_poly, y)
linear_reg2 = LinearRegression()
linear_reg2.fit(X_poly, y)
17 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
linear_reg2.predict(poly_reg.fit_transform(X)), color =
'blue')
plt.title('Predicting the age of abalone from physical
measurements.')
plt.xlabel('Rings')
plt.ylabel('length')
plt.show()
18 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
19 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
#Polynomial Regression
#Gender column
ct = ColumnTransformer([("Gender", OneHotEncoder(), [0])],
remainder = 'passthrough')
dataset = ct.fit_transform(dataset)
"""#anyway we wont use this column we will simply use 1
independent variable i.e. column ‘Length’ of abalone data
set and X our dependent variable i.e.‘number of rings’
from the last column.
X = dataset[:,10:]
y = dataset[:,3]
X_poly = poly_reg.fit_transform(X)
20 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X_poly
21 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
22 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
23 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
24 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
y = sc_y.fit_transform(y.reshape(-1,1))
25 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
26 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
y = sc_y.fit_transform(y.reshape(-1,1))
lin.fit(X, y)
27 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
plt.show()
28 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
29 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
30 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
y = sc_y.fit_transform(y.reshape(-1,1))
31 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
32 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
# import export_graphviz
from sklearn.tree import export_graphviz
33 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
34 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
y = sc_y.fit_transform(y.reshape(-1,1))
lin.fit(X, y)
35 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
plt.ylabel('Pressure')
plt.show()
"""
The tree is finally exported and we can visualized using
http://www.webgraphviz.com/ by copying the data from the
‘tree.dot’ file."""
import pickle
#save the model to disk
filename = 'final_model.sav'
pickle.dump(dt_model, open(filename, 'wb'))
36 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
37 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
38 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
y = sc_y.fit_transform(y.reshape(-1,1))
39 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
dt_model = DecisionTreeRegressor(random_state = 0)
dt_model.fit(X, y)
40 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
41 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
X = sc_y.fit_transform(X.reshape(-1,1))
#X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1,1))
42 of 43 10/14/2021, 10:14 AM
5 Types Regression in 45 lines of code | by Bob Rupak Roy - II... https://bobrupakroy.medium.com/5-types-regression-in-45-lin...
plt.ylabel('Pressure')
plt.show()
43 of 43 10/14/2021, 10:14 AM