**the code predict the house price with polynomial regression model and fastapi **`
make class have an one parameter and have a 4 value
JavaScript
x
6
1
class features(BaseModel):
2
X2_house_age: float
3
X3_distance_to_the_nearest_MRT_station: float
4
X4_number_of_convenience_stores: float
5
year: int
6
#The train_plynomial_model is a function that takes the Features and returns polynomial model
JavaScript
1
18
18
1
@app.post("/predict")
2
3
def train_plynomial_model(req : features):
4
X2_house_age=req.X2_house_age
5
X3_distance_to_the_nearest_MRT_station=req.X3_distance_to_the_nearest_MRT_station
6
X4_number_of_convenience_stores=req.X4_number_of_convenience_stores
7
year=req.year
8
features = list([X2_house_age,
9
X3_distance_to_the_nearest_MRT_station,
10
X4_number_of_convenience_stores,
11
year
12
])
13
poly = PolynomialFeatures(2)
14
poly_x_train = poly.fit_transform(features)
15
newfeatures= model.fit(poly_x_train, model.y_train)
16
newfeature=newfeatures.reshape(-1, 1)
17
return(newfeature)
18
The predict is a function that predict the house price
JavaScript
1
6
1
async def predict(train_plynomial_model):
2
newfeature=train_plynomial_model.newfeatures
3
prediction = model.predict([ [ newfeature] ])
4
return {'you can sell your house for {} '.format(prediction)}
5
6
“
I tried to put this sentencenewfeature=newfeature.reshape(-1, 1)
Advertisement
Answer
You should change features array not newfeatures.
Try reshaping like this and using a numpy array :
JavaScript
1
2
1
features = np.array(features).reshape((len(features), 1))
2