Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

I am trying to apply Gower distance implementation to my data frame. While it was smoothly working with the same dataset with more features, this time it gives an error when I call the Gower distance function. I import the Gower's function from another .py code in the same directory. Here is my code:

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)

and after executing this, I got the error below:

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''

I did not understand how it can give this error on the same dataset with only fewer features (columns). Is there anyone who can recognize the reason?

You need to specify dtype of the left operand to be non integer, some floating point type, e.g.

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

If dtype of a and b are both integers, then the error you get is: No loop matching the specified signature and casting was found for ufunc true_divide.

If dtype of a is integer but b - float, then the error you get is: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''.

I had the same issue. It seems that if all of your variables are integers, then it produces this error. So I have changed every integer column to string values.

cluster_data = cluster_data.astype(str)
cluster_data.dtypes.head()

This seems to fix the error.

np.zeros_like(a) creates an array with the same dtype as a and if the numpy operation that it is used as out= (such as np.divide, np.log, np.exp etc.) produce a float array, the dtypes won't match; hence the error. One way to get around the issue to use to np.zeros(a.shape).

a = b = np.random.default_rng().choice(10, size=1000000)
np.divide(a, b, out=np.zeros_like(a), where=b!=0)       # UFuncTypeError
np.divide(a, b, out=np.zeros(a.shape), where=b!=0)      # OK

or change dtype of the array passed to zeros_like into float (as suggested by Dan Oak).

X = dataset.iloc[:, 1:].values.astype(float)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.