有没有办法计算scipy稀疏矩阵的百分位数?
由于内存问题,我不想将稀疏矩阵转换为稠密矩阵。
下面是一个我想要使用密集numpy数组的工作示例。我目前使用的numpy版本< 1.22,但我不介意使用最新的numpy版本的解决方案。
>>> arr = 100 * np.random.rand(3,5)
>>> arr
array([[ 3.24955563, 76.40300826, 95.47390569, 24.19071006, 26.07447378],
[60.40003646, 38.50289778, 86.50299598, 27.00110588, 34.91898836],
[51.75939709, 99.00492787, 63.32860788, 23.91364962, 56.34410086]])
>>> col_q3 = np.percentile(arr, 75, interpolation='midpoint', axis=0)
>>> col_q3
array([56.07971677, 87.70396807, 90.98845084, 25.59590797, 45.63154461])
>>> row_q3 = np.percentile(arr, 75, interpolation='midpoint', axis=1)
>>> row_q3
array([76.40300826, 60.40003646, 63.32860788])
对我来说,计算这些值所花费的时间并不太重要。我更关心内存的使用。
2条答案
按热度按时间rjee0c151#
实际上,我希望有一个稀疏的例子,例如:
1.22
有一个关于interpolation
参数使用的弃用警告。我假设您已经搜索了
percentile
的sparse
文档。它认为你/我们需要深入研究np.percentile
代码,以确定它到底在做什么-就像行/列sum
,乘法等事情而言。Sparse实现了像
sum
这样的东西:稀疏求和实际上是用矩阵乘法来完成的。
非零值为:
尽管逐行(或逐列)获得它们需要迭代。
lf3rwulv2#
这是你需要的吗
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.QuantileTransformer.html