Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Random forest predicting wrong output shape

I am trying to build a regression model using Randomforest. My training data shape :

X_train.shape = (8,7) # 8 is the no. of samples -- 7 is the number of features
y_train.shape = (8,100) # 8 is the no. of samples -- 100 is the output array size

After fitting the model

model = RandomForestRegressor(n_estimators = 300, random_state=30)
model.fit(X, y)

And while predicting the output for a single new sample

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

X_test.shape = (1,7)

I am getting an output shape of (8,100) instead of (1,100). What is the issue here?

>Solution :

Maybe you are passing X_train to the predict method and not X_test, check you code for typos.

I verified this by creating a model based on random data. It is working as expected.

enter image description here

Here you go

from sklearn.ensemble import RandomForestRegressor 
import sklearn
print(sklearn.__version__)

X_train = np.random.random((8,7))
y_train = np.random.random((8,100))
print(X_train.shape, y_train.shape)

model = RandomForestRegressor(n_estimators = 300, random_state=30)
model.fit(X_train, y_train)

X_test = np.random.random((1,7))
predictions = model.predict(X_test) # Make sure it is X_test and not X_train
print(predictions.shape)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading