-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIAMONDS PRICE PREDICTION.py
291 lines (150 loc) · 4.83 KB
/
DIAMONDS PRICE PREDICTION.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
#!/usr/bin/env python
# coding: utf-8
# IMPORTING LIBRARIES
# In[1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import accuracy_score, f1_score, log_loss
from sklearn.model_selection import train_test_split, KFold, cross_val_score
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import classification_report , confusion_matrix ,accuracy_score
get_ipython().run_line_magic('matplotlib', 'inline')
# # IMPORTING DATASET
# In[2]:
Data = pd.read_csv('C:/CAREER/diamonds.csv')
print(Data)
# # EXPLORATORY DATA ANALYSIS
# In[3]:
Data = Data.drop('Unnamed: 0', axis = 1)
# In[4]:
Data.head(10)
# In[5]:
Data.tail(7)
# In[6]:
Data.shape
# In[7]:
Data.info()
# In[8]:
Data.describe()
# 3. DATA PREPROCESSING
# the following steps are executed in this step
# i) Finding missing values and dropping rows with missing values
# ii) Converting the data to Numpy
# iii) Dividing the data set into training data and test data
#
# In[9]:
Data.isnull().sum()
# Amazing! There were no missing values in the diamonds dataset
# I will convert the object value columns into integers. These three columns include:cut, color, and clarity
# In[10]:
Data['cut'] = Data['cut'].map({'Ideal':5,'Premium':4,'Very Good':3,'Good':2,'Fair':1})
Data['color'] = Data['color'].map({'D':7,'E':6,'F':5,'G':4,'H':3,'I':2,'J':1})
Data['clarity'] = Data['clarity'].map({'IF':8,'VVS1':7,'VVS2':6,'VS1':5,'VS2':4,'SI1':3,'SI2':2,'I1':1})
# In[29]:
Data.describe()
# In[11]:
Data=Data.rename(columns={"x": "Length", "y": "width","z":"height",})
Data
# In[30]:
Data.info()
# In[12]:
x=Data[['carat','cut','color','clarity','depth','price','table','Length','width','height']]
x
# In[13]:
y = Data['price']
print(y)
# 4. DATA VISUALIZATION
# I will python libraries such as Matplotlib and Seaborn to customize appealing plots for effective storytelling
# In[14]:
plt.figure(figsize = (12, 6))
sns.histplot(Data['price'], bins = 20)
# In[15]:
plt.figure(figsize=(5,5))
plt.bar(Data['color'].value_counts().index, Data['color'].value_counts())
plt.ylabel("Number of Diamonds")
plt.xlabel("Color")
plt.show()
# In[18]:
color_pivot = x.pivot_table(index='color', values='price')
color_pivot.plot(kind='bar', color='blue',figsize=(12,6))
plt.xlabel('color')
plt.ylabel('price')
plt.title('Impact of color on price')
plt.xticks(rotation=0)
plt.show()
# In[19]:
plt.figure(figsize = (10, 5))
sns.histplot(Data['carat'], color= "yellow", bins=10)
# In[20]:
plt.figure(figsize=(10, 10))
plt.pie(Data['cut'].value_counts(),labels=['Ideal','Premium','Very Good','Good','Fair'],autopct='%1.1f%%')
plt.title('Cut')
plt.show()
# In[21]:
plt.figure(figsize=(5,5))
plt.bar(x['clarity'].value_counts().index, x['clarity'].value_counts())
plt.title('Clarity')
plt.ylabel("Number of Diamonds")
plt.xlabel("Clarity")
plt.show()
# Grouped Visualization
# In[22]:
color_pivot = x.pivot_table(index='cut', values='price')
color_pivot.plot(kind='bar',figsize=(12,6))
plt.xlabel('cut')
plt.ylabel('price')
plt.title('Impact of cut on price')
plt.xticks(rotation=0)
plt.show()
# In[23]:
plt.figure(figsize = (12, 5))
sns.barplot(x="cut",
y="price",
hue="color",
data=Data)
plt.title("Cut - Price - Color")
# In[24]:
plt.figure(figsize = (12, 5))
sns.barplot(x="cut",
y="price",
hue="clarity",
data = Data)
plt.title("Cut - Price - Clarity")
# In[25]:
sns.jointplot(x = "price",
y = Data["carat"],
data = Data)
# In[27]:
Data= sns.load_dataset("diamonds")
sns.pairplot(Data,hue='color')
# In[28]:
sns.displot(Data, x="x", hue='color',kind="kde", fill="True")
# In[34]:
Data['cut'] = Data['cut'].map({'Ideal':5,'Premium':4,'Very Good':3,'Good':2,'Fair':1})
Data['color'] = Data['color'].map({'D':7,'E':6,'F':5,'G':4,'H':3,'I':2,'J':1})
Data['clarity'] = Data['clarity'].map({'IF':8,'VVS1':7,'VVS2':6,'VS1':5,'VS2':4,'SI1':3,'SI2':2,'I1':1})
# In[35]:
Data=Data.rename(columns={"x": "Length", "y": "width","z":"height",})
Data
# In[36]:
x = Data.drop('price', axis=1)
y = Data['price']
#print(X_features.head(10))
#data.replace([np.inf, -np.inf], np.nan, inplace=True)
#X_train, X_test, target_train, target_test = train_test_split(X, target, test_size = 0.20)
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=40)
# In[37]:
print(x)
# In[38]:
from sklearn.tree import DecisionTreeRegressor
DTR = DecisionTreeRegressor()
DTR.fit(x_train, y_train)
print(DTR)