0

当我偶然发现使用 scipy 的 pearson r 相关性解决方案时,我想添加 Rsquarred 值的注释

import pandas as pd
import numpy as np
import seaborn as sns
import scipy as sp
import matplotlib.pyplot as plt
from scipy.stats import linregress

g = sns.lmplot(x="X", y="Y",col="Param", data=df, sharex=False, sharey=False)

def annotate(data, **kws):
    r, p = sp.stats.pearsonr(data['X'], data['Y'])
    ax = plt.gca()
    ax.text(.05, .8, 'r={:.2f}'.format(r, p),
            transform=ax.transAxes)
    
g.map_dataframe(annotate)
plt.show()

阴谋

我想使用 scipy linregress 将 pearson r 相关性更改为 Rsquare 值,但我真的不明白它如何处理错误“太多值无法解包(预期为 4)”

g = sns.lmplot(x="X", y="Y",col="Param", data=df, sharex=False, sharey=False)

def annotate(data, **kws):
    slope, intercept, r, p= stats.linregress(data['X'], data['Y'])
    ax = plt.gca()
    ax.text(.05, .8, 'r={:.2f}'.format(slope, intercept, r,p),
            transform=ax.transAxes)
    
g.map_dataframe(annotate)
plt.show()

有谁知道如何正确使用它?谢谢

4

1 回答 1

0

显然您不需要使用 linregress 来查找 R 平方的注释值

只需将格式 r 更改为 r*r 并将小数点设置为 4 即可

import pandas as pd
import numpy as np
import seaborn as sns
import scipy as sp
import matplotlib.pyplot as plt
from scipy.stats import linregress

g = sns.lmplot(x="X", y="Y",col="Param", data=df, sharex=False, sharey=False)

def annotate(data, **kws):
    r, p = sp.stats.pearsonr(data['X'], data['Y'])
    ax = plt.gca()
    ax.text(.05, .8, 'r={:.4f}'.format(r*r, p), transform=ax.transAxes) #Change this line
    
g.map_dataframe(annotate)
plt.show()
于 2021-06-07T07:35:44.360 回答