Download inference.py from mahaqj/ml_assignment_3: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/inference.py
- Command line
-
hf download hf://mahaqj/ml_assignment_3/inference.py
-
curl -L -o inference.py https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/inference.py
1.18 kB
| import joblib | |
| import numpy as np | |
| import requests | |
| from io import BytesIO | |
| # urls to both model and scaler | |
| model_url = "https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/best_model.joblib" | |
| scaler_url = "https://huggingface.co/mahaqj/ml_assignment_3/resolve/main/scaler.joblib" | |
| # download and load model | |
| model_bytes = BytesIO(requests.get(model_url).content) | |
| model = joblib.load(model_bytes) | |
| # download and load scaler | |
| scaler_bytes = BytesIO(requests.get(scaler_url).content) | |
| scaler = joblib.load(scaler_bytes) | |
| # feature names | |
| features = ["MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup", "Latitude", "Longitude"] | |
| # collect user input | |
| print("Enter feature values!") | |
| user_input = [] | |
| for feature in features: | |
| val = float(input(f"Enter value for {feature}: ")) | |
| user_input.append(val) | |
| # convert to array and scale | |
| user_input = np.array(user_input).reshape(1, -1) | |
| user_input_scaled = scaler.transform(user_input) | |
| # predict and display | |
| prediction = model.predict(user_input_scaled)[0] | |
| predicted_price = prediction * 100000 # target is in 100000s (hundreds of thousands of dollars) | |
| print(f"\nPredicted median house value: ${predicted_price:,.5f}") |