Python3.7之后,sklearn 的linear_assignment不支持,用scipy代替

博客介绍了在Python3.7之后sklearn不再支持的情况,以及如何使用Scipy的linear_sum_assignment作为替代。转换过程中需要注意不同包的输出格式差异,并提供了代码示例进行匹配矩阵的转换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sklearn在python3.7之后不再支持了,如果还想继续使用from sklearn.utils.linear_assignment_ import linear_assignment,要么降低python版本,要么用scipy代替,但直接代替会出问题,两个包的输出类型和格式不一样,需要做一个转换则可以正常代替了,修改如下:
import numpy as np
from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment as linear_sum_assignment

const_matrix=np.array([[15,40,45],[20,60,35],[20,40,25]])
matches=linear_assignment(const_matrix)
matches1=linear_sum_assignment(const_matrix)
print("matches_type=",type(matches))
print("matches1_type=",type(matches1))
print("matches=",matches)
print("matches1=",matches1)
print("matches1_T=",np.array(matches1).T)

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值