-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboston_inference.py
35 lines (27 loc) · 1.32 KB
/
boston_inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import numpy as np
from xgboost import XGBRegressor
from fastapi import FastAPI, Request
from pydantic import BaseModel
class BostonHouseFeatures(BaseModel):
crim: float # per capita crime rate by town
zn: float # proportion of residential land zoned for lots over 25,000 sq.ft.
indus: float # proportion of non-retail business acres per town
chas: float # Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
nox: float # nitric oxides concentration (parts per 10 million)
rm: float # average number of rooms per dwelling
age: float # proportion of owner-occupied units built prior to 1940
dis: float # weighted distances to five Boston employment centres
rad: float # index of accessibility to radial highways
tax: float # full-value property-tax rate per $10,000
ptratio: float # pupil-teacher ratio by town
b: float # 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
lstat: float # % lower status of the population
# uvicorn boston_inference:app --reload
app = FastAPI()
xgb = XGBRegressor()
xgb.load_model("xgbregressor_boston.json")
@app.post("/predict")
async def predict_house_price(features: BostonHouseFeatures):
X = np.array(list(features.dict().values()))
y = float(xgb.predict(np.expand_dims(X, axis=0))[0])
return {"price": y}