如何在python中将对象数组转换为普通数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30666403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:47:05 来源:igfitidea点击:
How to convert an object array to a normal array in python
提问by Shashank
I have an object array which looks something like this
我有一个看起来像这样的对象数组
array([array([[2.4567]],dtype=object), array([[3.4567]],dtype=object), array([[4.4567]],dtype=object), array([[5.4567]],dtype=object) ... array([[6.4567]],dtype=object))
This is just an example, actual one is much bigger.
这只是一个例子,实际的要大得多。
So, how do I convert this into a normal floating value numpy array.
那么,如何将其转换为普通的浮点值 numpy 数组。
采纳答案by Ashwini Chaudhary
Use numpy.concatenate
:
>>> arr = array([array([[2.4567]],dtype=object),array([[3.4567]],dtype=object),array([[4.4567]],dtype=object),array([[5.4567]],dtype=object),array([[6.4567]], dtype=object)])
>>> np.concatenate(arr).astype(None)
array([[ 2.4567],
[ 3.4567],
[ 4.4567],
[ 5.4567],
[ 6.4567]])
回答by farhawa
Or, using reshape
:
或者,使用reshape
:
In [1]: a = array([array([[2.4567]],dtype=object), array([[3.4567]],dtype=object), array([[4.4567]],dtype=object)])
In [2]: a.astype(float).reshape(a.size,1)
Out[2]:
array([[ 2.4567],
[ 3.4567],
[ 4.4567]])