Answers.Body: <p>Take a look at either of these great posts about negative R2 values.</p>
  <ol>
  <li><a href="https://stats.stackexchange.com/questions/183265/what-does-negative-r-squared-mean">What does negative R-squared mean?</a></li>
  <li><a href="https://stats.stackexchange.com/a/12991/296394">When is R squared negative?</a></li>
  </ol>
  <p><strong>TLDR</strong> is that your model is poorly fit to the data.</p>
  <p>From looking at the code you attached I would try reducing the number of <code>x</code> 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 <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html#sklearn.feature_selection.RFE" rel="nofollow noreferrer">RFE</a> or recursive feature elimination and cross-validated <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFECV.html#sklearn.feature_selection.RFECV" rel="nofollow noreferrer">RFECV</a>.</p>
  <p>You would just need to do something like this.</p>
  <pre><code>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 
  selector.support_
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-28T03:44:15.207Z
Answers.LastActivityDate: 2021-07-28T03:44:15.207Z
Answers.UserDisplayName: David
Body: <p>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.</p>
  <p>My Data(Capture Image):
  <a href="https://i.sstatic.net/vEx4Q.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vEx4Q.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/6jyP7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6jyP7.png" alt="enter image description here" /></a></p>
  <p>My Code:</p>
  <pre><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&quot;/gdrive/MyDrive/Coal_Inert_Case/Inert_Case_1.csv&quot;) 
  
  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(&quot;RMSE:&quot;, rmse)
  print(&quot;R2 : &quot;, r2_y_predict)
  </code></pre>
  <p>The output (RMSE, R2) is:</p>
  <pre><code>RMSE: 0.6913667737213217
  R2 :  -7294765918428.414
  </code></pre>
  
Tags: <machine-learning>
Title: What is the meaning of R2 appearing as a negative in the RandomForestRegressor?
UserDisplayName: grapeorange
ThreadText: # What is the meaning of R2 appearing as a negative in the RandomForestRegressor?
  
  (Asked by: username_0 on 2021-07-28)
  
  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](https://i.sstatic.net/vEx4Q.png)](https://i.sstatic.net/vEx4Q.png)
  
  [![enter image description here](https://i.sstatic.net/6jyP7.png)](https://i.sstatic.net/6jyP7.png)
  
  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
  
  ```
  
  **Tags:** machine-learning
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-07-28. Score: 0 | Accepted answer
  
  Take a look at either of these great posts about negative R2 values.
  
  1. [What does negative R-squared mean?](https://stats.stackexchange.com/questions/183265/what-does-negative-r-squared-mean)
  2. [When is R squared negative?](https://stats.stackexchange.com/a/12991/296394)
  
  **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](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html#sklearn.feature_selection.RFE) or recursive feature elimination and cross-validated [RFECV](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFECV.html#sklearn.feature_selection.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 
  selector.support_
  
  ```
  
  
  ---
  
  