1

我从 python-numpy 收到一个非常奇怪和意外的错误

我正在使用以下库:

import os, glob, string, math, csv, json
import datetime as dt
import numpy as np
import scipy as sci
import pandas as pd
import matplotlib.pyplot as plt
import feedparser as fp
import cPickle as pickle
import networkx as nx
from urllib2 import urlopen
import statsmodels.formula.api as sm
import patsy

以下代码:

n,k = 2643605051, 648128.068241
print n,type(n)
print k, type(k)
nkvar = (k + 1)*(n + 2)/( (n+2) * (n+1)**2 )
print nkvar

n = np.int64(n)
k = np.float64(k)
print n,type(n)
print k, type(k)
nkvar = (k + 1)*(n + 2)/( (n+2) * (n+1)**2 )
print nkvar

产量:

2643605051 <type 'int'>
648128.068241 <type 'float'>
9.27402694708e-14
2643605051 <type 'numpy.int64'>
648128.068241 <type 'numpy.float64'>
-0.00383719008751

第二个答案显然是错误的!有人可以帮我理解发生了什么吗?

4

1 回答 1

4

你正遭受算术溢出的痛苦。使用 NumPy,为了速度,大多数操作不检查算术溢出。您有责任选择正确的 dtype 以避免溢出。

import numpy as np

n,k = 2643605051, 648128.068241
nkvar = (k + 1)*(n + 2)/((n+1)**2 * (n+2))
print "In foo nkvar = ", nkvar, "  from (n,k) = ", (n,k)
# In foo nkvar =  9.27402694708e-14   from (n,k) =  (2643605051L, 648128.068241)       

n,k = np.int64(2643605051), np.float32(648128.068241)
nkvar = (k + 1)*(n + 2)/((n+1)**2 * (n+2))
print "In foo nkvar = ", nkvar, "  from (n,k) = ", (n,k)
# In foo nkvar =  -0.00383719005352   from (n,k) =  (2643605051, 648128.06)

解决方法:由于没有足够大的 NumPy 整数 dtype 来执行计算而不会溢出,因此您需要先转换n为 Python int:

n = int(w.sum())

另一种选择是将dtypeof更改nfloat64

n,k = np.float64(2643605051), np.float64(648128.068241)
nkvar = (k + 1)*(n + 2)/((n+1)**2 * (n+2))
print "In foo nkvar = ", nkvar, "  from (n,k) = ", (n,k)
# In foo nkvar =  9.27402694708e-14   from (n,k) =  (2643605051.0, 648128.06824099994)
于 2013-09-11T18:20:11.740 回答