And*_*den 2 python arrays numpy
在Python的标准max函数中(我也可以传入一个关键参数):
s = numpy.array(['one','two','three'])
max(s) # 'two' (lexicographically last)
max(s, key=len) # 'three' (longest string)
Run Code Online (Sandbox Code Playgroud)
有了一个更大的(多维)数组,我不能再使用max,所以我尝试使用numpy.amax,但我似乎无法使用amax字符串...
t = np.array([['one','two','three'],['four','five','six']])
t.dtype # dtype('|S5')
numpy.amax(t, axis=0) #Error! Hoping for: [`two`, `six`]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 1833, in amax
return amax(axis, out)
TypeError: cannot perform reduce with flexible type
Run Code Online (Sandbox Code Playgroud)
是否可以使用amax(我正确使用它!),还是有其他numpy工具可以做到这一点?
numpy您可以尝试将它们存储为Python,object而不是将字符串存储为数组中的可变长度数据.Numpy会将这些视为对原始Python字符串对象的引用,然后您可以像对待预期一样对待它们:
t = np.array([['one','two','three'],['four','five','six']], dtype=object)
np.min(t)
# gives 'five'
np.max(t)
# gives 'two'
Run Code Online (Sandbox Code Playgroud)
请记住,在这里,np.min和np.max调用是按字典顺序排序字符串 - 所以"两个"确实在"五"之后.要更改比较运算符以查看每个字符串的长度,您可以尝试创建一个numpy形式相同的新数组,但包含每个字符串的长度而不是其引用.然后,您可以numpy.argmin对该数组执行调用(返回最小值的索引),并在原始数组中查找字符串的值.
示例代码:
# Vectorize takes a Python function and converts it into a Numpy
# vector function that operates on arrays
np_len = np.vectorize(lambda x: len(x))
np_len(t)
# gives array([[3, 3, 5], [4, 4, 3]])
idx = np_len(t).argmin(0) # get the index along the 0th axis
# gives array([0, 0, 1])
result = t
for i in idx[1:]:
result = result[i]
print result
# gives "two", the string with the smallest length
Run Code Online (Sandbox Code Playgroud)