gis
gis

Reputation: 21

ValueError while calculating R_Square: Found array with dim 3. Estimator expected <= 2

I have trained and tested a neural network in which I have 2 variables as my dependent variables hence, in output, there are two NumPy arrays of train and test, with each NumPy array having 2 outputs.

import pandas as pd
import numpy as np

y_test = 
array([[242.14,  61.],
   [179.48, 140.54],
   [ 98.16,  36.05],
   ...,
   [187.73, 111.39],
   [ 52.15,  25.82],
   [119.92,  54.52]])

y_pred = 

array([[[178.8299  ],
    [ 73.47848 ]],

   [[216.90152 ],
    [116.74648 ]],

   [[215.67923 ],
    [ 47.019424]],

   ...,

   [[236.51259 ],
    [167.34752 ]],

   [[156.02226 ],
    [ 61.292656]],

   [[151.5344  ],
    [ 56.285583]]],

and I am trying to calculate R-Square value using the following code

from sklearn.metrics import r2_score
r2 = r2_score(y_test, y_pred)
r2 = round(r2,2)
print('R2 =',r2)

However, am getting the error of ValueError: Found array with dim 3. Estimator expected <= 2.

Upvotes: 0

Views: 36

Answers (1)

AshSmith88
AshSmith88

Reputation: 181

Your y_pred array has 3 dimensions by the looks of it.

Try removing a square bracket from the start and end...

y_pred = 

array([[178.8299  ],
    [ 73.47848 ]],

   [[216.90152 ],
    [116.74648 ]],

   [[215.67923 ],
    [ 47.019424]],

   ...,

   [[236.51259 ],
    [167.34752 ]],

   [[156.02226 ],
    [ 61.292656]],

   [[151.5344  ],
    [ 56.285583]],

Upvotes: 0

Related Questions