pythonpython-3.xnumpyscikit-learntypeerror

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


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?


Solution

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