画图

使用polyfit最小二乘法进行多项式拟合,拟合出曲线的系数

curve_brute_force = np.polyfit(N, times_brute_force, 20) curve_backtrack = np.polyfit(N, times_backtrack, 20) curve_branch_bound = np.polyfit(N, times_branch_bound, 20)

生成一个包含100个数据点的均匀分布的序列由N确定范围

smooth_N = np.linspace(min(N), max(N), 100)

计算出在smooth_N上的平滑时间序列即x轴数据

smooth_times_brute_force = np.polyval(curve_brute_force, smooth_N) smooth_times_backtrack = np.polyval(curve_backtrack, smooth_N) smooth_times_branch_bound = np.polyval(curve_branch_bound, smooth_N)

绘制平滑后的曲线

plt.plot(smooth_N, smooth_times_brute_force, label='Brute Force (Curve)') plt.plot(smooth_N, smooth_times_backtrack, label='Backtrack (Curve)') plt.plot(smooth_N, smooth_times_branch_bound, label='Branch and Bound (Curve)')

设置x轴和y轴的标签

plt.xlabel('N') plt.ylabel('Time (s)')

显示图例

plt.legend()

显示图像

plt.show()

# 画图curve_brute_force = nppolyfitN times_brute_force 20curve_backtrack = nppolyfitN times_backtrack 20curve_branch_bound = nppolyfitN times_branch_bound 20 #使用polyfit最小二乘法进行多项式拟合smooth_N = npl

原文地址: https://www.cveoy.top/t/topic/hPtR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录