Spaces:
Runtime error
Runtime error
Bandisaiprasad
commited on
Commit
·
7055201
1
Parent(s):
5020360
app.py
Browse filesFiles regarding the radio
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
input_module1 = gr.inputs.Slider(-124.35, -114.35, step=5, label = "Longitude")
|
4 |
+
|
5 |
+
input_module2 = gr.inputs.Slider(32, 41, step=5, label = "Latitude")
|
6 |
+
|
7 |
+
input_module3 = gr.inputs.Slider(1, 52, step=5, label = "Housing_median_age (Year)")
|
8 |
+
|
9 |
+
input_module4 = gr.inputs.Slider(1, 39996, step=5, label = "Total_rooms")
|
10 |
+
|
11 |
+
input_module5 = gr.inputs.Slider(1, 6441, step=5, label = "Total_bedrooms")
|
12 |
+
|
13 |
+
input_module6 = gr.inputs.Slider(3, 35678, step=5, label = "Population")
|
14 |
+
|
15 |
+
input_module7 = gr.inputs.Slider(1, 6081, step=5, label = "Households")
|
16 |
+
|
17 |
+
input_module8 = gr.inputs.Slider(0, 15, step=5, label = "Median_income")
|
18 |
+
|
19 |
+
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]"]]
|
20 |
+
|
21 |
+
# Step 6.2: Define different output components
|
22 |
+
# a. define text data type
|
23 |
+
output_module1 = gr.outputs.Textbox(label = "Predicted Housing Prices")
|
24 |
+
|
25 |
+
# b. define image data type
|
26 |
+
output_module2 = gr.outputs.Image(label = "Output Image")
|
27 |
+
|
28 |
+
# you can define more output components
|
29 |
+
import pandas as pd
|
30 |
+
import numpy as np
|
31 |
+
|
32 |
+
housing = pd.read_csv('/housing-2.csv')
|
33 |
+
|
34 |
+
## 1. split data to get train and test set
|
35 |
+
from sklearn.model_selection import train_test_split
|
36 |
+
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=10)
|
37 |
+
|
38 |
+
## 2. clean the missing values
|
39 |
+
train_set_clean = train_set.dropna(subset=["total_bedrooms"])
|
40 |
+
train_set_clean
|
41 |
+
|
42 |
+
## 2. derive training features and training labels
|
43 |
+
train_labels = train_set_clean["median_house_value"].copy() # get labels for output label Y
|
44 |
+
train_features = train_set_clean.drop("median_house_value", axis=1) # drop labels to get features X for training set
|
45 |
+
|
46 |
+
|
47 |
+
## 4. scale the numeric features in training set
|
48 |
+
from sklearn.preprocessing import MinMaxScaler
|
49 |
+
scaler = MinMaxScaler() ## define the transformer
|
50 |
+
scaler.fit(train_features) ## call .fit() method to calculate the min and max value for each column in dataset
|
51 |
+
|
52 |
+
train_features_normalized = scaler.transform(train_features)
|
53 |
+
train_features_normalized
|
54 |
+
|
55 |
+
## Step 1: training the data using decision tree algorithm
|
56 |
+
from sklearn.tree import DecisionTreeRegressor ## import the DecisionTree Function
|
57 |
+
tree_reg = DecisionTreeRegressor(random_state=42) ## Initialize the class
|
58 |
+
tree_reg.fit(train_features_normalized, train_labels) # feed the training data X, and label Y for supervised learning
|
59 |
+
|
60 |
+
### Step 2: make a prediction using tree model
|
61 |
+
training_predictions_trees = tree_reg.predict(train_features_normalized)
|
62 |
+
training_predictions_trees
|
63 |
+
|
64 |
+
def predict_house(input1, input2, input3, input4, input5, input6, input7, input8):
|
65 |
+
input_features = list(input1, input2, input3, input4, input5, input6, input7, input8)
|
66 |
+
scaler = MinMaxScaler() ## define the transformer
|
67 |
+
scaler.fit(input_features)
|
68 |
+
input_features_normalized = scaler.transform(input_features)
|
69 |
+
output_predictions = tree_reg.predict(input_features_normalized)
|
70 |
+
|
71 |
+
import matplotlib as plt
|
72 |
+
from numpy import asarray
|
73 |
+
from PIL import Image
|
74 |
+
train_set.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4, s=train_set["population"]/100, label="population", figsize=(10,7),
|
75 |
+
c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=True,sharex=False)
|
76 |
+
plt.legend()
|
77 |
+
plt.plot(input_features[0], input_features[1], 'r*', markersize=25)
|
78 |
+
graph = plt.show()
|
79 |
+
return output_predictions[0], graph
|
80 |
+
|
81 |
+
# Step 6.4: Put all three component together into the gradio's interface function
|
82 |
+
#gr.Label('CSCI4750/5750 Demo 3: Web Application for Housing Price Prediction')
|
83 |
+
#gr.HTML(show_label= 'CSCI4750/5750 Demo 3: Web Application for Housing Price Prediction')
|
84 |
+
gr.Interface(fn=predict_house,
|
85 |
+
inputs=[input_module1, input_module2, input_module3,
|
86 |
+
input_module4, input_module5, input_module6,
|
87 |
+
input_module7, input_module8],
|
88 |
+
outputs=[output_module1, output_module2], examples = examples ).launch()
|