|
import pickle |
|
import numpy as np |
|
|
|
|
|
def load_model_and_features(model_path, feature_path): |
|
|
|
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): |
|
|
|
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 test_house_price_predictions(): |
|
|
|
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, "expected": 94}, |
|
{"location": "Banaswadi", "sqft": 1500, "bath": 3, "bhk": 3, "expected": 118}, |
|
{"location": "Basavangudi", "sqft": 1800, "bath": 3, "bhk": 4, "expected": 142}, |
|
{"location": "Nonexistent Location", "sqft": 1000, "bath": 2, "bhk": 3, "expected": 79}, |
|
{"location": "Electronic City Phase II", "sqft": 1056, "bath": 2, "bhk": 2, "expected": 83}, |
|
{"location": "Chikka Tirupathi", "sqft": 800, "bath": 2, "bhk": 2, "expected": 63} |
|
] |
|
|
|
|
|
for case in test_cases: |
|
location = case["location"] |
|
sqft = case["sqft"] |
|
bath = case["bath"] |
|
bhk = case["bhk"] |
|
expected = case["expected"] |
|
|
|
try: |
|
predicted_price = predict_price(location, sqft, bath, bhk, model, feature_names) |
|
assert round(predicted_price / 10) == expected, ( |
|
f"Failed for Location: {location}, " |
|
f"Expected: {expected}, Got: {predicted_price/10:.0f} lakhs" |
|
) |
|
print(f"Test Passed: Location: {location}, Predicted: {predicted_price/10:.0f} lakhs") |
|
except Exception as e: |
|
print(f"Prediction failed for Location: {location}, Error: {e}") |
|
|
|
|
|
if __name__ == "__main__": |
|
test_house_price_predictions() |
|
|