【发布时间】:2014-03-02 01:46:34
【问题描述】:
我正在查看问题Fastest way to rank items with multiple values and weightings 并提出以下解决方案,但还有两个问题:
import numpy as np
# set up values
keys = np.array([
['key1'],
['key2'],
['key3']
])
values = np.matrix([
[1.1, 1.2, 1.3, 1.4],
[2.1, 2.2, 2.3, 2.4],
[3.1, 3.2, 3.3, 3.4]
])
weights = np.matrix([10., 20., 30., 40.]).transpose()
# crunch the numbers
res = values * weights
# combine results with labels
items = np.hstack((np.array(res), keys))
# !First problem - .hstack has promoted the first column from float64 to S4:
# array([['130.', 'key1'],
# ['230.', 'key2'],
# ['330.', 'key3']],
# dtype='|S4')
# How can I force it to stay numeric?
items.sort(reverse=True) # doesn't work, no 'reverse' argument
# !Second problem - how to sort the array in descending order?
【问题讨论】: