Spaces:
Runtime error
Runtime error
File size: 2,773 Bytes
325589a |
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 |
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import gradio as gr
#import joblib
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RandomizedSearchCV
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
df = pd.read_csv('Housing.csv')
cat_columns = ['mainroad',
'guestroom', 'basement', 'hotwaterheating', 'airconditioning',
'prefarea']
def binary_mapping(x):
return x.map({'yes': 1, "no": 0})
df[cat_columns] = df[cat_columns].apply(binary_mapping)
ohe = OneHotEncoder(sparse=False, handle_unknown='error', drop='first')
ohe_df = pd.DataFrame(ohe.fit_transform(df[['furnishingstatus']]))
ohe_df.columns = ohe.get_feature_names(['status'])
df = pd.concat([df,ohe_df], axis=1)
df.drop(['furnishingstatus'], axis = 1, inplace = True)
df.head()
df_new = df.copy(deep=True)
num_columns = ['area', 'bedrooms', 'bathrooms', 'stories','parking']
scaler = MinMaxScaler().fit(df_new[num_columns])
df_new[num_columns] = scaler.transform(df_new[num_columns])
y = df_new.pop('price')
x = df_new
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
model = RandomForestRegressor()
model.fit(x_train, y_train)
def prediction(properties):
print(properties)
df = pd.DataFrame(properties, columns=x_test.columns)
print(df)
df = df[x_test.columns].iloc[0].to_frame().T
df[num_columns] = scaler.transform(df[num_columns])
return model.predict(df)
example = pd.DataFrame([7420, 4, 2, 3, 1, 0, 0, 0, 1, 2, 1, 0, 0]).T
example.columns = x_test.columns
demo = gr.Interface(
prediction,
[
gr.Dataframe(
headers=['area', 'bedrooms', 'bathrooms', 'stories', 'mainroad', 'guestroom',
'basement', 'hotwaterheating', 'airconditioning', 'parking', 'prefarea',
'status_semi-furnished', 'status_unfurnished'],
datatype=["number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number"],
)
],
"number",
description="Enter The Properties Of The Home",
title="California Housing Prices Prediction",
examples=[example],
)
demo.launch()
|