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
Read More:
- Arrays must all be same length [How to Solve]
- How to Fix Word Opening Error: the resource dll cannot be loaded
- How to Solve Namedparameterjdbctemplate.queryforobject() Return Error
- Angular select Tag Error trying to diff ‘2’. Only arrays and iterables are allowed
- How to Solve Maven Error: Return code is: 501 , ReasonPhrase:HTTPS Required.
- C++ new types may not be defined in a return type Error?
- How to Fix KeyError: ‘plotly_domain’
- How to Fix TNS 12560 Error
- How to Fix Common Arcpy Error
- How to Fix Node SASS Error in Vue + webpack project after nodejs upgrade
- Websocket: How to Fix Error Read Econnreset
- How to Fix error: conversion from “” to non-scalar type “”
- How to Fix Cannot convert value of type Error
- How to Fix win10 video dxgkrnl fatal error
- (How to Fix) Ora-00600: internal error code, arguments: [4194]
- How to Fix the common Warning Errors after Vue Project Startup
- How to Fix “initializer element is not constant” error
- WordPress update failed 429 too many requests (How to Fix)
- How to Fix “Microsoft Visual C ++ 14.0 is required” Error
- Start failure: spawn CMD enoent error (How to Fix)