gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

转载 作者:太空狗 更新时间:2023-10-30 02:04:54 24 4
gpt4 key购买 nike

我正在尝试使用原始值 salaries 计算来 self 的 sci-kit 学习模型的预测 y_train_actual 的均方误差。

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

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

如何为 sklearn.netrucs.mean_squared_error() 转换为正确的数组类型?

代码

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)

错误

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'

代码

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)

错误

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

代码

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

结果

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

打印 y_train[:10]

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

打印工资[:10]

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

打印列表(薪水)[:10]

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

打印 len(y_train)

244768

打印 len(薪水)

244768

最佳答案

TypeError 问题源于 salaries 是一个字符串列表,而 y_train_actual 是一个 float 列表。那些不能减去。

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

关于python - 类型错误 : unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com