Lokesh1024 commited on
Commit
cac300f
·
verified ·
1 Parent(s): d786fd2

made deployment ready code

Browse files
Files changed (1) hide show
  1. app.py +72 -72
app.py CHANGED
@@ -1,72 +1,72 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
4
- import joblib
5
- import matplotlib.pyplot as plt
6
-
7
- # Load the model and columns
8
- lr_clf = joblib.load("C:/Users/vijay/OneDrive/Desktop/Banglore Housing Project/banglore_home_prices_model.pkl")
9
- X_columns = pd.read_csv("C:/Users/vijay/OneDrive/Desktop/Banglore Housing Project/dora.csv")
10
- OHE = pd.read_csv("C:/Users/vijay/OneDrive/Desktop/Banglore Housing Project/B5.csv")
11
- locations = OHE['location'].tolist()
12
-
13
- # Non-changeable variables
14
- bhk1 = 5
15
- bath1 = 5
16
-
17
- def predict_price(location, sqft, bath, bhk):
18
- loc_index = np.where(X_columns.columns == location)[0][0]
19
-
20
- x = np.zeros(len(X_columns.columns))
21
- x[0] = sqft
22
- x[1] = bath
23
- x[2] = bhk
24
- if loc_index >= 0:
25
- x[loc_index] = 1
26
-
27
- return lr_clf.predict([x])[0]
28
-
29
- def get_price_predictions(location, sqft, bhk):
30
- all_predictions = []
31
- for bhk_val in range(1, bhk+1):
32
- predictions = []
33
- for bath in range(1, 6):
34
- price_prediction = predict_price(location, sqft, bath, bhk_val)
35
- predictions.append(price_prediction)
36
- all_predictions.append(predictions)
37
- return all_predictions
38
-
39
- st.title('House Price Prediction')
40
-
41
- # Sidebar with area and location selection
42
- sqft = st.sidebar.slider('Select the area in sq meters:', min_value=500.0, max_value=3000.0, value=500.0)
43
- location = st.sidebar.selectbox('Select a location:', locations)
44
- bhk = st.sidebar.slider('Select BHK (1-5):', min_value=1, max_value=5)
45
- bath = st.sidebar.slider('Select Bathrooms (1-5):', min_value=1, max_value=5)
46
-
47
- estimated_price = predict_price(location, sqft, bath, bhk)
48
- st.write(f"Estimated Price per sqft : ₹ {estimated_price}")
49
-
50
- # Predict prices for different numbers of BHKs
51
- predictions = get_price_predictions(location, sqft, bhk1)
52
-
53
- # Display a spreadsheet-like table of prices
54
- prices_table = pd.DataFrame(predictions, columns=[f"{i+1} BHK" for i in range(bhk1)], index=[f"{i} Bathrooms" for i in range(1, bath1+1)])
55
- st.table(prices_table)
56
-
57
- # Plot graphs for each number of BHKs
58
- fig, axs = plt.subplots(bhk1, 1, figsize=(10, bhk1*5), sharex=True)
59
- bath_values = range(1, 6)
60
- colors = ['blue', 'green', 'red', 'purple', 'orange'] # Define different colors for each BHK
61
-
62
- for i in range(bhk1):
63
- axs[i].plot(bath_values, predictions[i], label=f'{i+1} BHK', color=colors[i]) # Use a different color for each BHK
64
- axs[i].set_ylabel('Predicted Price per sqft (in ₹)')
65
- axs[i].set_title(f'Predicted Price for {i+1} BHK (in ₹)')
66
- axs[i].legend(loc='center left', bbox_to_anchor=(1, 0.5)) # Position legend to the right of the graph
67
-
68
- # Set common x-axis label
69
- fig.text(0.5, 0.04, 'Number of Bathrooms', ha='center', va='center')
70
-
71
- plt.tight_layout(pad=3.0)
72
- st.pyplot(fig)
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Load the model and columns
8
+ lr_clf = joblib.load("banglore_home_prices_model.pkl")
9
+ X_columns = pd.read_csv("dora.csv")
10
+ OHE = pd.read_csv("B5.csv")
11
+ locations = OHE['location'].tolist()
12
+
13
+ # Non-changeable variables
14
+ bhk1 = 5
15
+ bath1 = 5
16
+
17
+ def predict_price(location, sqft, bath, bhk):
18
+ loc_index = np.where(X_columns.columns == location)[0][0]
19
+
20
+ x = np.zeros(len(X_columns.columns))
21
+ x[0] = sqft
22
+ x[1] = bath
23
+ x[2] = bhk
24
+ if loc_index >= 0:
25
+ x[loc_index] = 1
26
+
27
+ return lr_clf.predict([x])[0]
28
+
29
+ def get_price_predictions(location, sqft, bhk):
30
+ all_predictions = []
31
+ for bhk_val in range(1, bhk+1):
32
+ predictions = []
33
+ for bath in range(1, 6):
34
+ price_prediction = predict_price(location, sqft, bath, bhk_val)
35
+ predictions.append(price_prediction)
36
+ all_predictions.append(predictions)
37
+ return all_predictions
38
+
39
+ st.title('House Price Prediction')
40
+
41
+ # Sidebar with area and location selection
42
+ sqft = st.sidebar.slider('Select the area in sq meters:', min_value=500.0, max_value=3000.0, value=500.0)
43
+ location = st.sidebar.selectbox('Select a location:', locations)
44
+ bhk = st.sidebar.slider('Select BHK (1-5):', min_value=1, max_value=5)
45
+ bath = st.sidebar.slider('Select Bathrooms (1-5):', min_value=1, max_value=5)
46
+
47
+ estimated_price = predict_price(location, sqft, bath, bhk)
48
+ st.write(f"Estimated Price per sqft : ₹ {estimated_price}")
49
+
50
+ # Predict prices for different numbers of BHKs
51
+ predictions = get_price_predictions(location, sqft, bhk1)
52
+
53
+ # Display a spreadsheet-like table of prices
54
+ prices_table = pd.DataFrame(predictions, columns=[f"{i+1} BHK" for i in range(bhk1)], index=[f"{i} Bathrooms" for i in range(1, bath1+1)])
55
+ st.table(prices_table)
56
+
57
+ # Plot graphs for each number of BHKs
58
+ fig, axs = plt.subplots(bhk1, 1, figsize=(10, bhk1*5), sharex=True)
59
+ bath_values = range(1, 6)
60
+ colors = ['blue', 'green', 'red', 'purple', 'orange'] # Define different colors for each BHK
61
+
62
+ for i in range(bhk1):
63
+ axs[i].plot(bath_values, predictions[i], label=f'{i+1} BHK', color=colors[i]) # Use a different color for each BHK
64
+ axs[i].set_ylabel('Predicted Price per sqft (in ₹)')
65
+ axs[i].set_title(f'Predicted Price for {i+1} BHK (in ₹)')
66
+ axs[i].legend(loc='center left', bbox_to_anchor=(1, 0.5)) # Position legend to the right of the graph
67
+
68
+ # Set common x-axis label
69
+ fig.text(0.5, 0.04, 'Number of Bathrooms', ha='center', va='center')
70
+
71
+ plt.tight_layout(pad=3.0)
72
+ st.pyplot(fig)