|
import pickle |
|
import numpy as np |
|
|
|
def load_model_and_features(model_path, feature_path): |
|
""" |
|
Load the trained model and feature names from pickle files. |
|
|
|
Args: |
|
model_path (str): Path to the trained model pickle file. |
|
feature_path (str): Path to the feature names pickle file. |
|
|
|
Returns: |
|
tuple: (trained model, feature names) |
|
""" |
|
|
|
with open(model_path, "rb") as file: |
|
model = pickle.load(file) |
|
|
|
|
|
with open(feature_path, "rb") as file: |
|
feature_names = pickle.load(file) |
|
|
|
return model, feature_names |
|
|
|
def predict_price(location, sqft, bath, bhk, model, feature_names): |
|
""" |
|
Predict the price using the trained model. |
|
|
|
Args: |
|
location (str): Location name. |
|
sqft (float): Total square footage. |
|
bath (int): Number of bathrooms. |
|
bhk (int): Number of bedrooms. |
|
model: Trained model object. |
|
feature_names (list): List of feature names. |
|
|
|
Returns: |
|
float: Predicted price. |
|
""" |
|
|
|
x = np.zeros(len(feature_names)) |
|
|
|
|
|
if 'total_sqft' in feature_names: |
|
x[feature_names.index('total_sqft')] = sqft |
|
if 'bath' in feature_names: |
|
x[feature_names.index('bath')] = bath |
|
if 'bhk' in feature_names: |
|
x[feature_names.index('bhk')] = bhk |
|
|
|
|
|
if location in feature_names: |
|
loc_index = feature_names.index(location) |
|
x[loc_index] = 1 |
|
|
|
|
|
return model.predict([x])[0] |
|
|
|
def main(): |
|
|
|
model_path = "models/lr_regg.pkl" |
|
feature_path = "models/feature_names.pkl" |
|
|
|
|
|
model, feature_names = load_model_and_features(model_path, feature_path) |
|
|
|
|
|
test_cases = [ |
|
{"location": "Whitefield", "sqft": 1200, "bath": 2, "bhk": 2}, |
|
{"location": "Banaswadi", "sqft": 1500, "bath": 3, "bhk": 3}, |
|
{"location": "Basavangudi", "sqft": 1800, "bath": 3, "bhk": 4}, |
|
{"location": "Nonexistent Location", "sqft": 1000, "bath": 2, "bhk": 3}, |
|
{"location": "Electronic City Phase II", "sqft": 1056, "bath": 2, "bhk": 2}, |
|
{"location": "Chikka Tirupathi", "sqft": 800, "bath": 2, "bhk": 2} |
|
] |
|
|
|
print("\nPredictions:") |
|
for case in test_cases: |
|
location = case["location"] |
|
sqft = case["sqft"] |
|
bath = case["bath"] |
|
bhk = case["bhk"] |
|
|
|
try: |
|
predicted_price = predict_price(location, sqft, bath, bhk, model, feature_names) |
|
print(f"Location: {location}, Sqft: {sqft}, Bath: {bath}, BHK: {bhk} -> Predicted Price: {predicted_price/10:.0f} lakhs") |
|
except Exception as e: |
|
print(f"Prediction failed for Location: {location}, Error: {e}") |
|
|
|
if __name__ == "__main__": |
|
main() |