Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

I am trying to calculate the Mean Squared Error of the predictions y_train_actual from my sci-kit learn model with the original values salaries.

Problem: However with mean_squared_error(y_train_actual, salaries), I am getting the error TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'. Using list(salaries) instead of salaries as the 2nd parameter gives the same error.

With mean_squared_error(y_train_actual, y_valid_actual) I am getting the error Found array with dim 40663. Expected 244768

How can I convert to the correct array types for sklearn.netrucs.mean_squared_error()?

Code

from sklearn.metrics import mean_squared_error

y_train_actual = [ np.exp(float(row)) for row in y_train ]
print mean_squared_error(y_train_actual, salaries)

Error

TypeError                                 Traceback (most recent call last)
<ipython-input-144-b6d4557ba9c5> in <module>()
      3 y_valid_actual = [ np.exp(float(row)) for row in y_valid ]
      4 
----> 5 print mean_squared_error(y_train_actual, salaries)
      6 print mean_squared_error(y_train_actual, y_valid_actual)

C:\Python27\lib\site-packages\sklearn\metrics\metrics.pyc in mean_squared_error(y_true, y_pred)
   1462     """
   1463     y_true, y_pred = check_arrays(y_true, y_pred)
-> 1464     return np.mean((y_pred - y_true) ** 2)
   1465 
   1466 

TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

Code

y_train_actual = [ np.exp(float(row)) for row in y_train ]
y_valid_actual = [ np.exp(float(row)) for row in y_valid ]

print mean_squared_error(y_train_actual, y_valid_actual)

Error

ValueError                                Traceback (most recent call last)
<ipython-input-146-7fcd0367c6f1> in <module>()
      4 
      5 #print mean_squared_error(y_train_actual, salaries)
----> 6 print mean_squared_error(y_train_actual, y_valid_actual)

C:\Python27\lib\site-packages\sklearn\metrics\metrics.pyc in mean_squared_error(y_true, y_pred)
   1461 
   1462     """
-> 1463     y_true, y_pred = check_arrays(y_true, y_pred)
   1464     return np.mean((y_pred - y_true) ** 2)
   1465 

C:\Python27\lib\site-packages\sklearn\utils\validation.pyc in check_arrays(*arrays, **options)
    191         if size != n_samples:
    192             raise ValueError("Found array with dim %d. Expected %d"
--> 193                              % (size, n_samples))
    194 
    195         if not allow_lists or hasattr(array, "shape"):

ValueError: Found array with dim 40663. Expected 244768

Code

print type(y_train)
print type(y_train_actual)
print type(salaries)

Result

<type 'list'>
<type 'list'>
<type 'tuple'>

print y_train[:10]

[10.126631103850338, 10.308952660644293, 10.308952660644293, 10.221941283654663, 10.126631103850338, 10.126631103850338, 11.225243392518447, 9.9987977323404529, 10.043249494911286, 11.350406535472453]

print salaries[:10]

('25000', '30000', '30000', '27500', '25000', '25000', '75000', '22000', '23000', '85000')

print list(salaries)[:10]

['25000', '30000', '30000', '27500', '25000', '25000', '75000', '22000', '23000', '85000']

print len(y_train)

244768

print len(salaries)

244768
like image 993
Nyxynyx Avatar asked May 02 '13 04:05

Nyxynyx


People also ask

How do I fix NumPy Ndarray?

The 'numpy. ndarray' object is not callable error occurs when you try to access the NumPy array as a function using the round brackets () instead of square brackets [] to retrieve the array elements. To fix this issue, use an array indexer with square brackets to access the elements of the array.

What is NumPy Ndarray type?

Ndarray is the n-dimensional array object defined in the numpy which stores the collection of the similar type of elements. In other words, we can define a ndarray as the collection of the data type (dtype) objects. The ndarray object can be accessed by using the 0 based indexing.

What does unsupported operand type S mean in Python?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num . Here is an example of how the error occurs.

Is Ndarray same as NumPy array?

NumPy is used to work with arrays. The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.


1 Answers

The TypeError problem stems from salaries being a list of strings while y_train_actual is a list of floats. Those cannot be subtracted.

For your second error, you should make sure that both arrays are of the same size, otherwise it cannot subtract them.

like image 89
fgb Avatar answered Sep 20 '22 12:09

fgb