| 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() |
| price_data = df.iloc[:, 7:] |
|
|
| |
| price_data = price_data.dropna(axis=1, how='all') |
|
|
| |
| corr_matrix = price_data.T.corr() |
|
|
| |
| x_data, y_data = np.meshgrid(zip_codes, zip_codes) |
| z_data = corr_matrix.values |
|
|
| |
| 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) |