lab04 / app.py
Praneeth383's picture
Update app.py
5011e64
raw
history blame
2.37 kB
def house_price_prediction(ft1,ft2,ft3,ft4,ft5,ft6,ft7,ft8):
# output=1
import pandas as pd
housing=pd.read_csv("housing.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
#model training
from sklearn.linear_model import LinearRegression ## import the LinearRegression Function
lin_reg = LinearRegression() ## Initialize the class
lin_reg.fit(train_features_normalized, train_labels) # feed the training data X, and label Y for supervised learning
#model prediction
import numpy as np
test_features=np.array([[ft1,ft2,ft3,ft4,ft5,ft6,ft7,ft8]])
training_predictions = lin_reg.predict(test_features)
return training_predictions
#return output
import gradio as gr
ip1 = gr.inputs.Slider(-124.35, -114.35, step=5, label = "Longitude")
ip2 = gr.inputs.Slider(32,41, step=5, label = "Latitude")
ip3 = gr.inputs.Slider(1,52, step=5, label = "Housing_median_age (Year)")
ip4 = gr.inputs.Slider(1,39996, step=5, label = "Total_rooms")
ip5 = gr.inputs.Slider(1,6441, step=5, label = "Total_bedrooms")
ip6 = gr.inputs.Slider(3,35678, step=5, label = "Population")
ip7 = gr.inputs.Slider(1,6081, step=5, label = "Households")
ip8 = gr.inputs.Slider(0,15, step=5, label = "Median_income")
op_module = gr.outputs.Textbox(label = "Output")
gr.Interface(fn=house_price_prediction,
inputs=[ip1, ip2, ip3,
ip4, ip5, ip6,
ip7,ip8],
outputs=[op_module]
).launch(debug= True)
op_module = gr.outputs.Textbox(label = "Output")