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
我运行了此代码,以了解max
和argmax
的含义,
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
,但我可以't了解测试最大的输出的含义:NAN
,测试的Argmax:1
和最大值的位置:(0,1)。我认为
测试的最大
和gragmax
应该是 0.816497 而不是NAN
和 1 分别;最大值的位置应为(6,1)
或(1,6)
,其中显示了0.816497的位置。
有人可以解释测试最大的代码
,test
和最大值
的位置做了什么?
I have this tf-idf
matrix
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
I ran this code to understand the meaning of max
and argmax
of the matrix
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()))
Which produced this output
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
But I couldn't understand the meaning of the output for max of test: nan
, argmax of test: 1
and location of max value: (0, 1)
. I thought the max of test
and argmax
should be 0.816497 instead of nan
and 1 respectively; and the location of the max value should be (6, 1)
or (1, 6)
where the value 0.816497 was displayed.
Could someone please explain what the code for max of test
, argmax of test
and location of max value
did?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
ndarray.max
遇到“ nan”,那就是它返回的。这是文档中描述的。您应该查看np.nanmax
。np.argmax
返回最大值的索引。If
ndarray.max
encounters a "nan", that's what it returns. That's described in the documentation. You should look atnp.nanmax
.np.argmax
returns the index of the maximum value.