-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarsales.py
191 lines (77 loc) · 2.46 KB
/
carsales.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
#!/usr/bin/env python
# coding: utf-8
# In[27]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
print(os.listdir("D:\\NCI_Course\\Sem 2\\Programming in Data Analytics\\PFDA-VM-Share"))
# In[28]:
df = pd.read_csv('D:\\NCI_Course\\Sem 2\\Programming in Data Analytics\\PFDA-VM-Share\\cars.csv')
# In[29]:
df.info()
# In[30]:
df.head()
# In[31]:
used_vehicles = df.drop(columns=['latitude', 'longitude'])
# In[32]:
used_vehicles.shape
# In[33]:
used_vehicles = used_vehicles[used_vehicles.price != 0]
used_vehicles.shape
# In[34]:
used_vehicles = used_vehicles[used_vehicles.price < 100000]
used_vehicles.shape
# In[35]:
used_vehicles = used_vehicles[used_vehicles.year > 1985]
used_vehicles.shape
# In[36]:
used_vehicles.odometer.quantile(.999)
# In[37]:
used_vehicles = used_vehicles[~(used_vehicles.odometer > 500000)]
used_vehicles.shape
# In[38]:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import train_test_split as split
import warnings
from sys import modules
# In[39]:
linear_features = used_vehicles[['odometer','year','price']]
# In[40]:
linear_features = linear_features.dropna()
linear_features.shape
# In[41]:
used_vehicles_train, used_vehicles_test = split(linear_features, train_size=0.75, random_state=4222)
# In[42]:
training_x = used_vehicles_train[['odometer','year']]
training_y = used_vehicles_train['price']
# In[43]:
used_vehicles_lm = LinearRegression(fit_intercept=True)
# In[44]:
used_vehicles_lm.fit(training_x, training_y)
# In[45]:
print("The model intercept is: {}".format(used_vehicles_lm.intercept_))
print("The model coefficients are: {}".format(used_vehicles_lm.coef_[0]))
# In[46]:
training_x['Price_prediction'] = used_vehicles_lm.predict(training_x)
training_x.head()
# In[47]:
training_rmse = np.sqrt(MSE(training_y, training_x['Price_prediction']))
print("RMSE = {:.2f}".format(training_rmse))
# In[48]:
veh_lm_test = LinearRegression()
# In[49]:
testing_x = used_vehicles_test[['odometer','year']]
testing_y = used_vehicles_test['price']
# In[50]:
veh_lm_test.fit(testing_x, testing_y)
# In[51]:
testing_x['price_prediction'] = veh_lm_test.predict(testing_x)
testing_x.head()
# In[52]:
testing_rmse = np.sqrt(MSE(testing_y, testing_x['price_prediction']))
print("RMSE = {:.2f}".format(testing_rmse))
# In[ ]:
# In[ ]: