|
import gradio as gr |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
import numpy as np |
|
|
|
def plot_real_estate_correlation(state): |
|
|
|
df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv') |
|
|
|
|
|
df = df[df['State'] == state.upper()] |
|
|
|
|
|
zip_codes = df['RegionName'].unique() |
|
|
|
|
|
date_columns = [] |
|
for col in df.columns[7:]: |
|
try: |
|
|
|
pd.to_datetime(col) |
|
date_columns.append(col) |
|
except: |
|
continue |
|
|
|
|
|
price_matrix = [] |
|
|
|
|
|
for zip_code in zip_codes: |
|
df_zip = df[df['RegionName'] == zip_code] |
|
|
|
|
|
prices = df_zip.loc[:, date_columns].values.flatten() |
|
|
|
|
|
if not np.isnan(prices).all(): |
|
price_matrix.append(prices) |
|
|
|
|
|
price_matrix_df = pd.DataFrame(price_matrix, index=zip_codes, columns=date_columns) |
|
|
|
|
|
price_matrix_df = price_matrix_df.T.dropna() |
|
|
|
|
|
corr_matrix = price_matrix_df.corr() |
|
|
|
|
|
z_data = corr_matrix.values |
|
x_data, y_data = np.meshgrid(zip_codes, zip_codes) |
|
|
|
|
|
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)]) |
|
|
|
|
|
fig.update_layout( |
|
title=f'3D Correlation Matrix of Housing Prices in {state}', |
|
scene=dict( |
|
xaxis_title='ZIP Code', |
|
yaxis_title='ZIP Code', |
|
zaxis_title='Correlation', |
|
), |
|
autosize=True |
|
) |
|
|
|
return fig |
|
|
|
iface = gr.Interface(fn=plot_real_estate_correlation, |
|
inputs=[gr.components.Textbox(label="State (e.g., 'NJ' for New Jersey)")], |
|
outputs=gr.Plot()) |
|
|
|
iface.launch(share=False, debug=True) |