scipy.sparse.csr.csr_matrix的max和argmax的含义



我有这个tf-idf矩阵

type(dt)  # output: scipy.sparse.csr.csr_matrix
pd.DataFrame(dt.toarray())
# output:
0          1            2           3        4          5
0   0.000000    0.000000    0.500000    0.500000    0.5    0.50000
1   0.707107    0.707107    0.000000    0.000000    0.0    0.00000
2   0.000000    0.000000    0.000000    0.000000    0.0    0.00000
3   0.000000    0.000000    0.707107    0.707107    0.0    0.00000
4   0.000000    0.000000    0.000000    0.000000    0.0    0.00000
5   0.000000    0.000000    0.000000    0.000000    0.0    0.00000
6   0.577350    0.577350    0.000000    0.000000    0.0    0.57735
7   0.000000    0.000000    0.000000    0.000000    0.0    0.00000
8   0.000000    0.000000    0.000000    0.000000    0.0    0.00000
9   0.000000    0.000000    0.000000    0.000000    1.0    0.00000

我运行此代码是为了理解矩阵的maxargmax的含义

test = np.dot(dt, np.transpose(dt))
test[test > 0.9999] = np.nan
ind = np.unravel_index(np.argmax(test), test.shape)
print('shape of test', test.shape)
print(f'max of test: {test.max()}')
print(f'argmax of test: {np.argmax(test)}')
print('location of max value:', ind)
print('value at the location:', test[ind])
print(pd.DataFrame(test.toarray()))

是谁产生了这个输出

shape of test (10, 10)
max of test: nan
argmax of test: 1
location of max value: (0, 1)
value at the location: 0.0
0         1    2         3    4    5         6    7    8    9
0       NaN  0.000000  0.0  0.707107  0.0  0.0  0.288675  0.0  0.0  0.5
1  0.000000       NaN  0.0  0.000000  0.0  0.0  0.816497  0.0  0.0  0.0
2  0.000000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  0.0
3  0.707107  0.000000  0.0       NaN  0.0  0.0  0.000000  0.0  0.0  0.0
4  0.000000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  0.0
5  0.000000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  0.0
6  0.288675  0.816497  0.0  0.000000  0.0  0.0       NaN  0.0  0.0  0.0
7  0.000000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  0.0
8  0.000000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  0.0
9  0.500000  0.000000  0.0  0.000000  0.0  0.0  0.000000  0.0  0.0  NaN

但我无法理解max of test: nanargmax of test: 1location of max value: (0, 1)的输出的含义。我认为max of testargmax应该分别为0.816497而不是nan1;并且最大值的位置应该是显示值0.816497的CCD_ 10或CCD_。

有人能解释一下max of testargmax of testlocation of max value的代码是什么吗?

如果ndarray.max遇到";nan";,这就是它的回报。文档中对此进行了描述。你应该看看np.nanmax

np.argmax返回最大值的索引。

最新更新