문제

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?

도움이 되었습니까?

해결책

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)

다른 팁

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top