Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from sklearn.neural_network import MLPRegressor
|
|
5 |
from sklearn.metrics import mean_squared_error
|
6 |
import gradio as gr
|
7 |
import plotly.express as px
|
|
|
8 |
|
9 |
# Load the dataset
|
10 |
df = pd.read_csv('california_housing_train.csv')
|
@@ -22,15 +23,25 @@ scaler = StandardScaler()
|
|
22 |
X_train_scaled = scaler.fit_transform(X_train)
|
23 |
X_test_scaled = scaler.transform(X_test)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Train the model
|
26 |
-
model =
|
27 |
model.fit(X_train_scaled, y_train)
|
28 |
|
29 |
-
# Evaluate the model
|
30 |
-
predictions = model.predict(X_test_scaled)
|
31 |
-
mse = mean_squared_error(y_test, predictions)
|
32 |
-
print(f'Mean Squared Error: {mse}')
|
33 |
-
|
34 |
# Create prediction function
|
35 |
def predict_house_price(longitude, latitude, housing_median_age, total_rooms,
|
36 |
total_bedrooms, population, households, median_income):
|
@@ -51,7 +62,12 @@ def create_dashboard():
|
|
51 |
fig3 = px.histogram(df, x='housing_median_age', nbins=30, title="Distribution of Housing Median Age",
|
52 |
labels={'housing_median_age': 'Housing Median Age'})
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Gradio interface for prediction
|
57 |
iface_predict = gr.Interface(
|
@@ -75,9 +91,9 @@ iface_predict = gr.Interface(
|
|
75 |
iface_dashboard = gr.Interface(
|
76 |
fn=create_dashboard,
|
77 |
inputs=[],
|
78 |
-
outputs=[gr.Plot(), gr.Plot(), gr.Plot()],
|
79 |
title="House Price Dashboard",
|
80 |
-
description="Visualizations of the housing dataset."
|
81 |
)
|
82 |
|
83 |
# Launch both interfaces
|
|
|
5 |
from sklearn.metrics import mean_squared_error
|
6 |
import gradio as gr
|
7 |
import plotly.express as px
|
8 |
+
import plotly.graph_objects as go
|
9 |
|
10 |
# Load the dataset
|
11 |
df = pd.read_csv('california_housing_train.csv')
|
|
|
23 |
X_train_scaled = scaler.fit_transform(X_train)
|
24 |
X_test_scaled = scaler.transform(X_test)
|
25 |
|
26 |
+
# Initialize lists to store loss metrics
|
27 |
+
training_losses = []
|
28 |
+
validation_losses = []
|
29 |
+
|
30 |
+
# Custom MLPRegressor class to capture loss metrics
|
31 |
+
class CustomMLPRegressor(MLPRegressor):
|
32 |
+
def _fit(self, X, y, incremental):
|
33 |
+
result = super()._fit(X, y, incremental)
|
34 |
+
training_loss = self.loss_
|
35 |
+
predictions = self.predict(X_test_scaled)
|
36 |
+
validation_loss = mean_squared_error(y_test, predictions)
|
37 |
+
training_losses.append(training_loss)
|
38 |
+
validation_losses.append(validation_loss)
|
39 |
+
return result
|
40 |
+
|
41 |
# Train the model
|
42 |
+
model = CustomMLPRegressor(hidden_layer_sizes=(100,), activation='relu', solver='adam', max_iter=1000)
|
43 |
model.fit(X_train_scaled, y_train)
|
44 |
|
|
|
|
|
|
|
|
|
|
|
45 |
# Create prediction function
|
46 |
def predict_house_price(longitude, latitude, housing_median_age, total_rooms,
|
47 |
total_bedrooms, population, households, median_income):
|
|
|
62 |
fig3 = px.histogram(df, x='housing_median_age', nbins=30, title="Distribution of Housing Median Age",
|
63 |
labels={'housing_median_age': 'Housing Median Age'})
|
64 |
|
65 |
+
fig4 = go.Figure()
|
66 |
+
fig4.add_trace(go.Scatter(y=training_losses, mode='lines', name='Training Loss'))
|
67 |
+
fig4.add_trace(go.Scatter(y=validation_losses, mode='lines', name='Validation Loss'))
|
68 |
+
fig4.update_layout(title="Model Loss Over Time", xaxis_title="Epoch", yaxis_title="Loss")
|
69 |
+
|
70 |
+
return fig1, fig2, fig3, fig4
|
71 |
|
72 |
# Gradio interface for prediction
|
73 |
iface_predict = gr.Interface(
|
|
|
91 |
iface_dashboard = gr.Interface(
|
92 |
fn=create_dashboard,
|
93 |
inputs=[],
|
94 |
+
outputs=[gr.Plot(), gr.Plot(), gr.Plot(), gr.Plot()],
|
95 |
title="House Price Dashboard",
|
96 |
+
description="Visualizations of the housing dataset and model performance."
|
97 |
)
|
98 |
|
99 |
# Launch both interfaces
|