File size: 12,309 Bytes
707b169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
 
e9fad40
707b169
e9fad40
 
707b169
 
e9fad40
 
 
 
 
 
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
e9fad40
707b169
 
 
e9fad40
 
 
 
 
707b169
 
e9fad40
 
 
 
 
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
 
707b169
 
e9fad40
707b169
 
e9fad40
707b169
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
 
e9fad40
707b169
 
 
e9fad40
 
 
 
 
 
 
 
 
 
707b169
 
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
 
 
e9fad40
 
 
 
 
 
 
 
707b169
 
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
e9fad40
 
 
 
 
707b169
e9fad40
 
 
 
 
 
 
 
 
707b169
 
 
 
e9fad40
 
 
707b169
 
e9fad40
 
 
707b169
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
 
 
e9fad40
 
 
 
 
 
 
707b169
 
e9fad40
 
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
707b169
e9fad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707b169
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
import plotly.express as px
import plotly.graph_objects as go

# Set page config
st.set_page_config(
    page_title="California Housing Data Explorer",
    page_icon="🏠",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
st.markdown("""
<style>
    .main-header {
        font-size: 2.5rem;
        color: #1f77b4;
        text-align: center;
        margin-bottom: 2rem;
    }
    .metric-card {
        background-color: #f0f2f6;
        padding: 1rem;
        border-radius: 0.5rem;
        border-left: 5px solid #1f77b4;
    }
    .sidebar .sidebar-content {
        background-color: #f8f9fa;
    }
</style>
""", unsafe_allow_html=True)

# Load and cache data
@st.cache_data
def load_housing_data():
    """Load California housing dataset"""
    housing = fetch_california_housing()
    df = pd.DataFrame(housing.data, columns=housing.feature_names)
    df['target'] = housing.target
    df['price_category'] = pd.cut(df['target'], 
                                 bins=[0, 1.5, 3.0, 5.0, float('inf')], 
                                 labels=['Low', 'Medium', 'High', 'Very High'])
    return df, housing

# Train models
@st.cache_data
def train_models(X, y):
    """Train and return ML models"""
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Linear Regression
    lr_model = LinearRegression()
    lr_model.fit(X_train, y_train)
    lr_pred = lr_model.predict(X_test)
    lr_mse = mean_squared_error(y_test, lr_pred)
    lr_r2 = r2_score(y_test, lr_pred)
    
    # Random Forest
    rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
    rf_model.fit(X_train, y_train)
    rf_pred = rf_model.predict(X_test)
    rf_mse = mean_squared_error(y_test, rf_pred)
    rf_r2 = r2_score(y_test, rf_pred)
    
    return {
        'models': {'Linear Regression': lr_model, 'Random Forest': rf_model},
        'predictions': {'Linear Regression': lr_pred, 'Random Forest': rf_pred},
        'metrics': {
            'Linear Regression': {'MSE': lr_mse, 'RΒ²': lr_r2},
            'Random Forest': {'MSE': rf_mse, 'RΒ²': rf_r2}
        },
        'test_data': (X_test, y_test)
    }

def main():
    # Header
    st.markdown('<h1 class="main-header">🏠 California Housing Data Explorer</h1>', unsafe_allow_html=True)
    
    # Load data
    try:
        df, housing_info = load_housing_data()
    except Exception as e:
        st.error(f"Error loading data: {e}")
        return
    
    # Sidebar
    st.sidebar.title("πŸ”§ Controls")
    st.sidebar.markdown("---")
    
    # Dataset info
    st.sidebar.subheader("πŸ“Š Dataset Info")
    st.sidebar.info(f"""
    **Samples:** {len(df):,}  
    **Features:** {len(df.columns)-2}  
    **Target:** House Value (Γ—$100k)
    """)
    
    # Feature selection
    feature_cols = [col for col in df.columns if col not in ['target', 'price_category']]
    selected_features = st.sidebar.multiselect(
        "Select Features for Analysis",
        feature_cols,
        default=feature_cols[:4]
    )
    
    # Main content tabs
    tab1, tab2, tab3, tab4 = st.tabs(["πŸ“ˆ Overview", "πŸ” Exploratory Analysis", "πŸ€– ML Models", "🎯 Predictions"])
    
    with tab1:
        st.subheader("Dataset Overview")
        
        # Key metrics
        col1, col2, col3, col4 = st.columns(4)
        with col1:
            st.metric("Total Houses", f"{len(df):,}")
        with col2:
            st.metric("Avg House Value", f"${df['target'].mean():.2f}Γ—100k")
        with col3:
            st.metric("Max House Value", f"${df['target'].max():.2f}Γ—100k")
        with col4:
            st.metric("Features", len(feature_cols))
        
        st.markdown("---")
        
        # Data preview
        col1, col2 = st.columns([2, 1])
        
        with col1:
            st.subheader("πŸ“‹ Data Sample")
            st.dataframe(df.head(10), use_container_width=True)
        
        with col2:
            st.subheader("πŸ“Š Price Distribution")
            fig = px.histogram(df, x='target', nbins=50, 
                             title="House Value Distribution",
                             labels={'target': 'House Value (Γ—$100k)', 'count': 'Frequency'})
            fig.update_layout(height=400)
            st.plotly_chart(fig, use_container_width=True)
        
        # Statistical summary
        st.subheader("πŸ“ˆ Statistical Summary")
        st.dataframe(df[feature_cols + ['target']].describe(), use_container_width=True)
    
    with tab2:
        st.subheader("Exploratory Data Analysis")
        
        if not selected_features:
            st.warning("Please select at least one feature from the sidebar.")
            return
        
        # Correlation heatmap
        st.subheader("πŸ”₯ Feature Correlation Matrix")
        corr_features = selected_features + ['target']
        corr_matrix = df[corr_features].corr()
        
        fig, ax = plt.subplots(figsize=(10, 8))
        sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', center=0, ax=ax)
        plt.title("Feature Correlation Matrix")
        st.pyplot(fig)
        plt.close()
        
        # Feature relationships
        st.subheader("πŸ“Š Feature Relationships")
        
        col1, col2 = st.columns(2)
        
        with col1:
            if len(selected_features) >= 2:
                feature_x = st.selectbox("X-axis Feature", selected_features, key="x_axis")
                feature_y = st.selectbox("Y-axis Feature", selected_features, index=1, key="y_axis")
                
                fig = px.scatter(df, x=feature_x, y=feature_y, color='target',
                               title=f"{feature_x} vs {feature_y}",
                               color_continuous_scale='viridis')
                st.plotly_chart(fig, use_container_width=True)
        
        with col2:
            if selected_features:
                selected_feature = st.selectbox("Feature for Distribution", selected_features)
                
                fig = px.box(df, y=selected_feature, x='price_category',
                           title=f"{selected_feature} by Price Category")
                st.plotly_chart(fig, use_container_width=True)
        
        # Geographic analysis (if coordinates available)
        if 'Longitude' in df.columns and 'Latitude' in df.columns:
            st.subheader("πŸ—ΊοΈ Geographic Distribution")
            
            fig = px.scatter_mapbox(df.sample(5000), lat='Latitude', lon='Longitude', 
                                  color='target', size='target',
                                  hover_data=['AveRooms', 'AveBedrms', 'Population'],
                                  color_continuous_scale='viridis',
                                  mapbox_style='open-street-map',
                                  title='California Housing Prices by Location',
                                  height=600)
            st.plotly_chart(fig, use_container_width=True)
    
    with tab3:
        st.subheader("Machine Learning Models")
        
        if not selected_features:
            st.warning("Please select features for model training.")
            return
        
        # Train models
        X = df[selected_features]
        y = df['target']
        
        with st.spinner("Training models..."):
            results = train_models(X, y)
        
        # Model comparison
        st.subheader("πŸ“Š Model Performance")
        
        col1, col2 = st.columns(2)
        
        with col1:
            # Metrics table
            metrics_df = pd.DataFrame(results['metrics']).T
            st.dataframe(metrics_df, use_container_width=True)
        
        with col2:
            # Performance visualization
            models = list(results['metrics'].keys())
            r2_scores = [results['metrics'][model]['RΒ²'] for model in models]
            
            fig = px.bar(x=models, y=r2_scores, 
                        title="Model Performance (RΒ² Score)",
                        labels={'x': 'Model', 'y': 'RΒ² Score'})
            st.plotly_chart(fig, use_container_width=True)
        
        # Feature importance (Random Forest)
        st.subheader("🎯 Feature Importance (Random Forest)")
        
        rf_model = results['models']['Random Forest']
        importance_df = pd.DataFrame({
            'Feature': selected_features,
            'Importance': rf_model.feature_importances_
        }).sort_values('Importance', ascending=False)
        
        fig = px.bar(importance_df, x='Importance', y='Feature', orientation='h',
                    title="Feature Importance in Random Forest Model")
        st.plotly_chart(fig, use_container_width=True)
        
        # Prediction vs Actual
        st.subheader("🎯 Predictions vs Actual Values")
        
        X_test, y_test = results['test_data']
        
        col1, col2 = st.columns(2)
        
        with col1:
            lr_pred = results['predictions']['Linear Regression']
            fig = px.scatter(x=y_test, y=lr_pred, 
                           title="Linear Regression: Predicted vs Actual",
                           labels={'x': 'Actual', 'y': 'Predicted'})
            fig.add_shape(type="line", x0=0, y0=0, x1=5, y1=5, 
                         line=dict(dash="dash", color="red"))
            st.plotly_chart(fig, use_container_width=True)
        
        with col2:
            rf_pred = results['predictions']['Random Forest']
            fig = px.scatter(x=y_test, y=rf_pred,
                           title="Random Forest: Predicted vs Actual",
                           labels={'x': 'Actual', 'y': 'Predicted'})
            fig.add_shape(type="line", x0=0, y0=0, x1=5, y1=5, 
                         line=dict(dash="dash", color="red"))
            st.plotly_chart(fig, use_container_width=True)
    
    with tab4:
        st.subheader("Make House Price Predictions")
        
        if not selected_features:
            st.warning("Please select features to make predictions.")
            return
        
        # Train models for prediction
        X = df[selected_features]
        y = df['target']
        results = train_models(X, y)
        
        st.write("Adjust the feature values below to predict house prices:")
        
        # Create input widgets
        input_data = {}
        cols = st.columns(min(3, len(selected_features)))
        
        for i, feature in enumerate(selected_features):
            col_idx = i % len(cols)
            with cols[col_idx]:
                min_val = float(df[feature].min())
                max_val = float(df[feature].max())
                mean_val = float(df[feature].mean())
                
                input_data[feature] = st.slider(
                    f"{feature}",
                    min_value=min_val,
                    max_value=max_val,
                    value=mean_val,
                    key=f"pred_{feature}"
                )
        
        # Make predictions
        if st.button("🎯 Predict House Price", type="primary"):
            input_df = pd.DataFrame([input_data])
            
            col1, col2 = st.columns(2)
            
            with col1:
                lr_pred = results['models']['Linear Regression'].predict(input_df)[0]
                st.success(f"**Linear Regression Prediction:**  \n${lr_pred:.2f} Γ— 100k = ${lr_pred*100:.0f}k")
            
            with col2:
                rf_pred = results['models']['Random Forest'].predict(input_df)[0]
                st.success(f"**Random Forest Prediction:**  \n${rf_pred:.2f} Γ— 100k = ${rf_pred*100:.0f}k")
            
            # Show input summary
            st.subheader("πŸ“‹ Input Summary")
            input_summary = pd.DataFrame([input_data]).T
            input_summary.columns = ['Value']
            st.dataframe(input_summary, use_container_width=True)

if __name__ == "__main__":
    main()