Maha commited on
Commit ·
56c732f
1
Parent(s): 93f3ec9
Add inference.py
Browse files- inference.py +35 -0
inference.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import numpy as np
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
# urls to both model and scaler
|
| 7 |
+
model_url = "https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/best_model.joblib"
|
| 8 |
+
scaler_url = "https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/scaler.joblib"
|
| 9 |
+
|
| 10 |
+
# download and load model
|
| 11 |
+
model_bytes = BytesIO(requests.get(model_url).content)
|
| 12 |
+
model = joblib.load(model_bytes)
|
| 13 |
+
|
| 14 |
+
# download and load scaler
|
| 15 |
+
scaler_bytes = BytesIO(requests.get(scaler_url).content)
|
| 16 |
+
scaler = joblib.load(scaler_bytes)
|
| 17 |
+
|
| 18 |
+
# feature names
|
| 19 |
+
features = ["MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup", "Latitude", "Longitude"]
|
| 20 |
+
|
| 21 |
+
# collect user input
|
| 22 |
+
print("Enter feature values!")
|
| 23 |
+
user_input = []
|
| 24 |
+
for feature in features:
|
| 25 |
+
val = float(input(f"Enter value for {feature}: "))
|
| 26 |
+
user_input.append(val)
|
| 27 |
+
|
| 28 |
+
# convert to array and scale
|
| 29 |
+
user_input = np.array(user_input).reshape(1, -1)
|
| 30 |
+
user_input_scaled = scaler.transform(user_input)
|
| 31 |
+
|
| 32 |
+
# predict and display
|
| 33 |
+
prediction = model.predict(user_input_scaled)[0]
|
| 34 |
+
predicted_price = prediction * 100000 # target is in 100000s (hundreds of thousands of dollars)
|
| 35 |
+
print(f"\nPredicted median house value: ${predicted_price:,.5f}")
|