-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcrete-analysis-modeling.py
325 lines (262 loc) · 10.5 KB
/
concrete-analysis-modeling.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
#supress warnings
import warnings
warnings.filterwarnings("ignore")
#import data
concrete_data = pd.read_csv('Concrete_Data_Yeh.csv')
#look at formatting of entries
concrete_data.head()
#look at null count and dtype
concrete_data.info()
#look at distribution of data
concrete_data.describe()
#look at numerical data distribution
for i in concrete_data.columns:
plt.hist(concrete_data[i])
plt.xticks()
plt.xlabel(i)
plt.ylabel('counts')
plt.show()
#heat map to find extreme positive and negative correlations in numerical data
plt.figure(figsize=(16, 6))
sns.heatmap(concrete_data.corr(), annot=True)
plt.title('Correlation Heatmap', fontdict={'fontsize':12}, pad=12);
concrete_data['bins']=pd.qcut(concrete_data['csMPa'], q=4)
bins = pd.qcut(concrete_data['csMPa'], q=4)
#look at how target is distributed among variables
sns.pairplot(concrete_data.loc[:, (concrete_data.columns != 'csMPa')], hue='bins')
plt.legend()
plt.show()
sns.lmplot(x='cement', y='csMPa',data=concrete_data)
plt.show()
concrete_data = concrete_data.drop('bins', axis=1)
#copy of variables and target
X = concrete_data.copy()
y = X.pop('csMPa')
X_mi = X.copy()
#label encoding for categorical variables
for colname in X_mi.select_dtypes("object"):
X_mi[colname], _ = X_mi[colname].factorize()
#all discrete features have int dtypes
discrete_features = X_mi.dtypes == object
#some continuous variables also have int dtypes
discrete_features[X_mi.columns] = False
#use regression since the target variable is continuous
from sklearn.feature_selection import mutual_info_regression
#define a function to produce mutual information scores
def make_mi_scores(X_mi, y, discrete_features):
mi_scores = mutual_info_regression(X_mi, y, discrete_features=discrete_features)
mi_scores = pd.Series(mi_scores, name="MI Scores", index=X_mi.columns)
mi_scores = mi_scores.sort_values(ascending=False)
return mi_scores
#compute mutual information scores
mi_scores = make_mi_scores(X_mi, y, discrete_features)
mi_scores
#define a function to plot mutual information scores
def plot_mi_scores(scores):
scores = scores.sort_values(ascending=True)
width = np.arange(len(scores))
ticks = list(scores.index)
plt.barh(width, scores)
plt.yticks(width, ticks)
plt.title("Mutual Information Scores")
#plot the scores
plt.figure(dpi=100, figsize=(8, 5))
plot_mi_scores(mi_scores)
#plot selling_price against car_name
fig, ax = plt.subplots(figsize=(12,4))
sns.scatterplot(x=X_mi.age, y=y, ax=ax)
plt.show()
#select all data except CUST_ID
X_for_PCA = X.copy()
#standardize
X_for_PCA_scaled = (X_for_PCA - X_for_PCA.mean(axis=0)) / X_for_PCA.std(axis=0)
from sklearn.decomposition import PCA
#create principal components (2 axes based on elbow method below)
pca = PCA(len(X.columns))
X_pca = pca.fit_transform(X_for_PCA_scaled)
#convert to dataframe
component_names = [f"PC{i+1}" for i in range(X_pca.shape[1])]
X_pca = pd.DataFrame(X_pca, columns=component_names)
#plot data using principal components
sns.scatterplot(x=X_pca.loc[:,'PC1'],y=X_pca.loc[:,'PC2'], hue=bins)
plt.show()
#determine loadings
loadings = pd.DataFrame(
pca.components_.T, # transpose the matrix of loadings
columns=component_names, # so the columns are the principal components
index=X.columns, # and the rows are the original features
)
loadings
#determine % explained variance and use % cumulative variance for elbow method to determine number of PCs
def plot_variance(pca, width=8, dpi=100):
# Create figure
fig, axs = plt.subplots(1, 2)
n = pca.n_components_
grid = np.arange(1, n + 1)
# Explained variance
evr = pca.explained_variance_ratio_
axs[0].bar(grid, evr)
axs[0].set(
xlabel="Component", title="% Explained Variance", ylim=(0.0, 1.0)
)
# Cumulative Variance
cv = np.cumsum(evr)
axs[1].plot(np.r_[0, grid], np.r_[0, cv], "o-")
axs[1].set(
xlabel="Component", title="% Cumulative Variance", ylim=(0.0, 1.0)
)
# Set up figure
fig.set(figwidth=8, dpi=100)
return axs
plot_variance(pca);
#generate OLS Regression Results
import statsmodels.api as sm
X_sm = sm.add_constant(X)
model = sm.OLS(y,X_sm)
model.fit().summary()
#import ML preprocessing packages
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
feature_names = X.columns
# train/test split with stratify making sure classes are evenlly represented across splits
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, random_state=1)
#numerical pipeline
scaler=MinMaxScaler()
#apply scaler to numerical data
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
#####pickle
import pickle
outfile = open('scaler.pkl', 'wb')
pickle.dump(scaler,outfile)
outfile.close()
#import ML packages
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import cross_val_score
from numpy import mean
from numpy import std
#LinearRegression mean cross-validation
lm = LinearRegression()
lm.fit(X_train, y_train)
cv = cross_val_score(lm,X_train,y_train,scoring='neg_mean_absolute_error',cv=5)
print('LinearRegression')
print(mean(cv), '+/-', std(cv))
#RandomForestRegressor mean cross-validation
rf = RandomForestRegressor(random_state = 1)
cv = cross_val_score(rf,X_train,y_train,scoring='neg_mean_absolute_error',cv=5)
print('RandomForestRegressor')
print(mean(cv), '+/-', std(cv))
#GradientBoostingRegressor mean cross-validation
gbr = GradientBoostingRegressor(random_state = 1)
cv = cross_val_score(gbr,X_train,y_train,scoring='neg_mean_absolute_error',cv=5)
print('GradientBoostingRegressor')
print(mean(cv), '+/-', std(cv))
#XGBoost mean cross-validation
xgb = XGBRegressor(random_state = 1)
cv = cross_val_score(xgb,X_train,y_train,scoring='neg_mean_absolute_error',cv=5)
print('XGBoost')
print(mean(cv), '+/-', std(cv))
#ml algorithm tuner
from sklearn.model_selection import GridSearchCV
#performance reporting function
def clf_performance(regressor, model_name):
print(model_name)
print('Best Score: {} +/- {}'.format(str(regressor.best_score_),str(regressor.cv_results_['std_test_score'][regressor.best_index_])))
print('Best Parameters: ' + str(regressor.best_params_))
#LinearRegression GridSearchCV
lm = LinearRegression()
param_grid = {
'fit_intercept':[True,False],
'normalize':[True,False],
'copy_X':[True, False]
}
clf_lm = GridSearchCV(lm, param_grid = param_grid, cv = 5, scoring='neg_mean_absolute_error', n_jobs = -1)
best_clf_lm = clf_lm.fit(X_train,y_train)
clf_performance(best_clf_lm,'LinearRegressor')
#RanddomForestRegressor GridSearchCV
rf = RandomForestRegressor(random_state = 1)
param_grid = {
'n_estimators': np.arange(160,200,2) ,
'bootstrap': [True,False],
# 'max_depth': [20,30,40],
# 'max_features': ['auto','sqrt','log2'],
# 'min_samples_leaf': [2],
# 'min_samples_split': [6,8,10]
}
clf_rf = GridSearchCV(rf, param_grid = param_grid, cv = 5, scoring='neg_mean_absolute_error', n_jobs = -1)
best_clf_rf = clf_rf.fit(X_train,y_train)
clf_performance(best_clf_rf,'RandomForestRegressor')
#GradientBoostingRegressor GridSearchCV
gbr = GradientBoostingRegressor(random_state = 1)
param_grid = {
'n_estimators': [160],
'max_depth': [4],
'max_features': ['auto'],
'learning_rate': np.arange(.1,1,.1),
'alpha': [0.0001],
'min_samples_leaf': [2],
'min_samples_split': np.arange(2,6,1)
}
clf_gbr = GridSearchCV(gbr, param_grid = param_grid, cv = 5, scoring='neg_mean_absolute_error', n_jobs = -1)
best_clf_gbr = clf_gbr.fit(X_train,y_train)
clf_performance(best_clf_gbr,'GradientBoostingRegressor')
#XGBoost GridSearchCV
xgb = XGBRegressor(random_state = 1)
param_grid = {
# 'nthread':[4],
# 'objective':['reg:linear'],
# 'learning_rate': [0.3],
'max_depth': [4],
# 'min_child_weight': [1],
# 'subsample': [1],
# 'colsample_bytree': np.arange(0.5,1,0.1),
'n_estimators': [500]
}
clf_xgb = GridSearchCV(xgb, param_grid = param_grid, cv = 5, scoring='neg_mean_absolute_error', n_jobs = -1)
best_clf_xgb = clf_xgb.fit(X_train,y_train)
clf_performance(best_clf_xgb,'XGBoost')
#import metrics packages
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
#RandomForestRegressor metrics
gbr = GradientBoostingRegressor(alpha = 0.0001,
learning_rate= 0.2,
max_depth= 4,
max_features='auto',
min_samples_leaf= 2,
min_samples_split= 2,
n_estimators= 160,
random_state = 1)
gbr.fit(X_train,y_train)
tpred_gbr=gbr.predict(X_test)
print('GradientBoostingRegressor')
print('MSE: {}'.format(mean_squared_error(y_test,tpred_gbr)))
print('RMSE: {}'.format(np.sqrt(mean_squared_error(y_test,tpred_gbr))))
print('MAE: {}'.format(mean_absolute_error(y_test,tpred_gbr)))
print('R-squared: {}'.format(r2_score(y_test,tpred_gbr)))
#XGBoost metrics
xgb = XGBRegressor(max_depth=4,
n_estimators=500,
random_state = 1)
xgb.fit(X_train,y_train)
tpred_xgb=xgb.predict(X_test)
print('XGBoost')
print('MSE: {}'.format(mean_squared_error(y_test,tpred_xgb)))
print('RMSE: {}'.format(np.sqrt(mean_squared_error(y_test,tpred_xgb))))
print('MAE: {}'.format(mean_absolute_error(y_test,tpred_xgb)))
print('R-squared: {}'.format(r2_score(y_test,tpred_xgb)))
#####pickle
outfile = open('xgboost_model.pkl', 'wb')
pickle.dump(gbr,outfile)
outfile.close()
import eli5
from eli5.sklearn import PermutationImportance
#permutation importance from xgboost
perm = PermutationImportance(xgb).fit(X_test, y_test)
eli5.show_weights(perm, feature_names = list(feature_names), top=len(feature_names))