如何从一个numpy数组构造一个ndarray?Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21088133/
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-18 22:02:09  来源:igfitidea点击:

How to construct a ndarray from a numpy array? python

pythonarraysnumpymultidimensional-array

提问by alvas

I can't seem to convert it into an ndarrayin numpy, i've read http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.htmlbut it didn't show me how i can convert my input data as shown below into an ndarray.

我似乎无法将它转换为ndarraynumpy,我已经阅读了http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html但它没有告诉我我怎么做将我的输入数据如下所示转换为ndarray.

How to construct a ndarray from a numpy array or a list of integer lists?*What's the difference between ndarray and array?* I could just use an arraytype right?

如何从 numpy 数组或整数列表列表构造 ndarray?* ndarray 和数组有什么区别?* 我可以只使用一种array类型,对吗?

I have a list of integer counts like this

我有一个像这样的整数计数列表

[[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]

And i manage to use this code to create a np.arrayas shown in http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array

我设法使用此代码创建一个np.arrayhttp://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array所示

import numpy as np
x = [[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]
print np.array(x)

[out]:

[出去]:

[[1 2 4 1 5]
 [6 0 0 0 2]
 [0 0 0 1 0]]

But I can't change it into a np.ndarraywith this code:

但是我不能np.ndarray用这个代码把它改成一个:

import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
print np.ndarray(x)

I got an error:

我有一个错误:

Traceback (most recent call last):
  File "/home/alvas/workspace/sklearntut/test.py", line 7, in <module>
    print np.ndarray(x)
TypeError: an integer is required

How do I create a np.ndarray with the list of integer counts i've got?What integer is the TypeError talking about?

如何使用我得到的整数计数列表创建 np.ndarray ?TypeError 谈论的是什么整数?

采纳答案by Fred Foo

An ndarrayisa NumPy array.

Anndarray一个 NumPy 数组。

>>> x = np.array([1, 2, 3])
>>> type(x)
<type 'numpy.ndarray'>

The difference between np.ndarrayand np.arrayis that the former is the actual type, while the latter is a flexible shorthand function for constructing arrays from data in other formats. The TypeErrorcomes your use of np.arrayarguments to np.ndarray, which takes completely different arguments (see docstrings).

np.ndarray和之间的区别在于,np.array前者是实际类型,而后者是一种灵活的速记函数,用于从其他格式的数据构造数组。在TypeError谈到您使用的np.array参数np.ndarray,这需要完全不同的参数(见文档字符串)。

回答by Andarin

Though the accepted response is correct, that didn't help me actually create a 1-dimensional array of arrays.

尽管接受的响应是正确的,但这并没有帮助我实际创建一个一维数组。

As this thread is the first answer at Google, I post my work around, even if it isn't elegant solution (please don't hesitate to point one out to me):

由于这个线程是谷歌的第一个答案,我发布了我的工作,即使它不是优雅的解决方案(请不要犹豫向我指出一个):

import numpy as np

# Create example array
initial_array = np.ones(shape = (2,2))

# Create array of arrays
array_of_arrays = np.ndarray(shape = (1,), dtype = "object")
array_of_arrays[0] = initial_array

Be aware that array_of_arraysis in this case mutable, i.e. changing initial_arrayautomatically changes array_of_arrays.

请注意,array_of_arrays在这种情况下是可变的,即initial_array自动更改更改array_of_arrays