-
Notifications
You must be signed in to change notification settings - Fork 2
/
scratchpad.txt
290 lines (229 loc) · 9.59 KB
/
scratchpad.txt
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
import pandas as pd
import numpy as np
import os
import sklearn.preprocessing
from sklearn.model_selection import train_test_split
from env import host, user, password
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
'''
*------------------*
| |
| ACQUIRE |
| |
*------------------*
'''
def get_connection(db, user=user, host=host, password=password):
'''
This function uses my info from my env file to
create a connection url to access the Codeup db.
'''
return f'mysql+pymysql://{user}:{password}@{host}/{db}'
def zillow17():
'''
This function reads in the zillow data from the Codeup db
and returns a pandas DataFrame with:
- all fields related to the properties that are available
- using all the tables in the database
- Only include properties with a transaction in 2017
- include only the last transaction for each property
- zestimate error
- date of transaction
- Only include properties that include a latitude and longitude value
'''
query = """
SELECT prop.*,
pred.logerror,
pred.transactiondate,
air.airconditioningdesc,
arch.architecturalstyledesc,
build.buildingclassdesc,
heat.heatingorsystemdesc,
landuse.propertylandusedesc,
story.storydesc,
construct.typeconstructiondesc
FROM properties_2017 prop
INNER JOIN (SELECT parcelid,
logerror,
Max(transactiondate) transactiondate
FROM predictions_2017
GROUP BY parcelid, logerror) pred
USING (parcelid)
LEFT JOIN airconditioningtype air USING (airconditioningtypeid)
LEFT JOIN architecturalstyletype arch USING (architecturalstyletypeid)
LEFT JOIN buildingclasstype build USING (buildingclasstypeid)
LEFT JOIN heatingorsystemtype heat USING (heatingorsystemtypeid)
LEFT JOIN propertylandusetype landuse USING (propertylandusetypeid)
LEFT JOIN storytype story USING (storytypeid)
LEFT JOIN typeconstructiontype construct USING (typeconstructiontypeid)
WHERE prop.latitude IS NOT NULL
AND prop.longitude IS NOT NULL
AND transactiondate like '2017%';
"""
return pd.read_sql(query, get_connection('zillow'))
###################### Amanda's Stuff ##############################
import pandas as pd
import numpy as np
import os
import sklearn.preprocessing
from sklearn.model_selection import train_test_split
from env import host, user, password
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
'''
*------------------*
| |
| ACQUIRE |
| |
*------------------*
'''
def get_connection(db, user=user, host=host, password=password):
'''
This function uses my info from my env file to
create a connection url to access the Codeup db.
'''
return f'mysql+pymysql://{user}:{password}@{host}/{db}'
def zillow17():
'''
This function reads in the zillow data from the Codeup db
and returns a pandas DataFrame with:
- all fields related to the properties that are available
- using all the tables in the database
- Only include properties with a transaction in 2017
- include only the last transaction for each property
- zestimate error
- date of transaction
- Only include properties that include a latitude and longitude value
'''
query = """
SELECT prop.*,
pred.logerror,
pred.transactiondate,
air.airconditioningdesc,
arch.architecturalstyledesc,
build.buildingclassdesc,
heat.heatingorsystemdesc,
landuse.propertylandusedesc,
story.storydesc,
construct.typeconstructiondesc
FROM properties_2017 prop
INNER JOIN (SELECT parcelid,
logerror,
Max(transactiondate) transactiondate
FROM predictions_2017
GROUP BY parcelid, logerror) pred
USING (parcelid)
LEFT JOIN airconditioningtype air USING (airconditioningtypeid)
LEFT JOIN architecturalstyletype arch USING (architecturalstyletypeid)
LEFT JOIN buildingclasstype build USING (buildingclasstypeid)
LEFT JOIN heatingorsystemtype heat USING (heatingorsystemtypeid)
LEFT JOIN propertylandusetype landuse USING (propertylandusetypeid)
LEFT JOIN storytype story USING (storytypeid)
LEFT JOIN typeconstructiontype construct USING (typeconstructiontypeid)
WHERE prop.latitude IS NOT NULL
AND prop.longitude IS NOT NULL
AND transactiondate like '2017%'
"""
return pd.read_sql(query, get_connection('zillow'))
'''
*------------------*
| |
| PREPARE |
| |
*------------------*
'''
def drop_based_on_pct(df, pc, pr):
"""
drop_based_on_pct takes in:
- dataframe,
- threshold percent of non-null values for columns(# between 0-1),
- threshold percent of non-null values for rows(# between 0-1)
Returns: a dataframe with the columns and rows dropped as indicated.
"""
tpc = 1-pc
tpr = 1-pr
df.dropna(axis = 1, thresh = tpc * len(df.index), inplace = True)
df.dropna(axis = 0, thresh = tpr * len(df.columns), inplace = True)
return df
def outlier(df, feature, m):
'''
outlier will take in a dataframe's feature:
- calculate it's 1st & 3rd quartiles,
- use their difference to calculate the IQR
- then apply to calculate upper and lower bounds
- using the `m` multiplier
'''
q1 = df[feature].quantile(.25)
q3 = df[feature].quantile(.75)
iqr = q3 - q1
multiplier = m
upper_bound = q3 + (multiplier * iqr)
lower_bound = q1 - (multiplier * iqr)
return upper_bound, lower_bound
def wrangle_zillow():
"""
wrangle_zillow will:
- read in zillow.csv acquired from SQL query
- filter data to single unit homes with min 1B/1B
"""
df = pd.read_csv('zillow.csv')
df = df.set_index("parcelid")
# Restrict df to only properties that meet single-use criteria
single_use = [260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 273, 275, 276, 279]
df = df[df.propertylandusetypeid.isin(single_use)]
# Filter those properties without at least 1 bath & bed and 500 sqft area
df = df[(df.bedroomcnt > 0) & (df.bathroomcnt > 0) & ((df.unitcnt<=1)|df.unitcnt.isnull())\
& (df.calculatedfinishedsquarefeet>500)]
# Handle missing values i.e. drop columns and rows based on a threshold
df = drop_based_on_pct(df, .6, .7)
# Add column for counties
df['county'] = np.where(df.fips == 6037, 'Los_Angeles',
np.where(df.fips == 6059, 'Orange',
'Ventura'))
# Drop unnecessary/redundant columns
df = df.drop(['id',
'calculatedbathnbr', 'finishedsquarefeet12', 'fullbathcnt', 'heatingorsystemtypeid'
,'propertycountylandusecode', 'propertylandusetypeid','propertyzoningdesc',
'censustractandblock', 'propertylandusedesc', 'heatingorsystemdesc'],axis=1)
# Replace nulls in unitcnt with 1
df.unitcnt.fillna(1, inplace = True)
# Replace nulls with median values for select columns
df.lotsizesquarefeet.fillna(7265, inplace = True)
df.buildingqualitytypeid.fillna(7.0, inplace = True)
# Drop any remaining nulls
df = df.dropna()
# Columns that need to be adjusted for outliers
df = df[df.taxvaluedollarcnt < 5_000_000]
df = df[df.calculatedfinishedsquarefeet < 12500]
# create column for age of home
df['home_age'] = 2021 - df.yearbuilt
# List of cols to convert to 'int'
cols = ['fips', 'buildingqualitytypeid', 'bedroomcnt', 'roomcnt',
'home_age', 'yearbuilt', 'assessmentyear', 'regionidcounty',
'regionidzip', 'unitcnt', 'home_age']
# loop through cols list in conversion
for col in cols:
df[col] = df[col].astype('int')
# Rename columns
df.rename(columns={"bathroomcnt": "bathrooms",
"bedroomcnt": "bedrooms",
"buildingqualitytypeid": "property_quality",
"calculatedfinishedsquarefeet": "sqft",
"lotsizesquarefeet": "lot_sqft",
"regionidzip": "zip_code",
"landtaxvaluedollarcnt": "land_value",
"structuretaxvaluedollarcnt": "structure_value",
"taxvaluedollarcnt ": "home_value"
}, inplace=True)
# create a categorical version of target by splitting into quartiles
df['logerror_quartiles'] = pd.qcut(df.logerror, q=4, labels=['q1', 'q2', 'q3', 'q4'])
return df
############################## check current version ###################################
import matplotlib
import sklearn
print('Versions')
print('Pandas:', pd.__version__)
print('Numpy:', np.__version__)
print('Matplotlib:', matplotlib.__version__)
print('Seaborn:', sns.__version__)
print('Scikit-Learn:', sklearn.__version__)