【问题标题】:Passing a list of itertools combinations to a function - map?将 itertools 组合列表传递给函数 - 映射?
【发布时间】:2020-06-13 13:20:16
【问题描述】:

这里是 Python 菜鸟,抱歉。 我正在玩 Anscombe 的四重奏,通过删除单个点(替换为组中位数)然后遍历数据以返回 Pearson r 和 p 值,然后为每个点绘制两者来探索“脆弱”相关性的想法源向量中的项目(Anscombe 的四重奏是灵感)。 迭代和替换单个值很容易:

import numpy as np 
import matplotlib.pyplot as plt
import itertools
import statistics
def new_list(x,y,n,replacex, replacey):
    '''Take 2 1D arrays (x and y) and replace item n with replacex and replacey respectively'''
    # First, copy the source arrays into the new arrays (newx, newy)
    newx=np.copy(x)
    newy=np.copy(y)
    #Now replace item n with the medians
    newx[n]=replacementx
    newy[n]=replacementy
    return(newx,newy)
#Initialise the dummy lists, assign the replacement values(medians), clear the temporary variables
newx=[] #temporary x list to run the new correlation
newy=[] #temporary y list to run the new correlation

p2values=[] #list of p values for the new correlations - this should change nearly every iteration
r2values=[] #list of r values for the new correlations - this should change nearly every iteration

replacementx=[] # single x value to be placed into the source list to run the new correlation. Currently using median
replacementy=[] # single y value to be placed into the source list to run the new correlation. Currently using median
#x,y values for one of Anscombe's Quartet as an example 
x=[8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]
y=[6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]
replacementx = statistics.median(x)
replacementy = statistics.median(y)
for n in range(len(x)):
    newx,newy = new_list(x,y,n,replacementx,replacementy)
    r,p = stats.pearsonr(x,y)
    r2,p2 = stats.pearsonr(newx,newy)

    p2values.append(p2)
    r2values.append(r2)

    newx=[]
    newy=[]

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('Item number')
ax1.set_ylabel('Pearson r', color=color)
ax1.set_ylim(0,1)
ax1.plot(range(len(r2values)), r2values, range(len(rvalues)),rvalues, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('p value', color=color)  # we already handled the x-label with ax1
ax2.plot(range(len(p2values)),p2values, range(len(pvalues)), pvalues, color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.show()

然后我想概括一下,我可以以某种方式使用 itertools.combinations() 传递源数据(在这种情况下是 Anscombe 的四重奏)和我想测试的数据点组合的数量,看看有多脆弱相关性是。我能得到的最远的是创建“候选”数据点,以从 Anscombe 的四重奏中删除,如下所示(对于 2 个数据点的所有组合):

import itertools
#x,y values for one of Anscombe's Quartet as an example 
x=[8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]
y=[6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]
data=list(zip(x,y))

replacement_candidates=list(itertools.combinations(data,2))
print(replacement_candidates)

我想我现在需要 ma​​p() 将结果列表返回到简单的 new_list 函数中,该函数运行相关性并返回结果 Pearson r 和 p 值,并将这些附加到 p2values[ ] 和 r2values[] 列表,但我在这里迷路了,不胜感激。 提前致谢, 棒

【问题讨论】:

    标签: python numpy dictionary statistics itertools


    【解决方案1】:

    好的,我已经想通了,所以在这里发布代码以防万一这对其他人有帮助。我完全走错了路。 此处的示例将 Anscombe 四重奏的第三个成员硬编码为 x,y 值和 3 的 n(对于 3 个值的所有组合),但您显然可以将它们换成您想要的任何东西。 n = 1 表明当第 8 项被替换时该成员是脆弱的(r,p 评估为 nan)。 sample output plot with n=1 我将把它重写为一个接受 x,y 值和 n 的函数,但由于组合可能很快失控,我想采取一些措施来防止内存不足错误和进度条或其他东西(这是超越我的atm,因为这基本上是我在Python中的“Hello World”) 结果与原始 r 和 p 的虚线参考线一起可视化,这对我有帮助。

    import numpy as np 
    import matplotlib.pyplot as plt
    import itertools
    import statistics
    from scipy import stats
    # Initialise the source values - x,y, and n
    # x,y are the source data for the correlation
    # n is the number of items to figure out all combinations to replace within x,y
    
    x=[8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]
    y=[6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]
    n=3
    
    r,p = stats.pearsonr(x,y)
    
    appendtuple=()
    p2values=[] #list of p-values for the new correlations - this should change nearly every iteration
    r2values=[] #list of r values for the new correlations - this should change nearly every iteration
    appendtuple=(statistics.median(x),statistics.median(y))
    data=list(zip(x,y))
    replacement_candidates=list(itertools.combinations(data,n))
    
    def Diff(li1,li2):
        return(list(set(li1)-set(li2)))
    
    for i in range(len(replacement_candidates)):
        a=[]
        a=Diff(data,replacement_candidates[i])
        for k in range(n):
            a.append(appendtuple)
        res_listx = [x[0] for x in a]
        res_listy= [y[1] for y in a]
        r2,p2 = stats.pearsonr(res_listx,res_listy)
        p2values.append(p2)
        r2values.append(r2)
        # print(res_listx,res_listy)
        # print(stats.pearsonr(res_listx,res_listy))
    
    fig, ax1 = plt.subplots()
    color = 'tab:red'
    ax1.set_xlabel('Item number')
    ax1.set_ylabel('Pearson r', color=color)
    ax1.set_ylim(0,1)
    ax1.plot(range(len(r2values)), r2values, range(len(rvalues)),rvalues, color=color)
    ax1.tick_params(axis='y', labelcolor=color)
    
    plt.hlines(r,0,len(p2values),linestyles='dotted',label='r', color='tab:red')
    
    ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
    
    color = 'tab:blue'
    ax2.set_ylabel('p value', color=color)  # we already handled the x-label with ax1
    ax2.plot(range(len(p2values)),p2values, range(len(pvalues)), pvalues, color=color)
    ax2.tick_params(axis='y', labelcolor=color)
    plt.hlines(p,0,len(p2values),linestyles='dotted',label='r', color='tab:blue')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2021-12-03
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多