dibend's picture
Create app.py
68f4690 verified
raw
history blame
1.53 kB
import gradio as gr
import pandas as pd
import plotly.graph_objects as go
import numpy as np
def plot_real_estate_correlation(state):
# Read the CSV file
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')
# Filter for the given state
df = df[df['State'] == state.upper()]
# Extract the list of ZIP codes and price data
zip_codes = df['RegionName'].unique()
price_data = df.iloc[:, 7:] # Assuming price data starts from the 8th column
# Drop rows with missing data to avoid issues in correlation calculation
price_data = price_data.dropna(axis=1, how='all')
# Calculate the correlation matrix for ZIP codes
corr_matrix = price_data.T.corr()
# Prepare the grid data for 3D plot
x_data, y_data = np.meshgrid(zip_codes, zip_codes)
z_data = corr_matrix.values
# Create the 3D surface plot
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
# Update plot layout
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)