TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'q')

18 人关注

我正试图将高尔距离的实现应用于我的数据框架。虽然它在处理具有更多特征的同一数据集时很顺利,但这次当我调用Gower距离函数时却出现了错误。我从同一目录下的另一个.py代码中导入了Gower的函数。下面是我的代码。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import gower_function as gf
# Importing the dataset with pandas
dataset = pd.read_excel('input_partial.xlsx')
X = dataset.iloc[:, 1:].values
df = pd.DataFrame(X)
#obtaining gower distances of instances
Gower = gf.gower_distances(X)

并在执行后,我得到以下错误。

File "<ipython-input-10-6a4c39600b0e>", line 1, in <module>
Gower = gf.gower_distances(X)
File "C:\Users\...\Clustering\Section 24 - K-Means 
Clustering\gower_function.py", line 184, in gower_distances
X_num = np.divide(X_num ,max_of_numeric,out=np.zeros_like(X_num), 
where=max_of_numeric!=0)
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to 
provided output parameter (typecode 'q') according to the casting rule 
''same_kind''

我不明白为什么在同样的数据集上,只有较少的特征(列)会出现这种错误。有谁能说出其中的原因吗?

1 个评论
你确定 df 的类型是你所期望的吗?替换代码1】会产生什么?
python
scikit-learn
cluster-analysis
spyder
Beg
Beg
发布于 2018-05-31
2 个回答
Dan Oak
Dan Oak
发布于 2021-03-12
已采纳
0 人赞同

你需要指定 dtype 的左边操作数为非整数,一些浮点类型,例如

a = np.array ( ... , dtype = float )
np.divide ( a , b , out = np.zeros_like ( a ) , where = b != 0)

如果dtypeab都是整数,那么你得到的错误是。没有为ufunc true_divide找到符合指定签名和铸造的循环。.

如果adtype是整数,而b是- 浮动,那么你得到的错误是。ufunc 'true_divide'的输出(类型码'd')无法根据铸造规则''same_kind''强制到提供的输出参数(类型码'l')。.

ali_arisoy
ali_arisoy
发布于 2021-03-12
0 人赞同