Both U32 and S32 indicate that your NumPy array is a string array, not a number array. Check whether there are strings in the dataset. If there are, just delete them. In NumPy array, as long as one item is a string, the type returned by the array is a string array.
If you need to convert numpy to floating-point number, please refer to the code:
train= train.astype (float)
train_ target = train_ target.astype (float)
Tag Archives: How to Fix Typeerror
How to Fix Error return arrays must be of arraytype
from math import log
import xlrd
***# from numpy import ****
import operator
def calcShannonEnt(dataSet):#calculata shannonEnt
numEntries = len(dataSet)
labelCounts = {}
for featVec in dataSet:#Add the current key value to the dictionary and record the number of occurrences of the category
currentLabel = featVec[-1]
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
shannonEnt = 0.0
for key in labelCounts:#Calculating Shannon entropy
prob = float(labelCounts[key])/numEntries#Calculate the probability of category occurrence using the frequency of occurrence of all class labels
tmp = prob*log(prob,2)
shannonEnt -= tmp#Get Shannon entropy
return shannonEnt
Typeerror: return arrays must be of array-type occurs when the code is running, because the second parameter of log is not base but out array. If you just want to perform normal log operations, you can choose to use numpy.math.log (1.1, 2) or use the log function of math module in Python instead of importing all the functions TT in numpy