Python 类型错误:不支持的操作数类型 -:'numpy.ndarray' 和 'numpy.ndarray'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16330366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:18:41  来源:igfitidea点击:

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

pythonpython-2.7numpyscipyscikit-learn

提问by Nyxynyx

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

我正在尝试y_train_actual使用原始值计算来自我的 sci-kit 学习模型的预测的均方误差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 salariesas the 2nd parameter gives the same error.

问题:但是mean_squared_error(y_train_actual, salaries),我收到了错误TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'。使用list(salaries)而不是salaries作为第二个参数会产生相同的错误。

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

随着mean_squared_error(y_train_actual, y_valid_actual)我收到错误Found array with dim 40663. Expected 244768

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

如何转换为正确的数组类型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]

打印 y_train[:10]

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

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

print salaries[:10]

打印工资[:10]

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

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

print list(salaries)[:10]

打印清单(工资)[:10]

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

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

print len(y_train)

打印 len(y_train)

244768

print len(salaries)

打印镜头(工资)

244768

采纳答案by fgb

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

TypeError问题从工资中是一个字符串列表,而y_train_actual是浮动的列表茎。那些不能减去。

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

对于第二个错误,您应该确保两个数组的大小相同,否则无法减去它们。