This question already has answers here:
Which is the simplest way to make a polynomial regression with sklearn?
                                
                                    (2个答案)
                                
                        
                        
                            polynomial regression using python
                                
                                    (3个答案)
                                
                        
                                去年关闭。
            
                    
在matplotlib中的scatter()上可以做多项式回归线吗?

这是我的图:
https://imgur.com/a/Xh1BO

    alg_n = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4...]
    orig_hc_runtime = [0.01, 0.02, 0.03, 0.04, 0.04, 0.04, 0.05, 0.09...]

    plt.scatter(alg_n, orig_hc_runtime, label="Orig HC", color="b", s=4)
    plt.scatter(alg_n, mod_hc_runtime, label="Mod HC", color="c", s=4)
    ...

    x_values = [x for x in range(5, n_init+2, 2)]
    y_values = [y for y in range(0, 10, 2)]

    plt.xlabel("Number of Queens")
    plt.ylabel("Time (sec)")
    plt.title("Algorithm Performance: Time")
    plt.xticks(x_values)
    plt.yticks(y_values)
    plt.grid(linewidth="1", color="white")
    plt.legend()
    plt.show()


是否可以为饮食数据集设置回归线?如果是这样,请您能解释一下我该怎么做。

最佳答案

我建议您使用Seaborn库。它基于matplotlib构建,并具有许多统计绘图例程。看一下regplotlmplot的示例:http://seaborn.pydata.org/tutorial/regression.html#functions-to-draw-linear-regression-models

就您而言,您可以执行以下操作:

import pandas as pd
import seaborn as sns
df = pd.DataFrame.from_dict({"Number of Queens": [1, 1, 1, 2, 2, 2, 3,
                                                  3, 3, 4, 4, 4],
                             "Time (sec)": [0.01, 0.02, 0.03, 0.04, 0.04, 0.04,
                                            0.05, 0.09, 0.12, 0.14, 0.15, 0.16]})
sns.lmplot('Number of Queens', 'Time (sec)', df, order=1)


python - Matplotlib scatter():多项式回归线-LMLPHP

如果要为不同组使用回归线,请添加带有组标签的列,并将其添加到huelm_plot参数。

关于python - Matplotlib scatter():多项式回归线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49357973/

10-11 16:33