Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
def plot_real_estate_correlation(state):
|
| 7 |
+
# Read the CSV file
|
| 8 |
+
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')
|
| 9 |
+
|
| 10 |
+
# Filter for the given state
|
| 11 |
+
df = df[df['State'] == state.upper()]
|
| 12 |
+
|
| 13 |
+
# Extract the list of ZIP codes and price data
|
| 14 |
+
zip_codes = df['RegionName'].unique()
|
| 15 |
+
price_data = df.iloc[:, 7:] # Assuming price data starts from the 8th column
|
| 16 |
+
|
| 17 |
+
# Drop rows with missing data to avoid issues in correlation calculation
|
| 18 |
+
price_data = price_data.dropna(axis=1, how='all')
|
| 19 |
+
|
| 20 |
+
# Calculate the correlation matrix for ZIP codes
|
| 21 |
+
corr_matrix = price_data.T.corr()
|
| 22 |
+
|
| 23 |
+
# Prepare the grid data for 3D plot
|
| 24 |
+
x_data, y_data = np.meshgrid(zip_codes, zip_codes)
|
| 25 |
+
z_data = corr_matrix.values
|
| 26 |
+
|
| 27 |
+
# Create the 3D surface plot
|
| 28 |
+
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
|
| 29 |
+
|
| 30 |
+
# Update plot layout
|
| 31 |
+
fig.update_layout(
|
| 32 |
+
title=f'3D Correlation Matrix of Housing Prices in {state}',
|
| 33 |
+
scene=dict(
|
| 34 |
+
xaxis_title='ZIP Code',
|
| 35 |
+
yaxis_title='ZIP Code',
|
| 36 |
+
zaxis_title='Correlation',
|
| 37 |
+
),
|
| 38 |
+
autosize=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return fig
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(fn=plot_real_estate_correlation,
|
| 44 |
+
inputs=[gr.components.Textbox(label="State (e.g., 'NJ' for New Jersey)")],
|
| 45 |
+
outputs=gr.Plot())
|
| 46 |
+
|
| 47 |
+
iface.launch(share=False, debug=True)
|