File size: 2,484 Bytes
1a68a0d
 
e3390fb
1a68a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#1. Importing Lib

import gradio as gr
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
#from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import r2_score

#2. Data Preprocessing

df=pd.read_csv("housing.csv")

df.head()

df.tail()

df.info()

df.describe()

df.isnull().sum()

df["ocean_proximity"].unique()
df.replace({"ocean_proximity":{"NEAR BAY":0,"<1H OCEAN":1,"INLAND":2,"NEAR OCEAN":3,"ISLAND":4}},inplace=True)

# Spliting data into X and y
x=df.drop("median_house_value",axis=1)
y=df["median_house_value"]

#3. Modeling Part
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
model=XGBRegressor()

model.fit(x_train,y_train)
model.fit(x_test,y_test)

x_predict=model.predict(x_train)
x_accuracy=r2_score(x_predict,y_train)

y_predict=model.predict(x_test)
y_accuracy=r2_score(y_predict,y_test)

#4. UI For Model

# Define the prediction function
def predict_house_value(longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, ocean_proximity):
    input_data = np.array([longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, ocean_proximity]).reshape(1, -1)
    prediction = model.predict(input_data)
    return prediction[0]

# Define the Gradio interface
inputs = [
    gr.Slider(label="Longitude", minimum=-124.35, maximum=-114.31),  # Longitude range from min to max
    gr.Slider(label="Latitude", minimum=32.54, maximum=41.95),  # Latitude range from min to max
    gr.Slider(label="Housing Median Age", minimum=1, maximum=52),  # Housing median age from min to max
    gr.Slider(label="Total Rooms", minimum=2, maximum=39320),  # Total rooms range from min to max
    gr.Slider(label="Total Bedrooms", minimum=1, maximum=6445),  # Total bedrooms range from min to max
    gr.Slider(label="Population", minimum=3, maximum=35682),  # Population range from min to max
    gr.Slider(label="Households", minimum=1, maximum=6082),  # Households range from min to max
    gr.Slider(label="Median Income", minimum=0.4999, maximum=15.0001),  # Median income range from min to max
    gr.Dropdown([0,1,2,3,4], label="Ocean Proximity( 0=NEAR BAY, 1=<1H OCEAN, 2=INLAND, 3=NEAR OCEAN, 4=ISLAND)")
]

outputs = gr.Textbox(label="Predicted House Value")

gr.Interface(fn=predict_house_value, inputs=inputs, outputs=outputs, live=True).launch()