简体   繁体   English

ufunc 'divide' 不支持输入类型......尝试获取 NumPy 平均值时出现错误问题

[英]ufunc 'divide' not supported for the input types...... error problem while trying to get the NumPy average

I am new to Numpy and I have been trying to get the average of an array I derived from another array.我是 Numpy 的新手,我一直在尝试获取从另一个数组派生的数组的平均值。

This is the code that have been giving me error: "ufunc 'divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' "这是给我错误的代码:“ufunc 'divide' 不支持输入类型,并且根据转换规则 ''safe'' 无法将输入安全地强制转换为任何受支持的类型”

import numpy as np
import pandas as pd
​
cars = pd.read_csv('data/co2_emissions_canada.csv')
cars_makes = cars['Make'].to_numpy()
cars_models = cars['Model'].to_numpy()
cars_classes = cars['Vehicle Class'].to_numpy()
cars_engine_sizes = cars['Engine Size(L)'].to_numpy()
cars_cylinders = cars['Cylinders'].to_numpy()
cars_transmissions = cars['Transmission'].to_numpy()
cars_fuel_types = cars['Fuel Type'].to_numpy()
cars_fuel_consumption = cars['Fuel Consumption Comb (L/100 km)'].to_numpy()
cars_co2_emissions = cars['CO2 Emissions(g/km)'].to_numpy()
​
#the median of the cars_engine_sizes
print(np.median(cars_engine_sizes))

#the average fuel consumption for regular gasoline (Fuel Type = X), #premium gasoline (Z), ethanol (E), and diesel (D)? 

fuel_typesx=np.array(cars_fuel_types[cars_fuel_types=='X'])
print(np.average(fuel_typesx))

fuel_typesz=np.array(cars_fuel_types[cars_fuel_types=='Z'])
print(np.average(fuel_typesz))

fuel_typese=np.array(cars_fuel_types[cars_fuel_types=='E'])
print(np.average(fuel_typese))

please, what am i missing请问,我错过了什么

I'm guessing the FULL error message looks something like this:我猜完整的错误消息看起来像这样:

In [753]: np.average(np.array(['A','B','C','A'],dtype=object))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [753], in <cell line: 1>()
----> 1 np.average(np.array(['A','B','C','A'],dtype=object))

File <__array_function__ internals>:5, in average(*args, **kwargs)

File ~\anaconda3\lib\site-packages\numpy\lib\function_base.py:380, in average(a, axis, weights, returned)
    377 a = np.asanyarray(a)
    379 if weights is None:
--> 380     avg = a.mean(axis)
    381     scl = avg.dtype.type(a.size/avg.size)
    382 else:

File ~\anaconda3\lib\site-packages\numpy\core\_methods.py:191, in _mean(a, axis, dtype, out, keepdims, where)
    189         ret = ret.dtype.type(ret / rcount)
    190 else:
--> 191     ret = ret / rcount
    193 return ret

TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

cars_fuel_types comes from a dataframe, and evidently contains strings like 'E'. cars_fuel_types来自 dataframe,显然包含像“E”这样的字符串。 So it is object dtype.所以它是object Even if you select like values, you can't take an 'average'.即使你 select 喜欢值,你也不能取“平均值”。

average takes the sum of values and divides by the count. average取值的总和并除以计数。 sum for python strings is concatenation, not some sort of math. python 字符串的sum是连接,而不是某种数学。

In [754]: np.sum(np.array(['A','B','C','A'],dtype=object))
Out[754]: 'ABCA'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Numpy 类型错误:输入类型不支持 ufunc 'isfinite' - Python Numpy TypeError: ufunc 'isfinite' not supported for the input types numpy TypeError:输入类型不支持 ufunc &#39;invert&#39;,并且输入 - numpy TypeError: ufunc 'invert' not supported for the input types, and the inputs 符号矩阵和numpy使用错误“ TypeError:输入类型不支持ufunc&#39;isfinite&#39;。” - Symbolic matrix and numpy usage error “TypeError: ufunc 'isfinite' not supported for the input types..” Numpy TypeError:输入类型不支持 ufunc &#39;bitwise_and&#39;, - Numpy TypeError: ufunc 'bitwise_and' not supported for the input types, 输入类型不支持 ufunc &#39;isnan&#39; - ufunc 'isnan' not supported for the input types numpy“TypeError:输入类型不支持 ufunc 'bitwise_and'”并且无法将输入安全地强制转换为任何受支持的类型 - numpy "TypeError: ufunc 'bitwise_and' not supported for the input types" and the inputs could not be safely coerced to any supported types Folium TypeError:输入类型不支持 ufunc &#39;isnan&#39; - Folium TypeError: ufunc 'isnan' not supported for the input types TypeError: 输入类型不支持 ufunc 'bitwise_and' - TypeError: ufunc 'bitwise_and' not supported for the input types numpy &quot;TypeError: ufunc &#39;bitwise_and&#39; not supported for the input types&quot; 使用动态创建的布尔掩码时 - numpy "TypeError: ufunc 'bitwise_and' not supported for the input types" when using a dynamically created boolean mask 如何将hist用于列表并解决错误:输入类型不支持“ ufunc&#39;isnan&#39; - How to use hist for list and solve the error: "ufunc 'isnan' not supported for the input types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM