Spaces:
Runtime error
Runtime error
File size: 4,079 Bytes
7055201 |
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 |
import gradio as gr
input_module1 = gr.inputs.Slider(-124.35, -114.35, step=5, label = "Longitude")
input_module2 = gr.inputs.Slider(32, 41, step=5, label = "Latitude")
input_module3 = gr.inputs.Slider(1, 52, step=5, label = "Housing_median_age (Year)")
input_module4 = gr.inputs.Slider(1, 39996, step=5, label = "Total_rooms")
input_module5 = gr.inputs.Slider(1, 6441, step=5, label = "Total_bedrooms")
input_module6 = gr.inputs.Slider(3, 35678, step=5, label = "Population")
input_module7 = gr.inputs.Slider(1, 6081, step=5, label = "Households")
input_module8 = gr.inputs.Slider(0, 15, step=5, label = "Median_income")
examples = [["Longitude[-118,-123,-154,-129,-121]"], ["Latitude[32,23,12,16,17]"],["Housing_median_age (Year)[32,23,1,16,49]"], ["Total_rooms[32,23,1,16,49]"],["Total_bedrooms[32,23,1,16,49]"],["Population[3204,234563,3531,53316,35649]"], ["Households[3204,4563,3531,3316,5349]"], ["Median_income[4,11,6,3,14]"]]
# Step 6.2: Define different output components
# a. define text data type
output_module1 = gr.outputs.Textbox(label = "Predicted Housing Prices")
# b. define image data type
output_module2 = gr.outputs.Image(label = "Output Image")
# you can define more output components
import pandas as pd
import numpy as np
housing = pd.read_csv('/housing-2.csv')
## 1. split data to get train and test set
from sklearn.model_selection import train_test_split
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=10)
## 2. clean the missing values
train_set_clean = train_set.dropna(subset=["total_bedrooms"])
train_set_clean
## 2. derive training features and training labels
train_labels = train_set_clean["median_house_value"].copy() # get labels for output label Y
train_features = train_set_clean.drop("median_house_value", axis=1) # drop labels to get features X for training set
## 4. scale the numeric features in training set
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler() ## define the transformer
scaler.fit(train_features) ## call .fit() method to calculate the min and max value for each column in dataset
train_features_normalized = scaler.transform(train_features)
train_features_normalized
## Step 1: training the data using decision tree algorithm
from sklearn.tree import DecisionTreeRegressor ## import the DecisionTree Function
tree_reg = DecisionTreeRegressor(random_state=42) ## Initialize the class
tree_reg.fit(train_features_normalized, train_labels) # feed the training data X, and label Y for supervised learning
### Step 2: make a prediction using tree model
training_predictions_trees = tree_reg.predict(train_features_normalized)
training_predictions_trees
def predict_house(input1, input2, input3, input4, input5, input6, input7, input8):
input_features = list(input1, input2, input3, input4, input5, input6, input7, input8)
scaler = MinMaxScaler() ## define the transformer
scaler.fit(input_features)
input_features_normalized = scaler.transform(input_features)
output_predictions = tree_reg.predict(input_features_normalized)
import matplotlib as plt
from numpy import asarray
from PIL import Image
train_set.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4, s=train_set["population"]/100, label="population", figsize=(10,7),
c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=True,sharex=False)
plt.legend()
plt.plot(input_features[0], input_features[1], 'r*', markersize=25)
graph = plt.show()
return output_predictions[0], graph
# Step 6.4: Put all three component together into the gradio's interface function
#gr.Label('CSCI4750/5750 Demo 3: Web Application for Housing Price Prediction')
#gr.HTML(show_label= 'CSCI4750/5750 Demo 3: Web Application for Housing Price Prediction')
gr.Interface(fn=predict_house,
inputs=[input_module1, input_module2, input_module3,
input_module4, input_module5, input_module6,
input_module7, input_module8],
outputs=[output_module1, output_module2], examples = examples ).launch() |