相关疑难解决方法(0)

如何在没有科学记法和精确度的情况下漂亮打印numpy.array?

我很好奇,是否有任何方式打印格式化numpy.arrays,例如,以类似于这样的方式:

x = 1.23456
print '%.3f' % x
Run Code Online (Sandbox Code Playgroud)

如果我想打印numpy.array浮点数,它会打印几个小数,通常采用"科学"格式,即使对于低维数组也很难读取.但是,numpy.array显然必须打印成一个字符串,即用%s.这有解决方案吗?

python numpy pretty-print python-2.x

295
推荐指数
9
解决办法
33万
查看次数

将NumPy字符串数组映射到整数

问题:

给定一个字符串数据数组

dataSet = np.array(['kevin', 'greg', 'george', 'kevin'], dtype='U21'), 
Run Code Online (Sandbox Code Playgroud)

我想要一个返回索引数据集的函数

indexed_dataSet = np.array([0, 1, 2, 0], dtype='int')
Run Code Online (Sandbox Code Playgroud)

和查找表

lookupTable = np.array(['kevin', 'greg', 'george'], dtype='U21')
Run Code Online (Sandbox Code Playgroud)

这样的

(lookupTable[indexed_dataSet] == dataSet).all()
Run Code Online (Sandbox Code Playgroud)

是真的.注意,indexed_dataSet并且lookupTable都可以被置换,使得上述成立并且很好(即,顺序不必lookupTable等于第一次出现的顺序dataSet).

慢解决方案:

我目前有以下慢速解决方案

def indexDataSet(dataSet):
    """Returns the indexed dataSet and a lookup table
       Input:
           dataSet         : A length n numpy array to be indexed
       Output:
           indexed_dataSet : A length n numpy array containing values in {0, len(set(dataSet))-1}
           lookupTable     : A lookup table such that …
Run Code Online (Sandbox Code Playgroud)

python arrays string performance numpy

11
推荐指数
2
解决办法
4266
查看次数

标签 统计

numpy ×2

python ×2

arrays ×1

performance ×1

pretty-print ×1

python-2.x ×1

string ×1