Spaces:
Sleeping
Sleeping
creating new file
Browse files- housing_pred.py +54 -0
housing_pred.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
housing = pd.read_csv("housing.csv")
|
6 |
+
housing.head()
|
7 |
+
|
8 |
+
def pred(input1, input2, input3, input4, input5, input6, input7, input8):
|
9 |
+
## 1. split data to get train and test set
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=10)
|
12 |
+
|
13 |
+
## 2. clean the missing values
|
14 |
+
train_set_clean = train_set.dropna(subset=["total_bedrooms"])
|
15 |
+
train_set_clean
|
16 |
+
|
17 |
+
## 2. derive training features and training labels
|
18 |
+
train_labels = train_set_clean["median_house_value"].copy() # get labels for output label Y
|
19 |
+
train_features = train_set_clean.drop("median_house_value", axis=1) # drop labels to get features X for training set
|
20 |
+
|
21 |
+
## 4. scale the numeric features in training set
|
22 |
+
from sklearn.preprocessing import MinMaxScaler
|
23 |
+
scaler = MinMaxScaler() ## define the transformer
|
24 |
+
scaler.fit(train_features) ## call .fit() method to calculate the min and max value for each column in dataset
|
25 |
+
|
26 |
+
train_features_normalized = scaler.transform(train_features)
|
27 |
+
train_features_normalized
|
28 |
+
|
29 |
+
from sklearn.linear_model import LinearRegression ## import the LinearRegression Function
|
30 |
+
lin_reg = LinearRegression() ## Initialize the class
|
31 |
+
lin_reg.fit(train_features_normalized, train_labels)
|
32 |
+
|
33 |
+
#testing array
|
34 |
+
testing = np.array([[1,1,1,1,1,1,1,1]])
|
35 |
+
normalized_testing = scaler.transform(testing)
|
36 |
+
|
37 |
+
training_predictions = lin_reg.predict(normalized_testing)
|
38 |
+
|
39 |
+
return training_predictions
|
40 |
+
|
41 |
+
input_module1 = gr.inputs.Textbox(label = "Feature 1: ")
|
42 |
+
input_module2 = gr.inputs.Textbox(label = "Feature 2: ")
|
43 |
+
input_module3 = gr.inputs.Textbox(label = "Feature 3: ")
|
44 |
+
input_module4 = gr.inputs.Textbox(label = "Feature 4: ")
|
45 |
+
input_module5 = gr.inputs.Textbox(label = "Feature 5: ")
|
46 |
+
input_module6 = gr.inputs.Textbox(label = "Feature 6: ")
|
47 |
+
input_module7 = gr.inputs.Textbox(label = "Feature 7: ")
|
48 |
+
input_module8 = gr.inputs.Textbox(label = "Feature 8: ")
|
49 |
+
|
50 |
+
output_module = gr.outputs.Textbox(label = "Predicted housing price: ")
|
51 |
+
|
52 |
+
gr.Interface(fn=pred,
|
53 |
+
inputs=[input_module1,input_module2,input_module3,input_module4,input_module5,input_module6,input_module7,input_module8],
|
54 |
+
outputs=output_module).launch()
|