gpt4 book ai didi

python - numpy.int64 和/或 numpy.float64 的奇怪基本计算问题?

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:58 28 4
gpt4 key购买 nike

我从 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

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

最佳答案

您正在遭受算术溢出。使用 NumPy,为了速度,most operations do not check for arithmetic overflow .您有责任选择合适的 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 整数数据类型来执行计算而不会溢出,您需要先将 n 转换为 Python int:

n = int(w.sum())

另一种选择是将ndtype更改为float64:

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)

关于python - numpy.int64 和/或 numpy.float64 的奇怪基本计算问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748383/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com