Machine learning model was created by reading an Excel file where data was stored. I applied RandomForestRegressor to create a model that predicts the size of the sieve particles according to pressure, but the value of R2 is too large negative. I found out through googling that R2 can be negative, but I don't know what it means to have such a large negative. When I applied the same amount of different data to the designed model, R2 showed a result that was close to 1, but I don't know why this data is only large negative. RMSE and SCORE scores are good, but I don't understand if only R2 scores are bad... I would appreciate it if you could let me know what is the problem and what to consider.

My Data(Capture Image): enter image description here

enter image description here

My Code:

import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from google.colab import drive 
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score 

drive.mount('/gdrive', force_remount=True)

data = pd.read_csv(r"/gdrive/MyDrive/Coal_Inert_Case/Inert_Case_1.csv") 

x =data[['press1', 'press2', 'press3', 'press4', 'press5', 'press6', 'press7', 'press8', 'press9', 'press10', 'press11', 'press12', 'press13',
 'press14', 'press15', 'press16', 'press17', 'press18', 'press19', 'press20', 'press21',
 'press22', 'press23', 'press24', 'press25', 'press26', 'press27', 'press28', 'press29',
 'press30', 'press31', 'press32', 'press33', 'press34', 'press35', 'press36', 'press37',
 'press38', 'press39', 'press40', 'press41', 'press42', 'press43', 'press44', 'press45',
 'press46', 'press47', 'press48', 'press49', 'press50', 'press51', 'press52', 'press53']]

y = data[['Sieve 16000', 'Sieve 11000', 'Sieve 8000', 'Sieve 5600', 'Sieve 4000',
       'Sieve 2800', 'Sieve 2000', 'Sieve 1400', 'Sieve 1000', 'Sieve 710',
       'Sieve 500', 'Sieve 355', 'Sieve 250', 'Sieve 180', 'Sieve 125',
       'Sieve 90', 'Sieve 63', 'Sieve 44', 'Sieve 31', 'Sieve 0']] 

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3,random_state= 42)

forest = RandomForestRegressor(n_estimators=1000,random_state= 42) 
forest.fit(X_train, y_train) 
y_pred = forest.predict(X_test)

mse = mean_squared_error(y_test, y_pred) 
rmse = np.sqrt(mse)
r2_y_predict = r2_score(y_test, y_pred)

print("RMSE:", rmse)
print("R2 : ", r2_y_predict)

The output (RMSE, R2) is:

RMSE: 0.6913667737213217
R2 :  -7294765918428.414

1 Answer 1


Take a look at either of these great posts about negative R2 values.

  1. What does negative R-squared mean?
  2. When is R squared negative?

TLDR is that your model is poorly fit to the data.

From looking at the code you attached I would try reducing the number of x features you are using. It is possible that there is multicollinearity or some feature just are not useful. Using sklearn you can use recursive feature elimination RFE or recursive feature elimination and cross-validated RFECV.

You would just need to do something like this.

from sklearn.feature_selection import RFE

# fit the selector
selector = RFE(forest, n_features_to_select=5, step=1)
selector.fit(X_train, y_train) 

# print the top features 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .