Domanda

I have a little problem. In my program, in Python 3.3, I made a list of decimal values (x):

  [Decimal('646'), Decimal('651'), Decimal('657')]

And I want to know the mean value using Numpy.

So I wrote:

  tempArray = numpy.array(x, dtype=np.dtype(decimal.Decimal))

But I got the error:

  TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float'

What is wrong with my code?

È stato utile?

Soluzione

The following works just fine for me on Python 2.7

import numpy
from decimal import Decimal

x = [Decimal('646'), Decimal('651'), Decimal('657')]
tempArray = numpy.array(x, dtype=numpy.dtype(Decimal))
print numpy.mean(tempArray)

Altri suggerimenti

Why do you need to use Numpy? This can be done easily with

>>> sum(x)/len(x)
Decimal('651.3333333333333333333333333')

That said, I was able to do

>>> np.array(x).mean()
Decimal('651.3333333333333333333333333')
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top