【发布时间】:2020-02-29 20:41:03
【问题描述】:
我正在尝试计算 numpy 二维数组的两列之间的相关性。数组看起来像:
a = np.array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2]])
所以第 1 列和第 3 列之间的相关性计算为:
import scipy
x, y = scipy.stats.pearsonr(a[:,0], a[:,2])
但是,它抱怨:
unsupported operand type(s) for -: 'tuple' and 'float'
这意味着,例如,下面的事情正在发生:
print((0.1,0.2) - 0.3)
TypeError: unsupported operand type(s) for -: 'tuple' and 'float'
更新: 完整代码:
import pandas as pd
from scipy import stats
import numpy as np
a = pd.read_csv("src/iris.csv").drop('species', axis=1).values
def lengths():
x, y = stats.pearsonr(a[:,0],a[:,2])
return x, y
print(lengths())
【问题讨论】: