-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis-1.py
296 lines (230 loc) · 8.61 KB
/
analysis-1.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
# -*- coding: utf-8 -*-
"""Analysis.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1amM_iYzI73lNVlDyvf2yz5VEDlbfDZoT
# Cleaning
"""
# Importing libraries and setting up access to Drive
import pandas as pd
import numpy as np
pd.set_option('display.max_columns', None)
df = pd.read_excel('/content/up10k.xlsx')
# Drop the ID column and index column
df = df.drop(columns=['id', 'Unnamed: 0'])
# Clean condition column
# Split the "condition" column into three columns using commas as the separator
df[['accidents', 'num_owners', 'personal_use']] = df['condition'].str.split(',', expand=True)
df.drop('condition', axis=1, inplace=True)
# Define a function to extract the numeric portion of a string
def extract_number(s):
try:
return int(s.split()[0])
except ValueError:
return 0
# Apply the function to the "accidents" column
df['accidents'] = df['accidents'].apply(extract_number)
# Apply to "num_owners"
df['num_owners'] = df['num_owners'].apply(extract_number)
# Dummy for "personal_use" or "fleet_use"
df['personal_use'] = df['personal_use'].str.contains('personal', case=False).astype(int)
import re
# Remove non-numeric characters from the "mileage" values using regular expressions
df['mileage'] = df['mileage'].apply(lambda x: re.sub('[^0-9]', '', x))
df['mileage'] = pd.to_numeric(df['mileage'], errors='coerce')
# For model:
# Create dummies for every unique name in column "model"
model_dummies = pd.get_dummies(df['model'])
# Add prefix "model" to the new columns
model_dummies = model_dummies.add_prefix('model_')
# Drop the old "model" column
df.drop('model', axis=1, inplace=True)
# Concatenate the new "model" dummies columns to the original dataframe
df = pd.concat([df, model_dummies], axis=1)
# For location:
# Extract state code from "location" column
df["State"] = df["location"].str.split(",").str[1].str.strip()
# Create dummies for state code
state_dummies = pd.get_dummies(df["State"], prefix="loc")
# Add the state dummies to the original dataframe
df = pd.concat([df, state_dummies], axis=1)
# Inspecting the columns with most missing observations
df.isnull().sum()
df = df.dropna()
df = df.reset_index(drop=True)
# GDP data
gdp = pd.read_excel('/content/statistic_id248053_us-real-gross-domestic-product-2022-by-state.xlsx')
state_abbr = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'Florida': 'FL',
'Georgia': 'GA',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virginia': 'VA',
'Washington': 'WA',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
# use .replace() method to change state names to abbreviations
gdp['State'] = gdp['State'].replace(state_abbr)
merged_df = pd.merge(df, gdp, on='State')
merged_df = merged_df.rename(columns={'Real Gross Domestic Product (GDP) of the United States in Q3 2022, by state (in billion chained 2012 U.S. dollars)': 'gdp'})
df = merged_df
# Drop original "location" and "state" columns
df.drop(["location", "State"], axis=1, inplace=True)
df.describe()
df.to_csv('full_data.csv', index=False)
"""# Analysis
## Optimizing
"""
# Modules
import inspect
import numpy as np
import pandas as pd
from sklearn import ensemble, metrics, model_selection, preprocessing, tree
from matplotlib import pyplot
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import accuracy_score, recall_score
# separate "price" column from df and assign it to a new variable
target = df.pop('price')
# sample split:
X_train, X_test, y_train, y_test = model_selection.train_test_split(
df, target, train_size=.75, test_size=.25,
shuffle=True)
# ============================== tree tuning ==============================:
# Define the parameter grid to search over
param_grid = {
"max_depth": [2, 4, 6, 8],
"min_samples_split": [2, 4, 8],
"min_samples_leaf": [1, 2, 4],
}
# Create a decision tree regressor object
dt_reg = DecisionTreeRegressor()
# Create a GridSearchCV object with the parameter grid and 5-fold cross validation
grid_search = GridSearchCV(
dt_reg, param_grid, cv=5, scoring="neg_mean_squared_error", n_jobs=-1
)
# Fit the GridSearchCV object to the data
grid_search.fit(X_train, y_train)
# Print the best hyperparameters and the corresponding mean squared error
print(
f"Best hyperparameters: {grid_search.best_params_} \nBest negative mean squared error: {grid_search.best_score_}"
)
# RF tuning
# Define the parameter grid to search over
param_grid = {
"n_estimators": [50, 100, 150],
"max_depth": [2, 4, 6, 8, 10],
"min_samples_split": [2, 4, 8, 16],
"min_samples_leaf": [1, 2, 4, 8],
}
# Create a Random Forest regressor object
rf_reg = RandomForestRegressor()
# Create a GridSearchCV object with the parameter grid and 5-fold cross validation
grid_search = GridSearchCV(
rf_reg, param_grid, cv=5, scoring="neg_mean_squared_error", n_jobs=-1
)
# Fit the GridSearchCV object to the data
grid_search.fit(X_train, y_train)
# Print the best hyperparameters and the corresponding mean squared error
print(
f"Best hyperparameters: {grid_search.best_params_} \nBest negative mean squared error: {grid_search.best_score_}"
)
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import GridSearchCV
# KNN tuning
# Define the parameter grid to search over
param_grid = {
'n_neighbors': [3, 5, 10, 15],
}
# Create a KNN regressor object
knn_reg = KNeighborsRegressor()
# Create a GridSearchCV object with the parameter grid and 5-fold cross validation
grid_search = GridSearchCV(knn_reg, param_grid, cv=5, scoring='neg_mean_squared_error', n_jobs=-1)
# Fit the GridSearchCV object to the data
grid_search.fit(X_train, y_train)
# Print the best hyperparameters and the corresponding mean squared error
print(f'Best hyperparameters: {grid_search.best_params_} \nBest negative mean squared error: {grid_search.best_score_}')
"""## Fitting"""
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error
# Train linear regression
lr = LinearRegression()
lr.fit(X_train, y_train)
lr_preds = lr.predict(X_test)
lr_mse = mean_squared_error(y_test, lr_preds)
print(f"Linear Regression MSE: {lr_mse}")
lr_importances = pd.DataFrame({"feature": X_train.columns, "importance": lr.coef_}).nlargest(5, "importance")
print(f"Top 5 Linear Regression Features: \n{lr_importances}")
# Train decision tree
dt = DecisionTreeRegressor(max_depth=6, min_samples_leaf=2, min_samples_split=8)
dt.fit(X_train, y_train)
dt_preds = dt.predict(X_test)
dt_mse = mean_squared_error(y_test, dt_preds)
print(f"Decision Tree MSE: {dt_mse}")
dt_importances = pd.DataFrame({"feature": X_train.columns, "importance": dt.feature_importances_}).nlargest(5, "importance")
print(f"Top 5 Decision Tree Features: \n{dt_importances}")
# Train random forest
rf = RandomForestRegressor(max_depth=10, min_samples_leaf=2, min_samples_split=8, n_estimators=100)
rf.fit(X_train, y_train)
rf_preds = rf.predict(X_test)
rf_mse = mean_squared_error(y_test, rf_preds)
print(f"Random Forest MSE: {rf_mse}")
rf_importances = pd.DataFrame({"feature": X_train.columns, "importance": rf.feature_importances_}).nlargest(5, "importance")
print(f"Top 5 Random Forest Features: \n{rf_importances}")
# Train KNN
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error
knn = KNeighborsRegressor(n_neighbors=5)
knn.fit(X_train, y_train)
knn_preds = knn.predict(X_test)
knn_mse = mean_squared_error(y_test, knn_preds)
print(f"KNN MSE: {knn_mse}")
print(f"Top 5 KNN Features: Not applicable")