-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegressionAndRegularization.py
More file actions
154 lines (139 loc) · 4.68 KB
/
LinearRegressionAndRegularization.py
File metadata and controls
154 lines (139 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from warnings import filterwarnings
filterwarnings('ignore')
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression,Lasso,LassoCV,Ridge,RidgeCV,ElasticNet,ElasticNetCV
from sklearn.metrics import mean_absolute_error,r2_score
import pickle
# Create Dataframe and Read the dataset using Pandas
df = pd.read_csv('./data/Algerian_forest_fires_dataset_CLEANED.csv')
print(df.head())
print(df.columns)
df.drop(['day', 'month', 'year'],axis=1,inplace=True)
# print(df.head())
print(df['Classes'].value_counts())
df['Classes']=np.where(df['Classes'].str.contains("not fire"),0,1)
print(df['Classes'].value_counts())
print(df.tail())
# Independent and dependent features, Let's calculate Fire Weather Index (0 to 31.1)
X=df.drop('FWI',axis=1)
y=df["FWI"]
print(X.head())
print(y.head())
# Train test split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=42)
print(X_train.shape,X_test.shape)
# Feature selection based on correlation
print(X_train.corr())
# If independent features are highly correlated, we need to eliminate those features, both features will be conveying same info==>Multicollinearity
# check for multicollinearity
plt.figure(figsize=(12,10))
corr=X_train.corr()
sns.heatmap(corr,annot=True)
plt.show()
def correlation(dataset,threshold):
col_corr=set()
corr_matrix=dataset.corr()
for i in range(len(corr_matrix.columns)):
for j in range(i):
if abs(corr_matrix.iloc[i,j])>threshold:
colname=corr_matrix.columns[i]
col_corr.add(colname)
return col_corr
# threshold-domain expert
corr_feature=correlation(X_train,0.85)
print(corr_feature)
# Drop features when correlation is > threshold
X_train.drop(corr_feature,axis=1,inplace=True)
X_test.drop(corr_feature,axis=1,inplace=True)
print(X_train.shape,X_test.shape)
# Feature scaling or Standardization
scaler=StandardScaler()
Xtrains=scaler.fit_transform(X_train)
Xtests=scaler.transform(X_test)
print(Xtrains)
# Box plot to understand effect of standardization
plt.subplots(figsize=(15,5))
plt.subplot(121)
sns.boxplot(data=X_train)
plt.title('X_train before scaling')
plt.subplot(122)
sns.boxplot(data=Xtrains)
plt.title('X_train after scaling')
plt.show()
# Linear Regression model
linreg=LinearRegression()
linreg.fit(Xtrains,y_train)
ypred=linreg.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred)
# plt.show()
# Lasso Regression
lasso=Lasso()
lasso.fit(Xtrains,y_train)
ypred=lasso.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='red')
# plt.show()
# Cross Validation Lasso
lassocv=LassoCV(cv=5,random_state=0)
lassocv.fit(Xtrains,y_train)
ypred=lassocv.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='green')
# plt.show()
print('*****************************************')
# print(lassocv.alpha_,lassocv.alphas_,lassocv.mse_path_)
# Ridge Regression
ridge=Ridge()
ridge.fit(Xtrains,y_train)
ypred=ridge.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='yellow')
# plt.show()
# Ridge Cross validation
ridgecv=RidgeCV(cv=7)
ridgecv.fit(Xtrains,y_train)
ypred=ridgecv.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='pink')
plt.show()
print(ridgecv.get_params())
# ElasticNEt Regression
elast=ElasticNet()
elast.fit(Xtrains,y_train)
ypred=elast.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='yellow')
# plt.show()
# elasticnet Cross validation
elastcv=ElasticNetCV(cv=7)
elastcv.fit(Xtrains,y_train)
ypred=elastcv.predict(Xtests)
mae=mean_absolute_error(y_test,ypred)
score=r2_score(y_test,ypred)
print('mae:',mae,', r2 score:',score)
plt.scatter(y_test,ypred,c='pink')
plt.show()
# print(elastcv.get_params())
# Pickle the machine learning models, preprocessing model standardscaler
pickle.dump(scaler,open(r'./data/scaler.pkl','wb'))
pickle.dump(ridge,open(r'./data/ridge.pkl','wb'))