File size: 4,878 Bytes
56ead55
 
 
 
 
 
c2261d4
56ead55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acae2bb
56ead55
 
 
 
 
 
acae2bb
56ead55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rcParams["figure.figsize"] = (20, 10)

path = 'bengaluru_house_prices.csv'
df = pd.read_csv(path)

df = df.drop(['area_type','society','balcony','availability'], axis = 'columns')

df=df.dropna()
df['BHK'] = df['size'].apply(lambda x: int(x.split(' ')[0]))


def isfloat(x):
  token = x.split('-')
  if len(token)==2:
    return (float(token[0])+float(token[1]))/2
  try:
    return float(x)
  except:
    return None


df['total_sqft'] = df['total_sqft'].apply(isfloat)

df=df.drop(['size'], axis = 'columns')


df['price_per_sqft'] = df['price']*100000/df['total_sqft']


df.location = df.location.apply(lambda x: x.strip())
loc_stats = df.groupby('location')['location'].agg('count').sort_values(ascending = False)


len(loc_stats[loc_stats <= 10])

loc_stats_ten = loc_stats[loc_stats<=10]


df.location = df.location.apply(lambda x: 'other' if x in loc_stats_ten else x)


df = df[~(df.total_sqft/df.BHK < 300)]


def rem_out(df):
  df_out = pd.DataFrame()
  for key, subdf in df.groupby('location'):
    mu = np.mean(subdf.price_per_sqft)
    std = np.std(subdf.price_per_sqft)
    dft = subdf[(subdf.price_per_sqft > (mu-std)) & (subdf.price_per_sqft <= (mu+std))]
    df_out = pd.concat([df_out, dft], ignore_index = True)
  return df_out

df = rem_out(df);


def remove_outlier(df):
  exclude = np.array([])
  for location, location_df in df.groupby('location'):
    bhk_stat = {}
    for BHK, bhk_df in location_df.groupby('BHK'):
      bhk_stat[BHK] = {
          'mean' : np.mean(bhk_df.price_per_sqft),
          'std' : np.std(bhk_df.price_per_sqft),
          'count' : bhk_df.shape[0]
      }
    # print(bhk_stat)
    for BHK, bhk_df in location_df.groupby('BHK'):
      stat = bhk_stat.get(BHK-1)
      # print(stat)
      if stat and stat['count']>5:
        exclude = np.append(exclude, bhk_df[bhk_df.price_per_sqft<(stat['mean'])].index.values)
  return df.drop(exclude, axis='index')

df = remove_outlier(df)

df = df[df.bath < df.BHK+2]

df = df.drop(['price_per_sqft'], axis = 'columns')

dummies = pd.get_dummies(df.location)

df = pd.concat([df, dummies.drop('other', axis = 'columns')], axis = 'columns')

df = df.drop('location', axis = 'columns')

x = df.drop('price', axis = 'columns')

y = df.price

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 10)

from sklearn.linear_model import LinearRegression
lr_clf = LinearRegression()
lr_clf.fit(X_train, y_train)
lr_clf.score(X_test, y_test)

from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import cross_val_score

cv = ShuffleSplit(n_splits = 5, test_size = 0.2, random_state = 10)

cross_val_score(LinearRegression(), x, y, cv = cv)

from sklearn.model_selection import GridSearchCV

from sklearn.linear_model import Lasso
from sklearn.tree import DecisionTreeRegressor

def find_best_model(x, y):
  algos = {
      'linear_reg' : {
          'model' : LinearRegression(),
          'params' : {
              'fit_intercept': [True, False],
              'copy_X': [True, False],
              'n_jobs': [None, -1],
              'positive': [True, False]
          }
      },
      'lasso' : {
          'model' : Lasso(),
          'params' : {
              'alpha' : [1,2],
              'selection' : ['random', 'cyclic']
          }
      },
      'dec_tree' : {
          'model' : DecisionTreeRegressor(),
          'params' : {
              'criterion': ['friedman_mse', 'squared_error', 'poisson', 'absolute_error'],
              'splitter': ['best', 'random'],
          }
      }
  }
  scores = []
  cv = ShuffleSplit(n_splits = 5, test_size = 0.2, random_state = 10)
  for algo_name, config in algos.items():
    gs = GridSearchCV(config['model'], config['params'], cv = cv, return_train_score = False)
    gs.fit(x,y);
    scores.append({
        'model' : algo_name,
        'best_score' : gs.best_score_,
        'best_params' : gs.best_params_
    })
  return pd.DataFrame(scores, columns = ['model', 'best_score', 'best_params'])

find_best_model(x,y)

def predict_price_func(location, sqft, bath, bhk):
  loc_index = np.where(x.columns == location)[0][0]

  xdash = np.zeros(len(x.columns))
  xdash[0] = sqft
  xdash[1] = bath
  xdash[2] = bhk

  if loc_index >= 0:
    xdash[loc_index] = 1

  return lr_clf.predict([xdash])[0]

import gradio as gr

from gradio.components import Textbox, Number

interface = gr.Interface(
    fn=predict_price_func,
    inputs=[
        gr.inputs.Textbox(),  # For location (text)
        gr.inputs.Number(),  # For area (numeric)
        gr.inputs.Number(),  # For bedrooms (numeric)
        gr.inputs.Number()   # For bathrooms (numeric)
    ],
    outputs="text",
    theme="huggingface"
)

interface.launch()