我可以用哪种方法?

2024-04-20 09:12:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在和anaconda一起工作下一个代码是取出两个矩阵之间的相关系数。 第一个矩阵读取矩阵左上角的16个文件。 求和就是得到平均值,与另一个文件的结果进行比较

`` `python
for i in range(0,16):
    i = i + 5
    file = pd.read_csv(path,header=None)
    file=file.fillna(0)
    file = pd.DataFrame(file)
    matrix = np.matrix(file)
    matrix = np.flip(matrix, 1)
    b = np.copy(matrix) 
    b = np.swapaxes(b, 1, 0)
    np.fill_diagonal(b, 0)
    c = matrix + b
    sum = c.sum(0) / c.shape[0]
    sum=pd.DataFrame(sum)
    file2 = pd.read_csv(path,header=None)
    file2=pd.DataFrame(file2)
    file2 = file2.drop(file2.columns[48], axis=1)

` ``

如果sum是(1,48)的矩阵,file2是(16,48)的矩阵,则两个文件之间的相关系数。你知道吗


Tags: 文件csvpathnonedataframereadnp矩阵
1条回答
网友
1楼 · 发布于 2024-04-20 09:12:39

我做了一些研究,希望下面能有所帮助:

  1. numpy.corrcoef
numpy.corrcoef(x, y=None, rowvar=True, bias=<no value>, ddof=<no value>)

Return Pearson product-moment correlation coefficients.

  1. Computing the correlation coefficient between two multi-dimensional arrays

Correlation (default 'valid' case) between two 2D arrays:

You can simply use matrix-multiplication np.dot like so -

out = np.dot(arr_one,arr_two.T)

Correlation with the default "valid" case between each pairwise row combinations (row1,row2) of the two input arrays would correspond to multiplication result at each (row1,row2) position.

请澄清你的问题,以防我误解。你知道吗

相关问题 更多 >