ValueError:Reshape your data either using array.reshape(-1, 1)if your data has a single feature or array.reshape(1, -1) if it contains a single sample

Question:

**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)

Asked By: Hanan Zatar

||

Answers:

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

features = np.array(features).reshape((len(features), 1))
Answered By: El Mehdi Agunaou