我是 Numpy 的新手,我一直在尝试获取从另一个数组派生的数组的平均值。
这是给我错误的代码:“输入类型不支持 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))
请问我缺少什么
我猜完整的错误消息看起来像这样:
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
来自数据帧,显然包含“E”等字符串。 所以它是 object
dtype。 即使您选择相似的值,也不能取“平均值”。
average
取值的总和并除以计数。 sum
对于 Python 字符串来说是连接,而不是某种数学。
In [754]: np.sum(np.array(['A','B','C','A'],dtype=object))
Out[754]: 'ABCA'
我发现使用
evidently.ai时的
UFuncTypeError
与作为 reference_data 或 current_data 传递的数据帧的索引奇怪地相关。如果您的数据帧有一个命名索引,它将导致错误:“UFuncTypeError:ufunc'multiply'不包含具有签名匹配类型的循环(dtype('解决方案:要解决此问题,请从数据帧中删除(删除)索引:
x = df.copy()
x.reset_index(drop=True, inplace=True) # <- remove index
report = Report(metrics=[ColumnDriftMetric(column_name="premium")]) # 'premium' is an arbitrary feature in my dataset
report.run(reference_data=x, current_data=x) # <- note: you should set reference_data and current_data accordingly
report
希望这有帮助!