|
''' |
|
git clone https://github.com/geopandas/geopandas.git |
|
cd geopandas |
|
pip install . |
|
''' |
|
import requests |
|
import pandas as pd |
|
import numpy as np |
|
import requests |
|
import geopandas as gpd |
|
from shapely.geometry import Point |
|
|
|
|
|
neighborhood = gpd.read_file("https://raw.githubusercontent.com/HathawayLiu/Housing_dataset/main/Neighborhood_Map_Atlas_Districts.geojson") |
|
url = "https://github.com/HathawayLiu/Housing_dataset/raw/main/Building_Permits_20240213.csv" |
|
df = pd.read_csv(url) |
|
|
|
|
|
df['OriginalZip'] = pd.to_numeric(df['OriginalZip'], errors='coerce').fillna('NA').astype(str) |
|
df['OriginalZip'] = df['OriginalZip'].replace(0, 'NA') |
|
df['OriginalCity'] = df['OriginalCity'].fillna('SEATTLE') |
|
df['OriginalState'] = df['OriginalState'].fillna('WA') |
|
df['EstProjectCost'] = pd.to_numeric(df['EstProjectCost'], errors='coerce').astype(float) |
|
df['IssuedDate'] = pd.to_datetime(df['IssuedDate'], errors='coerce') |
|
df['HousingUnits'] = pd.to_numeric(df['HousingUnits'], errors='coerce').fillna(0).astype(int) |
|
df['HousingUnitsRemoved'] = pd.to_numeric(df['HousingUnitsRemoved'], errors='coerce').fillna(0).astype(int) |
|
df['HousingUnitsAdded'] = pd.to_numeric(df['HousingUnitsAdded'], errors='coerce').fillna(0).astype(int) |
|
df['Longitude'] = pd.to_numeric(df['Longitude'], errors='coerce') |
|
df['Latitude'] = pd.to_numeric(df['Latitude'], errors='coerce') |
|
|
|
|
|
def get_zip_code_from_coordinates(latitude, longitude, api_key): |
|
if pd.isna(latitude) or pd.isna(longitude): |
|
return 'NA' |
|
|
|
api_url = f"https://maps.googleapis.com/maps/api/geocode/json?latlng={latitude},{longitude}&key={api_key}" |
|
response = requests.get(api_url) |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
if data['results']: |
|
for component in data['results'][0]['address_components']: |
|
if 'postal_code' in component['types']: |
|
return component['long_name'] |
|
return 'NA' |
|
else: |
|
return 'NA' |
|
|
|
|
|
api_key = 'Your Own API Key' |
|
for index, row in df.iterrows(): |
|
if row['OriginalZip'] == 'NA': |
|
zip_code = get_zip_code_from_coordinates(row['Latitude'], row['Longitude'], api_key) |
|
df.at[index, 'OriginalZip'] = zip_code |
|
print(f"Updated row {index} with Zip Code: {zip_code}") |
|
|
|
|
|
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude), crs='EPSG:4326') |
|
def get_neighborhood_name(point, neighborhoods): |
|
for _, row in neighborhoods.iterrows(): |
|
if point.within(row['geometry']): |
|
print(row['L_HOOD']) |
|
return row['L_HOOD'] |
|
return 'NA' |
|
|
|
gdf['NeighborDistrict'] = gdf['geometry'].apply(lambda x: get_neighborhood_name(x, neighborhood) if pd.notna(x) else 'NA') |
|
|
|
df['NeighborDistrict'] = gdf['NeighborDistrict'] |
|
|
|
df_filtered = df[df['IssuedDate'].dt.year >= 2000] |
|
df_filtered['IssuedDate'] = df['IssuedDate'].astype(str) |
|
df_filtered.fillna('NA', inplace=True) |
|
|
|
''' |
|
Following code is for spliting datasets in train and test dataset |
|
''' |
|
|
|
housing_df = pd.read_csv('https://github.com/HathawayLiu/Housing_dataset/raw/main/Building_Permits_Cleaned.csv') |
|
|
|
housing_df = housing_df.sample(frac=1).reset_index(drop=True) |
|
|
|
|
|
split_ratio = 0.8 |
|
split_index = int(len(housing_df) * split_ratio) |
|
|
|
train_df = housing_df[:split_index] |
|
test_df = housing_df[split_index:] |
|
|
|
|
|
train_df.to_csv('/Users/hathawayliu/Desktop/train_dataset.csv', index=False) |
|
test_df.to_csv('/Users/hathawayliu/Desktop/test_dataset.csv', index=False) |