I wanted to re-plot a graph with exponential moving averages, so I defined smooth curve below.
def smooth_curve(points, factor=0.8):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor point * (1 - factor))
else:
smoothed_points.append(point)
return
And I tried to plot with matplotlib.pyplot as plt
plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')
But I got the error below.
plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')
Traceback (most recent call last):
Input In [119] in <cell line: 1>
plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')
File C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py:2757 in plot
return gca().plot(
File C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632 in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:312 in __call__
yield from self._plot_args(this, kwargs)
File C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:459 in _plot_args
raise ValueError("x, y, and format string must not be None")
ValueError: x, y, and format string must not be None
I checked the smooth_curve(acc)
to be compiled successfully.
CodePudding user response:
You need to return smoothed_points
variable after the function execution. If you just use return
, the function will return a None
null value causing issues with the rest of the code:
def smooth_curve(points, factor=0.8):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor point * (1 - factor))
else:
smoothed_points.append(point)
return smoothed_points