Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 962
Beg Avatar asked May 31 '18 13:05

Beg


2 Answers

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

like image 192
Dan Oak Avatar answered Oct 29 '22 06:10

Dan Oak


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.

like image 33
ali_arisoy Avatar answered Oct 29 '22 04:10

ali_arisoy