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

3

19

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?

Redeem answered 31/5, 2018 at 13:53 Comment(1)
Are you sure that the types of df are what you expect? What does print(df.dtypes) yield?Korea
S
5

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.

Sinewy answered 12/3, 2021 at 13:15 Comment(0)
S
11

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

Shortsighted answered 15/4, 2020 at 18:29 Comment(0)
S
5

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.

Sinewy answered 12/3, 2021 at 13:15 Comment(0)
C
1

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

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)
Cachinnate answered 29/3, 2023 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.