【发布时间】: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)
我想我现在需要 map() 将结果列表返回到简单的 new_list 函数中,该函数运行相关性并返回结果 Pearson r 和 p 值,并将这些附加到 p2values[ ] 和 r2values[] 列表,但我在这里迷路了,不胜感激。 提前致谢, 棒
【问题讨论】:
标签: python numpy dictionary statistics itertools