-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
53 lines (36 loc) · 1.41 KB
/
Model.py
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
#Import Libraries
import numpy as np
import pandas as pd
import joblib
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
#load data
df = pd.read_csv("data/ohe_data_reduce_cat_class.csv")
# Split data
X= df.drop('price', axis=1)
y= df['price']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=51)
# feature scaling
sc = StandardScaler()
sc.fit(X_train)
X_train = sc.transform(X_train)
X_test = sc.transform(X_test)
###### Load Model
model = joblib.load('bangalore_house_price_prediction_rfr_model.pkl')
# it help to get predicted value of house by providing features value
def predict_house_price(bath,balcony,total_sqft_int,bhk,area_type,location):
x =np.zeros(len(X.columns)) # create zero numpy array, len = 107 as input value for model
# adding feature's value accorind to their column index
x[0]=bath
x[1]=balcony
x[2]=total_sqft_int
x[3]=bhk
if 'area_type'+area_type in X.columns:
area_type_index = np.where(X.columns=="area_type"+area_type)[0][0]
x[area_type_index] =1
if 'location_'+location in X.columns:
loc_index = np.where(X.columns=="location_"+location)[0][0]
x[loc_index] =1
# feature scaling
x = sc.transform([x])[0] # give 2d np array for feature scaling and get 1d scaled np array
return model.predict([x])[0] # return the predicted value by train XGBoost model