-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.py
278 lines (202 loc) · 7.81 KB
/
project1.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
# -*- coding: utf-8 -*-
"""Copy of Copy 44 Untitled1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1UsAVBsFGTXnNG0vWps5m_3Cht6QQqZeG
# **[call all the basic libraries:]**
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder , OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
from pandas.plotting import scatter_matrix
from sklearn.impute import SimpleImputer
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
"""# **start reading the training and testing data sets:**"""
train_df=pd.read_csv('/content/drive/MyDrive/train.csv')
train_df.head()
train_df.info()
dim=pd.read_csv('/content/drive/MyDrive/train.csv')
dim.info()
test_df=pd.read_csv('/content/drive/MyDrive/test.csv')
test_df.head()
test_df.info()
mean_d = dim['price'].mean()
print("Mean Value of Diamonds: $", mean_d)
dim.describe()
dim.describe(include='object')
dim.nunique()
"""# 3. **visualize** the data"""
dim.hist(figsize=(18,10))
sns.pairplot(dim, y_vars='price')
sns.pairplot(dim)
dim["cut"].value_counts() / len(dim)
corr_matrix = dim.corr()
corr_matrix
corr_matrix['price'].sort_values(ascending=False)
plt.figure(figsize = (12,8))
corr_matrix['price'].sort_values(ascending = False).plot(kind = 'bar')
plt.figure(figsize = (16,5))
heato=sns.heatmap(corr_matrix ,cmap='BrBG' ,annot=True )
heato.set_title('Correlation Heatmap', fontdict={'fontsize':25})
dim.plot.scatter(x='carat', y='price' ,figsize=(10,5))
dim.plot.scatter(x='z', y='price' ,figsize=(10,5))
d.plot.scatter(x='z', y='price' ,figsize=(10,5))
input_cat_columns = dim.select_dtypes(include = ['object']).columns.to_list()
for col in input_cat_columns:
sns.catplot(x=col, y="price", kind="box", dodge=False, height = 5, aspect = 3,data=dim);
"""# **Removing the outliers:**
"""
Q1=dim['depth'].quantile(0.25)
Q3=dim['depth'].quantile(0.75)
IQR=Q3-Q1
idx=~((dim['depth']<(Q1 - 1.5*IQR)) | (dim['depth'] >(Q3 + 1.5*IQR)))
d1=dim[idx]
d1.info()
Q1x=dim['x'].quantile(0.25)
Q3x=dim['x'].quantile(0.75)
IQRx=Q3x-Q1x
idxx=(d1['x']>(Q1x - 1.5*IQRx)) & (d1['x'] <(Q3x + 1.5*IQRx))
dx=d1[idxx]
dx.info()
Q1y=dim['y'].quantile(0.25)
Q3y=dim['y'].quantile(0.75)
IQRy=Q3y-Q1y
idxy=(dx['x']>(Q1y - 1.5*IQRy)) & (dx['x'] <(Q3y + 1.5*IQRy))
dy=dx[idxy]
dy.info()
Q1z=dim['z'].quantile(0.25)
Q3z=dim['z'].quantile(0.75)
IQRz=Q3z-Q1z
idxz=(dy['z']>(Q1z - 1.5*IQRz)) & (dy['z'] <(Q3z + 1.5*IQRz))
dz=dy[idxz]
dz.describe()
# dz.info()
Q1ca=dim['carat'].quantile(0.25)
Q3ca=dim['carat'].quantile(0.75)
IQRca=Q3-Q1
idxca=(dz['carat']>(Q1ca - 1.5*IQRca)) & (dz['x'] <(Q3ca + 1.5*IQRca))
dca=dz[idx]
dca.info()
Q1ta=dim['table'].quantile(0.25)
Q3ta=dim['table'].quantile(0.75)
IQRta=Q3ta-Q1ta
idxta=(dca['table']>(Q1ta - 1.5*IQRta)) & (dca['x'] <(Q3ta + 1.5*IQRta))
d=dca[idxta]
d.info()
dix=dim.drop('price',axis=1)
diy=dim['price']
x_train, x_test , y_train , y_test = train_test_split(dix, diy, test_size=0.25 , random_state=42)
# def prepare_data(df):
# num_attribs=df.select_dtypes(include=[np.number]).columns.to_list()
# num_pipeline = Pipeline([('std_scaler', StandardScaler())])
# cat_attribs = ["color","clarity","cut"]
# full_pipeline = ColumnTransformer([
# ("num", num_pipeline, num_attribs),
# ("cat", OneHotEncoder(), cat_attribs),
# ])
# data_prepared = full_pipeline.fit_transform(df)
num_attribs=x_train.select_dtypes(include=[np.number]).columns.to_list()
num_pipeline = Pipeline([('std_scaler', StandardScaler())])
cat_attribs = ["color","clarity","cut"]
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), cat_attribs),
])
train_prepared = full_pipeline.fit_transform(x_train)
test_prepared= full_pipeline.fit_transform(x_test)
# full_pipeline = ColumnTransformer([
# ("num", num_pipeline, num_attribs),
# ("cat", OneHotEncoder(), cat_attribs),
# ])
# cat_cols= d.select_dtypes(include='object').columns.to_list()
# dim1=pd.get_dummies(d , columns=cat_cols , drop_first=True)
# x=dim1.drop('price', axis=1)
# y=dim1['price']
# x_train, x_test , y_train , y_test = train_test_split(x,y, test_size=0.3 , random_state=42)
"""# **start Selecting and Training some Models**
**1. LinearRegression model**
"""
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(train_prepared, y_train)
# some_data_prepared = full_pipeline.transform(some_data)
# print("Predictions:", lin_reg.predict(some_data_prepared))
# print("Labels:", list(some_labels))
dim1_predictions = lin_reg.predict(test_prepared)
lin_mse = mean_squared_error(y_test, dim1_predictions)
lin_rmse = np.sqrt(lin_mse)
lin_rmse
"""*** Using Cross-Validation***"""
lin_scores = cross_val_score(lin_reg, train_prepared, y_train, scoring="neg_mean_squared_error", cv=10)
lin_rmse_scores = np.sqrt(-lin_scores)
print("Scores: ", lin_rmse_scores)
print("Mean: ", lin_rmse_scores.mean())
print("Standard Deviation: ", lin_rmse_scores.std())
"""**2. Decision Tree Regressor model**
"""
tree_reg = DecisionTreeRegressor()
tree_reg.fit(train_prepared, y_train)
dimtree_predictions = tree_reg.predict(test_prepared)
tree_mse = mean_squared_error(y_test, dimtree_predictions)
tree_rmse = np.sqrt(tree_mse)
tree_rmse
"""*** Using Cross-Validation***"""
scores = cross_val_score(tree_reg, train_prepared, y_train, scoring="neg_mean_squared_error", cv=10)
tree_rmse_scores = np.sqrt(-scores)
print("Scores: ", tree_rmse_scores)
print("Mean: ", tree_rmse_scores.mean())
print("Standard Deviation: ", tree_rmse_scores.std())
"""**3. RandomForest Model:**"""
rand_for= RandomForestRegressor()
rand_for.fit(train_prepared, y_train)
ranfor_predictions = tree_reg.predict(test_prepared)
tree_mse = mean_squared_error(y_test, ranfor_predictions)
tree_rmse = np.sqrt(tree_mse)
tree_rmse
"""## Evaluation Models Using Cross-Validation
"""
forest_scores = cross_val_score(rand_for ,train_prepared, y_train,scoring = "neg_mean_squared_error", cv = 10)
forest_rmse_scores = np.sqrt(-forest_scores)
print("Scores: ", forest_rmse_scores)
print("Mean: ", forest_rmse_scores.mean())
print("Standard Deviation: ", forest_rmse_scores.std())
final_test=full_pipeline.fit_transform(test_df)
predictions=pd.Series(rand_for.predict(test_prepared))
pred=pd.DataFrame({'Id': test_df['Id'], 'price': predictions})
pred
"""# **Final Tune using Grid Search:**"""
from sklearn.model_selection import GridSearchCV
param_grid = [
{'n_estimators': [3,10,30], 'max_features':[2,4,6,8]},
{'bootstrap':[False], 'max_features':[2,3,4],'n_estimators':[3,10]}
]
fore_reg = RandomForestRegressor(random_state = 42)
grid_search = GridSearchCV(fore_reg, param_grid,cv = 5, scoring = 'neg_mean_squared_error',return_train_score = True)
grid_search.fit(train_prepared, y_train)
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params)
feature_importances = grid_search.best_estimator_.feature_importances_
feature_importances
final_model = grid_search.best_estimator_
final_model
final_predictions = final_model.predict(test_prepared)
final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)
final_rmse
final_predictions=pd.Series(rand_for.predict(test_prepared))
final_pred=pd.DataFrame({'Id': test_df['Id'], 'price': final_predictions})