绘制map()时python梯度下降错误

时间:2019-04-19 06:30:53

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

我尝试运行下面的代码,

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))))

但下面显示错误

  

回溯(最近一次拨打电话):文件“ C:\ Program   文件\ Python37 \ lib \ site-packages \ matplotlib \ units.py”,第168行,在   get_converter       如果不是np.all(xravel.mask):AttributeError:'numpy.ndarray'对象没有属性'mask'

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近通话最近):文件   “ 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行,在图中       不是其他{}},** 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中       作者= self.axes.yaxis.update_units(y)文件“ C:\ Program Files \ Python37 \ lib \ site-packages \ matplotlib \ axis.py”,行1467,在   update_units       转换器= munits.registry.get_converter(data)文件“ C:\ Program Files \ Python37 \ lib \ site-packages \ matplotlib \ units.py”,第181行,在   get_converter       转换器= self.get_converter(next_item)文件“ C:\ Program Files \ Python37 \ lib \ site-packages \ matplotlib \ units.py”,第187行,在   get_converter       thisx = safe_first_element(x)文件“ C:\ Program Files \ Python37 \ lib \ site-packages \ matplotlib \ cbook__init __。py”,行   1635,在safe_first_element中       引发RuntimeError(“ matplotlib不支持生成器” RuntimeError:matplotlib不支持生成器作为输入   [以0.7秒完成]

我的嫌疑犯在这条线上,

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

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

当我使用上面的map函数时,我应该怎么做才能使代码显示图?

2 个答案:

答案 0 :(得分:0)

  

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,您的代码对我来说工作正常。您正在使用什么版本?

答案 1 :(得分:0)

您将需要将生成器转换为值列表。例如。使用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")
相关问题