python - 属性错误 : 'module' object has no attribute 'Zeros'

标签 python

我在第 4 行使用了 np.Zeros 并且它没有给出错误为什么它在第 17 行给出错误 AttributeError: 'module' object has no attribute 'Zeros' 而在第 27 行它给出错误.. 错误y= newtonRaphson(残差,startSoln(x),1.0e-5)

import numpy as np
from newtonRaphson import *

def residual(y):
    r=np.Zeros((m+1))
    r[0]=y[0]
    r[m]=y[m]-1.0
    for i in range(1,m):
        r[i]=y[i-1]
    return r

def F(x,y,yPrime):
    F=-3.0*y*yPrime
    return F

def startSoln(x):
    y=np.Zeros((m+1))
    for i in range(m+1):
        y[i]=0.5*x[i]
    return y

xStart=0.0
xStop=2.0
m=10
h=(xStop-xStart)/m
x=np.arange(xStart,xStop+h,h)
y= newtonRaphson(residual,startSoln(x),1.0e-5)
print '\n           x            y'
for i in range(m+1):
    print x[i],'           '.y[i]
residual(0.0005)

这是 newtonRaphson 模块

def newtonRaphson(f,df,a,d,tol=1.09e-9):
    import error
    fa= f(a)
    if fa == 0.0 :return a
    fb = f(b)
    if fb ==0.0: return b
    if fa*fb >0.0: error.err
    x= 0.5*(a+b)
    for i in range(30):
        if abs(fx)<tol :return x
        if fa*fx <0.0:
            b=x
        else:
            a=x ;fa=fx
        dfx=df(X)
        try:dx= -fx/dfx
        except ZeroDivisionError: dx=b-a
        x=x+dx
        if (b-x)*(x-a)<0.0:
            dx=0.5*(b-a)
            x=a+dx
        if abs(dx)< tol*max(abs(b),1.0): return x
        print 'Too many iterations in Newton-Raphson'

最佳答案

是因为the function is called zeros ,而不是 Zeros。将 np.Zeros 更改为 np.zeros

关于python - 属性错误 : 'module' object has no attribute 'Zeros' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261763/

相关文章:

python - 如何使用 flask 在 Openshift 上安装 mysqldb

Python - 如何在大文件中搜索字符串

Python:追加到一个字典值,如果已经存在,该值是一个列表,会添加到所有键的值而不是

python Pandas : select column with the number of unique values greater than 10

python - 用 2 个生成器替换 3 个列表

python : List of string in key value pairs to map

Python Ariadne GraphQL "cannot import name ' GraphQLNamedType'"

python - 将验证附加到现有的 Python 列表实例

python - 如何在Python中随意旋转,重新排列等pdf页面?

python - 在 matplotlib 中分层轮廓图和表面图