dibend commited on
Commit
475dc77
·
verified ·
1 Parent(s): f3f41ff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_zip_code_correlation(zip_codes_str):
7
+ # Convert input string to list of zip codes
8
+ zip_codes = [z.strip() for z in zip_codes_str.split(",")]
9
+
10
+ # Read the CSV file
11
+ 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')
12
+
13
+ # Filter for the given ZIP codes
14
+ df = df[df['RegionName'].astype(str).isin(zip_codes)]
15
+
16
+ if df.empty:
17
+ raise ValueError("No data found for the provided ZIP codes.")
18
+
19
+ # Extract columns that are valid date strings only
20
+ date_columns = []
21
+ for col in df.columns[7:]:
22
+ try:
23
+ pd.to_datetime(col)
24
+ date_columns.append(col)
25
+ except:
26
+ continue
27
+
28
+ # Initialize a DataFrame to hold price data for correlation calculation
29
+ price_matrix = []
30
+ zip_list = []
31
+
32
+ for zip_code in zip_codes:
33
+ df_zip = df[df['RegionName'].astype(str) == zip_code]
34
+ if not df_zip.empty:
35
+ prices = df_zip.loc[:, date_columns].values.flatten()
36
+ if not np.isnan(prices).all():
37
+ price_matrix.append(prices)
38
+ zip_list.append(zip_code)
39
+
40
+ # Check if there is enough data
41
+ if len(price_matrix) < 2:
42
+ raise ValueError("Not enough data for correlation calculation.")
43
+
44
+ price_matrix_df = pd.DataFrame(price_matrix, index=zip_list, columns=date_columns)
45
+ price_matrix_df = price_matrix_df.T.dropna()
46
+
47
+ # Calculate correlation
48
+ corr_matrix = price_matrix_df.corr()
49
+
50
+ # Prepare data for 3D plot
51
+ z_data = corr_matrix.values
52
+ x_data, y_data = np.meshgrid(zip_list, zip_list)
53
+
54
+ # Create the 3D surface plot
55
+ fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
56
+
57
+ fig.update_layout(
58
+ title='3D Correlation Matrix of Housing Prices for Selected ZIP Codes',
59
+ scene=dict(
60
+ xaxis_title='ZIP Code',
61
+ yaxis_title='ZIP Code',
62
+ zaxis_title='Correlation',
63
+ ),
64
+ autosize=True
65
+ )
66
+
67
+ return fig
68
+
69
+ iface = gr.Interface(
70
+ fn=plot_zip_code_correlation,
71
+ inputs=gr.Textbox(label="Enter comma-separated ZIP codes (e.g., 07001, 07002, 07003)"),
72
+ outputs=gr.Plot(),
73
+ title="ZIP Code 3D Correlation Matrix"
74
+ )
75
+
76
+ iface.launch(debug=True)