【问题标题】:Convenient way to show calculation for Seaborn regression line显示 Seaborn 回归线计算的便捷方式
【发布时间】:2021-08-02 15:38:42
【问题描述】:

我正在寻找一种方便的方法来显示 Seaborn regplot 中回归线的计算。我在 Jupyter Notebook 中使用 Python。

查看了其他一些stackoverflow帖子后,我理解为您无法直接显示图中使用的fit_reg计算。

相反,我发现了使用 from scipy import stats 模块的建议。 但是,这似乎对我不起作用:

from scipy import stats

#Read frame and create array of scores
read_frame = pd.DataFrame([each])
dates = list(read_frame.keys())
stock = dates.pop(0)
stock = each[stock]
stock_score = []
for key in dates:
    score = read_frame[key].to_list()
    stock_score.extend(score)

# y = dates, x = stock_score
slope = stats.linregress(dates, stock_score)
print(slope)

#[EDIT] Added data used for calculation.
# dates: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# stock_score: [0.06933703703703704, 0.07373142857142856, 0.12000285714285716, 0.1634861111111112, 0.1726382978723404, 0.548125, 0.04427257383966246, 0.08823866666666677, 0.039461915367483294, -0.03524617834394902, 0.033837903225806445, 0.023088636363636363, -0.25486666666666663, 0.07539999999999997, 0.08250487804878054]

我收到的输出,以及显示我的数据点的图表。

# Output
LinregressResult(slope=nan, intercept=nan, rvalue=nan, pvalue=nan, stderr=nan)

我想做的是以方便的方式获得回归线的计算。

下面是使用的整个代码段(包括要绘制的代码)。

# Scatterplot with regressionline for each stock

import seaborn as sns
from scipy import stats

colors = ['black', 'red', 'yellow', 'blue', 'green', 'orange', 'purple', 'pink', 'brown', 'coral', 'teal', 'navy', 'grey', 'violet', 'maroon']
ctr = 0
for each in new_stock_scores:
    read_frame = pd.DataFrame([each])
    dates = list(read_frame.keys())
    stock = dates.pop(0)
    stock = each[stock]
    stock_score = []
    for key in dates:
        score = read_frame[key].to_list()
        stock_score.extend(score)
    
    #Regression line
    slope = stats.linregress(dates, stock_score)
    print(slope)

    #Regression plot
    sns.set(style="whitegrid")
    sns.set_context("paper")
    ax = sns.regplot(x = dates, y = stock_score, color = colors[ctr], ci = None)
    ax.set_title(stock, weight ='bold').set_fontsize('16')
    ax.set(xticks = dates)
    ax.set(xlabel = 'Date', ylabel = 'Score')
    plt.xlim(0,16)
    plt.show()
    ctr += 1

【问题讨论】:

    标签: python jupyter-notebook seaborn linear-regression scipy.stats


    【解决方案1】:

    我似乎无法重现您的问题

    import scipy.stats as sps
    
    dates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    stock_score = [0.06933703703703704, 0.07373142857142856, 0.12000285714285716, 0.1634861111111112, 0.1726382978723404, 0.548125, 0.04427257383966246, 0.08823866666666677, 0.039461915367483294, -0.03524617834394902, 0.033837903225806445, 0.023088636363636363, -0.25486666666666663, 0.07539999999999997, 0.08250487804878054]
    
    linreg = sps.linregress(dates, stock_score)
    
    print(linreg)
    
    print('slope', linreg.slope)
    

    给了

    LinregressResult(slope=-0.014006090719584158, intercept=0.19498288977241957, rvalue=-0.38523447967018376, pvalue=0.1561897736602469, stderr=0.009305431260444107, intercept_stderr=0.08460610819916589)
    
    slope -0.014006090719584158
    

    您可以尝试检查您的scipy 版本

    import scipy
    print(scipy.__version__)
    
    1.6.1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-17
      • 2014-08-11
      • 2018-02-04
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多