Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import gradio as gr
|
5 |
+
#import joblib
|
6 |
+
from sklearn.linear_model import LinearRegression
|
7 |
+
from sklearn.tree import DecisionTreeRegressor
|
8 |
+
from sklearn.ensemble import RandomForestRegressor
|
9 |
+
from sklearn.model_selection import StratifiedShuffleSplit
|
10 |
+
from sklearn.impute import SimpleImputer
|
11 |
+
from sklearn.pipeline import Pipeline
|
12 |
+
from sklearn.compose import ColumnTransformer
|
13 |
+
from sklearn.preprocessing import StandardScaler
|
14 |
+
from sklearn.preprocessing import OneHotEncoder
|
15 |
+
from sklearn.metrics import mean_squared_error
|
16 |
+
from sklearn.model_selection import cross_val_score
|
17 |
+
from sklearn.model_selection import RandomizedSearchCV
|
18 |
+
from sklearn.preprocessing import MinMaxScaler
|
19 |
+
from sklearn.model_selection import train_test_split
|
20 |
+
|
21 |
+
df = pd.read_csv('Housing.csv')
|
22 |
+
cat_columns = ['mainroad',
|
23 |
+
'guestroom', 'basement', 'hotwaterheating', 'airconditioning',
|
24 |
+
'prefarea']
|
25 |
+
|
26 |
+
def binary_mapping(x):
|
27 |
+
return x.map({'yes': 1, "no": 0})
|
28 |
+
|
29 |
+
df[cat_columns] = df[cat_columns].apply(binary_mapping)
|
30 |
+
|
31 |
+
|
32 |
+
ohe = OneHotEncoder(sparse=False, handle_unknown='error', drop='first')
|
33 |
+
ohe_df = pd.DataFrame(ohe.fit_transform(df[['furnishingstatus']]))
|
34 |
+
|
35 |
+
ohe_df.columns = ohe.get_feature_names(['status'])
|
36 |
+
|
37 |
+
df = pd.concat([df,ohe_df], axis=1)
|
38 |
+
df.drop(['furnishingstatus'], axis = 1, inplace = True)
|
39 |
+
df.head()
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
df_new = df.copy(deep=True)
|
44 |
+
num_columns = ['area', 'bedrooms', 'bathrooms', 'stories','parking']
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
scaler = MinMaxScaler().fit(df_new[num_columns])
|
49 |
+
df_new[num_columns] = scaler.transform(df_new[num_columns])
|
50 |
+
y = df_new.pop('price')
|
51 |
+
x = df_new
|
52 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
|
53 |
+
model = RandomForestRegressor()
|
54 |
+
model.fit(x_train, y_train)
|
55 |
+
|
56 |
+
def prediction(properties):
|
57 |
+
print(properties)
|
58 |
+
df = pd.DataFrame(properties, columns=x_test.columns)
|
59 |
+
print(df)
|
60 |
+
df = df[x_test.columns].iloc[0].to_frame().T
|
61 |
+
df[num_columns] = scaler.transform(df[num_columns])
|
62 |
+
return model.predict(df)
|
63 |
+
example = pd.DataFrame([7420, 4, 2, 3, 1, 0, 0, 0, 1, 2, 1, 0, 0]).T
|
64 |
+
example.columns = x_test.columns
|
65 |
+
|
66 |
+
demo = gr.Interface(
|
67 |
+
prediction,
|
68 |
+
[
|
69 |
+
gr.Dataframe(
|
70 |
+
headers=['area', 'bedrooms', 'bathrooms', 'stories', 'mainroad', 'guestroom',
|
71 |
+
'basement', 'hotwaterheating', 'airconditioning', 'parking', 'prefarea',
|
72 |
+
'status_semi-furnished', 'status_unfurnished'],
|
73 |
+
datatype=["number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number"],
|
74 |
+
)
|
75 |
+
|
76 |
+
],
|
77 |
+
"number",
|
78 |
+
description="Enter The Properties Of The Home",
|
79 |
+
title="California Housing Prices Prediction",
|
80 |
+
examples=[example],
|
81 |
+
|
82 |
+
|
83 |
+
)
|
84 |
+
|
85 |
+
demo.launch()
|