【问题标题】:python gradient descent error when plotting map()绘制map()时出现python梯度下降错误
【发布时间】:2019-04-19 06:30:52
【问题描述】:

我尝试运行下面的代码,

import math
import matplotlib.pyplot as plt
from functools import partial

def difference_quotient(f,x,h):
    return(f(x+h)-f(x))/h


def square(x):
    return x*x

def derivative(x):
    return 2*x


derivative_estimate = partial(difference_quotient,square,h=0.0001)
x = range(-10,10)
y = range(-10,10)
plt.title("actual vs estimation")
plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")
plt.show()
print(len(list(map(derivative,x))))

但它在下面显示错误

Traceback(最近一次调用最后一次):文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py",第 168 行,在 获取转换器 如果不是 np.all(xravel.mask): AttributeError: 'numpy.ndarray' object has no attribute 'mask'

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):文件 “C:\Users\asus\Documents\Sublime\dataScience\gradient.py”,第 20 行,在 plt.plot(x,map(derivative,x),'rx',label="actual") 文件 "C:\Program Files\Python37\lib\site-packages\matplotlib\pyplot.py", 第 2811 行,在情节中 is not None else {}), **kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib__init__.py”,第 1810 行, 在内部 返回 func(ax, *args, **kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_axes.py”,第 1611 行, 在情节 对于 self._get_lines(*args, **kwargs) 中的行:文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 393 行, 在 _grab_next_args 来自 self._plot_args(this, kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 370 行, 在_plot_args x, y = self._xy_from_xy(x, y) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 205 行, 在 _xy_from_xy by = self.axes.yaxis.update_units(y) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axis.py”,第 1467 行,在 更新单位 转换器 = munits.registry.get_converter(data) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py”,第 181 行,在 获取转换器 转换器 = self.get_converter(next_item) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py”,第 187 行,在 获取转换器 thisx = safe_first_element(x) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\cbook__init__.py”,行 第1635章 raise RuntimeError("matplotlib 不支持生成器" RuntimeError: matplotlib 不支持生成器作为输入 [0.7s完成]

我的嫌疑人在这条线上,

plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")

当我尝试使用范围 (-10,10) 的 y 更改 map(derivative,x) 和 map(derivative_estimate,x) 时,它可以工作。

当我使用上面的地图功能时,我应该怎么做才能让代码显示绘图?

【问题讨论】:

  • 请放回原始回溯。使用 >python /n traceback text

标签: python python-3.x matplotlib python-3.7


【解决方案1】:

您需要将生成器转换为值列表。例如。而不是map(func, values),使用list(map(func, values))。在你的情况下:

plt.plot(x, list(map(derivative,x)), 'rx', label="actual")
plt.plot(x, list(map(derivative_estimate,x)), 'b+', label="estimate")

【讨论】:

    【解决方案2】:

    RuntimeError: matplotlib 不支持生成器作为输入

    意味着您不能使用 put 作为参数,称为 python generator
    您需要将map(derivative,x)map(derivative_estimate,x) 的实际值分配给实际变量。

    试试这个:

    import math
    import matplotlib.pyplot as plt
    from functools import partial
    
    def difference_quotient(f,x,h):
        return(f(x+h)-f(x))/h
    
    
    def square(x):
        return x*x
    
    def derivative(x):
        return 2*x
    
    derivative_estimate = partial(difference_quotient,square,h=0.0001)
    x = range(-10,10)
    y = range(-10,10)
    
    a = map(derivative,x)
    b = map(derivative_estimate,x)
    
    plt.title("actual vs estimation")
    plt.plot(x,a,'rx',label="actual")
    plt.plot(x,b,'b+',label="estimate")
    plt.show()
    print(len(list(map(derivative,x))))
    

    但是,您的代码使用python 3.4.3matplotlib==2.2.4 对我来说运行良好。你自己用的是什么版本?

    【讨论】:

    • 我使用的python是3.7,matplotlib == 3.0.3
    • @AriyaSusanto 你试过我的提议了吗?
    • 抱歉回复晚了,我做到了,但您提供的解决方案显示错误 Traceback (最近一次调用最后一次): File "C:\Program Files\Python37\lib\site-packages\matplotlib \units.py",第 168 行,如果不是 np.all(xravel.mask),则在 get_converter 中:AttributeError: 'numpy.ndarray' object has no attribute 'mask'
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2019-08-08
    • 2020-11-15
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多