HousingPrediction / housing_pred.py
glin2's picture
creating new file
a942870
raw
history blame
2.25 kB
import pandas as pd
import numpy as np
import gradio as gr
housing = pd.read_csv("housing.csv")
housing.head()
def pred(input1, input2, input3, input4, input5, input6, input7, input8):
## 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
from sklearn.linear_model import LinearRegression ## import the LinearRegression Function
lin_reg = LinearRegression() ## Initialize the class
lin_reg.fit(train_features_normalized, train_labels)
#testing array
testing = np.array([[1,1,1,1,1,1,1,1]])
normalized_testing = scaler.transform(testing)
training_predictions = lin_reg.predict(normalized_testing)
return training_predictions
input_module1 = gr.inputs.Textbox(label = "Feature 1: ")
input_module2 = gr.inputs.Textbox(label = "Feature 2: ")
input_module3 = gr.inputs.Textbox(label = "Feature 3: ")
input_module4 = gr.inputs.Textbox(label = "Feature 4: ")
input_module5 = gr.inputs.Textbox(label = "Feature 5: ")
input_module6 = gr.inputs.Textbox(label = "Feature 6: ")
input_module7 = gr.inputs.Textbox(label = "Feature 7: ")
input_module8 = gr.inputs.Textbox(label = "Feature 8: ")
output_module = gr.outputs.Textbox(label = "Predicted housing price: ")
gr.Interface(fn=pred,
inputs=[input_module1,input_module2,input_module3,input_module4,input_module5,input_module6,input_module7,input_module8],
outputs=output_module).launch()