TypeError: ‘numpy.ndarray’对象不可调用,它从哪里获取一个数组?

huangapple go评论65阅读模式
英文:

TypeError: 'numpy.ndarray' object is not callable , where does it take an array from?

问题

Here's the translated code portion you provided:

def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color=colors, marker='o')
    for i in range(nb):
        x1 = g(x0)
        plt.plot([x0, x0], [x0, x1], color=colors)
        plt.plot([x0, x1], [x1, x1], color=colors)
        x0 = x1

x = np.linspace(0, 1, 999)
y1 = 1 - np.sin(π)
y2 = np.exp(-4 * x)
y3 = 0.4 * np.exp(π) - 0.25
y4 = 1 / (1 + ((1 - x) / x) ** 2)
y = x
y1_dev = -np.cos(x)
y2_dev = -4 * y2
y3_dev = 0.4 * np.exp(x)
y4_dev = 2 * ((1 + ((1 - x) / x) ** 2) ** -2) * ((1 - x) / x ** 3)
functions = [y1, y2, y3, y4]
derivatives = [y1_dev, y2_dev, y3_dev, y4_dev]
iterations = [10, 10, 10, 10]
colors = ["r", "g", "c", "m"]
fixed_points = [0.5, 0.3, 0.3, 0.5]
for i in range(len(functions)):
    plt.plot(x, functions[i])
    plt.plot(x, y)
    plt.show()
for j in range(len(derivatives)):
    plt.plot(x, derivatives[j])
    plt.show()

for k in range(len(functions)):
    draw_PF(functions[k], (fixed_points[k] - 0.1), iterations[k], colors[k])
    plt.show()

I've translated the code and replaced special characters with their respective Python equivalents. Please note that you should ensure you have the necessary libraries imported (e.g., import numpy as np and import matplotlib.pyplot as plt) for this code to work properly.

英文:
def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color = colors, marker='o')
    for i in range(nb):
        x1 = g(x0)
        plt.plot([x0,x0], [x0,x1], color = colors)
        plt.plot([x0,x1], [x1,x1], color = colors)
        x0 = x1

x = np.linspace(0,1,999)
y1 = 1-np.sin(𝑥)
y2 = np.exp(-4*x)
y3 = 0.4 * np.exp(𝑥) - 0.25
y4 = 1/(1 + ((1-x)/x)**2)
y = x
y1_dev = -np.cos(x)
y2_dev = -4 * y2
y3_dev = 0.4*np.exp(x)
y4_dev = 2*((1 + ((1-x)/x)**2)**-2)*((1-x)/x**3)
functions = [y1,y2,y3,y4]
derivatives = [y1_dev,y2_dev,y3_dev,y4_dev]
iterations = [10,10,10,10]
colors = ["r","g","c","m"]
fixed_points = [0.5,0.3,0.3,0.5]
for i in range(len(functions)):
    plt.plot(x,functions[i])
    plt.plot(x,y)
    plt.show()
for j in range(len(derivatives)):
    plt.plot(x,derivatives[j])
    plt.show()

for k in range(len(functions)):
    dessiner_PF(functions[k],(fixed_points[k] - 0.1), iterations[k], couleurs[k])
    plt.show()
TypeError                                 Traceback (most recent call last)
Cell In[28], line 25
     22     plt.show()
     24 for k in range(len(functions)):
---> 25     draw_PF(functions[k],(fixed_points[k] - 0.1), iterations[k], colors[k])
     26     plt.show()

Cell In[27], line 15, in draw_PF(g, x0, nb, colors)
     13 plt.plot([x0], [x0], color = colors, marker='o')
     14 for i in range(nb):
---> 15     x1 = g(x0)
     16     plt.plot([x0,x0], [x0,x1], color = colors)
     17     plt.plot([x0,x1], [x1,x1], color = colors)

TypeError: 'numpy.ndarray' object is not callable``

'Hello, I keep receiving this error message, but i dont understand quite why...
I am trying to implement a Picard algorithm to find fix points, but I keep having an issue with my call, I especially don't understand from where it takes the numpy array... thank you in advance.'

答案1

得分: 1

以下是翻译好的部分:

错误来自于这个函数:

def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color=colors, marker='o')
    for i in range(nb):
        x1 = g(x0)  # 错误出现在这里
        plt.plot([x0, x0], [x0, x1], color=colors)
        plt.plot([x0, x1], [x1, x1], color=colors)
        x0 = x1

g(x0) 不能工作,因为在你的代码中,g 本身没有定义为一个函数。如果你执行 print(g),你会得到一个形状为 (999,)numpy.ndarray。这是因为你在 draw_PF 中定义了表示 g 的第一个对象 y1,如下所示:

x = np.linspace(0, 1, 999)
y1 = 1 - np.sin(x)

由于 y1 是一个 numpy 数组,它不可调用(也就是说,它不像一个函数那样工作,因为它是一个数组),因此 Python 返回给你这个错误 numpy.ndarray' object is not callable。对于 y2y3y4,也是同样的情况。

如果有帮助,请告诉我!

英文:

The error comes from this function:

def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color = colors, marker='o')
    for i in range(nb):
        x1 = g(x0)  # error is here
        plt.plot([x0,x0], [x0,x1], color = colors)
        plt.plot([x0,x1], [x1,x1], color = colors)
        x0 = x1

g(x0) does not work because g itself is not defined as a function in your code. If you do print(g) you get a numpy.ndarray with the shape (999,). This is because you define y1, which is the first object representing g in draw_PF, as follows:

x = np.linspace(0,1,999)
y1 = 1-np.sin(x)

Since y1 is a numpy array, it is not callable (that is to say it does not behave like a function since it's an array) and therefore python returns you this error numpy.ndarray' object is not callable. The same applies for y2, y3, and y4.

Let me know if this helped!

答案2

得分: 0

g 似乎是一个 np.ndarray,您正在尝试访问元素 x0。在这种情况下,您需要使用 g[x0] 来访问列表的第 x0 个元素。

英文:

There's not enough information to know for sure, but it seems like g is a np.ndarray, and you're trying to access element x0. In that case, you want g[x0], to access the x0'th element of the list.

huangapple
  • 本文由 发表于 2023年5月11日 02:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定