Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,17 @@
|
|
| 1 |
-
# prompt: make a web app for house price using Deep Learning ang and dashbording using Gradio
|
| 2 |
-
#!pip install scikit-learn --upgrade
|
| 3 |
import pandas as pd
|
| 4 |
from sklearn.model_selection import train_test_split
|
| 5 |
from sklearn.preprocessing import StandardScaler
|
| 6 |
from sklearn.neural_network import MLPRegressor
|
| 7 |
from sklearn.metrics import mean_squared_error
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
|
| 10 |
-
# ... (Rest of your code)
|
| 11 |
# Load the dataset
|
| 12 |
df = pd.read_csv('california_housing_train.csv')
|
| 13 |
|
| 14 |
# Select features and target
|
| 15 |
features = df[['longitude', 'latitude', 'housing_median_age', 'total_rooms',
|
| 16 |
-
|
| 17 |
target = df['median_house_value']
|
| 18 |
|
| 19 |
# Split the data
|
|
@@ -33,29 +31,56 @@ predictions = model.predict(X_test_scaled)
|
|
| 33 |
mse = mean_squared_error(y_test, predictions)
|
| 34 |
print(f'Mean Squared Error: {mse}')
|
| 35 |
|
| 36 |
-
# Create
|
| 37 |
def predict_house_price(longitude, latitude, housing_median_age, total_rooms,
|
| 38 |
-
total_bedrooms, population, households, median_income):
|
| 39 |
-
|
| 40 |
-
total_bedrooms, population, households, median_income]])
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
from sklearn.model_selection import train_test_split
|
| 3 |
from sklearn.preprocessing import StandardScaler
|
| 4 |
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')
|
| 11 |
|
| 12 |
# Select features and target
|
| 13 |
features = df[['longitude', 'latitude', 'housing_median_age', 'total_rooms',
|
| 14 |
+
'total_bedrooms', 'population', 'households', 'median_income']]
|
| 15 |
target = df['median_house_value']
|
| 16 |
|
| 17 |
# Split the data
|
|
|
|
| 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):
|
| 37 |
+
input_data = scaler.transform([[longitude, latitude, housing_median_age, total_rooms,
|
| 38 |
+
total_bedrooms, population, households, median_income]])
|
| 39 |
+
prediction = model.predict(input_data)[0]
|
| 40 |
+
return prediction
|
| 41 |
+
|
| 42 |
+
# Create dashboard function
|
| 43 |
+
def create_dashboard():
|
| 44 |
+
fig1 = px.scatter(df, x='longitude', y='latitude', color='median_house_value',
|
| 45 |
+
title="House Prices by Location",
|
| 46 |
+
labels={'longitude': 'Longitude', 'latitude': 'Latitude', 'median_house_value': 'House Value'})
|
| 47 |
+
|
| 48 |
+
fig2 = px.histogram(df, x='median_income', nbins=30, title="Distribution of Median Income",
|
| 49 |
+
labels={'median_income': 'Median Income'})
|
| 50 |
+
|
| 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 |
+
return fig1, fig2, fig3
|
| 55 |
+
|
| 56 |
+
# Gradio interface for prediction
|
| 57 |
+
iface_predict = gr.Interface(
|
| 58 |
+
fn=predict_house_price,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Number(label="Longitude"), # Use gr.Number directly
|
| 61 |
+
gr.Number(label="Latitude"),
|
| 62 |
+
gr.Number(label="Housing Median Age"),
|
| 63 |
+
gr.Number(label="Total Rooms"),
|
| 64 |
+
gr.Number(label="Total Bedrooms"),
|
| 65 |
+
gr.Number(label="Population"),
|
| 66 |
+
gr.Number(label="Households"),
|
| 67 |
+
gr.Number(label="Median Income"),
|
| 68 |
+
],
|
| 69 |
+
outputs="text",
|
| 70 |
+
title="House Price Prediction",
|
| 71 |
+
description="Enter the features to get the predicted house price."
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# Gradio interface for dashboard
|
| 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
|
| 84 |
+
iface = gr.TabbedInterface([iface_predict, iface_dashboard], ["Prediction", "Dashboard"])
|
| 85 |
+
|
| 86 |
+
iface.launch()
|