AttributeError: 'module' object has no attribute 'Zeros'

Question:

I have used np.Zeros in line 4 and its not giving error why it is giving error in line 17 that AttributeError: ‘module’ object has no attribute ‘Zeros’ and in line 27 it is giving error.. error in y= newtonRaphson(residual,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)

here is the newtonRaphson module

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'
Asked By: user3036166

||

Answers:

It’s because the function is called zeros, not Zeros. Change np.Zeros to np.zeros.

Answered By: Jakub Roztocil

Reason 1:

There shouldn’t be any package or file name with numpy in your project, which can shadow the actually numpy package and will give
this error.

Reason 2:
You are using the incorrect function name from the actual package, function names are case sensitive.

Answered By: Arpan Saini
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.