【发布时间】:2018-05-19 13:15:27
【问题描述】:
我正在开发一个项目,该项目在给定几个数据列表时具有许多功能。我已经分离了列表,并且定义了一些我知道可以正常工作的函数,即平均函数和标准差函数。我的问题是在测试我的列表时,我得到了正确的均值、正确的标准差,但相关系数不正确。我的数学可以在这里关闭吗?我只需要用 Python 的标准库找到相关系数。
我的密码:
def correlCo(someList1, someList2):
# First establish the means and standard deviations for both lists.
xMean = mean(someList1)
yMean = mean(someList2)
xStandDev = standDev(someList1)
yStandDev = standDev(someList2)
zList1 = []
zList2 = []
# Create 2 new lists taking (a[i]-a's Mean)/standard deviation of a
for x in someList1:
z1 = ((float(x)-xMean)/xStandDev)
zList1.append(z1)
for y in someList2:
z2 = ((float(y)-yMean)/yStandDev)
zList2.append(z2)
# Mapping out the lists to be float values instead of string
zList1 = list(map(float,zList1))
zList2 = list(map(float,zList2))
# Multiplying each value from the lists
zFinal = [a*b for a,b in zip(zList1,zList2)]
totalZ = 0
# Taking the sum of all the products
for a in zFinal:
totalZ += a
# Finally calculating correlation coefficient
r = (1/(len(someList1) - 1)) * totalZ
return r
样品运行:
我有一个 [1,2,3,4,4,8] 和 [3,3,4,5,8,9] 的列表
我期望 r = 0.8848 的正确答案,但得到 r = .203727
编辑:包括我所做的均值和标准差函数。
def mean(someList):
total = 0
for a in someList:
total += float(a)
mean = total/len(someList)
return mean
def standDev(someList):
newList = []
sdTotal = 0
listMean = mean(someList)
for a in someList:
newNum = (float(a) - listMean)**2
newList.append(newNum)
for z in newList:
sdTotal += float(z)
standardDeviation = sdTotal/(len(newList))
return standardDeviation
【问题讨论】:
-
可以添加mean,standDev函数
-
在编辑中添加了这些功能。它们似乎都工作正常。
标签: python python-3.x statistics