Only length-1 arrays can be converted to Python scalars with log

2024/11/16 3:37:23
from numpy import * 
from pylab import * 
from scipy import * 
from scipy.signal import * 
from scipy.stats import * testimg = imread('path')  hist = hist(testimg.flatten(), 256, range=[0.0,1.0])[0]
hist = hist + 0.000001
prob = hist/sum(hist)entropia = -1.0*sum(prob*log(prob))#here is error
print 'Entropia: ', entropia

I have this code and I do not know what could be the problem, thanks for any help

Answer

This is an example of why you should never use from module import *. You lose sight of where functions come from. When you use multiple from module import * calls, one module's namespace may clobber another module's namespace. Indeed, based on the error message, that appears to be what is happening here.

Notice that when log refers to numpy.log, then -1.0*sum(prob*np.log(prob)) can be computed without error:

In [43]: -1.0*sum(prob*np.log(prob))
Out[43]: 4.4058820963782122

but when log refers to math.log, then a TypeError is raised:

In [44]: -1.0*sum(prob*math.log(prob))
TypeError: only length-1 arrays can be converted to Python scalars

The fix is to use explicit module imports and explicit references to functions from the module's namespace:

import numpy as np
import matplotlib.pyplot as plttestimg = np.random.random((10,10))hist = plt.hist(testimg.flatten(), 256, range=[0.0,1.0])[0]
hist = hist + 0.000001
prob = hist/sum(hist)# entropia = -1.0*sum(prob*np.log(prob))
entropia = -1.0*(prob*np.log(prob)).sum()
print 'Entropia: ', entropia
# prints something like:  Entropia:  4.33996609845

The code you posted does not produce the error, but somewhere in your actual code log must be getting bound to math.log instead of numpy.log. Using import module and referencing functions with module.function will help you avoid this kind of error in the future.

https://en.xdnf.cn/q/118974.html

Related Q&A

Deploying Django with apache using wsgi.py

Im trying to deploy a Django project on a linode server that has apache, some other django projects and a php project on it. Also my project is in a virualenv and the other django projects arent.My Dja…

Building a decision tree using user inputs for ordering goods

I am trying to program a decision tree to allow customers to order goods based on their input. So far, I have devised a nested if-elif conditional structure to decide if customer want to order what or…

How to de-serialize the spark data frame into another data frame [duplicate]

This question already has answers here:Explode array data into rows in spark [duplicate](3 answers)Closed 4 years ago.I am trying to de-serialize the the spark data frame into another data frame as exp…

How to pull specific key from this nested dictionary?

{"newData": [{"env1": [{"sins": [{"host": "test.com","deployTime": "2015-07-23 11:54 AM",…}],"name": “hello”}, {"…

Dropping cell if it is NaN in a Dataframe in python

I have a dataframe like this.Project 4 Project1 Project2 Project3 0 NaN laptio AB NaN 1 NaN windows ten NaN 0 one NaN NaN 1 …

How can I iterate through excel files sheets and insert formula in Python?

I get this error TypeError: Workbook object is not subscriptablewhen i run this code import xlsxwriter from openpyxl import load_workbookin_folder = rxxx #Input folder out_folder = rxxx #Output folde…

How to bind all frame widgets to Enter event

I the following code I want to bind all frame1 items to <Enter> Event, but it does not work. I mean canvas.focus_set() does not take effect. How can I solve my problem?for w in frame1.winfo_chil…

Typeerror takes no arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…