0

**the code predict the house price with polynomial regression model and fastapi **`

make class have an one parameter and have a 4 value

class features(BaseModel):
    X2_house_age: float
    X3_distance_to_the_nearest_MRT_station: float
    X4_number_of_convenience_stores: float
    year: int

#The train_plynomial_model is a function that takes the Features and returns polynomial model

@app.post("/predict")

def train_plynomial_model(req : features):
    X2_house_age=req.X2_house_age
    X3_distance_to_the_nearest_MRT_station=req.X3_distance_to_the_nearest_MRT_station
    X4_number_of_convenience_stores=req.X4_number_of_convenience_stores
    year=req.year
    features = list([X2_house_age,
                    X3_distance_to_the_nearest_MRT_station,
                    X4_number_of_convenience_stores,
                    year
                    ])
    poly = PolynomialFeatures(2)
    poly_x_train = poly.fit_transform(features)
    newfeatures= model.fit(poly_x_train, model.y_train)
    newfeature=newfeatures.reshape(-1, 1)
    return(newfeature)

The predict is a function that predict the house price

async def predict(train_plynomial_model):
   newfeature=train_plynomial_model.newfeatures
   prediction = model.predict([ [ newfeature]  ])
    return {'you can sell your house for {} '.format(prediction)}

``

I tried to put this sentencenewfeature=newfeature.reshape(-1, 1)

  • Does this answer your question? [Can we use Pydantic models (Basemodel) directly inside model.predict() using FastAPI, and if not ,why?](https://stackoverflow.com/questions/71849683/can-we-use-pydantic-models-basemodel-directly-inside-model-predict-using-fas) – Chris Nov 08 '22 at 15:46
  • See related answers [here](https://stackoverflow.com/a/71874694/17865804) and [here](https://stackoverflow.com/a/72755158/17865804). – Chris Nov 08 '22 at 15:47
  • i try it but doesn't fix it – Hanan Zatar Nov 08 '22 at 16:55

1 Answers1

0

You should change features array not newfeatures.
Try reshaping like this and using a numpy array :

features = np.array(features).reshape((len(features), 1))